Permalink
Browse files

feat(bundler): support per package wrapShim setting on dependency config

  • Loading branch information...
huochunpeng committed Aug 14, 2018
1 parent fd49eb1 commit 3c796ac2d72c3004b2d211c745e8d3ec8efa085d
Showing with 44 additions and 5 deletions.
  1. +10 −5 lib/build/bundled-source.js
  2. +34 −0 spec/lib/build/bundled-source.spec.js
@@ -116,18 +116,14 @@ exports.BundledSource = class {
let contents = cjsTransform(modulePath, this.contents, forceCjsWrap);
deps = findDeps(modulePath, contents);
const writeTransform = allWriteTransforms({
stubModules: loaderConfig.stubModules,
wrapShim: loaderConfig.wrapShim
});
let context = {pkgsMainMap: {}, config: {shim: {}}};
let desc = dependencyInclusion && dependencyInclusion.description;
if (desc && desc.mainId === moduleId) {
// main file of node package
context.pkgsMainMap[moduleId] = desc.name;
}
let wrapShim = false;
if (dependencyInclusion) {
let description = dependencyInclusion.description;
@@ -142,8 +138,17 @@ exports.BundledSource = class {
// force deps for shimed package
deps.push.apply(deps, description.loaderConfig.deps);
}
if (description.loaderConfig.wrapShim) {
wrapShim = true;
}
}
const writeTransform = allWriteTransforms({
stubModules: loaderConfig.stubModules,
wrapShim: wrapShim || loaderConfig.wrapShim
});
contents = writeTransform(context, moduleId, modulePath, contents);
this.contents = contents;
}
@@ -463,6 +463,40 @@ exports.t = t;
.toBe('(function(root) {define("foo/foo", ["bar"], function() { return (function() {var Foo = "Foo";return root.Foo = Foo; }).apply(root, arguments);});}(this));');
});
it('transforms npm package legacy js file with per package wrapShim', () => {
let file = {
path: path.resolve(cwd, 'node_modules/foo/foo.js'),
contents: 'var Foo = "Foo";'
};
let bs = new BundledSource(bundler, file);
bs._getProjectRoot = () => 'src';
bs.includedBy = {
includedBy: {
description: {
name: 'foo',
mainId: 'foo/foo',
loaderConfig: {
name: 'foo',
path: '../node_modules/foo',
main: 'foo',
deps: ['bar'],
'exports': 'Foo',
wrapShim: true
}
}
}
};
bs._getLoaderPlugins = () => [];
bs._getLoaderConfig = () => ({paths: {}});
let deps = bs.transform();
expect(deps).toEqual(['bar']);
expect(bs.requiresTransform).toBe(false);
expect(bs.contents.replace(/\r|\n/g, ''))
.toBe('(function(root) {define("foo/foo", ["bar"], function() { return (function() {var Foo = "Foo";return root.Foo = Foo; }).apply(root, arguments);});}(this));');
});
it('transforms npm package legacy js file with wrapShim but no exports', () => {
let file = {
path: path.resolve(cwd, 'node_modules/foo/foo.js'),

0 comments on commit 3c796ac

Please sign in to comment.