Skip to content

Commit

Permalink
Merge pull request embroider-build#544 from hjdivad/hjblue/node-esm
Browse files Browse the repository at this point in the history
Add support for consumers using Node ES Module support (e.g. `type=module` in `package.json`)
  • Loading branch information
ef4 authored Oct 31, 2022
2 parents 210fb56 + 54cd274 commit 5741550
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/ember-auto-import/ts/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export default class WebpackBundler extends Plugin implements Bundler {
let entry: { [name: string]: string[] } = {};
this.opts.bundles.names.forEach((bundle) => {
entry[bundle] = [
join(stagingDir, 'l.js'),
join(stagingDir, `${bundle}.js`),
join(stagingDir, 'l.cjs'),
join(stagingDir, `${bundle}.cjs`),
];
});

Expand Down Expand Up @@ -184,7 +184,7 @@ export default class WebpackBundler extends Plugin implements Bundler {
},
plugins: removeUndefined([stylePlugin]),
module: {
noParse: (file: string) => file === join(stagingDir, 'l.js'),
noParse: (file: string) => file === join(stagingDir, 'l.cjs'),
rules: [
this.babelRule(stagingDir),
{
Expand Down Expand Up @@ -389,7 +389,7 @@ export default class WebpackBundler extends Plugin implements Bundler {

private writeEntryFile(name: string, deps: BundleDependencies) {
writeFileSync(
join(this.stagingDir, `${name}.js`),
join(this.stagingDir, `${name}.cjs`),
entryTemplate({
staticImports: deps.staticImports,
dynamicImports: deps.dynamicImports,
Expand All @@ -403,7 +403,7 @@ export default class WebpackBundler extends Plugin implements Bundler {
}

private writeLoaderFile() {
writeFileSync(join(this.stagingDir, `l.js`), loader);
writeFileSync(join(this.stagingDir, `l.cjs`), loader);
}

private linkDeps(bundleDeps: Map<string, BundleDependencies>) {
Expand Down
44 changes: 44 additions & 0 deletions test-scenarios/node-es-modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import merge from 'lodash/merge';
import { Scenarios } from 'scenario-tester';
import { baseApp } from './scenarios';
import { PreparedApp } from 'scenario-tester';
import QUnit from 'qunit';
const { module: Qmodule, test } = QUnit;

let template = Scenarios.fromProject(baseApp);

template
.map('node ES modules', project => {
project.linkDevDependency('ember-auto-import', { baseDir: __dirname });
// Support for ember-cli internally to respect `ember-cli-build.cjs` landed in https://github.com/ember-cli/ember-cli/pull/10053
project.linkDevDependency('ember-cli', { baseDir: __dirname });
project.linkDependency('webpack', { baseDir: __dirname });

project.files['ember-cli-build.cjs'] = project.files['ember-cli-build.js'];
delete project.files['ember-cli-build.js'];

project.files['testem.cjs'] = project.files['testem.js'];
delete project.files['testem.js'];

project.pkg.type = 'module';
project.pkg.scripts = project.pkg.scripts || {};
project.pkg.scripts.test = 'node ./node_modules/ember-cli/bin/ember test --config-file testem.cjs';
merge(project.files, {
config: {
'package.json': JSON.stringify({ type: 'commonjs' }),
},
});
})
.forEachScenario(scenario => {
Qmodule(scenario.name, function (hooks) {
let app: PreparedApp;
hooks.before(async () => {
app = await scenario.prepare();
});
test('npm run test', async function (assert) {
console.log(app.dir);
let result = await app.execute('volta run npm -- run test');
assert.equal(result.exitCode, 0, result.output);
});
});
});

0 comments on commit 5741550

Please sign in to comment.