diff --git a/README.md b/README.md index 83683939..d14ae032 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,8 @@ middleware. Takes the following options: should be used as a persistent deamon to watch files and update itself * `assetRoots` array: Where should the packager look for assets +* `getTransformOptions` function: Middleware to get custom options for the + transformer based on the bundle and module being transformed. ### ReactPackager.buildPackageFromUrl(options, url) diff --git a/react-packager/src/Bundler/Bundle.js b/react-packager/src/Bundler/Bundle.js index b945dd42..007dca29 100644 --- a/react-packager/src/Bundler/Bundle.js +++ b/react-packager/src/Bundler/Bundle.js @@ -58,6 +58,10 @@ class Bundle { return this._modules; } + getMainModuleId() { + return this._mainModuleId; + } + setNumPrependedModules(n) { this._numPrependedModules = n; } diff --git a/react-packager/src/Bundler/__tests__/Bundler-test.js b/react-packager/src/Bundler/__tests__/Bundler-test.js index ccdc8496..37d200f2 100644 --- a/react-packager/src/Bundler/__tests__/Bundler-test.js +++ b/react-packager/src/Bundler/__tests__/Bundler-test.js @@ -81,6 +81,7 @@ describe('Bundler', function() { bundler = new Bundler({ projectRoots: ['/root'], assetServer: assetServer, + getTransformOptions: () => ({}), }); modules = [ diff --git a/react-packager/src/Bundler/index.js b/react-packager/src/Bundler/index.js index 31794a9b..19174129 100644 --- a/react-packager/src/Bundler/index.js +++ b/react-packager/src/Bundler/index.js @@ -135,6 +135,8 @@ class Bundler { this._projectRoots = opts.projectRoots; this._assetServer = opts.assetServer; + + this._getTransformOptions = opts.getTransformOptions; } kill() { @@ -322,7 +324,8 @@ class Bundler { return generateJSONModule(module); } else { return this._transformer.loadFileAndTransform( - path.resolve(module.path) + path.resolve(module.path), + this._getTransformOptions({bundle, module, platform}) ); } } diff --git a/react-packager/src/JSTransformer/index.js b/react-packager/src/JSTransformer/index.js index 5c819381..07225252 100644 --- a/react-packager/src/JSTransformer/index.js +++ b/react-packager/src/JSTransformer/index.js @@ -82,16 +82,18 @@ class Transformer { this._cache.invalidate(filePath); } - loadFileAndTransform(filePath) { + loadFileAndTransform(filePath, options) { if (this._transform == null) { return Promise.reject(new Error('No transfrom module')); } debug('transforming file', filePath); + const optionsJSON = JSON.stringify(options); + return this._cache.get( filePath, - 'transformedSource', + 'transformedSource-' + optionsJSON, // TODO: use fastfs to avoid reading file from disk again () => readFile(filePath).then( buffer => { @@ -100,6 +102,7 @@ class Transformer { return this._transform({ sourceCode, filename: filePath, + options, }).then(res => { if (res.error) { console.warn( diff --git a/react-packager/src/Server/index.js b/react-packager/src/Server/index.js index 121dcddb..a1fb415a 100644 --- a/react-packager/src/Server/index.js +++ b/react-packager/src/Server/index.js @@ -64,6 +64,10 @@ const validateOpts = declareOpts({ type: 'number', required: false, }, + getTransformOptions: { + type: 'function', + default: () => ({}), + } }); const bundleOpts = declareOpts({