From cc9f590fcc593caeddace9701ffdcd29d650a36f Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Tue, 7 May 2024 16:39:49 -0400 Subject: [PATCH] Make ember-source compat adapter tolerant of upcoming ember-source changes I'm working to land a [Build Reform](https://github.com/emberjs/ember.js/pull/20675) branch in ember-source that, among other things, uses only rollup for Ember's prepublication build, ensuring that it's all ES-module clean. The inter-package imports within `ember-source/dist/packages` switch from being package names (which require non-standard resolving to work) to relative imports (which do not). As a consequence of that it's simpler to ship all of `dist/packages` and `dist/dependencies` together as `dist/packages`. So our compat adapter needs to tolerate `dist/dependencies` not existing. The special handling we had for enumerating the contents of `dist/dependencies` and removing them from package.json dependencies was only needed to deal with the magical inter-package resolving, so it's correct that it becomes a no-op for these new ember versions. --- .../src/compat-adapters/ember-source.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/compat/src/compat-adapters/ember-source.ts b/packages/compat/src/compat-adapters/ember-source.ts index f2187269c..0eb73cced 100644 --- a/packages/compat/src/compat-adapters/ember-source.ts +++ b/packages/compat/src/compat-adapters/ember-source.ts @@ -11,6 +11,7 @@ import type * as Babel from '@babel/core'; import type { NodePath } from '@babel/traverse'; import Plugin from 'broccoli-plugin'; import type { Node } from 'broccoli-node-api'; +import { existsSync } from 'fs'; export default class extends V1Addon { get v2Tree() { @@ -21,11 +22,27 @@ export default class extends V1Addon { return this.app.options.staticEmberSource; } - // ember-source inlines a whole bunch of dependencies into itself + // versions of ember-source prior to + // https://github.com/emberjs/ember.js/pull/20675 ship dist/packages and + // dist/dependencies separately and the imports between them are package-name + // imports. Since many of the dependencies are also true package.json + // dependencies (in order to get typescript types), and our module-resolver + // prioritizes true dependencies, it's necessary to detect and remove the + // package.json dependencies. + // + // After the above linked change, ember-source ships only dist/packages and + // the inter-package imports are all relative. Some of the things in + // dist/packages are still the rolled-in dependencies, but now that the + // imports are all relative we need no special handling for them (beyond the + // normal v2 addon renamed-modules support. @Memoize() private get includedDependencies() { let result: string[] = []; - for (let name of readdirSync(resolve(this.root, 'dist', 'dependencies'))) { + let depsDir = resolve(this.root, 'dist', 'dependencies'); + if (!existsSync(depsDir)) { + return result; + } + for (let name of readdirSync(depsDir)) { if (name[0] === '@') { for (let innerName of readdirSync(resolve(this.root, 'dist', 'dependencies', name))) { if (innerName.endsWith('.js')) { @@ -87,6 +104,7 @@ export default class extends V1Addon { packages, buildFunnel(this.rootTree, { srcDir: 'dist/dependencies', + allowEmpty: true, }), ];