getTransformOptions based on bundle and module

Summary:
Currently, the app server accepts `transformModulePath` which allows us to use different transformation variants. However, these options persist through the lifetime of the server. So we cannot conditionally transform a module differently for two bundles without restarting the server with different options.

`getTransformOptions` basically allows us to change the options to the transformer at runtime based on the bundle and module being transformed.

These options are also used as a cache key for the transformedSource to ensure that if a file is transformed with different options, caching doesn't cause any inconsistencies.

public

Reviewed By: martinbigio

Differential Revision: D2776399

fb-gh-sync-id: 1e0f34d8fa7f0377fcf81f23eb6f3236bd397d56
This commit is contained in:
Bhuwan Khattar 2015-12-21 12:14:33 -08:00 committed by facebook-github-bot-7
parent 90d1b6ee62
commit da44545090
6 changed files with 20 additions and 3 deletions

View File

@ -112,6 +112,8 @@ middleware. Takes the following options:
should be used as a persistent deamon to watch files and update should be used as a persistent deamon to watch files and update
itself itself
* `assetRoots` array: Where should the packager look for assets * `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) ### ReactPackager.buildPackageFromUrl(options, url)

View File

@ -58,6 +58,10 @@ class Bundle {
return this._modules; return this._modules;
} }
getMainModuleId() {
return this._mainModuleId;
}
setNumPrependedModules(n) { setNumPrependedModules(n) {
this._numPrependedModules = n; this._numPrependedModules = n;
} }

View File

@ -81,6 +81,7 @@ describe('Bundler', function() {
bundler = new Bundler({ bundler = new Bundler({
projectRoots: ['/root'], projectRoots: ['/root'],
assetServer: assetServer, assetServer: assetServer,
getTransformOptions: () => ({}),
}); });
modules = [ modules = [

View File

@ -135,6 +135,8 @@ class Bundler {
this._projectRoots = opts.projectRoots; this._projectRoots = opts.projectRoots;
this._assetServer = opts.assetServer; this._assetServer = opts.assetServer;
this._getTransformOptions = opts.getTransformOptions;
} }
kill() { kill() {
@ -322,7 +324,8 @@ class Bundler {
return generateJSONModule(module); return generateJSONModule(module);
} else { } else {
return this._transformer.loadFileAndTransform( return this._transformer.loadFileAndTransform(
path.resolve(module.path) path.resolve(module.path),
this._getTransformOptions({bundle, module, platform})
); );
} }
} }

View File

@ -82,16 +82,18 @@ class Transformer {
this._cache.invalidate(filePath); this._cache.invalidate(filePath);
} }
loadFileAndTransform(filePath) { loadFileAndTransform(filePath, options) {
if (this._transform == null) { if (this._transform == null) {
return Promise.reject(new Error('No transfrom module')); return Promise.reject(new Error('No transfrom module'));
} }
debug('transforming file', filePath); debug('transforming file', filePath);
const optionsJSON = JSON.stringify(options);
return this._cache.get( return this._cache.get(
filePath, filePath,
'transformedSource', 'transformedSource-' + optionsJSON,
// TODO: use fastfs to avoid reading file from disk again // TODO: use fastfs to avoid reading file from disk again
() => readFile(filePath).then( () => readFile(filePath).then(
buffer => { buffer => {
@ -100,6 +102,7 @@ class Transformer {
return this._transform({ return this._transform({
sourceCode, sourceCode,
filename: filePath, filename: filePath,
options,
}).then(res => { }).then(res => {
if (res.error) { if (res.error) {
console.warn( console.warn(

View File

@ -64,6 +64,10 @@ const validateOpts = declareOpts({
type: 'number', type: 'number',
required: false, required: false,
}, },
getTransformOptions: {
type: 'function',
default: () => ({}),
}
}); });
const bundleOpts = declareOpts({ const bundleOpts = declareOpts({