Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processModuleFilter and remove postProcessModules #215

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ These options are only useful with React Native projects.
| `getPolyfills` | `({platform: ?string}) => $ReadOnlyArray<string>` | An optional list of polyfills to include in the bundle. The list defaults to a set of common polyfills for Number, String, Array, Object... |
| `postProcessBundleSourcemap` | `PostProcessBundleSourcemap` | An optional function that can modify the code and source map of the bundle before it is written. Applied once for the entire bundle. |
| `getModulesRunBeforeMainModule` | `(entryFilePath: string) => Array<string>` | An array of modules to be required before the entry point. It should contain the absolute path of each module. |
| `postProcessModules` | `(modules: Array<Module>, entryFiles: Array<string>) => Array<Module>` | A function that can change the resulting modules output. |
| `processModuleFilter` | `(module: Array<Module>) => boolean` | A filter function to discard specific modules from the output. |
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Object {
"enhanceMiddleware": [Function],
"getUseGlobalHotkey": [Function],
"port": 8080,
"postProcessModules": [Function],
"processModuleFilter": [Function],
"transformVariants": [Function],
},
"serverOptions": Object {
Expand Down Expand Up @@ -91,7 +91,7 @@ Object {
"enhanceMiddleware": undefined,
"getUseGlobalHotkey": [Function],
"port": undefined,
"postProcessModules": undefined,
"processModuleFilter": undefined,
"transformVariants": [Function],
},
"serverOptions": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Object {
"getRunModuleStatement": [Function],
"polyfillModuleNames": Array [],
"postProcessBundleSourcemap": [Function],
"postProcessModules": [Function],
"processModuleFilter": [Function],
},
"server": Object {
"enhanceMiddleware": [Function],
Expand Down Expand Up @@ -166,7 +166,7 @@ Object {
"getRunModuleStatement": [Function],
"polyfillModuleNames": Array [],
"postProcessBundleSourcemap": [Function],
"postProcessModules": [Function],
"processModuleFilter": [Function],
},
"server": Object {
"enhanceMiddleware": [Function],
Expand Down
21 changes: 4 additions & 17 deletions packages/metro-config/src/configTypes.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import type {
PostProcessBundleSourcemap,
} from 'metro/src/Bundler';
import type {TransformResult} from 'metro/src/DeltaBundler';
import type {
Module,
TransformVariants,
} from 'metro/src/ModuleGraph/types.flow.js';
import type {TransformVariants} from 'metro/src/ModuleGraph/types.flow.js';
import type {Module} from 'metro/src/DeltaBundler/types.flow.js';
import type {DynamicRequiresBehavior} from 'metro/src/ModuleGraph/worker/collectDependencies';
import type Server from 'metro/src/Server';
import type {Reporter} from 'metro/src/lib/reporting';
Expand Down Expand Up @@ -201,10 +199,7 @@ export type OldConfigT = {
*/
createModuleIdFactory?: () => (path: string) => number,

postProcessModules?: (
modules: $ReadOnlyArray<Module>,
entryFiles: Array<string>,
) => $ReadOnlyArray<Module>,
processModuleFilter: (modules: Module<>) => boolean,

transformVariants?: () => TransformVariants,
};
Expand Down Expand Up @@ -318,11 +313,6 @@ export type InputConfigT = {
* contain the absolute path of each module.
*/
getModulesRunBeforeMainModule?: (entryFilePath: string) => Array<string>,

postProcessModules?: (
modules: $ReadOnlyArray<Module>,
paths: Array<string>,
) => $ReadOnlyArray<Module>,
},
transformer?: {
assetRegistryPath?: string,
Expand Down Expand Up @@ -430,10 +420,7 @@ export type IntermediateConfigT = {
getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
postProcessBundleSourcemap: PostProcessBundleSourcemap,
getModulesRunBeforeMainModule: (entryFilePath: string) => Array<string>,
postProcessModules: (
modules: $ReadOnlyArray<Module>,
paths: Array<string>,
) => $ReadOnlyArray<Module>,
processModuleFilter: (modules: Module<>) => boolean,
createModuleIdFactory: () => (path: string) => number,
},
transformer: {
Expand Down
17 changes: 5 additions & 12 deletions packages/metro-config/src/convertConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ const getMaxWorkers = require('metro/src/lib/getMaxWorkers');
const {Terminal} = require('metro-core');

import type {ConfigT, OldConfigT, Middleware} from './configTypes.flow';
import type {
Module,
TransformVariants,
} from 'metro/src/ModuleGraph/types.flow.js';
import type {TransformVariants} from 'metro/src/ModuleGraph/types.flow.js';
import type Server from 'metro/src/Server';
import type {Reporter} from 'metro/src/lib/reporting';
import type {Options as ServerOptions} from 'metro/src/shared/types.flow';
Expand Down Expand Up @@ -88,7 +85,7 @@ function convertOldToNew({
getWorkerPath,
extraNodeModules,
transformVariants,
postProcessModules,
processModuleFilter,
} = config;

const assetExts = defaults.assetExts.concat(
Expand Down Expand Up @@ -127,7 +124,7 @@ function convertOldToNew({
getRunModuleStatement,
getPolyfills,
postProcessBundleSourcemap,
postProcessModules: postProcessModules || (modules => modules),
processModuleFilter: processModuleFilter || (module => true),
getModulesRunBeforeMainModule,
},
server: {
Expand Down Expand Up @@ -167,10 +164,6 @@ export type ConvertedOldConfigT = {
port: number,
getUseGlobalHotkey: () => boolean,
transformVariants: () => TransformVariants,
postProcessModules: (
modules: $ReadOnlyArray<Module>,
paths: Array<string>,
) => $ReadOnlyArray<Module>,
},
};

Expand Down Expand Up @@ -215,8 +208,8 @@ function convertNewToOld(newConfig: ConfigT): ConvertedOldConfigT {
getPolyfills,
postProcessBundleSourcemap,
getModulesRunBeforeMainModule,
postProcessModules,
createModuleIdFactory,
processModuleFilter,
} = serializer;

const {useGlobalHotkey, port, enhanceMiddleware} = server;
Expand Down Expand Up @@ -274,8 +267,8 @@ function convertNewToOld(newConfig: ConfigT): ConvertedOldConfigT {
enhanceMiddleware,
getUseGlobalHotkey: () => useGlobalHotkey,
port,
processModuleFilter,
transformVariants: () => transformVariants,
postProcessModules,
},
};

Expand Down
2 changes: 1 addition & 1 deletion packages/metro-config/src/defaults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const getDefaultValues = (projectRoot: ?string): IntermediateConfigT => ({
getPolyfills: () => [],
postProcessBundleSourcemap: ({code, map, outFileName}) => ({code, map}),
getModulesRunBeforeMainModule: () => [],
postProcessModules: modules => modules,
processModuleFilter: module => true,
createModuleIdFactory: defaultCreateModuleIdFactory,
},

Expand Down
2 changes: 1 addition & 1 deletion packages/metro-config/src/oldConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const DEFAULT = ({
getResolverMainFields: () => ['browser', 'main'],
getModulesRunBeforeMainModule: () => [],
getWorkerPath: () => null,
postProcessModules: modules => modules,
processModuleFilter: module => true,
}: ConfigT);

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const graph = {
};

const options = {
processModuleFilter: module => true,
createModuleId: createModuleIdFactory(),
dev: true,
getRunModuleStatement: moduleId => `require(${JSON.stringify(moduleId)});`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ describe('getOrderedDependencyPaths', () => {
await getAllFiles(
[{path: '/tmp/0.js', output: [{type: 'js/module'}]}],
graph,
{},
{
processModuleFilter: () => true,
},
),
).toEqual([
'/tmp/0.js',
Expand All @@ -60,7 +62,9 @@ describe('getOrderedDependencyPaths', () => {
]),
};

expect(await getAllFiles([], graph, {})).toEqual([
expect(
await getAllFiles([], graph, {processModuleFilter: () => true}),
).toEqual([
'/tmp/1.js',
'/tmp/2.png@2x',
'/tmp/2.png@3x',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ it('should return the bundle assets', async () => {
]),
};

expect(await getAssets(graph, {watchFolders: ['/tmp']})).toEqual([
expect(
await getAssets(graph, {
watchFolders: ['/tmp'],
processModuleFilter: () => true,
}),
).toEqual([
{path: '/tmp/3.png', localPath: '3.png'},
{path: '/tmp/5.mov', localPath: '5.mov'},
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const getRunModuleStatement = moduleId =>
it('should return the RAM bundle info', async () => {
expect(
await getRamBundleInfo('/root/entry.js', pre, graph, {
processModuleFilter: module => true,
createModuleId: path => path,
excludeSource: false,
getRunModuleStatement,
Expand All @@ -72,6 +73,7 @@ it('should use the preloadedModules and ramGroup configs to build a RAM bundle',
});

const bundleInfo = await getRamBundleInfo('/root/entry.js', pre, graph, {
processModuleFilter: module => true,
createModuleId: path => path,
excludeSource: false,
getRunModuleStatement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ it('should serialize a very simple bundle', () => {
entryPoints: ['foo'],
},
{
processModuleFilter: () => true,
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
Expand Down Expand Up @@ -88,6 +89,7 @@ it('should add runBeforeMainModule statements if found in the graph', () => {
entryPoints: ['/root/foo'],
},
{
processModuleFilter: () => true,
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement,
Expand Down Expand Up @@ -122,6 +124,7 @@ it('should handle numeric module ids', () => {
entryPoints: ['/root/foo'],
},
{
processModuleFilter: () => true,
createModuleId: createModuleIdFactory(),
dev: true,
getRunModuleStatement,
Expand Down Expand Up @@ -156,6 +159,7 @@ it('outputs custom runModule statements', () => {
entryPoints: ['/root/foo'],
},
{
processModuleFilter: () => true,
createModuleId: filePath => path.basename(filePath),
dev: true,
getRunModuleStatement: moduleId =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ it('should serialize a very simple bundle', () => {
dependencies: new Map([['foo', fooModule], ['bar', barModule]]),
entryPoints: ['foo'],
},
{excludesSource: false},
{
excludesSource: false,
processModuleFilter: module => true,
},
),
),
).toEqual({
Expand Down
9 changes: 6 additions & 3 deletions packages/metro/src/DeltaBundler/Serializers/deltaJSBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {getJsOutput, isJsModule} = require('./helpers/js');
import type {DeltaResult, Graph, Module} from '../types.flow';

type Options = {|
+processModuleFilter: (module: Module<>) => boolean,
+createModuleId: string => number | string,
+dev: boolean,
+getRunModuleStatement: (number | string) => string,
Expand All @@ -39,8 +40,10 @@ function deltaJSBundle(
const outputPost = [];
const outputDelta = [];

const {processModuleFilter} = options;

for (const module of delta.modified.values()) {
if (isJsModule(module)) {
if (isJsModule(module) && processModuleFilter(module)) {
outputDelta.push([
options.createModuleId(module.path),
wrapModule(module, options),
Expand All @@ -56,7 +59,7 @@ function deltaJSBundle(
let i = -1;

for (const module of pre) {
if (isJsModule(module)) {
if (isJsModule(module) && processModuleFilter(module)) {
outputPre.push([i, getJsOutput(module).data.code]);
i--;
}
Expand All @@ -65,7 +68,7 @@ function deltaJSBundle(
const appendScripts = getAppendScripts(entryPoint, graph, options);

for (const module of appendScripts) {
if (isJsModule(module)) {
if (isJsModule(module) && processModuleFilter(module)) {
outputPost.push([
options.createModuleId(module.path),
getJsOutput(module).data.code,
Expand Down
8 changes: 6 additions & 2 deletions packages/metro/src/DeltaBundler/Serializers/getAllFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {Graph, Module} from '../types.flow';

type Options = {|
platform: ?string,
+processModuleFilter: (module: Module<>) => boolean,
|};

async function getAllFiles(
Expand All @@ -25,15 +26,18 @@ async function getAllFiles(
options: Options,
): Promise<$ReadOnlyArray<string>> {
const modules = graph.dependencies;
const {processModuleFilter} = options;

const promises = [];

for (const module of pre) {
promises.push([module.path]);
if (processModuleFilter(module)) {
promises.push([module.path]);
}
}

for (const module of modules.values()) {
if (!isJsModule(module)) {
if (!isJsModule(module) || !processModuleFilter(module)) {
continue;
}

Expand Down
10 changes: 8 additions & 2 deletions packages/metro/src/DeltaBundler/Serializers/getAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const {getAssetData} = require('../../Assets');
const {getJsOutput, isJsModule} = require('./helpers/js');

import type {AssetData} from '../../Assets';
import type {Graph} from '../types.flow';
import type {Graph, Module} from '../types.flow';

type Options = {|
+processModuleFilter: (module: Module<>) => boolean,
assetPlugins: $ReadOnlyArray<string>,
platform: ?string,
watchFolders: $ReadOnlyArray<string>,
Expand All @@ -29,9 +30,14 @@ async function getAssets(
options: Options,
): Promise<$ReadOnlyArray<AssetData>> {
const promises = [];
const {processModuleFilter} = options;

for (const module of graph.dependencies.values()) {
if (isJsModule(module) && getJsOutput(module).type === 'js/module/asset') {
if (
isJsModule(module) &&
processModuleFilter(module) &&
getJsOutput(module).type === 'js/module/asset'
) {
promises.push(
getAssetData(
module.path,
Expand Down
Loading