diff --git a/lib/build/bundled-source.js b/lib/build/bundled-source.js index 4e3faf700..b0737e459 100644 --- a/lib/build/bundled-source.js +++ b/lib/build/bundled-source.js @@ -116,11 +116,6 @@ 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) { @@ -128,6 +123,7 @@ exports.BundledSource = class { 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; } diff --git a/spec/lib/build/bundled-source.spec.js b/spec/lib/build/bundled-source.spec.js index 0942a0c37..4746737fa 100644 --- a/spec/lib/build/bundled-source.spec.js +++ b/spec/lib/build/bundled-source.spec.js @@ -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'),