diff --git a/lib/build/dependency-inclusion.js b/lib/build/dependency-inclusion.js index 8685fb37..c231403d 100644 --- a/lib/build/dependency-inclusion.js +++ b/lib/build/dependency-inclusion.js @@ -28,7 +28,7 @@ exports.DependencyInclusion = class { } let main = this.description.loaderConfig.main; - if (mainIsJs) { + if (mainIsJs && Utils.knownExtensions.indexOf(ext) === -1) { main += '.js'; } diff --git a/lib/build/find-deps.js b/lib/build/find-deps.js index 2d2ef198..eb84f8f9 100644 --- a/lib/build/find-deps.js +++ b/lib/build/find-deps.js @@ -288,7 +288,7 @@ exports.findHtmlDeps = function(filename, contents, loaderType = 'require') { exports.findDeps = function(filename, contents, loaderType = 'require') { let ext = path.extname(filename).toLowerCase(); - if (ext === '.js') { + if (ext === '.js' || ext === '.mjs' || ext === '.cjs') { return exports.findJsDeps(filename, contents, loaderType); } else if (ext === '.html' || ext === '.htm') { return exports.findHtmlDeps(filename, contents, loaderType); diff --git a/spec/lib/build/dependency-inclusion.spec.js b/spec/lib/build/dependency-inclusion.spec.js index bab2fce5..a72e9856 100644 --- a/spec/lib/build/dependency-inclusion.spec.js +++ b/spec/lib/build/dependency-inclusion.spec.js @@ -103,6 +103,36 @@ describe('the DependencyInclusion module', () => { .catch(e => done.fail(e)); }); + it('adds mjs main file to the bundle when there is a main file', done => { + let bundle = { + bundler: bundler, + addAlias: jasmine.createSpy('addAlias'), + includes: [], + createMatcher: function(pattern) { + return new Minimatch(pattern, { + dot: true + }); + } + }; + + let description = new DependencyDescription('my-package', 'npm'); + description.loaderConfig = { + path: '../node_modules/my-package', + name: 'my-package', + main: 'index.mjs' + }; + + let sut = new DependencyInclusion(bundle, description); + sut.traceResources() + .then(() => { + expect(bundle.includes.length).toBe(1); + expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'index.mjs')); + expect(bundle.addAlias).toHaveBeenCalledWith('my-package', 'my-package/index.mjs'); + done(); + }) + .catch(e => done.fail(e)); + }); + it('aliases main file when both package name and main file share same non-js extension', done => { let bundle = { bundler: bundler, diff --git a/spec/lib/build/find-deps.spec.js b/spec/lib/build/find-deps.spec.js index 31f55ca3..f0efbe35 100644 --- a/spec/lib/build/find-deps.spec.js +++ b/spec/lib/build/find-deps.spec.js @@ -71,6 +71,9 @@ describe('find-deps', () => { expect(findJsDeps('ignore.js', contents).sort()) .toEqual(['./b/c', 'a']); + expect(findJsDeps('ignore.mjs', "define(['./b.mjs'], () => 1);").sort()) + .toEqual(['./b.mjs']); + expect(findJsDeps('ignore.js', 'define(() => 1);').length).toBe(0); });