packager: worker: remove `extern` option

Reviewed By: davidaurelio

Differential Revision: D4867367

fbshipit-source-id: ebe5fa860566e87bd6d042ee41b9a7aa9c777fc6
This commit is contained in:
Jean Lauliac 2017-04-11 08:08:12 -07:00 committed by Facebook Github Bot
parent 5790a4b57f
commit 87effd2669
5 changed files with 13 additions and 64 deletions

View File

@ -140,18 +140,6 @@ describe('code transformation worker:', () => {
} }
); );
it('does not extract requires if files are marked as "extern"', done => {
const opts = {extern: true};
transformCode(transformer, 'filename', 'code', opts, (error, data) => {
expect(error).toBeNull();
const {dependencies, dependencyOffsets} = data.result;
expect(extractDependencies).not.toBeCalled();
expect(dependencies).toEqual([]);
expect(dependencyOffsets).toEqual([]);
done();
});
});
it('does not extract requires of JSON files', done => { it('does not extract requires of JSON files', done => {
const jsonStr = '{"arbitrary":"json"}'; const jsonStr = '{"arbitrary":"json"}';
transformCode(transformer, 'arbitrary.json', jsonStr, {}, (error, data) => { transformCode(transformer, 'arbitrary.json', jsonStr, {}, (error, data) => {

View File

@ -37,21 +37,20 @@ type Transformer = {
export type TransformOptions = { export type TransformOptions = {
+dev: boolean, +dev: boolean,
generateSourceMaps: boolean, +generateSourceMaps: boolean,
+hot: boolean, +hot: boolean,
+inlineRequires: {+blacklist: {[string]: true}} | boolean, +inlineRequires: {+blacklist: {[string]: true}} | boolean,
platform: string, +platform: string,
preloadedModules?: Array<string> | false, +preloadedModules?: Array<string> | false,
projectRoots: Array<string>, +projectRoots: Array<string>,
ramGroups?: Array<string>, +ramGroups?: Array<string>,
} & BabelTransformOptions; } & BabelTransformOptions;
export type Options = { export type Options = {
+dev: boolean, +dev: boolean,
+extern?: boolean,
+minify: boolean, +minify: boolean,
platform: string, +platform: string,
transform: TransformOptions, +transform: TransformOptions,
}; };
export type Data = { export type Data = {
@ -119,7 +118,7 @@ function transformCode(
code = code.replace(/^#!.*/, ''); code = code.replace(/^#!.*/, '');
} }
const depsResult = isJson || options.extern const depsResult = isJson
? {dependencies: [], dependencyOffsets: []} ? {dependencies: [], dependencyOffsets: []}
: extractDependencies(code); : extractDependencies(code);
@ -149,7 +148,7 @@ exports.transformAndExtractDependencies = (
) => { ) => {
/* $FlowFixMe: impossible to type a dynamic require */ /* $FlowFixMe: impossible to type a dynamic require */
const transformModule = require(transform); const transformModule = require(transform);
transformCode(transformModule, filename, sourceCode, options || {}, callback); transformCode(transformModule, filename, sourceCode, options, callback);
}; };
exports.minify = ( exports.minify = (

View File

@ -335,14 +335,14 @@ class OptionsHasher {
* cleanup will be necessary to enable rock-solid typing. * cleanup will be necessary to enable rock-solid typing.
*/ */
hashTransformWorkerOptions(hash: crypto$Hash, options: TransformWorkerOptions): crypto$Hash { hashTransformWorkerOptions(hash: crypto$Hash, options: TransformWorkerOptions): crypto$Hash {
const {dev, minify, platform, transform, extern, ...unknowns} = options; const {dev, minify, platform, transform, ...unknowns} = options;
const unknownKeys = Object.keys(unknowns); const unknownKeys = Object.keys(unknowns);
if (unknownKeys.length > 0) { if (unknownKeys.length > 0) {
const message = `these worker option fields are unknown: ${JSON.stringify(unknownKeys)}`; const message = `these worker option fields are unknown: ${JSON.stringify(unknownKeys)}`;
throw new CannotHashOptionsError(message); throw new CannotHashOptionsError(message);
} }
// eslint-disable-next-line no-undef, no-bitwise // eslint-disable-next-line no-undef, no-bitwise
hash.update(new Buffer([+dev | +minify << 1 | +!!extern << 2])); hash.update(new Buffer([+dev | +minify << 1]));
hash.update(JSON.stringify(platform)); hash.update(JSON.stringify(platform));
return this.hashTransformOptions(hash, transform); return this.hashTransformOptions(hash, transform);
} }

View File

@ -421,14 +421,7 @@ class Module {
_getCacheProps(transformOptions: TransformOptions, transformOptionsKey: string) { _getCacheProps(transformOptions: TransformOptions, transformOptionsKey: string) {
const sourceCode = this._readSourceCode(); const sourceCode = this._readSourceCode();
const moduleDocBlock = this._readDocBlock();
const getTransformCacheKey = this._getTransformCacheKey; const getTransformCacheKey = this._getTransformCacheKey;
// Ignore requires in JSON files or generated code. An example of this
// is prebuilt files like the SourceMap library.
const extern = this.isJSON() || 'extern' in moduleDocBlock;
if (extern) {
transformOptions = {...transformOptions, extern};
}
return { return {
filePath: this.path, filePath: this.path,
sourceCode, sourceCode,

View File

@ -213,40 +213,11 @@ describe('Module', () => {
); );
}); });
it('passes module and file contents if the file is annotated with @extern', () => {
const module = createModule({transformCode});
const customFileContents = `
/**
* @extern
*/
`;
mockIndexFile(customFileContents);
return module.read().then(() => {
expect(transformCode).toBeCalledWith(module, customFileContents, {extern: true});
});
});
it('passes the module and file contents to the transform for JSON files', () => { it('passes the module and file contents to the transform for JSON files', () => {
mockPackageFile(); mockPackageFile();
const module = createJSONModule({transformCode}); const module = createJSONModule({transformCode});
return module.read().then(() => { return module.read().then(() => {
expect(transformCode).toBeCalledWith(module, packageJson, {extern: true}); expect(transformCode).toBeCalledWith(module, packageJson, undefined);
});
});
it('does not extend the passed options object if the file is annotated with @extern', () => {
const module = createModule({transformCode});
const customFileContents = `
/**
* @extern
*/
`;
mockIndexFile(customFileContents);
const options = {arbitrary: 'foo'};
return module.read(options).then(() => {
expect(options).not.toEqual(jasmine.objectContaining({extern: true}));
expect(transformCode)
.toBeCalledWith(module, customFileContents, {...options, extern: true});
}); });
}); });
@ -255,9 +226,7 @@ describe('Module', () => {
const module = createJSONModule({transformCode}); const module = createJSONModule({transformCode});
const options = {arbitrary: 'foo'}; const options = {arbitrary: 'foo'};
return module.read(options).then(() => { return module.read(options).then(() => {
expect(options).not.toEqual(jasmine.objectContaining({extern: true})); expect(transformCode).toBeCalledWith(module, packageJson, options);
expect(transformCode)
.toBeCalledWith(module, packageJson, {...options, extern: true});
}); });
}); });