├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | npm-debug.log 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2016 Mike Bostock 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of the author nor the names of contributors may be used to 15 | endorse or promote products derived from this software without specific prior 16 | written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rollup-plugin-ascii 2 | 3 | Rewrite JavaScript to escape any non-ASCII characters in string literals. For example, the following input: 4 | 5 | ```js 6 | console.log("Ich ♥ Bücher"); 7 | ``` 8 | 9 | Becomes the following output: 10 | 11 | ```js 12 | console.log("Ich \u2665 B\xFCcher"); 13 | ``` 14 | 15 | Only string literals are escaped! If you want to escape Unicode symbols, too, you’ll need something like UglifyJS2. 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var acorn = require("acorn"), 2 | jsesc = require("jsesc"), 3 | walk = require("estree-walker").walk, 4 | MagicString = require("magic-string"), 5 | createFilter = require("rollup-pluginutils").createFilter, 6 | extname = require("path").extname; 7 | 8 | module.exports = function(options) { 9 | if (options == null) options = {}; 10 | var filter = createFilter(options.include, options.exclude), 11 | map = options.sourceMap !== false; 12 | return { 13 | transform: function(code, id) { 14 | if (!filter(id) || extname(id) !== ".js") return; 15 | 16 | var ast, magic = new MagicString(code), modified; 17 | 18 | try { 19 | ast = acorn.parse(code, {ecmaVersion: 6, sourceType: "module"}); 20 | } catch (error) { 21 | error.message += " in " + id; 22 | throw error; 23 | } 24 | 25 | walk(ast, { 26 | enter: function(node, parent) { 27 | if (map) { 28 | magic.addSourcemapLocation(node.start); 29 | magic.addSourcemapLocation(node.end); 30 | } 31 | if (node.type === "Literal" && typeof node.value === "string") { 32 | var raw0 = node.raw, 33 | raw1 = jsesc(node.value, {wrap: true, quotes: raw0[0] === "'" ? "single" : "double"}); 34 | if (raw0 !== raw1) { 35 | modified = true; 36 | magic.overwrite(node.start, node.end, raw1); 37 | } 38 | } 39 | } 40 | }); 41 | 42 | return modified && { 43 | code: magic.toString(), 44 | map: map && magic.generateMap() 45 | }; 46 | } 47 | }; 48 | }; 49 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-ascii", 3 | "version": "0.0.3", 4 | "description": "Rewrite JavaScript to escape any non-ASCII characters in string literals.", 5 | "keywords": [ 6 | "rollup-plugin", 7 | "ascii" 8 | ], 9 | "homepage": "https://github.com/mbostock/rollup-plugin-ascii", 10 | "license": "BSD-3-Clause", 11 | "author": { 12 | "name": "Mike Bostock", 13 | "url": "https://bost.ocks.org/mike" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/mbostock/rollup-plugin-ascii.git" 18 | }, 19 | "main": "index.js", 20 | "scripts": { 21 | "postpublish": "git push && git push --tags" 22 | }, 23 | "dependencies": { 24 | "acorn": "^3.2.0", 25 | "estree-walker": "^0.2.1", 26 | "jsesc": "^2.2.0", 27 | "magic-string": "^0.15.1", 28 | "rollup-pluginutils": "^1.5.0" 29 | } 30 | } 31 | --------------------------------------------------------------------------------