Skip to content

Commit

Permalink
Add support for node type=module
Browse files Browse the repository at this point in the history
Adds support for Ember addons that opt-in to node's ES module syntax via
type=module.

When writing to a directory whose package.json is controlled by a third
party, maximum compatibility is achieved by writing either `.cjs` or `.mjs`
files as appropriate, and not `.js` files.

If neither package is concerned with backwards compatibility, writing ES
modules into a `.js` file can be fine, but writing commonjs modules into
a `.js` file in a directory whose package.json is controlled by a third
party (e.g. another package) is not generally going to be
forwards-compatible

Co-authored-by: Robert Jackson <rjackson@linkedin.com>
  • Loading branch information
hjdivad and rwjblue committed Oct 20, 2022
1 parent 210fb56 commit 54cd274
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 54cd274

Please sign in to comment.