Browse Source

support for transformers

contingency-plan
Rich Harris 10 years ago
parent
commit
836f6b93b3
  1. 8
      src/utils/load.js
  2. 21
      test/function/transformer-multiple/_config.js
  3. 1
      test/function/transformer-multiple/foo.js
  4. 3
      test/function/transformer-multiple/main.js
  5. 13
      test/function/transformer-single/_config.js
  6. 1
      test/function/transformer-single/foo.js
  7. 3
      test/function/transformer-single/main.js

8
src/utils/load.js

@ -1,6 +1,10 @@
import { readFileSync } from 'sander';
export function defaultLoader ( path, options ) {
// TODO support plugins and transformers?
return readFileSync( path, { encoding: 'utf-8' });
// TODO support plugins e.g. !css and !json?
const source = readFileSync( path, { encoding: 'utf-8' });
return options.transform.reduce( ( source, transformer ) => {
return transformer( source, path );
}, source );
}

21
test/function/transformer-multiple/_config.js

@ -0,0 +1,21 @@
var assert = require( 'assert' );
module.exports = {
description: 'accepts multiple transformer functions',
options: {
transform: [
function ( code, path ) {
return code.replace( /MAGIC_NUMBER/g, 3 );
},
function ( code, path ) {
return code.replace( /\d+/g, function ( match ) {
return 2 * +match;
});
}
]
},
exports: function ( exports ) {
assert.equal( exports.magicNumber, 6 );
}
}

1
test/function/transformer-multiple/foo.js

@ -0,0 +1 @@
export default MAGIC_NUMBER;

3
test/function/transformer-multiple/main.js

@ -0,0 +1,3 @@
import foo from './foo';
export var magicNumber = foo;

13
test/function/transformer-single/_config.js

@ -0,0 +1,13 @@
var assert = require( 'assert' );
module.exports = {
description: 'accepts a single transformer function',
options: {
transform: function ( code, path ) {
return code.replace( /MAGIC_NUMBER/g, 3 );
}
},
exports: function ( exports ) {
assert.equal( exports.magicNumber, 3 );
}
}

1
test/function/transformer-single/foo.js

@ -0,0 +1 @@
export default MAGIC_NUMBER;

3
test/function/transformer-single/main.js

@ -0,0 +1,3 @@
import foo from './foo';
export var magicNumber = foo;
Loading…
Cancel
Save