From 836f6b93b32a7c3d8cd7fa310d9e3a77dae1d7c6 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 May 2015 16:25:06 -0400 Subject: [PATCH] support for transformers --- src/utils/load.js | 8 +++++-- test/function/transformer-multiple/_config.js | 21 +++++++++++++++++++ test/function/transformer-multiple/foo.js | 1 + test/function/transformer-multiple/main.js | 3 +++ test/function/transformer-single/_config.js | 13 ++++++++++++ test/function/transformer-single/foo.js | 1 + test/function/transformer-single/main.js | 3 +++ 7 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/function/transformer-multiple/_config.js create mode 100644 test/function/transformer-multiple/foo.js create mode 100644 test/function/transformer-multiple/main.js create mode 100644 test/function/transformer-single/_config.js create mode 100644 test/function/transformer-single/foo.js create mode 100644 test/function/transformer-single/main.js diff --git a/src/utils/load.js b/src/utils/load.js index 1d8d826..9b79d9a 100644 --- a/src/utils/load.js +++ b/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 ); } diff --git a/test/function/transformer-multiple/_config.js b/test/function/transformer-multiple/_config.js new file mode 100644 index 0000000..34ddbf7 --- /dev/null +++ b/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 ); + } +} diff --git a/test/function/transformer-multiple/foo.js b/test/function/transformer-multiple/foo.js new file mode 100644 index 0000000..119481e --- /dev/null +++ b/test/function/transformer-multiple/foo.js @@ -0,0 +1 @@ +export default MAGIC_NUMBER; diff --git a/test/function/transformer-multiple/main.js b/test/function/transformer-multiple/main.js new file mode 100644 index 0000000..0fb821d --- /dev/null +++ b/test/function/transformer-multiple/main.js @@ -0,0 +1,3 @@ +import foo from './foo'; + +export var magicNumber = foo; diff --git a/test/function/transformer-single/_config.js b/test/function/transformer-single/_config.js new file mode 100644 index 0000000..4cf7a76 --- /dev/null +++ b/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 ); + } +} diff --git a/test/function/transformer-single/foo.js b/test/function/transformer-single/foo.js new file mode 100644 index 0000000..119481e --- /dev/null +++ b/test/function/transformer-single/foo.js @@ -0,0 +1 @@ +export default MAGIC_NUMBER; diff --git a/test/function/transformer-single/main.js b/test/function/transformer-single/main.js new file mode 100644 index 0000000..0fb821d --- /dev/null +++ b/test/function/transformer-single/main.js @@ -0,0 +1,3 @@ +import foo from './foo'; + +export var magicNumber = foo;