Skip to content

Commit 436aa4e

Browse files
committed
Initial commit!
0 parents  commit 436aa4e

7 files changed

Lines changed: 154 additions & 0 deletions

File tree

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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}]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist
2+
node_modules
3+
package-lock.json
4+
*.log

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- "latest"

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# rollup-plugin-postprocess
2+
3+
Apply regex find-and-replace postprocessing to your Rollup bundle.
4+
5+
## Installation
6+
7+
`npm i -D rollup-plugin-postprocess`
8+
9+
## Usage
10+
11+
```js
12+
import postprocess from 'rollup-plugin-postprocess';
13+
14+
export default {
15+
plugins: [
16+
postprocess([
17+
[/\b(module\.exports=|export default )([a-zA-Z])/, '$1$2']
18+
])
19+
]
20+
}
21+
```
22+
23+
24+
## License
25+
26+
[MIT](https://oss.ninja/mit/developit)

package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "rollup-plugin-postprocess",
3+
"version": "1.0.0",
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 <jason@developit.ca> (http://jasonformat.com)",
32+
"license": "MIT",
33+
"devDependencies": {
34+
"eslint": "^4.13.1",
35+
"eslint-config-developit": "^1.1.1",
36+
"microbundle": "^0.3.0"
37+
},
38+
"dependencies": {
39+
"magic-string": "^0.22.4"
40+
}
41+
}

postprocess.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 }) {
10+
let str = new MagicString(code);
11+
let replacements = typeof allReplacements==='function' ? allReplacements({ code, sourceMap }) : allReplacements;
12+
13+
for (let i=0; i<replacements.length; i++) {
14+
let [find, replace=''] = replacements[i];
15+
if (typeof find==='string') find = new RegExp(find);
16+
if (!find.global) {
17+
find = new RegExp(find.source, 'g' + String(find).split('/').pop());
18+
}
19+
20+
let token;
21+
while (token=find.exec(code)) {
22+
let value;
23+
if (typeof replace==='function') {
24+
value = replace.apply(null, token);
25+
if (value==null) value = '';
26+
}
27+
else {
28+
currentToken = token;
29+
value = replace.replace(/\$(\d+)/, replacer);
30+
}
31+
str.overwrite(token.index, token.index + token[0].length, value);
32+
}
33+
}
34+
35+
return sourceMap===false ? str.toString() : {
36+
code: str.toString(),
37+
map: str.generateMap({ hires: true })
38+
};
39+
}
40+
};
41+
}

test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint-disable */
2+
3+
var assert = require('assert');
4+
var postprocess = require('.');
5+
6+
var code = 'function t(t,o,e,i){for(i=0,o=o.split?o.split("."):o;t&&i<o.length;)t=t[o[i++]];return void 0===t?e:t}module.exports=t;';
7+
8+
var name, exportPrefix;
9+
var out = postprocess([
10+
[/(module\.exports\s*=\s*|export\s*default\s*)([a-zA-Z$_][a-zA-Z0-9$_]*)[;,]?/, function(str, prefix, id) {
11+
name = id;
12+
exportPrefix = prefix;
13+
}],
14+
[/^function\s([a-zA-Z$_][a-zA-Z0-9$_]*)/, function(str, id) {
15+
if (id===name) {
16+
return exportPrefix + str;
17+
}
18+
return str;
19+
}]
20+
]).transformBundle(code, { sourceMap: false });
21+
22+
assert.equal(out, 'module.exports=function t(t,o,e,i){for(i=0,o=o.split?o.split("."):o;t&&i<o.length;)t=t[o[i++]];return void 0===t?e:t}');
23+
console.log('✅ Tests Passed');
24+
process.exit(0);

0 commit comments

Comments
 (0)