├── .editorconfig ├── .gitignore ├── .travis.yml ├── README.md ├── package.json ├── postprocess.js └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,.*rc,*.yml,*.md}] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | package-lock.json 4 | *.log 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rollup-plugin-postprocess [![npm](https://img.shields.io/npm/v/rollup-plugin-postprocess.svg?style=flat)](https://www.npmjs.org/package/rollup-plugin-postprocess) [![travis](https://travis-ci.org/developit/rollup-plugin-postprocess.svg?branch=master)](https://travis-ci.org/developit/rollup-plugin-postprocess) 2 | 3 | Apply regex find-and-replace postprocessing to your Rollup bundle. 4 | 5 | 6 | ## Installation 7 | 8 | `npm i -D rollup-plugin-postprocess` 9 | 10 | 11 | ## Usage 12 | 13 | Works just like a [JavaScript String replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace), including the funtion callback option. 14 | 15 | `postprocess()` expects an Array of `[(RexExp) find, (String|Function) replace]` pairs. Alternatively, if a function is provided, it will be invoked for each bundle and can return said pairs. 16 | 17 | 18 | ## Example 19 | 20 | ```js 21 | import postprocess from 'rollup-plugin-postprocess'; 22 | 23 | export default { 24 | plugins: [ 25 | postprocess([ 26 | [/\b(module\.exports=|export default )([a-zA-Z])/, '$1$2'] 27 | ]) 28 | ] 29 | } 30 | ``` 31 | 32 | 33 | ## Complex Example 34 | 35 | This example is more practical. Rollup places exports at the end of your bundle, which can often create single-use variables that Uglify does not collapse. Let's implement a find & replace that "moves" the export inline to save some bytes. 36 | 37 | In this example, we'll make use of the fact that find/replacement pairs are executed in sequence. The first pair is used both to remove the existing export statement _and_ to find the export type & identifier. By the time the second find/replace pair is processed, it can make use of the values found in the first pass. 38 | 39 | ```js 40 | import postprocess from 'rollup-plugin-postprocess'; 41 | 42 | let name, exportPrefix; 43 | export default { 44 | plugins: [ 45 | postprocess([ 46 | [ 47 | /(module\.exports\s*=\s*|export\s*default\s*)([a-zA-Z$_][a-zA-Z0-9$_]*)[;,]?/, 48 | (str, prefix, id) => { 49 | name = id; 50 | exportPrefix = prefix; 51 | // returning nothing is the same as an empty string 52 | } 53 | ], 54 | [ 55 | /^function\s([a-zA-Z$_][a-zA-Z0-9$_]*)/, 56 | (str, id) => id==name ? `${exportPrefix} function` : str 57 | ] 58 | ]) 59 | ] 60 | }; 61 | ``` 62 | 63 | 64 | ## License 65 | 66 | [MIT](https://oss.ninja/mit/developit) 67 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-postprocess", 3 | "version": "1.0.2", 4 | "description": "Find-and-replace postprocessing for Rollup output.", 5 | "main": "dist/rollup-plugin-postprocess.js", 6 | "module": "dist/rollup-plugin-postprocess.m.js", 7 | "source": "postprocess.js", 8 | "scripts": { 9 | "prepare": "microbundle --external all --target node --format cjs,es --no-compress", 10 | "test": "eslint postprocess.js && npm run -s prepare && node test", 11 | "release": "npm run -s prepare && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" 12 | }, 13 | "eslintConfig": { 14 | "extends": "eslint-config-developit", 15 | "rules": { 16 | "no-cond-assign": 0, 17 | "prefer-spread": 0 18 | } 19 | }, 20 | "keywords": [ 21 | "rollup", 22 | "plugin", 23 | "replace", 24 | "post replace" 25 | ], 26 | "files": [ 27 | "index.js", 28 | "dist" 29 | ], 30 | "repository": "developit/rollup-plugin-postprocess", 31 | "author": "Jason Miller (http://jasonformat.com)", 32 | "license": "MIT", 33 | "devDependencies": { 34 | "eslint": "^4.13.1", 35 | "eslint-config-developit": "^1.1.1", 36 | "microbundle": "^0.4.1" 37 | }, 38 | "dependencies": { 39 | "magic-string": "^0.22.4" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /postprocess.js: -------------------------------------------------------------------------------- 1 | import MagicString from 'magic-string'; 2 | 3 | let currentToken; 4 | const replacer = (str, index) => currentToken[index]; 5 | 6 | export default function postprocess(allReplacements) { 7 | return { 8 | name: 'postprocess', 9 | transformBundle(code, { sourceMap, format }) { 10 | let str = new MagicString(code); 11 | let replacements = typeof allReplacements==='function' ? allReplacements({ code, sourceMap, format }) : allReplacements; 12 | 13 | for (let i=0; i