Skip to content

Commit

Permalink
getTransformOptions based on bundle and module
Browse files Browse the repository at this point in the history
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
  • Loading branch information
voideanvalue authored and facebook-github-bot-7 committed Dec 21, 2015
1 parent 4cb7752 commit 6c02d5b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packager/README.md
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions packager/react-packager/src/Bundler/Bundle.js
Expand Up @@ -58,6 +58,10 @@ class Bundle {
return this._modules;
}

getMainModuleId() {
return this._mainModuleId;
}

setNumPrependedModules(n) {
this._numPrependedModules = n;
}
Expand Down
Expand Up @@ -81,6 +81,7 @@ describe('Bundler', function() {
bundler = new Bundler({
projectRoots: ['/root'],
assetServer: assetServer,
getTransformOptions: () => ({}),
});

modules = [
Expand Down
5 changes: 4 additions & 1 deletion packager/react-packager/src/Bundler/index.js
Expand Up @@ -135,6 +135,8 @@ class Bundler {

this._projectRoots = opts.projectRoots;
this._assetServer = opts.assetServer;

this._getTransformOptions = opts.getTransformOptions;
}

kill() {
Expand Down Expand Up @@ -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})
);
}
}
Expand Down
7 changes: 5 additions & 2 deletions packager/react-packager/src/JSTransformer/index.js
Expand Up @@ -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 => {
Expand All @@ -100,6 +102,7 @@ class Transformer {
return this._transform({
sourceCode,
filename: filePath,
options,
}).then(res => {
if (res.error) {
console.warn(
Expand Down
4 changes: 4 additions & 0 deletions packager/react-packager/src/Server/index.js
Expand Up @@ -64,6 +64,10 @@ const validateOpts = declareOpts({
type: 'number',
required: false,
},
getTransformOptions: {
type: 'function',
default: () => ({}),
}
});

const bundleOpts = declareOpts({
Expand Down

0 comments on commit 6c02d5b

Please sign in to comment.