From 35eb9a3889ae505715818f55156bc5655bcda97a Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Sat, 7 Sep 2024 11:05:42 -0400 Subject: [PATCH 1/5] Add test for library export coditions --- .../library-exports-conditions-test.ts | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tests/scenarios/library-exports-conditions-test.ts diff --git a/tests/scenarios/library-exports-conditions-test.ts b/tests/scenarios/library-exports-conditions-test.ts new file mode 100644 index 000000000..b9b629ad9 --- /dev/null +++ b/tests/scenarios/library-exports-conditions-test.ts @@ -0,0 +1,86 @@ +import { join } from 'path'; +import { appScenarios } from './scenarios'; +import { type PreparedApp, Project } from 'scenario-tester'; +import QUnit from 'qunit'; +import merge from 'lodash/merge'; +import globby from 'globby'; +import { readFile } from 'fs-extra'; + +const { module: Qmodule, test } = QUnit; + +const nativeESM = { + prodText: `production - from native-esm`, + devText: `dev - from native-esm`, +}; + +appScenarios + // we are primarily interested in the resolving of export conditions + // from nested libraries, we don't need to test per host-app version + .only('canary') + .map('export-conditions', async project => { + let addon = new Project('native-esm', '0.0.0', { + files: { + dist: { + dev: { 'index.js': `export const location = '${nativeESM.devText}';` }, + prod: { 'index.js': `export const location = '${nativeESM.prodText}';` }, + }, + }, + }); + addon.pkg.files = ['dist']; + addon.pkg.exports = { + '.': { + development: { + default: './dist/dev/index.js', + }, + default: './dist/prod/index.js', + }, + }; + + project.addDevDependency(addon); + + merge(project.files, { + app: { + routes: { + 'application.js': ` +import Route from '@ember/routing/route'; +import * as nativeESM from 'native-esm'; + +console.log({ nativeESM }); +export default class Application extends Route {}; +`, + }, + }, + }); + }) + .forEachScenario(scenario => { + Qmodule(scenario.name, function (hooks) { + let app: PreparedApp; + + hooks.before(async assert => { + app = await scenario.prepare(); + let result = await app.execute('pnpm build'); + assert.equal(result.exitCode, 0, result.output); + }); + + Qmodule('Consuming app', function () { + test(`dist assets contain the prod strings, not the development`, async function (assert) { + let files = await globby('**/*.js', { cwd: join(app.dir, 'dist') }); + let found = false; + + for (let file of files) { + let fullPath = join(app.dir, 'dist', file); + let buffer = await readFile(fullPath); + let contents = buffer.toString(); + + let hasProdText = contents.includes(nativeESM.prodText); + + assert.notOk(contents.includes(nativeESM.devText), `${fullPath} does not contain '${nativeESM.devText}'`); + + if (hasProdText) found = true; + } + + assert.ok(found, `Found text '${nativeESM.prodText}' within ${app.dir}/dist`); + }); + }); + }); + }); From 2c06ddce8605c08608dd44ba0daf228f5f3ed049 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Sat, 7 Sep 2024 11:15:14 -0400 Subject: [PATCH 2/5] Simulate the relationship ember-source has with glimmer-vm --- .../library-exports-conditions-test.ts | 74 ++++++++++++++++--- 1 file changed, 65 insertions(+), 9 deletions(-) diff --git a/tests/scenarios/library-exports-conditions-test.ts b/tests/scenarios/library-exports-conditions-test.ts index b9b629ad9..35bb9c67e 100644 --- a/tests/scenarios/library-exports-conditions-test.ts +++ b/tests/scenarios/library-exports-conditions-test.ts @@ -1,5 +1,5 @@ import { join } from 'path'; -import { appScenarios } from './scenarios'; +import { appScenarios, baseAddon } from './scenarios'; import { type PreparedApp, Project } from 'scenario-tester'; import QUnit from 'qunit'; import merge from 'lodash/merge'; @@ -12,6 +12,10 @@ const nativeESM = { prodText: `production - from native-esm`, devText: `dev - from native-esm`, }; +const nestedNativeESM = { + prodText: `production - from nested-native-esm`, + devText: `dev - from nested-native-esm`, +}; appScenarios // we are primarily interested in the resolving of export conditions @@ -35,8 +39,35 @@ appScenarios default: './dist/prod/index.js', }, }; + let nestedAddon = new Project('nested-native-esm', '0.0.0', { + files: { + dist: { + dev: { 'index.js': `export const location = '${nestedNativeESM.devText}';` }, + prod: { 'index.js': `export const location = '${nestedNativeESM.prodText}';` }, + }, + }, + }); + nestedAddon.pkg.files = ['dist']; + nestedAddon.pkg.exports = { + '.': { + development: { + default: './dist/dev/index.js', + }, + default: './dist/prod/index.js', + }, + }; + + let proxyV1Addon = baseAddon(); + proxyV1Addon.pkg.name = 'proxy-v1-addon'; + proxyV1Addon.mergeFiles({ + addon: { + 'index.js': `export { location } from 'nested-native-esm';`, + }, + }); + proxyV1Addon.addDependency(nestedAddon); project.addDevDependency(addon); + project.addDevDependency(proxyV1Addon); merge(project.files, { app: { @@ -44,8 +75,10 @@ appScenarios 'application.js': ` import Route from '@ember/routing/route'; import * as nativeESM from 'native-esm'; +import * as proxyV1Addon from 'proxy-v1-addon'; + +console.log({ nativeESM, proxyV1Addon }); -console.log({ nativeESM }); export default class Application extends Route {}; `, }, @@ -55,32 +88,55 @@ export default class Application extends Route {}; .forEachScenario(scenario => { Qmodule(scenario.name, function (hooks) { let app: PreparedApp; + let filePaths: string[]; hooks.before(async assert => { app = await scenario.prepare(); let result = await app.execute('pnpm build'); assert.equal(result.exitCode, 0, result.output); + + let files = await globby('**/*.js', { cwd: join(app.dir, 'dist') }); + + filePaths = files.map(file => join(app.dir, 'dist', file)); }); - Qmodule('Consuming app', function () { - test(`dist assets contain the prod strings, not the development`, async function (assert) { - let files = await globby('**/*.js', { cwd: join(app.dir, 'dist') }); + Qmodule('dist assets contain the prod strings, not the development', function () { + test(`native-esm`, async function (assert) { let found = false; - for (let file of files) { - let fullPath = join(app.dir, 'dist', file); - let buffer = await readFile(fullPath); + for (let file of filePaths) { + let buffer = await readFile(file); let contents = buffer.toString(); let hasProdText = contents.includes(nativeESM.prodText); - assert.notOk(contents.includes(nativeESM.devText), `${fullPath} does not contain '${nativeESM.devText}'`); + assert.notOk(contents.includes(nativeESM.devText), `${file} does not contain '${nativeESM.devText}'`); if (hasProdText) found = true; } assert.ok(found, `Found text '${nativeESM.prodText}' within ${app.dir}/dist`); }); + + test(`nested-native-esm`, async function (assert) { + let found = false; + + for (let file of filePaths) { + let buffer = await readFile(file); + let contents = buffer.toString(); + + let hasProdText = contents.includes(nestedNativeESM.prodText); + + assert.notOk( + contents.includes(nestedNativeESM.devText), + `${file} does not contain '${nestedNativeESM.devText}'` + ); + + if (hasProdText) found = true; + } + + assert.ok(found, `Found text '${nestedNativeESM.prodText}' within ${app.dir}/dist`); + }); }); }); }); From edf3f5d8d86f47811fa28fe9a14cb30684d9f07b Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Sat, 7 Sep 2024 11:18:00 -0400 Subject: [PATCH 3/5] Forgot a detail --- tests/scenarios/library-exports-conditions-test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/scenarios/library-exports-conditions-test.ts b/tests/scenarios/library-exports-conditions-test.ts index 35bb9c67e..355b51f86 100644 --- a/tests/scenarios/library-exports-conditions-test.ts +++ b/tests/scenarios/library-exports-conditions-test.ts @@ -30,6 +30,7 @@ appScenarios }, }, }); + addon.pkg.type = 'module'; addon.pkg.files = ['dist']; addon.pkg.exports = { '.': { @@ -47,6 +48,7 @@ appScenarios }, }, }); + nestedAddon.pkg.type = 'module'; nestedAddon.pkg.files = ['dist']; nestedAddon.pkg.exports = { '.': { From 0ae9385a0ec6f50efe692d59dff7e85c1c873555 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Sat, 7 Sep 2024 11:18:07 -0400 Subject: [PATCH 4/5] Forgot a detail --- tests/scenarios/library-exports-conditions-test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/scenarios/library-exports-conditions-test.ts b/tests/scenarios/library-exports-conditions-test.ts index 355b51f86..92c299580 100644 --- a/tests/scenarios/library-exports-conditions-test.ts +++ b/tests/scenarios/library-exports-conditions-test.ts @@ -67,6 +67,7 @@ appScenarios }, }); + proxyV1Addon.linkDependency('ember-auto-import', { baseDir: __dirname }); proxyV1Addon.addDependency(nestedAddon); project.addDevDependency(addon); project.addDevDependency(proxyV1Addon); From acf154908bfc8b73a6fb9bdcc89d50688f1b935c Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Sat, 7 Sep 2024 11:19:13 -0400 Subject: [PATCH 5/5] Update messages --- tests/scenarios/library-exports-conditions-test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/scenarios/library-exports-conditions-test.ts b/tests/scenarios/library-exports-conditions-test.ts index 92c299580..fe5394292 100644 --- a/tests/scenarios/library-exports-conditions-test.ts +++ b/tests/scenarios/library-exports-conditions-test.ts @@ -118,7 +118,7 @@ export default class Application extends Route {}; if (hasProdText) found = true; } - assert.ok(found, `Found text '${nativeESM.prodText}' within ${app.dir}/dist`); + assert.ok(found, `Expected text '${nativeESM.prodText}' within ${app.dir}/dist`); }); test(`nested-native-esm`, async function (assert) { @@ -138,7 +138,7 @@ export default class Application extends Route {}; if (hasProdText) found = true; } - assert.ok(found, `Found text '${nestedNativeESM.prodText}' within ${app.dir}/dist`); + assert.ok(found, `Expected text '${nestedNativeESM.prodText}' within ${app.dir}/dist`); }); }); });