Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Support file-relative values via functions #2

Merged
merged 4 commits into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"name": "rollup-plugin-replace",
"version": "1.1.0",
"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",
"mocha": "^2.3.3",
"rollup": "^0.22.0",
Expand All @@ -16,7 +18,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/*",
Expand Down
File renamed without changes.
33 changes: 30 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 );
}
Expand Down
3 changes: 3 additions & 0 deletions test/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [ "es2015" ]
}
25 changes: 0 additions & 25 deletions test/index.js

This file was deleted.

1 change: 1 addition & 0 deletions test/samples/relative/dir/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default __filename;
5 changes: 5 additions & 0 deletions test/samples/relative/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import foo from './dir/foo.js';

var bar = __filename;

export { foo, bar };
47 changes: 47 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as assert from 'assert';
import * as path from 'path';
import { rollup } from 'rollup';
import replace from '..';

process.chdir( __dirname );

describe( 'rollup-plugin-replace', () => {
it( 'replaces strings', () => {
return rollup({
entry: 'samples/basic/main.js',
plugins: [
replace({
ENV: "'production'"
})
]
}).then( bundle => {
const generated = bundle.generate();
const code = generated.code;

assert.ok( code.indexOf( "'production' !== 'production'" ) !== -1 );
});
});

it( 'allows replacement to be a function', () => {
return rollup({
entry: 'samples/relative/main.js',
plugins: [
replace({
__filename: id => `'${id.slice( path.resolve( __dirname, 'samples/relative' ).length + 1 )}'`
})
]
}).then( 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
});