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

View File

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

View File

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