mirror of https://github.com/status-im/metro.git
Move module minification logic to Bundler
Reviewed By: jeanlauliac Differential Revision: D6674420 fbshipit-source-id: ee552812437da731f419871cfca9ae930fa8f6a9
This commit is contained in:
parent
eb489bf105
commit
a5b0f236e4
|
@ -42,6 +42,7 @@ var commonOptions = {
|
|||
enableBabelRCLookup: true,
|
||||
extraNodeModules: {},
|
||||
platforms: defaults.platforms,
|
||||
postMinifyProcess: e => e,
|
||||
resetCache: false,
|
||||
sourceExts: defaults.sourceExts,
|
||||
transformModulePath: '/path/to/transformer.js',
|
||||
|
@ -105,4 +106,26 @@ describe('Bundler', function() {
|
|||
});
|
||||
expect(b._opts.platforms).toEqual(['android', 'vr']);
|
||||
});
|
||||
|
||||
it('should minify code using the Transformer', async () => {
|
||||
const code = 'arbitrary(code)';
|
||||
const id = 'arbitrary.js';
|
||||
|
||||
const minifiedCode = 'minified(code)';
|
||||
const minifiedMap = {
|
||||
version: 3,
|
||||
file: ['minified'],
|
||||
sources: [],
|
||||
mappings: '',
|
||||
};
|
||||
|
||||
bundler._transformer.minify = jest
|
||||
.fn()
|
||||
.mockReturnValue(Promise.resolve({code: minifiedCode, map: minifiedMap}));
|
||||
|
||||
const result = await bundler.minifyModule(id, code, []);
|
||||
|
||||
expect(result.code).toEqual(minifiedCode);
|
||||
expect(result.map).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -20,9 +20,16 @@ const defaults = require('../defaults');
|
|||
const fs = require('fs');
|
||||
const getTransformCacheKeyFn = require('../lib/getTransformCacheKeyFn');
|
||||
|
||||
const {
|
||||
compactMapping,
|
||||
fromRawMappings,
|
||||
toRawMappings,
|
||||
} = require('metro-source-map');
|
||||
|
||||
import type {PostProcessModules} from '../DeltaBundler';
|
||||
import type {Options as JSTransformerOptions} from '../JSTransformer/worker';
|
||||
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
|
||||
import type {CompactRawMappings} from '../lib/SourceMap';
|
||||
import type {MappingsMap, SourceMap} from '../lib/SourceMap';
|
||||
import type {TransformCache} from '../lib/TransformCaching';
|
||||
import type {Reporter} from '../lib/reporting';
|
||||
|
@ -133,8 +140,6 @@ class Bundler {
|
|||
globalTransformCache: opts.globalTransformCache,
|
||||
hasteImpl: opts.hasteImpl,
|
||||
maxWorkers: opts.maxWorkers,
|
||||
minifyCode: this._transformer.minify.bind(this._transformer),
|
||||
postMinifyProcess: this._opts.postMinifyProcess,
|
||||
platforms: new Set(opts.platforms),
|
||||
polyfillModuleNames: opts.polyfillModuleNames,
|
||||
projectRoots: opts.projectRoots,
|
||||
|
@ -246,6 +251,25 @@ class Bundler {
|
|||
getResolver(): Promise<Resolver> {
|
||||
return this._resolverPromise;
|
||||
}
|
||||
|
||||
async minifyModule(
|
||||
path: string,
|
||||
code: string,
|
||||
map: CompactRawMappings,
|
||||
): Promise<{code: string, map: CompactRawMappings}> {
|
||||
const sourceMap = fromRawMappings([{code, source: code, map, path}]).toMap(
|
||||
undefined,
|
||||
{},
|
||||
);
|
||||
|
||||
const minified = await this._transformer.minify(path, code, sourceMap);
|
||||
const result = await this._opts.postMinifyProcess({...minified});
|
||||
|
||||
return {
|
||||
code: result.code,
|
||||
map: result.map ? toRawMappings(result.map).map(compactMapping) : [],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function verifyRootExists(root) {
|
||||
|
|
|
@ -451,11 +451,7 @@ class DeltaTransformer extends EventEmitter {
|
|||
}
|
||||
|
||||
const {code, map} = transformOptions.minify
|
||||
? await this._resolver.minifyModule(
|
||||
module.path,
|
||||
wrappedCode,
|
||||
metadata.map,
|
||||
)
|
||||
? await this._bundler.minifyModule(module.path, wrappedCode, metadata.map)
|
||||
: {code: wrappedCode, map: metadata.map};
|
||||
|
||||
const id = this._getModuleId(module.path);
|
||||
|
|
|
@ -1,98 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @emails oncall+javascript_foundation
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
jest.useRealTimers();
|
||||
|
||||
jest.mock('path');
|
||||
|
||||
const DependencyGraph = jest.fn();
|
||||
jest.setMock('../../node-haste/DependencyGraph', DependencyGraph);
|
||||
let Module;
|
||||
|
||||
describe('Resolver', function() {
|
||||
let Resolver, path;
|
||||
|
||||
beforeEach(function() {
|
||||
Resolver = require('../');
|
||||
path = require('path');
|
||||
DependencyGraph.mockClear();
|
||||
|
||||
Module = jest.fn(function() {
|
||||
this.isPolyfill = jest.fn().mockReturnValue(false);
|
||||
this.isJSON = jest.fn().mockReturnValue(false);
|
||||
});
|
||||
|
||||
DependencyGraph.load = jest
|
||||
.fn()
|
||||
.mockImplementation(opts => Promise.resolve(new DependencyGraph(opts)));
|
||||
|
||||
DependencyGraph.prototype.createPolyfill = jest.fn();
|
||||
DependencyGraph.prototype.getDependencies = jest.fn();
|
||||
|
||||
// For the polyfillDeps
|
||||
path.join = jest.fn((a, b) => b);
|
||||
|
||||
DependencyGraph.prototype.load = jest.fn(() => Promise.resolve());
|
||||
});
|
||||
|
||||
function createModule(id, dependencies) {
|
||||
var module = new Module({});
|
||||
module.path = id;
|
||||
|
||||
return module;
|
||||
}
|
||||
|
||||
describe('minification:', () => {
|
||||
const code = 'arbitrary(code)';
|
||||
const id = 'arbitrary.js';
|
||||
let depResolver, minifyCode, module, sourceMap;
|
||||
|
||||
beforeEach(() => {
|
||||
minifyCode = jest.fn((filename, code, map) =>
|
||||
Promise.resolve({code, map}),
|
||||
);
|
||||
module = createModule(id);
|
||||
module.path = '/arbitrary/path.js';
|
||||
|
||||
sourceMap = [];
|
||||
return Resolver.load({
|
||||
projectRoot: '/root',
|
||||
minifyCode,
|
||||
postMinifyProcess: e => e,
|
||||
}).then(r => {
|
||||
depResolver = r;
|
||||
});
|
||||
});
|
||||
|
||||
it('should use minified code', () => {
|
||||
expect.assertions(2);
|
||||
const minifiedCode = 'minified(code)';
|
||||
const minifiedMap = {
|
||||
version: 3,
|
||||
file: ['minified'],
|
||||
sources: [],
|
||||
mappings: '',
|
||||
};
|
||||
minifyCode.mockReturnValue(
|
||||
Promise.resolve({code: minifiedCode, map: minifiedMap}),
|
||||
);
|
||||
return depResolver
|
||||
.minifyModule(module.path, code, sourceMap)
|
||||
.then(({code, map}) => {
|
||||
expect(code).toEqual(minifiedCode);
|
||||
expect(map).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
|
@ -14,16 +14,7 @@
|
|||
|
||||
const DependencyGraph = require('../node-haste/DependencyGraph');
|
||||
|
||||
const {
|
||||
compactMapping,
|
||||
fromRawMappings,
|
||||
toRawMappings,
|
||||
} = require('metro-source-map');
|
||||
|
||||
import type {PostMinifyProcess} from '../Bundler';
|
||||
import typeof {minify as MinifyCode} from '../JSTransformer/worker';
|
||||
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
|
||||
import type {CompactRawMappings} from '../lib/SourceMap';
|
||||
import type {
|
||||
TransformCache,
|
||||
GetTransformCacheKey,
|
||||
|
@ -41,8 +32,6 @@ type Options = {|
|
|||
+globalTransformCache: ?GlobalTransformCache,
|
||||
+hasteImpl?: ?HasteImpl,
|
||||
+maxWorkers: number,
|
||||
+minifyCode: MinifyCode,
|
||||
+postMinifyProcess: PostMinifyProcess,
|
||||
+platforms: Set<string>,
|
||||
+polyfillModuleNames?: Array<string>,
|
||||
+projectRoots: $ReadOnlyArray<string>,
|
||||
|
@ -57,12 +46,8 @@ type Options = {|
|
|||
|
||||
class Resolver {
|
||||
_depGraph: DependencyGraph;
|
||||
_minifyCode: MinifyCode;
|
||||
_postMinifyProcess: PostMinifyProcess;
|
||||
|
||||
constructor(opts: Options, depGraph: DependencyGraph) {
|
||||
this._minifyCode = opts.minifyCode;
|
||||
this._postMinifyProcess = opts.postMinifyProcess;
|
||||
this._depGraph = depGraph;
|
||||
}
|
||||
|
||||
|
@ -92,25 +77,6 @@ class Resolver {
|
|||
return new Resolver(opts, depGraph);
|
||||
}
|
||||
|
||||
async minifyModule(
|
||||
path: string,
|
||||
code: string,
|
||||
map: CompactRawMappings,
|
||||
): Promise<{code: string, map: CompactRawMappings}> {
|
||||
const sourceMap = fromRawMappings([{code, source: code, map, path}]).toMap(
|
||||
undefined,
|
||||
{},
|
||||
);
|
||||
|
||||
const minified = await this._minifyCode(path, code, sourceMap);
|
||||
const result = await this._postMinifyProcess({...minified});
|
||||
|
||||
return {
|
||||
code: result.code,
|
||||
map: result.map ? toRawMappings(result.map).map(compactMapping) : [],
|
||||
};
|
||||
}
|
||||
|
||||
getDependencyGraph(): DependencyGraph {
|
||||
return this._depGraph;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue