Skip to content

Commit

Permalink
feat(bundler): support per package wrapShim setting on dependency config
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Sep 27, 2018
1 parent fd49eb1 commit 3c796ac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/build/bundled-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/build/bundled-source.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 3c796ac

Please sign in to comment.