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

Virtual entrypoint export modules #2006

Merged
merged 5 commits into from
Jun 27, 2024
Merged
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
29 changes: 17 additions & 12 deletions packages/core/src/virtual-entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ export function renderEntrypoint(
}

const entryTemplate = compile(`
import { importSync as i, macroCondition, getGlobalConfig } from '@embroider/macros';
let w = window;
let d = w.define;
import { macroCondition, getGlobalConfig } from '@embroider/macros';

import environment from './config/environment';

Expand All @@ -168,22 +166,18 @@ import environment from './config/environment';

{{#if defineModulesFrom ~}}
import implicitModules from "{{js-string-escape defineModulesFrom}}";

for(const [name, module] of Object.entries(implicitModules)) {
d(name, function() { return module });
}
{{/if}}


{{#each eagerModules as |eagerModule| ~}}
import "{{js-string-escape eagerModule}}";
{{/each}}

{{#each amdModules as |amdModule index| ~}}
import * as amdModule{{index}} from "{{js-string-escape amdModule.buildtime}}"
d("{{js-string-escape amdModule.runtime}}", function(){ return amdModule{{index}}; });
{{/each}}

let exportFastbootModules = {};

{{#if fastbootOnlyAmdModules}}
if (macroCondition(getGlobalConfig().fastboot?.isRunning)) {
let fastbootModules = {};
Expand All @@ -195,14 +189,14 @@ import environment from './config/environment';
const resolvedValues = await Promise.all(Object.values(fastbootModules));

Object.keys(fastbootModules).forEach((k, i) => {
d(k, function(){ return resolvedValues[i];});
exportFasbootModules[k] = resolvedValues[i];
})
}
{{/if}}


{{#if lazyRoutes}}
w._embroiderRouteBundles_ = [
window._embroiderRouteBundles_ = [
{{#each lazyRoutes as |route|}}
{
names: {{json-stringify route.names}},
Expand All @@ -215,7 +209,7 @@ w._embroiderRouteBundles_ = [
{{/if}}

{{#if lazyEngines}}
w._embroiderEngineBundles_ = [
window._embroiderEngineBundles_ = [
{{#each lazyEngines as |engine|}}
{
names: {{json-stringify engine.names}},
Expand All @@ -226,6 +220,17 @@ w._embroiderEngineBundles_ = [
{{/each}}
]
{{/if}}

export default Object.assign(
{},
implicitModules,
{
{{#each amdModules as |amdModule index| ~}}
"{{js-string-escape amdModule.runtime}}": amdModule{{index}},
{{/each}}
},
exportFastbootModules
);
`) as (params: {
amdModules: { runtime: string; buildtime: string }[];
fastbootOnlyAmdModules?: { runtime: string; buildtime: string }[];
Expand Down
9 changes: 8 additions & 1 deletion tests/addon-template/tests/dummy/app/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import Application from '@ember/application';
import compatModules from '@embroider/core/entrypoint';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from 'dummy/config/environment';

import '@embroider/core/entrypoint';
let d = window.define;

for (const [name, module] of Object.entries(compatModules)) {
d(name, function () {
return module;
});
}

export default class App extends Application {
modulePrefix = config.modulePrefix;
Expand Down
9 changes: 8 additions & 1 deletion tests/app-template/app/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import Application from '@ember/application';
import compatModules from '@embroider/core/entrypoint';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

import '@embroider/core/entrypoint';
let d = window.define;

for (const [name, module] of Object.entries(compatModules)) {
d(name, function () {
return module;
});
}

export default class App extends Application {
modulePrefix = config.modulePrefix;
Expand Down
64 changes: 22 additions & 42 deletions tests/scenarios/compat-stage2-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,33 +246,24 @@ stage2Scenarios
expectModule.withContents(contents => {
const result = /import \* as (\w+) from ".*in-repo-b\/_app_\/service\/in-repo.js.*/.exec(contents) ?? [];
const [, amdModule] = result;
assert.codeContains(
contents,
`d("my-app/service/in-repo", function () {
return ${amdModule};
});`
assert.ok(
contents.includes(`"my-app/service/in-repo": ${amdModule}`),
'expected module is in the export list'
);
return true;
}, 'module imports from the correct place');
expectModule.withContents(contents => {
const result = /import \* as (\w+) from ".*dep-b\/_app_\/service\/addon.js.*/.exec(contents) ?? [];
const [, amdModule] = result;
assert.codeContains(
contents,
`d("my-app/service/addon", function () {
return ${amdModule};
});`
);
assert.ok(contents.includes(`"my-app/service/addon": ${amdModule}`), 'expected module is in the export list');
return true;
}, 'module imports from the correct place');
expectModule.withContents(contents => {
const result = /import \* as (\w+) from ".*dev-c\/_app_\/service\/dev-addon.js.*/.exec(contents) ?? [];
const [, amdModule] = result;
assert.codeContains(
contents,
`d("my-app/service/dev-addon", function () {
return ${amdModule};
});`
assert.ok(
contents.includes(`"my-app/service/dev-addon": ${amdModule}`),
'expected module is in the export list'
);
return true;
}, 'module imports from the correct place');
Expand All @@ -283,11 +274,9 @@ stage2Scenarios
const result =
/import \* as (\w+) from ".*dep-b\/_app_\/service\/dep-wins-over-dev.js.*/.exec(contents) ?? [];
const [, amdModule] = result;
assert.codeContains(
contents,
`d("my-app/service/dep-wins-over-dev", function () {
return ${amdModule};
});`
assert.ok(
contents.includes(`"my-app/service/dep-wins-over-dev": ${amdModule}`),
'expected module is in the export list'
);
return true;
});
Expand All @@ -298,11 +287,9 @@ stage2Scenarios
const result =
/import \* as (\w+) from ".*in-repo-a\/_app_\/service\/in-repo-over-deps.js.*/.exec(contents) ?? [];
const [, amdModule] = result;
assert.codeContains(
contents,
`d("my-app/service/in-repo-over-deps", function () {
return ${amdModule};
});`
assert.ok(
contents.includes(`"my-app/service/in-repo-over-deps": ${amdModule}`),
'expected module is in the export list'
);
return true;
});
Expand All @@ -312,11 +299,9 @@ stage2Scenarios
resolveEntryPoint(expectAudit).withContents(contents => {
const result = /import \* as (\w+) from ".*dev-d\/_app_\/service\/test-before.js.*/.exec(contents) ?? [];
const [, amdModule] = result;
assert.codeContains(
contents,
`d("my-app/service/test-before", function () {
return ${amdModule};
});`
assert.ok(
contents.includes(`"my-app/service/test-before": ${amdModule}`),
'expected module is in the export list'
);
return true;
});
Expand All @@ -326,11 +311,9 @@ stage2Scenarios
resolveEntryPoint(expectAudit).withContents(contents => {
const result = /import \* as (\w+) from ".*dev-b\/_app_\/service\/test-after.js.*/.exec(contents) ?? [];
const [, amdModule] = result;
assert.codeContains(
contents,
`d("my-app/service/test-after", function () {
return ${amdModule};
});`
assert.ok(
contents.includes(`"my-app/service/test-after": ${amdModule}`),
'expected module is in the export list'
);
return true;
});
Expand Down Expand Up @@ -845,12 +828,9 @@ stage2Scenarios
}

const [, amdModule] = result;

assert.codeContains(
contents,
`d("my-app/non-static-dir/another-library", function () {
return ${amdModule};
});`
assert.ok(
contents.includes(`"my-app/non-static-dir/another-library": ${amdModule}`),
'expected module is in the export list'
);
return true;
});
Expand Down
36 changes: 12 additions & 24 deletions tests/scenarios/compat-template-colocation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,9 @@ scenarios
}

const [, amdModule] = result;

assert.codeContains(
contents,
`d("my-app/components/has-colocated-template", function () {
return ${amdModule};
});`
assert.ok(
contents.includes(`"my-app/components/has-colocated-template": ${amdModule}`),
'expected module is in the export list'
);
});
});
Expand Down Expand Up @@ -395,12 +392,9 @@ appScenarios
}

const [, podComponentAmd] = result;

assert.codeContains(
content,
`d("my-app/components/pod-component/component", function () {
return ${podComponentAmd};
});`
assert.ok(
content.includes(`"my-app/components/pod-component/component": ${podComponentAmd}`),
'expected module is in the export list'
);

result = /import \* as (\w+) from "\/components\/pod-component\/template\.hbs.*"/.exec(content);
Expand All @@ -410,12 +404,9 @@ appScenarios
}

const [, podComponentTemplateAmd] = result;

assert.codeContains(
content,
`d("my-app/components/pod-component/template", function () {
return ${podComponentTemplateAmd};
});`
assert.ok(
content.includes(`"my-app/components/pod-component/template": ${podComponentTemplateAmd}`),
'expected module is in the export list'
);

result = /import \* as (\w+) from "\/components\/template-only\/template\.hbs.*"/.exec(content);
Expand All @@ -425,12 +416,9 @@ appScenarios
}

const [, templateOnlyAmd] = result;

assert.codeContains(
content,
`d("my-app/components/template-only/template", function () {
return ${templateOnlyAmd};
});`
assert.ok(
content.includes(`"my-app/components/template-only/template": ${templateOnlyAmd}`),
'expected module is in the export list'
);

return true;
Expand Down
9 changes: 8 additions & 1 deletion tests/ts-app-template/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import Application from '@ember/application';
import compatModules from '@embroider/core/entrypoint';
BlueCutOfficial marked this conversation as resolved.
Show resolved Hide resolved
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from 'ts-app-template/config/environment';

import '@embroider/core/entrypoint';
let d = window.define;

for (const [name, module] of Object.entries(compatModules)) {
d(name, function () {
return module;
});
}

export default class App extends Application {
modulePrefix = config.modulePrefix;
Expand Down
6 changes: 6 additions & 0 deletions tests/ts-app-template/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
import '@glint/environment-ember-loose';

declare global {
interface Window {
define: any;
}
}
1 change: 1 addition & 0 deletions tests/ts-app-template/types/ts-app-template/entry.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@embroider/core/entrypoint';