From b23c2c071204fc73a42d1142f1da05bad995ffcf Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Thu, 5 Oct 2023 15:44:18 +0100 Subject: [PATCH] [wip] change the structure of rewritten-packages This is intended to help vite and esbuild discover our packages better --- packages/compat/src/compat-addons.ts | 7 ++++++- packages/compat/src/standalone-addon-build.ts | 11 ++++++++--- packages/core/src/module-resolver.ts | 2 +- packages/vite/src/esbuild-resolver.ts | 4 ++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/compat/src/compat-addons.ts b/packages/compat/src/compat-addons.ts index 58abb25eb7..4579b4e080 100644 --- a/packages/compat/src/compat-addons.ts +++ b/packages/compat/src/compat-addons.ts @@ -35,7 +35,12 @@ export default class CompatAddons implements Stage { async ready(): Promise<{ outputPath: string }> { return { - outputPath: resolve(locateEmbroiderWorkingDir(this.compatApp.root), 'rewritten-app'), + outputPath: resolve( + locateEmbroiderWorkingDir(this.compatApp.root), + 'rewritten-app', + 'node_modules', + this.compatApp.name + ), }; } diff --git a/packages/compat/src/standalone-addon-build.ts b/packages/compat/src/standalone-addon-build.ts index 149197e599..8722483c67 100644 --- a/packages/compat/src/standalone-addon-build.ts +++ b/packages/compat/src/standalone-addon-build.ts @@ -44,6 +44,10 @@ ${summarizePeerDepViolations(violations)}` return new Funnel(interior, { destDir: index.packages[pkg.root] }); }); + let fakeTargets = Object.values(index.packages).map(dir => { + return writeFile(join(dir, '..', 'superFakeTarget.js'), ''); + }); + return broccoliMergeTrees([ ...exteriorTrees, new Funnel(compatApp.synthesizeStylesPackage(interiorTrees), { @@ -53,6 +57,7 @@ ${summarizePeerDepViolations(violations)}` destDir: '@embroider/synthesized-vendor', }), writeFile('index.json', JSON.stringify(index, null, 2)), + ...fakeTargets, ]); } @@ -62,7 +67,7 @@ function buildAddonIndex(compatApp: CompatApp, appPackage: Package, packages: Se extraResolutions: {}, }; for (let oldPkg of packages) { - let newRoot = `${oldPkg.name}.${hashed(oldPkg.root)}`; + let newRoot = `${oldPkg.name}.${hashed(oldPkg.root)}/node_modules/${oldPkg.name}`; content.packages[oldPkg.root] = newRoot; let nonResolvableDeps = oldPkg.nonResolvableDeps; if (nonResolvableDeps) { @@ -75,14 +80,14 @@ function buildAddonIndex(compatApp: CompatApp, appPackage: Package, packages: Se // yet. This directory lives outside our rewritten-pacakges directory because // it's produced by a separate build stage, and it's easier to have them // writing into separate directories. - content.packages[compatApp.root] = join('..', 'rewritten-app'); + content.packages[compatApp.root] = join('..', 'rewritten-app', 'node_modules', compatApp.name); let nonResolvableDeps = appPackage.nonResolvableDeps; if (nonResolvableDeps) { let extraRoots = [...nonResolvableDeps.values()].map(v => v.root); // the app gets extraResolutions support just like every addon does - content.extraResolutions[join('..', 'rewritten-app')] = extraRoots; + content.extraResolutions[join('..', 'rewritten-app', 'node_modules', compatApp.name)] = extraRoots; // but it also gets extraResolutions registered against its *original* // location, because the app is unique because stage2 needs a Package diff --git a/packages/core/src/module-resolver.ts b/packages/core/src/module-resolver.ts index db1fccc0ac..eb7cf2bf26 100644 --- a/packages/core/src/module-resolver.ts +++ b/packages/core/src/module-resolver.ts @@ -833,7 +833,7 @@ export class Resolver { return request.rehome(resolve(pkg.root, 'package.json')); } else { // otherwise we need to just assume that internal naming is simple - return request.alias(request.specifier.replace(pkg.name, '.')).rehome(resolve(pkg.root, 'package.json')); + return request.rehome(resolve(pkg.root, '..', 'superFakeTarget.js')); } } diff --git a/packages/vite/src/esbuild-resolver.ts b/packages/vite/src/esbuild-resolver.ts index d531f22889..8f6e90ff7e 100644 --- a/packages/vite/src/esbuild-resolver.ts +++ b/packages/vite/src/esbuild-resolver.ts @@ -39,7 +39,7 @@ export function esBuildResolver(root = process.cwd()): EsBuildPlugin { let src = virtualContent(path, resolverLoader.resolver); if (!macrosConfig) { macrosConfig = readJSONSync( - resolve(locateEmbroiderWorkingDir(root), 'rewritten-app', 'macros-config.json') + resolve(locateEmbroiderWorkingDir(root), 'rewritten-app', 'node_modules', 'vite-app', 'macros-config.json') ) as PluginItem; } return { contents: runMacros(src, path, macrosConfig) }; @@ -54,7 +54,7 @@ export function esBuildResolver(root = process.cwd()): EsBuildPlugin { } if (!macrosConfig) { macrosConfig = readJSONSync( - resolve(locateEmbroiderWorkingDir(root), 'rewritten-app', 'macros-config.json') + resolve(locateEmbroiderWorkingDir(root), 'rewritten-app', 'node_modules', 'vite-app', 'macros-config.json') ) as PluginItem; } return { contents: runMacros(src, path, macrosConfig) };