Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@embroider/macros and type=module #1906

Draft
wants to merge 10 commits into
base: stable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/macros/src/babel/macros-babel-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default function main(context: typeof Babel): unknown {
let r = t.identifier('require');
state.generatedRequires.add(r);
path.replaceWith(
t.callExpression(state.importUtil.import(path, state.pathToOurAddon('es-compat2'), 'default', 'esc'), [
t.callExpression(state.importUtil.import(path, state.pathToOurAddon('es-compat2.js'), 'default', 'esc'), [
t.callExpression(r, path.node.arguments),
])
);
Expand Down
93 changes: 93 additions & 0 deletions tests/scenarios/v2-addon-type-module-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { appScenarios, baseV2Addon } from './scenarios';
import type { PreparedApp } from 'scenario-tester';
import QUnit from 'qunit';
import merge from 'lodash/merge';

const { module: Qmodule, test } = QUnit;

appScenarios
.only('release')
.map('v2-addon-as-type-module', async project => {
let addon = baseV2Addon();
addon.pkg.name = 'v2-addon';
addon.pkg.type = 'module';
addon.pkg.files = ['src'];
addon.pkg.exports = {
'./*': './src/*.js',
'./addon-main.cjs': './addon-main.cjs',
};

merge(addon.files, {
src: {
'side-effecting.js': `window.__secret_side_effect = 'hello';`,
/**
* NOTE: importSync shouldn't be used like this in practice,
* as it's meant for compatibility and macroCondition imports before we have
* support for top-level await.
*/
'demo.js': `
import { importSync } from '@embroider/macros';

importSync('./side-effecting.js');
`,
},
});

addon.linkDependency('@embroider/addon-shim', { baseDir: __dirname });

project.addDevDependency(addon);
project.linkDevDependency('@embroider/macros', { baseDir: __dirname });

merge(project.files, {
tests: {
// the app is not set up with typescript
'the-test.js': `
import { module, test } from 'qunit';
import 'v2-addon/demo';

module('v2 addon tests', function (hooks) {
test('macro condition runs without error', async function (assert) {
assert.strictEqual(window.__secret_side_effect, 'hello');
});
});
`,
},
'ember-cli-build.js': `
'use strict';

const EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function (defaults) {
let app = new EmberApp(defaults, {
});

const { compatBuild, recommendedOptions } = require('@embroider/compat');

const { maybeEmbroider } = require('@embroider/test-setup');
return maybeEmbroider(app, {
skipBabel: [
{
package: 'qunit',
},
],
});
};
`,
});
})
.forEachScenario(scenario => {
Qmodule(scenario.name, function (hooks) {
let app: PreparedApp;

hooks.before(async () => {
app = await scenario.prepare();
});

Qmodule('Consuming app', function () {
test(`pnpm test`, async function (assert) {
let result = await app.execute('pnpm test');
assert.equal(result.exitCode, 0, result.output);
});
});
});
});
2 changes: 1 addition & 1 deletion tests/v2-addon-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"type": "addon",
"version": 2,
"app-js": {},
"main": "addon-main.js"
"main": "addon-main.cjs"
},
"exports": {
"./*": "./*"
Expand Down
Loading