mirror of https://github.com/status-im/metro.git
Add support for the postProcessModules param
Reviewed By: davidaurelio Differential Revision: D6277777 fbshipit-source-id: b9a0341f17f3829ccc3daf7db89275f07e435803
This commit is contained in:
parent
42a547551f
commit
f1728e4c7b
|
@ -36,6 +36,7 @@ import type {Options as JSTransformerOptions} from '../JSTransformer/worker';
|
||||||
import type {Reporter} from '../lib/reporting';
|
import type {Reporter} from '../lib/reporting';
|
||||||
import type {TransformCache} from '../lib/TransformCaching';
|
import type {TransformCache} from '../lib/TransformCaching';
|
||||||
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
|
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
|
||||||
|
import type {PostProcessModules} from '../DeltaBundler';
|
||||||
|
|
||||||
export type BundlingOptions = {|
|
export type BundlingOptions = {|
|
||||||
+preloadedModules: ?{[string]: true} | false,
|
+preloadedModules: ?{[string]: true} | false,
|
||||||
|
@ -80,18 +81,6 @@ export type ExtendedAssetDescriptor = AssetDescriptor & {
|
||||||
+fileSystemLocation: string,
|
+fileSystemLocation: string,
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PostProcessModulesOptions = {|
|
|
||||||
dev: boolean,
|
|
||||||
minify: boolean,
|
|
||||||
platform: string,
|
|
||||||
|};
|
|
||||||
|
|
||||||
export type PostProcessModules = (
|
|
||||||
modules: Array<ModuleTransport>,
|
|
||||||
entryFile: string,
|
|
||||||
options: PostProcessModulesOptions,
|
|
||||||
) => Array<ModuleTransport>;
|
|
||||||
|
|
||||||
export type PostMinifyProcess = ({
|
export type PostMinifyProcess = ({
|
||||||
code: string,
|
code: string,
|
||||||
map: ?MappingsMap,
|
map: ?MappingsMap,
|
||||||
|
|
|
@ -19,9 +19,9 @@ const {providesModuleNodeModules} = require('./defaults');
|
||||||
import type {
|
import type {
|
||||||
GetTransformOptions,
|
GetTransformOptions,
|
||||||
PostMinifyProcess,
|
PostMinifyProcess,
|
||||||
PostProcessModules,
|
|
||||||
PostProcessBundleSourcemap,
|
PostProcessBundleSourcemap,
|
||||||
} from './Bundler';
|
} from './Bundler';
|
||||||
|
import type {PostProcessModules} from './DeltaBundler';
|
||||||
import type {PostProcessModules as PostProcessModulesForBuck} from './ModuleGraph/types.flow.js';
|
import type {PostProcessModules as PostProcessModulesForBuck} from './ModuleGraph/types.flow.js';
|
||||||
import type {TransformVariants} from './ModuleGraph/types.flow';
|
import type {TransformVariants} from './ModuleGraph/types.flow';
|
||||||
import type {HasteImpl} from './node-haste/Module';
|
import type {HasteImpl} from './node-haste/Module';
|
||||||
|
|
|
@ -95,10 +95,14 @@ class DeltaPatcher {
|
||||||
return this._lastModifiedDate;
|
return this._lastModifiedDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
getAllModules(): Array<DeltaEntry> {
|
getAllModules(
|
||||||
|
modifierFn: (
|
||||||
|
modules: $ReadOnlyArray<DeltaEntry>,
|
||||||
|
) => $ReadOnlyArray<DeltaEntry> = modules => modules,
|
||||||
|
): $ReadOnlyArray<DeltaEntry> {
|
||||||
return [].concat(
|
return [].concat(
|
||||||
Array.from(this._lastBundle.pre.values()),
|
Array.from(this._lastBundle.pre.values()),
|
||||||
Array.from(this._lastBundle.modules.values()),
|
modifierFn(Array.from(this._lastBundle.modules.values())),
|
||||||
Array.from(this._lastBundle.post.values()),
|
Array.from(this._lastBundle.post.values()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ import type {Options as JSTransformerOptions} from '../JSTransformer/worker';
|
||||||
import type Resolver from '../Resolver';
|
import type Resolver from '../Resolver';
|
||||||
import type {CompactRawMappings} from '../lib/SourceMap';
|
import type {CompactRawMappings} from '../lib/SourceMap';
|
||||||
import type Module from '../node-haste/Module';
|
import type Module from '../node-haste/Module';
|
||||||
import type {Options as BundleOptions} from './';
|
import type {Options as BundleOptions, MainOptions} from './';
|
||||||
import type {DependencyEdges} from './traverseDependencies';
|
import type {DependencyEdges} from './traverseDependencies';
|
||||||
|
|
||||||
export type DeltaEntryType =
|
export type DeltaEntryType =
|
||||||
|
@ -56,11 +56,6 @@ export type DeltaTransformResponse = {|
|
||||||
+reset: boolean,
|
+reset: boolean,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
type Options = {|
|
|
||||||
+getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
|
|
||||||
+polyfillModuleNames: $ReadOnlyArray<string>,
|
|
||||||
|};
|
|
||||||
|
|
||||||
const globalCreateModuleId = createModuleIdFactory();
|
const globalCreateModuleId = createModuleIdFactory();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -94,7 +89,7 @@ class DeltaTransformer extends EventEmitter {
|
||||||
bundler: Bundler,
|
bundler: Bundler,
|
||||||
resolver: Resolver,
|
resolver: Resolver,
|
||||||
deltaCalculator: DeltaCalculator,
|
deltaCalculator: DeltaCalculator,
|
||||||
options: Options,
|
options: MainOptions,
|
||||||
bundleOptions: BundleOptions,
|
bundleOptions: BundleOptions,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
|
@ -122,7 +117,7 @@ class DeltaTransformer extends EventEmitter {
|
||||||
|
|
||||||
static async create(
|
static async create(
|
||||||
bundler: Bundler,
|
bundler: Bundler,
|
||||||
options: Options,
|
options: MainOptions,
|
||||||
bundleOptions: BundleOptions,
|
bundleOptions: BundleOptions,
|
||||||
): Promise<DeltaTransformer> {
|
): Promise<DeltaTransformer> {
|
||||||
const resolver = await bundler.getResolver();
|
const resolver = await bundler.getResolver();
|
||||||
|
|
|
@ -76,14 +76,12 @@ async function fullSourceMap(
|
||||||
deltaBundler: DeltaBundler,
|
deltaBundler: DeltaBundler,
|
||||||
options: Options,
|
options: Options,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const {id, delta} = await _build(deltaBundler, {
|
const {modules} = await _getAllModules(deltaBundler, {
|
||||||
...options,
|
...options,
|
||||||
wrapModules: true,
|
wrapModules: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const deltaPatcher = DeltaPatcher.get(id).applyDelta(delta);
|
return fromRawMappings(modules).toString(undefined, {
|
||||||
|
|
||||||
return fromRawMappings(deltaPatcher.getAllModules()).toString(undefined, {
|
|
||||||
excludeSource: options.excludeSource,
|
excludeSource: options.excludeSource,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -92,14 +90,12 @@ async function fullSourceMapObject(
|
||||||
deltaBundler: DeltaBundler,
|
deltaBundler: DeltaBundler,
|
||||||
options: Options,
|
options: Options,
|
||||||
): Promise<MappingsMap> {
|
): Promise<MappingsMap> {
|
||||||
const {id, delta} = await _build(deltaBundler, {
|
const {modules} = await _getAllModules(deltaBundler, {
|
||||||
...options,
|
...options,
|
||||||
wrapModules: true,
|
wrapModules: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const deltaPatcher = DeltaPatcher.get(id).applyDelta(delta);
|
return fromRawMappings(modules).toMap(undefined, {
|
||||||
|
|
||||||
return fromRawMappings(deltaPatcher.getAllModules()).toMap(undefined, {
|
|
||||||
excludeSource: options.excludeSource,
|
excludeSource: options.excludeSource,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -111,18 +107,17 @@ async function fullBundle(
|
||||||
deltaBundler: DeltaBundler,
|
deltaBundler: DeltaBundler,
|
||||||
options: Options,
|
options: Options,
|
||||||
): Promise<{bundle: string, numModifiedFiles: number, lastModified: Date}> {
|
): Promise<{bundle: string, numModifiedFiles: number, lastModified: Date}> {
|
||||||
const {id, delta} = await _build(deltaBundler, {
|
const {modules, numModifiedFiles, lastModified} = await _getAllModules(
|
||||||
...options,
|
deltaBundler,
|
||||||
wrapModules: true,
|
options,
|
||||||
});
|
);
|
||||||
|
|
||||||
const deltaPatcher = DeltaPatcher.get(id).applyDelta(delta);
|
const code = modules.map(m => m.code);
|
||||||
const code = deltaPatcher.getAllModules().map(m => m.code);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
bundle: code.join('\n'),
|
bundle: code.join('\n'),
|
||||||
lastModified: deltaPatcher.getLastModifiedDate(),
|
lastModified,
|
||||||
numModifiedFiles: deltaPatcher.getLastNumModifiedFiles(),
|
numModifiedFiles,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,43 +125,62 @@ async function getAllModules(
|
||||||
deltaBundler: DeltaBundler,
|
deltaBundler: DeltaBundler,
|
||||||
options: Options,
|
options: Options,
|
||||||
): Promise<$ReadOnlyArray<DeltaEntry>> {
|
): Promise<$ReadOnlyArray<DeltaEntry>> {
|
||||||
const {id, delta} = await _build(deltaBundler, {
|
const {modules} = await _getAllModules(deltaBundler, {
|
||||||
...options,
|
...options,
|
||||||
wrapModules: true,
|
wrapModules: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
return DeltaPatcher.get(id)
|
return modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function _getAllModules(
|
||||||
|
deltaBundler: DeltaBundler,
|
||||||
|
options: Options,
|
||||||
|
): Promise<{
|
||||||
|
modules: $ReadOnlyArray<DeltaEntry>,
|
||||||
|
numModifiedFiles: number,
|
||||||
|
lastModified: Date,
|
||||||
|
deltaTransformer: DeltaTransformer,
|
||||||
|
}> {
|
||||||
|
const {id, delta, deltaTransformer} = await _build(deltaBundler, {
|
||||||
|
...options,
|
||||||
|
wrapModules: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const deltaPatcher = DeltaPatcher.get(id);
|
||||||
|
|
||||||
|
const modules = deltaPatcher
|
||||||
.applyDelta(delta)
|
.applyDelta(delta)
|
||||||
.getAllModules();
|
.getAllModules(deltaBundler.getPostProcessModulesFn(options.entryFile));
|
||||||
|
|
||||||
|
return {
|
||||||
|
deltaTransformer,
|
||||||
|
lastModified: deltaPatcher.getLastModifiedDate(),
|
||||||
|
modules,
|
||||||
|
numModifiedFiles: deltaPatcher.getLastNumModifiedFiles(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getRamBundleInfo(
|
async function getRamBundleInfo(
|
||||||
deltaBundler: DeltaBundler,
|
deltaBundler: DeltaBundler,
|
||||||
options: Options,
|
options: Options,
|
||||||
): Promise<RamBundleInfo> {
|
): Promise<RamBundleInfo> {
|
||||||
const {id, delta, deltaTransformer} = await _build(deltaBundler, {
|
const {modules, deltaTransformer} = await _getAllModules(deltaBundler, {
|
||||||
...options,
|
...options,
|
||||||
wrapModules: true,
|
wrapModules: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const modules = DeltaPatcher.get(id)
|
const ramModules = modules.map(module => ({
|
||||||
.applyDelta(delta)
|
id: module.id,
|
||||||
.getAllModules()
|
code: module.code,
|
||||||
.map(module => {
|
map: fromRawMappings([module]).toMap(module.path, {
|
||||||
const map = fromRawMappings([module]).toMap(module.path, {
|
excludeSource: options.excludeSource,
|
||||||
excludeSource: options.excludeSource,
|
}),
|
||||||
});
|
name: module.name,
|
||||||
|
sourcePath: module.path,
|
||||||
return {
|
source: module.source,
|
||||||
id: module.id,
|
type: module.type,
|
||||||
code: module.code,
|
}));
|
||||||
map,
|
|
||||||
name: module.name,
|
|
||||||
sourcePath: module.path,
|
|
||||||
source: module.source,
|
|
||||||
type: module.type,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
preloadedModules,
|
preloadedModules,
|
||||||
|
@ -178,7 +192,7 @@ async function getRamBundleInfo(
|
||||||
|
|
||||||
const startupModules = [];
|
const startupModules = [];
|
||||||
const lazyModules = [];
|
const lazyModules = [];
|
||||||
modules.forEach(module => {
|
ramModules.forEach(module => {
|
||||||
if (preloadedModules.hasOwnProperty(module.sourcePath)) {
|
if (preloadedModules.hasOwnProperty(module.sourcePath)) {
|
||||||
startupModules.push(module);
|
startupModules.push(module);
|
||||||
return;
|
return;
|
||||||
|
@ -226,7 +240,7 @@ async function getAssets(
|
||||||
deltaBundler: DeltaBundler,
|
deltaBundler: DeltaBundler,
|
||||||
options: Options,
|
options: Options,
|
||||||
): Promise<$ReadOnlyArray<AssetData>> {
|
): Promise<$ReadOnlyArray<AssetData>> {
|
||||||
const modules = await getAllModules(deltaBundler, options);
|
const {modules} = await _getAllModules(deltaBundler, options);
|
||||||
|
|
||||||
const assets = await Promise.all(
|
const assets = await Promise.all(
|
||||||
modules.map(async module => {
|
modules.map(async module => {
|
||||||
|
|
|
@ -21,6 +21,7 @@ describe('Serializers', () => {
|
||||||
const getDelta = jest.fn();
|
const getDelta = jest.fn();
|
||||||
const getDependenciesFn = jest.fn();
|
const getDependenciesFn = jest.fn();
|
||||||
const getRamOptions = jest.fn();
|
const getRamOptions = jest.fn();
|
||||||
|
const postProcessModules = jest.fn();
|
||||||
let deltaBundler;
|
let deltaBundler;
|
||||||
|
|
||||||
const deltaResponse = {
|
const deltaResponse = {
|
||||||
|
@ -47,6 +48,7 @@ describe('Serializers', () => {
|
||||||
ramGroups: [],
|
ramGroups: [],
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
postProcessModules.mockImplementation(modules => modules);
|
||||||
|
|
||||||
deltaBundler = {
|
deltaBundler = {
|
||||||
async getDeltaTransformer() {
|
async getDeltaTransformer() {
|
||||||
|
@ -66,6 +68,9 @@ describe('Serializers', () => {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
getPostProcessModulesFn() {
|
||||||
|
return postProcessModules;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
setCurrentTime(CURRENT_TIME);
|
setCurrentTime(CURRENT_TIME);
|
||||||
|
@ -253,4 +258,18 @@ describe('Serializers', () => {
|
||||||
}),
|
}),
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should post-process the modules', async () => {
|
||||||
|
postProcessModules.mockImplementation(modules => {
|
||||||
|
return modules.sort(
|
||||||
|
(a, b) => +(a.path < b.path) || +(a.path === b.path) - 1,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
await Serializers.getAllModules(deltaBundler, {
|
||||||
|
deltaBundleId: 10,
|
||||||
|
}),
|
||||||
|
).toMatchSnapshot();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -28,6 +28,35 @@ exports[`Serializers should build the full Source Maps 1`] = `"{\\"version\\":3,
|
||||||
|
|
||||||
exports[`Serializers should build the full Source Maps 2`] = `"{\\"version\\":3,\\"sources\\":[],\\"sourcesContent\\":[],\\"names\\":[],\\"mappings\\":\\"\\"}"`;
|
exports[`Serializers should build the full Source Maps 2`] = `"{\\"version\\":3,\\"sources\\":[],\\"sourcesContent\\":[],\\"names\\":[],\\"mappings\\":\\"\\"}"`;
|
||||||
|
|
||||||
|
exports[`Serializers should post-process the modules 1`] = `
|
||||||
|
Array [
|
||||||
|
Object {
|
||||||
|
"code": "pre;",
|
||||||
|
"id": 1,
|
||||||
|
"path": "/pre.js",
|
||||||
|
"type": "script",
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"code": "another;",
|
||||||
|
"id": 4,
|
||||||
|
"path": "/4.js",
|
||||||
|
"type": "module",
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"code": "module3;",
|
||||||
|
"id": 3,
|
||||||
|
"path": "/3.js",
|
||||||
|
"type": "module",
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"code": "post;",
|
||||||
|
"id": 2,
|
||||||
|
"path": "/p",
|
||||||
|
"type": "require",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`Serializers should return all the bundle modules 1`] = `
|
exports[`Serializers should return all the bundle modules 1`] = `
|
||||||
Array [
|
Array [
|
||||||
Object {
|
Object {
|
||||||
|
|
|
@ -16,10 +16,17 @@ const DeltaTransformer = require('./DeltaTransformer');
|
||||||
|
|
||||||
import type Bundler from '../Bundler';
|
import type Bundler from '../Bundler';
|
||||||
import type {BundleOptions} from '../Server';
|
import type {BundleOptions} from '../Server';
|
||||||
|
import type {DeltaEntry} from './DeltaTransformer';
|
||||||
|
|
||||||
type MainOptions = {|
|
export type PostProcessModules = (
|
||||||
|
modules: $ReadOnlyArray<DeltaEntry>,
|
||||||
|
entryFile: string,
|
||||||
|
) => $ReadOnlyArray<DeltaEntry>;
|
||||||
|
|
||||||
|
export type MainOptions = {|
|
||||||
getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
|
getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
|
||||||
polyfillModuleNames: $ReadOnlyArray<string>,
|
polyfillModuleNames: $ReadOnlyArray<string>,
|
||||||
|
postProcessModules?: PostProcessModules,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
export type Options = BundleOptions & {
|
export type Options = BundleOptions & {
|
||||||
|
@ -83,6 +90,18 @@ class DeltaBundler {
|
||||||
id: bundleId,
|
id: bundleId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getPostProcessModulesFn(
|
||||||
|
entryPoint: string,
|
||||||
|
): (modules: $ReadOnlyArray<DeltaEntry>) => $ReadOnlyArray<DeltaEntry> {
|
||||||
|
const postProcessFn = this._options.postProcessModules;
|
||||||
|
|
||||||
|
if (!postProcessFn) {
|
||||||
|
return modules => modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
return entries => postProcessFn(entries, entryPoint);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = DeltaBundler;
|
module.exports = DeltaBundler;
|
||||||
|
|
|
@ -36,7 +36,6 @@ import type {Reporter} from '../lib/reporting';
|
||||||
import type {Options as DeltaBundlerOptions} from '../DeltaBundler/Serializers';
|
import type {Options as DeltaBundlerOptions} from '../DeltaBundler/Serializers';
|
||||||
import type {
|
import type {
|
||||||
GetTransformOptions,
|
GetTransformOptions,
|
||||||
PostProcessModules,
|
|
||||||
PostMinifyProcess,
|
PostMinifyProcess,
|
||||||
PostProcessBundleSourcemap,
|
PostProcessBundleSourcemap,
|
||||||
} from '../Bundler';
|
} from '../Bundler';
|
||||||
|
@ -45,7 +44,7 @@ import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
|
||||||
import type {SourceMap, Symbolicate} from './symbolicate';
|
import type {SourceMap, Symbolicate} from './symbolicate';
|
||||||
import type {AssetData} from '../AssetServer';
|
import type {AssetData} from '../AssetServer';
|
||||||
import type {RamBundleInfo} from '../DeltaBundler/Serializers';
|
import type {RamBundleInfo} from '../DeltaBundler/Serializers';
|
||||||
|
import type {PostProcessModules} from '../DeltaBundler';
|
||||||
const {
|
const {
|
||||||
createActionStartEntry,
|
createActionStartEntry,
|
||||||
createActionEndEntry,
|
createActionEndEntry,
|
||||||
|
@ -242,6 +241,7 @@ class Server {
|
||||||
this._deltaBundler = new DeltaBundler(this._bundler, {
|
this._deltaBundler = new DeltaBundler(this._bundler, {
|
||||||
getPolyfills: this._opts.getPolyfills,
|
getPolyfills: this._opts.getPolyfills,
|
||||||
polyfillModuleNames: this._opts.polyfillModuleNames,
|
polyfillModuleNames: this._opts.polyfillModuleNames,
|
||||||
|
postProcessModules: this._opts.postProcessModules,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue