packager Module.js: make transformCode function required

Reviewed By: davidaurelio

Differential Revision: D4147343

fbshipit-source-id: 4c00a5905e698a9d257a632f3b02b63587b4644a
This commit is contained in:
Jean Lauliac 2016-11-10 09:06:58 -08:00 committed by Facebook Github Bot
parent f40aafac8a
commit b2653ed36c
3 changed files with 15 additions and 17 deletions

View File

@ -58,8 +58,8 @@ function getCacheFilePaths(props: {
type CachedResult = { type CachedResult = {
code: string, code: string,
dependencies?: ?Array<string>, dependencies: Array<string>,
dependencyOffsets?: ?Array<number>, dependencyOffsets: Array<number>,
map?: ?{}, map?: ?{},
}; };
@ -205,8 +205,8 @@ function readMetadataFileSync(
): ?{ ): ?{
cachedResultHash: number, cachedResultHash: number,
cachedSourceHash: number, cachedSourceHash: number,
dependencies: ?Array<string>, dependencies: Array<string>,
dependencyOffsets: ?Array<number>, dependencyOffsets: Array<number>,
sourceMap: ?{}, sourceMap: ?{},
} { } {
const metadataStr = fs.readFileSync(metadataFilePath, 'utf8'); const metadataStr = fs.readFileSync(metadataFilePath, 'utf8');
@ -232,8 +232,8 @@ function readMetadataFileSync(
if ( if (
typeof cachedResultHash !== 'number' || typeof cachedResultHash !== 'number' ||
typeof cachedSourceHash !== 'number' || typeof cachedSourceHash !== 'number' ||
!(dependencies == null || (Array.isArray(dependencies) && dependencies.every(dep => typeof dep === 'string'))) || !(Array.isArray(dependencies) && dependencies.every(dep => typeof dep === 'string')) ||
!(dependencyOffsets == null || (Array.isArray(dependencyOffsets) && dependencyOffsets.every(offset => typeof offset === 'number'))) || !(Array.isArray(dependencyOffsets) && dependencyOffsets.every(offset => typeof offset === 'number')) ||
!(sourceMap == null || typeof sourceMap === 'object') !(sourceMap == null || typeof sourceMap === 'object')
) { ) {
return null; return null;

View File

@ -27,8 +27,8 @@ import type FastFs from './fastfs';
type TransformedCode = { type TransformedCode = {
code: string, code: string,
dependencies?: ?Array<string>, dependencies: Array<string>,
dependencyOffsets?: ?Array<number>, dependencyOffsets: Array<number>,
map?: ?{}, map?: ?{},
}; };
@ -225,18 +225,13 @@ class Module {
transformOptions: mixed, transformOptions: mixed,
callback: (error: ?Error, result: ?TransformedCode) => void, callback: (error: ?Error, result: ?TransformedCode) => void,
) { ) {
const transformCode = this._transformCode; const {_transformCode} = this;
// AssetModule_DEPRECATED doesn't provide transformCode, but these should // AssetModule_DEPRECATED doesn't provide transformCode, but these should
// never be transformed anyway. // never be transformed anyway.
invariant(transformCode != null, 'missing code transform funtion'); invariant(_transformCode != null, 'missing code transform funtion');
this._readSourceCode().then(sourceCode => { this._readSourceCode().then(sourceCode => {
if (!transformCode) { return _transformCode(this, sourceCode, transformOptions)
return callback(null, {code: sourceCode}); .then(freshResult => callback(undefined, freshResult));
}
const codePromise = transformCode(this, sourceCode, transformOptions);
return codePromise.then(freshResult => {
callback(undefined, freshResult);
});
}, callback); }, callback);
} }

View File

@ -69,6 +69,9 @@ describe('Module', () => {
options: { options: {
cacheTransformResults: true, cacheTransformResults: true,
}, },
transformCode: (module, sourceCode, transformOptions) => {
return Promise.resolve({code: sourceCode});
},
...options, ...options,
cache, cache,
fastfs, fastfs,