pax_global_header00006660000000000000000000000064131535116300014510gustar00rootroot0000000000000052 comment=a0a7473bd28c98f6e6876d723c6d79692b20b279 rollup-plugin-replace-2.0.0/000077500000000000000000000000001315351163000157315ustar00rootroot00000000000000rollup-plugin-replace-2.0.0/.eslintrc000066400000000000000000000012541315351163000175570ustar00rootroot00000000000000{ "rules": { "indent": [ 2, "tab", { "SwitchCase": 1 } ], "quotes": [ 2, "single" ], "linebreak-style": [ 2, "unix" ], "semi": [ 2, "always" ], "keyword-spacing": [ 2, { "before": true, "after": true } ], "space-before-blocks": [ 2, "always" ], "space-before-function-paren": [ 2, "always" ], "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ], "no-cond-assign": [ 0 ] }, "env": { "es6": true, "browser": true, "mocha": true, "node": true }, "extends": "eslint:recommended", "parserOptions": { "sourceType": "module", "ecmaVersion": 6 } } rollup-plugin-replace-2.0.0/.gitignore000066400000000000000000000000451315351163000177200ustar00rootroot00000000000000.DS_Store node_modules dist .gobble* rollup-plugin-replace-2.0.0/.travis.yml000066400000000000000000000002071315351163000200410ustar00rootroot00000000000000sudo: false language: node_js node_js: - "stable" env: global: - BUILD_TIMEOUT=10000 install: npm install # script: npm run ci rollup-plugin-replace-2.0.0/CHANGELOG.md000066400000000000000000000011771315351163000175500ustar00rootroot00000000000000# rollup-plugin-replace changelog ## 2.0.0 * Only match on word boundaries, unless delimiters are empty strings ([#10](https://github.com/rollup/rollup-plugin-replace/pull/10)) ## 1.2.1 * Match longest keys first ([#8](https://github.com/rollup/rollup-plugin-replace/pull/8)) * Escape keys ([#9](https://github.com/rollup/rollup-plugin-replace/pull/9)) ## 1.2.0 * Allow replacement to be a function that takes a module ID ([#1](https://github.com/rollup/rollup-plugin-replace/issues/1)) ## 1.1.1 * Return a `name` ## 1.1.0 * Generate sourcemaps by default ## 1.0.1 * Include correct files in package ## 1.0.0 * First release rollup-plugin-replace-2.0.0/README.md000066400000000000000000000036501315351163000172140ustar00rootroot00000000000000# rollup-plugin-replace Replace strings in files while bundling them. ## Installation ```bash npm install --save-dev rollup-plugin-replace ``` ## Usage Generally, you need to ensure that rollup-plugin-replace goes *before* other things (like rollup-plugin-commonjs) in your `plugins` array, so that those plugins can apply any optimisations such as dead code removal. ```js // rollup.config.js import replace from 'rollup-plugin-replace'; export default { // ... plugins: [ replace({ ENVIRONMENT: JSON.stringify('production') }) ] }; ``` ## Options ```js { // a minimatch pattern, or array of patterns, of files that // should be processed by this plugin (if omitted, all files // are included by default)... include: 'config.js', // ...and those that shouldn't, if `include` is otherwise // too permissive exclude: 'node_modules/**', // To replace every occurence of `<@foo@>` instead of every // occurence of `foo`, supply delimiters delimiters: ['<@', '@>'], // All other options are treated as `string: replacement` // replacers... VERSION: '1.0.0', ENVIRONMENT: JSON.stringify('development'), // or `string: (id) => replacement` functions... __dirname: (id) => `'${path.dirname(id)}'`, // ...unless you want to be careful about separating // values from other options, in which case you can: values: { VERSION: '1.0.0', ENVIRONMENT: JSON.stringify('development') } } ``` ## Word boundaries By default, values will only match if they are surrounded by *word boundaries* — i.e. with options like this... ```js { changed: 'replaced' } ``` ...and code like this... ```js console.log('changed'); console.log('unchanged'); ``` ...the result will be this: ```js console.log('replaced'); console.log('unchanged'); ``` If that's not what you want, specify empty strings as delimiters: ```js { changed: 'replaced', delimiters: ['', ''] } ``` ## License MIT rollup-plugin-replace-2.0.0/appveyor.yml000066400000000000000000000010611315351163000203170ustar00rootroot00000000000000# http://www.appveyor.com/docs/appveyor-yml version: "{build}" clone_depth: 10 init: - git config --global core.autocrlf false environment: matrix: # node.js - nodejs_version: stable install: - ps: Install-Product node $env:nodejs_version - npm install build: off test_script: - node --version && npm --version - npm test matrix: fast_finish: false # cache: # - C:\Users\appveyor\AppData\Roaming\npm-cache -> package.json # npm cache # - node_modules -> package.json # local npm modules rollup-plugin-replace-2.0.0/package.json000066400000000000000000000017541315351163000202260ustar00rootroot00000000000000{ "name": "rollup-plugin-replace", "version": "2.0.0", "devDependencies": { "eslint": "^4.6.1", "mocha": "^3.5.0", "rollup": "^0.49.2", "rollup-plugin-buble": "^0.15.0" }, "main": "dist/rollup-plugin-replace.cjs.js", "module": "dist/rollup-plugin-replace.es.js", "dependencies": { "magic-string": "^0.22.4", "minimatch": "^3.0.2", "rollup-pluginutils": "^2.0.1" }, "scripts": { "test": "mocha", "pretest": "npm run build", "build": "rollup -c", "prebuild": "rm -rf dist/*", "prepublish": "npm test" }, "files": [ "src", "dist", "README.md" ], "repository": "rollup/rollup-plugin-replace", "keywords": [ "rollup", "rollup-plugin", "es2015", "npm", "modules" ], "author": "Rich Harris ", "license": "MIT", "bugs": { "url": "https://github.com/rollup/rollup-plugin-replace/issues" }, "homepage": "https://github.com/rollup/rollup-plugin-replace#readme" } rollup-plugin-replace-2.0.0/rollup.config.js000066400000000000000000000004601315351163000210500ustar00rootroot00000000000000import buble from 'rollup-plugin-buble'; import pkg from './package.json'; var external = Object.keys(pkg.dependencies).concat('path'); export default { input: 'src/index.js', plugins: [ buble() ], external, output: [ { file: pkg.main, format: 'cjs' }, { file: pkg.module, format: 'es' } ] }; rollup-plugin-replace-2.0.0/src/000077500000000000000000000000001315351163000165205ustar00rootroot00000000000000rollup-plugin-replace-2.0.0/src/index.js000066400000000000000000000033241315351163000201670ustar00rootroot00000000000000import MagicString from 'magic-string'; import { createFilter } from 'rollup-pluginutils'; function escape(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); } function functor(thing) { if (typeof thing === 'function') return thing; return () => thing; } function longest(a, b) { return b.length - a.length; } export default function replace(options = {}) { const filter = createFilter(options.include, options.exclude); const { delimiters } = options; let values; if (options.values) { values = options.values; } else { values = Object.assign({}, options); delete values.delimiters; delete values.include; delete values.exclude; } const keys = Object.keys(values).sort(longest).map(escape); const pattern = delimiters ? new RegExp( `${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}`, 'g' ) : new RegExp( `\\b(${keys.join('|')})\\b`, 'g' ); // convert all values to functions Object.keys(values).forEach(key => { values[key] = functor(values[key]); }); return { name: 'replace', transform(code, id) { if (!filter(id)) return null; const magicString = new MagicString(code); let hasReplacements = false; let match; let start, end, replacement; while ((match = pattern.exec(code))) { hasReplacements = true; start = match.index; end = start + match[0].length; replacement = String(values[match[1]](id)); magicString.overwrite(start, end, replacement); } if (!hasReplacements) return null; let result = { code: magicString.toString() }; if (options.sourceMap !== false && options.sourcemap !== false) result.map = magicString.generateMap({ hires: true }); return result; } }; } rollup-plugin-replace-2.0.0/test/000077500000000000000000000000001315351163000167105ustar00rootroot00000000000000rollup-plugin-replace-2.0.0/test/samples/000077500000000000000000000000001315351163000203545ustar00rootroot00000000000000rollup-plugin-replace-2.0.0/test/samples/basic/000077500000000000000000000000001315351163000214355ustar00rootroot00000000000000rollup-plugin-replace-2.0.0/test/samples/basic/main.js000066400000000000000000000000241315351163000227130ustar00rootroot00000000000000console.log(ANSWER);rollup-plugin-replace-2.0.0/test/samples/boundaries/000077500000000000000000000000001315351163000225075ustar00rootroot00000000000000rollup-plugin-replace-2.0.0/test/samples/boundaries/main.js000066400000000000000000000000751315351163000237730ustar00rootroot00000000000000export const foo = 'unchanged'; export const bar = 'changed';rollup-plugin-replace-2.0.0/test/samples/longest-first/000077500000000000000000000000001315351163000231545ustar00rootroot00000000000000rollup-plugin-replace-2.0.0/test/samples/longest-first/main.js000066400000000000000000000000531315351163000244340ustar00rootroot00000000000000console.log('BUILD version BUILD_VERSION');rollup-plugin-replace-2.0.0/test/samples/relative/000077500000000000000000000000001315351163000221675ustar00rootroot00000000000000rollup-plugin-replace-2.0.0/test/samples/relative/dir/000077500000000000000000000000001315351163000227455ustar00rootroot00000000000000rollup-plugin-replace-2.0.0/test/samples/relative/dir/foo.js000066400000000000000000000000331315351163000240620ustar00rootroot00000000000000export default __filename; rollup-plugin-replace-2.0.0/test/samples/relative/main.js000066400000000000000000000001151315351163000234460ustar00rootroot00000000000000import foo from './dir/foo.js'; var bar = __filename; export { foo, bar }; rollup-plugin-replace-2.0.0/test/samples/special-chars/000077500000000000000000000000001315351163000230725ustar00rootroot00000000000000rollup-plugin-replace-2.0.0/test/samples/special-chars/main.js000066400000000000000000000000551315351163000243540ustar00rootroot00000000000000const one = require('one'); console.log(one);rollup-plugin-replace-2.0.0/test/test.js000066400000000000000000000046151315351163000202330ustar00rootroot00000000000000const assert = require('assert'); const path = require('path'); const { rollup } = require('rollup'); const replace = require('../dist/rollup-plugin-replace.cjs.js'); process.chdir( __dirname ); async function evaluate(sample, opts) { const bundle = await rollup({ input: `samples/${sample}/main.js`, plugins: [ replace(opts) ] }); const { code } = await bundle.generate({ format: 'cjs' }); const fn = new Function('module', 'exports', code); const module = { exports: {} }; fn(module, module.exports); return module.exports; } describe('rollup-plugin-replace', () => { it('replaces strings', async () => { const bundle = await rollup({ input: 'samples/basic/main.js', plugins: [ replace({ ANSWER: '42' }) ] }); const { code } = await bundle.generate({ format: 'es' }); assert.equal(code.trim(), 'console.log(42);'); }); it('allows replacement to be a function', async () => { const bundle = await rollup({ input: 'samples/relative/main.js', plugins: [ replace({ __filename: id => JSON.stringify(id.slice(path.resolve(__dirname, 'samples/relative').length + 1)) }) ] }); const { code } = await bundle.generate({ format: 'cjs' }); const fn = new Function('module', 'exports', code); const module = { exports: {} }; fn(module, module.exports); assert.equal(module.exports.foo, path.join('dir', 'foo.js')); assert.equal(module.exports.bar, 'main.js'); }); it('matches most specific variables', async () => { const bundle = await rollup({ input: 'samples/longest-first/main.js', plugins: [ replace({ BUILD: 'beta', BUILD_VERSION: '1.0.0' }) ] }) const { code } = await bundle.generate({ format: 'es' }); assert.equal(code.trim(), `console.log('beta version 1.0.0');`); }); it('supports special characters' , async () => { const bundle = await rollup({ input: 'samples/special-chars/main.js', plugins: [ replace({ "require('one')": "1", delimiters: ['', ''] }) ] }); const { code } = await bundle.generate({ format: 'es' }); assert.equal(code.trim(), 'const one = 1;\nconsole.log(one);'); }); it('uses word boundaries if delimiters are unspecified', async () => { const exports = await evaluate('boundaries', { changed: 'replaced' }); assert.deepEqual(exports, { foo: 'unchanged', bar: 'replaced' }); }); // TODO tests for delimiters, sourcemaps, etc });