Skip to content

Commit 3c796ac

Browse files
committed
feat(bundler): support per package wrapShim setting on dependency config
1 parent fd49eb1 commit 3c796ac

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

lib/build/bundled-source.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,14 @@ exports.BundledSource = class {
116116
let contents = cjsTransform(modulePath, this.contents, forceCjsWrap);
117117
deps = findDeps(modulePath, contents);
118118

119-
const writeTransform = allWriteTransforms({
120-
stubModules: loaderConfig.stubModules,
121-
wrapShim: loaderConfig.wrapShim
122-
});
123-
124119
let context = {pkgsMainMap: {}, config: {shim: {}}};
125120
let desc = dependencyInclusion && dependencyInclusion.description;
126121
if (desc && desc.mainId === moduleId) {
127122
// main file of node package
128123
context.pkgsMainMap[moduleId] = desc.name;
129124
}
130125

126+
let wrapShim = false;
131127
if (dependencyInclusion) {
132128
let description = dependencyInclusion.description;
133129

@@ -142,8 +138,17 @@ exports.BundledSource = class {
142138
// force deps for shimed package
143139
deps.push.apply(deps, description.loaderConfig.deps);
144140
}
141+
142+
if (description.loaderConfig.wrapShim) {
143+
wrapShim = true;
144+
}
145145
}
146146

147+
const writeTransform = allWriteTransforms({
148+
stubModules: loaderConfig.stubModules,
149+
wrapShim: wrapShim || loaderConfig.wrapShim
150+
});
151+
147152
contents = writeTransform(context, moduleId, modulePath, contents);
148153
this.contents = contents;
149154
}

spec/lib/build/bundled-source.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,40 @@ exports.t = t;
463463
.toBe('(function(root) {define("foo/foo", ["bar"], function() { return (function() {var Foo = "Foo";return root.Foo = Foo; }).apply(root, arguments);});}(this));');
464464
});
465465

466+
it('transforms npm package legacy js file with per package wrapShim', () => {
467+
let file = {
468+
path: path.resolve(cwd, 'node_modules/foo/foo.js'),
469+
contents: 'var Foo = "Foo";'
470+
};
471+
472+
let bs = new BundledSource(bundler, file);
473+
bs._getProjectRoot = () => 'src';
474+
bs.includedBy = {
475+
includedBy: {
476+
description: {
477+
name: 'foo',
478+
mainId: 'foo/foo',
479+
loaderConfig: {
480+
name: 'foo',
481+
path: '../node_modules/foo',
482+
main: 'foo',
483+
deps: ['bar'],
484+
'exports': 'Foo',
485+
wrapShim: true
486+
}
487+
}
488+
}
489+
};
490+
bs._getLoaderPlugins = () => [];
491+
bs._getLoaderConfig = () => ({paths: {}});
492+
493+
let deps = bs.transform();
494+
expect(deps).toEqual(['bar']);
495+
expect(bs.requiresTransform).toBe(false);
496+
expect(bs.contents.replace(/\r|\n/g, ''))
497+
.toBe('(function(root) {define("foo/foo", ["bar"], function() { return (function() {var Foo = "Foo";return root.Foo = Foo; }).apply(root, arguments);});}(this));');
498+
});
499+
466500
it('transforms npm package legacy js file with wrapShim but no exports', () => {
467501
let file = {
468502
path: path.resolve(cwd, 'node_modules/foo/foo.js'),

0 commit comments

Comments
 (0)