From aa9faf7c91df7c9d7d07becef716db8d30778685 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Tue, 12 Nov 2024 10:19:52 -0500 Subject: [PATCH 1/8] Add the 'browser' condition for conditions when resolving --- packages/core/src/module-resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/module-resolver.ts b/packages/core/src/module-resolver.ts index ea9efb8a5..fbca07d75 100644 --- a/packages/core/src/module-resolver.ts +++ b/packages/core/src/module-resolver.ts @@ -1033,7 +1033,7 @@ export class Resolver { if (pkg.packageJSON.exports) { let found = resolveExports(pkg.packageJSON, request.specifier, { browser: true, - conditions: ['default', 'imports'], + conditions: ['default', 'imports', 'browser'], }); if (found?.[0]) { return logTransition( From da560000c14454b0a96fdea31825b1d55c496514 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Wed, 13 Nov 2024 18:14:06 -0500 Subject: [PATCH 2/8] Match vite's conditions --- packages/core/src/module-resolver.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/core/src/module-resolver.ts b/packages/core/src/module-resolver.ts index fbca07d75..129cc2ed6 100644 --- a/packages/core/src/module-resolver.ts +++ b/packages/core/src/module-resolver.ts @@ -1033,7 +1033,11 @@ export class Resolver { if (pkg.packageJSON.exports) { let found = resolveExports(pkg.packageJSON, request.specifier, { browser: true, - conditions: ['default', 'imports', 'browser'], + /** + * Ideally, these would be exported by vite, but the constant + * that defines client conditions is not exposed to us + */ + conditions: ['import', 'module', 'browser' /*, development | production */, 'default'], }); if (found?.[0]) { return logTransition( From ddb7d6d5e738910d0f6378056a685e01859ceef5 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Wed, 13 Nov 2024 20:48:42 -0500 Subject: [PATCH 3/8] Add content-tag to test --- tests/scenarios/package.json | 1 + tests/scenarios/v2-addon-test.ts | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/scenarios/package.json b/tests/scenarios/package.json index 3639c69f3..6f777c742 100644 --- a/tests/scenarios/package.json +++ b/tests/scenarios/package.json @@ -63,6 +63,7 @@ "broccoli-merge-trees": "^3.0.2", "broccoli-persistent-filter": "^3.1.2", "broccoli-stew": "^3.0.0", + "content-tag": "^3.0.0", "ember-bootstrap": "^5.0.0", "ember-cli": "~3.28.0", "ember-cli-4.12": "npm:ember-cli@~4.12.0", diff --git a/tests/scenarios/v2-addon-test.ts b/tests/scenarios/v2-addon-test.ts index f67fe825d..96f3887d4 100644 --- a/tests/scenarios/v2-addon-test.ts +++ b/tests/scenarios/v2-addon-test.ts @@ -34,11 +34,17 @@ appScenarios 'import-from-npm.js': ` export default async function() { let { message } = await import('third-party'); - return message() + + let { Processor } = await import('content-tag'); + + console.log(new Processor()); + + return message(); } `, }); + addon.linkDependency('content-tag', { baseDir: __dirname }); addon.addDependency('third-party', { files: { 'index.js': ` From 5848c2c2c206fd4f9e3b1456a899e55c0a729461 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:57:51 -0500 Subject: [PATCH 4/8] Link to docs --- packages/core/src/module-resolver.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core/src/module-resolver.ts b/packages/core/src/module-resolver.ts index 129cc2ed6..f310b112c 100644 --- a/packages/core/src/module-resolver.ts +++ b/packages/core/src/module-resolver.ts @@ -1036,6 +1036,8 @@ export class Resolver { /** * Ideally, these would be exported by vite, but the constant * that defines client conditions is not exposed to us + * + * See: https://vite.dev/config/shared-options.html#resolve-conditions */ conditions: ['import', 'module', 'browser' /*, development | production */, 'default'], }); From 20e4f8b18aee94f143a3f0739c172beb4af73d24 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Thu, 14 Nov 2024 16:03:59 -0500 Subject: [PATCH 5/8] Update lockfile --- pnpm-lock.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2bb51dfda..929aadff3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1821,6 +1821,9 @@ importers: broccoli-stew: specifier: ^3.0.0 version: 3.0.0 + content-tag: + specifier: ^3.0.0 + version: 3.0.0 ember-bootstrap: specifier: ^5.0.0 version: 5.1.1(@babel/core@7.25.2)(ember-source@3.28.12)(webpack@5.94.0) From f3e095ca03f11de490a211c0991819e561155a4b Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:08:49 -0500 Subject: [PATCH 6/8] Tell the optimizer to use esntext, and tell the test's vite config to use a newer browser target that supports top-level await --- packages/vite/src/optimize-deps.ts | 4 ++ tests/scenarios/v2-addon-test.ts | 74 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/packages/vite/src/optimize-deps.ts b/packages/vite/src/optimize-deps.ts index d0ee47ff2..3263191cc 100644 --- a/packages/vite/src/optimize-deps.ts +++ b/packages/vite/src/optimize-deps.ts @@ -10,6 +10,10 @@ export function optimizeDeps(): OptimizeDeps { exclude: ['@embroider/macros'], extensions: ['.hbs', '.gjs', '.gts'], esbuildOptions: { + // When optimizing deps for development, + // always allow the latest featuers + // (such as top level await) + target: 'esnext', plugins: [esBuildResolver()], }, }; diff --git a/tests/scenarios/v2-addon-test.ts b/tests/scenarios/v2-addon-test.ts index 96f3887d4..9f7878450 100644 --- a/tests/scenarios/v2-addon-test.ts +++ b/tests/scenarios/v2-addon-test.ts @@ -153,6 +153,80 @@ appScenarios `, }, }, + // Default + different build.target + 'vite.config.mjs': ` + import { defineConfig } from "vite"; + import { + resolver, + hbs, + scripts, + templateTag, + optimizeDeps, + compatPrebuild, + assets, + contentFor, + } from "@embroider/vite"; + import { resolve } from "path"; + import { babel } from "@rollup/plugin-babel"; + + export default defineConfig(({ mode }) => { + return { + cacheDir: resolve("node_modules", ".vite"), + resolve: { + extensions: [ + ".mjs", + ".gjs", + ".js", + ".mts", + ".gts", + ".ts", + ".hbs", + ".hbs.js", + ".json", + ], + }, + plugins: [ + hbs(), + templateTag(), + scripts(), + resolver(), + compatPrebuild(), + assets(), + contentFor(), + + babel({ + babelHelpers: "runtime", + + // this needs .hbs because our hbs() plugin above converts them to + // javascript but the javascript still also needs babel, but we don't want + // to rename them because vite isn't great about knowing how to hot-reload + // them if we resolve them to made-up names. + extensions: [".gjs", ".js", ".hbs", ".ts", ".gts"], + }), + ], + optimizeDeps: optimizeDeps(), + server: { + port: 4200, + }, + build: { + outDir: "dist", + target: ['chrome130'], + rollupOptions: { + input: { + main: "index.html", + ...(shouldBuildTests(mode) + ? { tests: "tests/index.html" } + : undefined), + }, + }, + }, + }; + }); + + function shouldBuildTests(mode) { + return mode !== "production" || process.env.FORCE_BUILD_TESTS; + } + `, }); }) .forEachScenario(scenario => { From ecf9039efaf17883db55dc35e909a318930945c9 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:15:35 -0500 Subject: [PATCH 7/8] Update test --- tests/scenarios/v2-addon-test.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/scenarios/v2-addon-test.ts b/tests/scenarios/v2-addon-test.ts index 9f7878450..dcc6e5d19 100644 --- a/tests/scenarios/v2-addon-test.ts +++ b/tests/scenarios/v2-addon-test.ts @@ -34,7 +34,6 @@ appScenarios 'import-from-npm.js': ` export default async function() { let { message } = await import('third-party'); - let { Processor } = await import('content-tag'); console.log(new Processor()); @@ -55,6 +54,7 @@ appScenarios }, }); + project.linkDevDependency('vite-plugin-wasm', { baseDir: __dirname }); project.addDevDependency(addon); // a v1 addon, which will have a v2 addon as a dep @@ -153,7 +153,9 @@ appScenarios `, }, }, - // Default + different build.target + // Default + // + different build.target + // + wasm plugin 'vite.config.mjs': ` import { defineConfig } from "vite"; import { @@ -168,6 +170,7 @@ appScenarios } from "@embroider/vite"; import { resolve } from "path"; import { babel } from "@rollup/plugin-babel"; + import wasm from "vite-plugin-wasm"; export default defineConfig(({ mode }) => { return { @@ -186,6 +189,7 @@ appScenarios ], }, plugins: [ + wasm(), hbs(), templateTag(), scripts(), @@ -210,7 +214,7 @@ appScenarios }, build: { outDir: "dist", - target: ['chrome130'], + target: ['esnext'], rollupOptions: { input: { main: "index.html", From c904d231b5c4a5c90d0c2e66f74f65031620e70c Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:43:28 -0500 Subject: [PATCH 8/8] Make more minimal --- pnpm-lock.yaml | 3 - tests/scenarios/package.json | 1 - tests/scenarios/v2-addon-test.ts | 97 +++++--------------------------- 3 files changed, 15 insertions(+), 86 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 929aadff3..2bb51dfda 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1821,9 +1821,6 @@ importers: broccoli-stew: specifier: ^3.0.0 version: 3.0.0 - content-tag: - specifier: ^3.0.0 - version: 3.0.0 ember-bootstrap: specifier: ^5.0.0 version: 5.1.1(@babel/core@7.25.2)(ember-source@3.28.12)(webpack@5.94.0) diff --git a/tests/scenarios/package.json b/tests/scenarios/package.json index 6f777c742..3639c69f3 100644 --- a/tests/scenarios/package.json +++ b/tests/scenarios/package.json @@ -63,7 +63,6 @@ "broccoli-merge-trees": "^3.0.2", "broccoli-persistent-filter": "^3.1.2", "broccoli-stew": "^3.0.0", - "content-tag": "^3.0.0", "ember-bootstrap": "^5.0.0", "ember-cli": "~3.28.0", "ember-cli-4.12": "npm:ember-cli@~4.12.0", diff --git a/tests/scenarios/v2-addon-test.ts b/tests/scenarios/v2-addon-test.ts index dcc6e5d19..a02466227 100644 --- a/tests/scenarios/v2-addon-test.ts +++ b/tests/scenarios/v2-addon-test.ts @@ -7,6 +7,18 @@ const { module: Qmodule, test } = QUnit; appScenarios .map('v2-addon-basics', project => { + let fakeContentTag = baseV2Addon(); + fakeContentTag.pkg.name = 'fake-content-tag'; + fakeContentTag.pkg.exports = { + browser: './browser.js', + default: './not-browser.js', + }; + fakeContentTag.pkg.files = ['browser.js', 'not-browser.js']; + merge(fakeContentTag.files, { + 'browser.js': `export const value = 'browser'`, + 'not-browser.js': `export const value = 'not browser'`, + }); + let addon = baseV2Addon(); addon.pkg.name = 'v2-addon'; (addon.pkg as any)['ember-addon']['app-js']['./components/example-component.js'] = @@ -34,16 +46,16 @@ appScenarios 'import-from-npm.js': ` export default async function() { let { message } = await import('third-party'); - let { Processor } = await import('content-tag'); + let { value } = await import('fake-content-tag'); - console.log(new Processor()); + if (value !== 'browser') throw new Error('Incorrect conditions for fake-content-tag'); return message(); } `, }); - addon.linkDependency('content-tag', { baseDir: __dirname }); + addon.addDependency(fakeContentTag); addon.addDependency('third-party', { files: { 'index.js': ` @@ -54,7 +66,6 @@ appScenarios }, }); - project.linkDevDependency('vite-plugin-wasm', { baseDir: __dirname }); project.addDevDependency(addon); // a v1 addon, which will have a v2 addon as a dep @@ -153,84 +164,6 @@ appScenarios `, }, }, - // Default - // + different build.target - // + wasm plugin - 'vite.config.mjs': ` - import { defineConfig } from "vite"; - import { - resolver, - hbs, - scripts, - templateTag, - optimizeDeps, - compatPrebuild, - assets, - contentFor, - } from "@embroider/vite"; - import { resolve } from "path"; - import { babel } from "@rollup/plugin-babel"; - import wasm from "vite-plugin-wasm"; - - export default defineConfig(({ mode }) => { - return { - cacheDir: resolve("node_modules", ".vite"), - resolve: { - extensions: [ - ".mjs", - ".gjs", - ".js", - ".mts", - ".gts", - ".ts", - ".hbs", - ".hbs.js", - ".json", - ], - }, - plugins: [ - wasm(), - hbs(), - templateTag(), - scripts(), - resolver(), - compatPrebuild(), - assets(), - contentFor(), - - babel({ - babelHelpers: "runtime", - - // this needs .hbs because our hbs() plugin above converts them to - // javascript but the javascript still also needs babel, but we don't want - // to rename them because vite isn't great about knowing how to hot-reload - // them if we resolve them to made-up names. - extensions: [".gjs", ".js", ".hbs", ".ts", ".gts"], - }), - ], - optimizeDeps: optimizeDeps(), - server: { - port: 4200, - }, - build: { - outDir: "dist", - target: ['esnext'], - rollupOptions: { - input: { - main: "index.html", - ...(shouldBuildTests(mode) - ? { tests: "tests/index.html" } - : undefined), - }, - }, - }, - }; - }); - - function shouldBuildTests(mode) { - return mode !== "production" || process.env.FORCE_BUILD_TESTS; - } - `, }); }) .forEachScenario(scenario => {