Skip to content

Commit

Permalink
fix(cli-bundler): allow tracing of mjs file
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Oct 7, 2023
1 parent e87227d commit 611da5e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/build/dependency-inclusion.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports.DependencyInclusion = class {
}

let main = this.description.loaderConfig.main;
if (mainIsJs) {
if (mainIsJs && Utils.knownExtensions.indexOf(ext) === -1) {
main += '.js';
}

Expand Down
2 changes: 1 addition & 1 deletion lib/build/find-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions spec/lib/build/dependency-inclusion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions spec/lib/build/find-deps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit 611da5e

Please sign in to comment.