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
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)

View File

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

View File

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

View File

@ -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})
);
}
}

View File

@ -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(

View File

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