From 2f4c9f31958eb9669737f6f077341d9a814884ca Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 27 Dec 2015 13:28:55 -0500 Subject: [PATCH 1/4] support file-relative values via functions --- src/index.js | 33 +++++++++++++++++++++++++++++--- test/index.js | 24 +++++++++++++++++++++++ test/samples/relative/dir/foo.js | 1 + test/samples/relative/main.js | 5 +++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 test/samples/relative/dir/foo.js create mode 100644 test/samples/relative/main.js diff --git a/src/index.js b/src/index.js index 6fe220e..28a5e17 100644 --- a/src/index.js +++ b/src/index.js @@ -5,12 +5,39 @@ function escape ( str ) { return str.replace( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&' ); } +function assign ( target, source ) { + Object.keys( source ).forEach( key => { + target[ key ] = source[ key ]; + }); + return target; +} + +function functor ( thing ) { + if ( typeof thing === 'function' ) return thing; + return () => thing; +} + export default function replace ( options = {} ) { - const values = options.values || options; + const filter = createFilter( options.include, options.exclude ); const delimiters = ( options.delimiters || [ '', '' ] ).map( escape ); + + let values; + + if ( options.values ) { + values = options.values; + } else { + values = assign( {}, options ); + delete values.delimiters; + delete values.include; + delete values.exclude; + } + const pattern = new RegExp( delimiters[0] + '(' + Object.keys( values ).join( '|' ) + ')' + delimiters[1], 'g' ); - const filter = createFilter( options.include, options.exclude ); + // convert all values to functions + Object.keys( values ).forEach( key => { + values[ key ] = functor( values[ key ] ); + }); return { transform ( code, id ) { @@ -27,7 +54,7 @@ export default function replace ( options = {} ) { start = match.index; end = start + match[0].length; - replacement = String( values[ match[1] ] ); + replacement = String( values[ match[1] ]( id ) ); magicString.overwrite( start, end, replacement ); } diff --git a/test/index.js b/test/index.js index 406f6dd..f8b3127 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,7 @@ var assert = require( 'assert' ); var rollup = require( 'rollup' ); var replace = require( '..' ); +var path = require( 'path' ); process.chdir( __dirname ); @@ -21,5 +22,28 @@ describe( 'rollup-plugin-replace', function () { }); }); + it( 'allows replacement to be a function', function () { + return rollup.rollup({ + entry: 'samples/relative/main.js', + plugins: [ + replace({ + __filename: function ( id ) { + return '"' + id.slice( path.resolve( __dirname, 'samples/relative' ).length + 1 ) + '"'; + } + }) + ] + }).then( function ( bundle ) { + const generated = bundle.generate({ format: 'cjs' }); + const code = generated.code; + + const fn = new Function( 'module', 'exports', code ); + const module = { exports: {} }; + fn( module, module.exports ); + + assert.equal( module.exports.foo, 'dir/foo.js' ); + assert.equal( module.exports.bar, 'main.js' ); + }); + }); + // TODO tests for delimiters, sourcemaps, etc }); diff --git a/test/samples/relative/dir/foo.js b/test/samples/relative/dir/foo.js new file mode 100644 index 0000000..9193a37 --- /dev/null +++ b/test/samples/relative/dir/foo.js @@ -0,0 +1 @@ +export default __filename; diff --git a/test/samples/relative/main.js b/test/samples/relative/main.js new file mode 100644 index 0000000..4a9e5e4 --- /dev/null +++ b/test/samples/relative/main.js @@ -0,0 +1,5 @@ +import foo from './dir/foo.js'; + +const bar = __filename; + +export { foo, bar }; From 57e414ab098fe64b6176a3226beef83fc30fd53d Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 27 Dec 2015 13:29:14 -0500 Subject: [PATCH 2/4] -> 1.2.0 --- CHANGELOG.md | 4 ++++ README.md | 8 ++++++++ package.json | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f17d55..236fe9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # rollup-plugin-replace changelog +## 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.0 * Generate sourcemaps by default diff --git a/README.md b/README.md index fb4ae62..29de8c6 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,14 @@ rollup({ } ``` +A replacement value can be supplied as a function that takes a module ID as its only argument: + +```js +{ + __dirname: ( id ) => `'${path.dirname( id )}'` +} +``` + ## License diff --git a/package.json b/package.json index 85cf663..5e15fcb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-replace", - "version": "1.1.0", + "version": "1.2.0", "devDependencies": { "babel-preset-es2015-rollup": "^1.0.0", "eslint": "^1.7.3", From 47a12707182c94555b9bab8b65a9b5e0cd651652 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 27 Dec 2015 13:35:29 -0500 Subject: [PATCH 3/4] make tests run in node 0.12 --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 5e15fcb..a824e95 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.2.0", "devDependencies": { "babel-preset-es2015-rollup": "^1.0.0", + "babel-register": "^6.3.13", "eslint": "^1.7.3", "mocha": "^2.3.3", "rollup": "^0.22.0", @@ -16,7 +17,7 @@ "rollup-pluginutils": "^1.1.0" }, "scripts": { - "test": "mocha", + "test": "mocha --compilers js:babel-register", "pretest": "npm run build", "build": "rollup -c -f cjs -o dist/rollup-plugin-replace.cjs.js && rollup -c -f es6 -o dist/rollup-plugin-replace.es6.js", "prebuild": "rm -rf dist/*", From d777707239d27bbfb3f370a9b0ae9362fe1c6c13 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 27 Dec 2015 13:41:54 -0500 Subject: [PATCH 4/4] sort out tests etc --- package.json | 1 + .babelrc => src/.babelrc | 0 test/.babelrc | 3 +++ test/samples/relative/main.js | 2 +- test/{index.js => test.js} | 26 ++++++++++++-------------- 5 files changed, 17 insertions(+), 15 deletions(-) rename .babelrc => src/.babelrc (100%) create mode 100644 test/.babelrc rename test/{index.js => test.js} (58%) diff --git a/package.json b/package.json index a824e95..e25b954 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "rollup-plugin-replace", "version": "1.2.0", "devDependencies": { + "babel-preset-es2015": "^6.3.13", "babel-preset-es2015-rollup": "^1.0.0", "babel-register": "^6.3.13", "eslint": "^1.7.3", diff --git a/.babelrc b/src/.babelrc similarity index 100% rename from .babelrc rename to src/.babelrc diff --git a/test/.babelrc b/test/.babelrc new file mode 100644 index 0000000..de9f4a8 --- /dev/null +++ b/test/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": [ "es2015" ] +} diff --git a/test/samples/relative/main.js b/test/samples/relative/main.js index 4a9e5e4..749a861 100644 --- a/test/samples/relative/main.js +++ b/test/samples/relative/main.js @@ -1,5 +1,5 @@ import foo from './dir/foo.js'; -const bar = __filename; +var bar = __filename; export { foo, bar }; diff --git a/test/index.js b/test/test.js similarity index 58% rename from test/index.js rename to test/test.js index f8b3127..1845b75 100644 --- a/test/index.js +++ b/test/test.js @@ -1,20 +1,20 @@ -var assert = require( 'assert' ); -var rollup = require( 'rollup' ); -var replace = require( '..' ); -var path = require( 'path' ); +import * as assert from 'assert'; +import * as path from 'path'; +import { rollup } from 'rollup'; +import replace from '..'; process.chdir( __dirname ); -describe( 'rollup-plugin-replace', function () { - it( 'replaces strings', function () { - return rollup.rollup({ +describe( 'rollup-plugin-replace', () => { + it( 'replaces strings', () => { + return rollup({ entry: 'samples/basic/main.js', plugins: [ replace({ ENV: "'production'" }) ] - }).then( function ( bundle ) { + }).then( bundle => { const generated = bundle.generate(); const code = generated.code; @@ -22,17 +22,15 @@ describe( 'rollup-plugin-replace', function () { }); }); - it( 'allows replacement to be a function', function () { - return rollup.rollup({ + it( 'allows replacement to be a function', () => { + return rollup({ entry: 'samples/relative/main.js', plugins: [ replace({ - __filename: function ( id ) { - return '"' + id.slice( path.resolve( __dirname, 'samples/relative' ).length + 1 ) + '"'; - } + __filename: id => `'${id.slice( path.resolve( __dirname, 'samples/relative' ).length + 1 )}'` }) ] - }).then( function ( bundle ) { + }).then( bundle => { const generated = bundle.generate({ format: 'cjs' }); const code = generated.code;