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

remove auto-upgraded from app package.json and add a basic exports block #1936

Merged
merged 8 commits into from
Aug 9, 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
42 changes: 22 additions & 20 deletions packages/compat/src/compat-app-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,23 +470,7 @@ export class CompatAppBuilder {
assetPaths.push('package.json');
}

let meta: AppMeta = {
type: 'app',
version: 2,
assets: assetPaths,
babel: {
filename: '_babel_config_.js',
isParallelSafe: true, // TODO
majorVersion: this.compatApp.babelMajorVersion(),
fileFilter: '_babel_filter_.js',
},
'root-url': this.rootURL(),
};

// all compat apps are auto-upgraded, there's no v2 app format here
meta['auto-upgraded'] = true;

let pkg = this.combinePackageJSON(meta);
let pkg = this.combinePackageJSON(assetPaths);
writeFileSync(join(this.root, 'package.json'), JSON.stringify(pkg, null, 2), 'utf8');

let resolverConfig = this.resolverConfig(appFiles);
Expand All @@ -502,15 +486,33 @@ export class CompatAppBuilder {
this.addMacrosConfig(this.compatApp.macrosConfig.babelPluginConfig()[0]);
}

private combinePackageJSON(meta: AppMeta): object {
private combinePackageJSON(assetPaths: string[]): object {
let pkgLayers: any[] = [this.origAppPackage.packageJSON];
let fastbootConfig = this.fastbootConfig;
if (fastbootConfig) {
// fastboot-specific package.json output is allowed to add to our original package.json
pkgLayers.push(fastbootConfig.packageJSON);
}
// but our own new v2 app metadata takes precedence over both

let meta: AppMeta = {
type: 'app',
version: 2,
assets: assetPaths,
babel: {
filename: '_babel_config_.js',
isParallelSafe: true, // TODO
majorVersion: this.compatApp.babelMajorVersion(),
fileFilter: '_babel_filter_.js',
},
'root-url': this.rootURL(),
};

if ((this.origAppPackage.packageJSON['ember-addon']?.version ?? 0) < 2) {
meta['auto-upgraded'] = true;
}

pkgLayers.push({ 'ember-addon': meta });

return combinePackageJSON(...pkgLayers);
}

Expand Down Expand Up @@ -621,7 +623,7 @@ export class CompatAppBuilder {
The following code is included via content-for 'config-module':

${configModule}

1. If you want to keep the same behavior, add it to the app/environment.js.
2. Once app/environment.js has the content you need, remove the present error by setting "useAddonConfigModule" to false in the build options.
`);
Expand Down
23 changes: 11 additions & 12 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1189,17 +1189,16 @@ export class Resolver {
}

// assertions on what native v2 addons can import
if (!pkg.meta['auto-upgraded']) {
if (
!pkg.meta['auto-upgraded'] &&
!appImportInAppTree(pkg, logicalPackage, packageName) &&
!reliablyResolvable(pkg, packageName)
) {
throw new Error(
`${pkg.name} is trying to import from ${packageName} but that is not one of its explicit dependencies`
);
}
if (
!pkg.needsLooseResolving() &&
!appImportInAppTree(pkg, logicalPackage, packageName) &&
!reliablyResolvable(pkg, packageName)
) {
throw new Error(
`${pkg.name} is trying to import from ${packageName} but that is not one of its explicit dependencies`
);
}

return request;
}

Expand Down Expand Up @@ -1339,7 +1338,7 @@ export class Resolver {
}

// auto-upgraded packages can fall back to the set of known active addons
if (pkg.meta['auto-upgraded']) {
if (pkg.needsLooseResolving()) {
let addon = this.locateActiveAddon(packageName);
if (addon) {
const rehomed = request.rehome(addon.canResolveFromFile);
Expand Down Expand Up @@ -1375,7 +1374,7 @@ export class Resolver {
}
}

if (pkg.meta['auto-upgraded'] && (request.meta?.runtimeFallback ?? true)) {
if (pkg.needsLooseResolving() && (request.meta?.runtimeFallback ?? true)) {
// auto-upgraded packages can fall back to attempting to find dependencies at
// runtime. Native v2 packages can only get this behavior in the
// isExplicitlyExternal case above because they need to explicitly ask for
Expand Down
4 changes: 4 additions & 0 deletions packages/shared-internals/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export default class Package {
return this.isV2Ember() && this.packageJSON['ember-addon'].type === 'app';
}

needsLooseResolving(): boolean {
return this.isV2App() || ((this.isV2Addon() && this.meta['auto-upgraded']) ?? false);
}

isV2Addon(): this is V2AddonPackage {
return this.isV2Ember() && this.packageJSON['ember-addon'].type === 'addon';
}
Expand Down
4 changes: 4 additions & 0 deletions packages/shared-internals/src/rewritten-package-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ class WrappedPackage implements PackageTheGoodParts {
return this.plainPkg.isV2Addon();
}

needsLooseResolving(): boolean {
return this.plainPkg.needsLooseResolving();
}

// it's important that we're calling this.dependencies here at this level, not
// plainPkg.dependencies, which wouldn't be correct
findDescendants(filter?: (pkg: Package) => boolean): Package[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default function main(babel: typeof Babel) {

if (state.opts.packageGuard) {
let owningPackage = PackageCache.shared('embroider', state.opts.appRoot).ownerOfFile(filename);
if (!owningPackage || !owningPackage.isV2Ember() || !owningPackage.meta['auto-upgraded']) {
if (!owningPackage || !owningPackage.needsLooseResolving()) {
debug('not handling colocation for %s', filename);
return;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/app-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"doc": "doc",
"test": "tests"
},
"exports": {
"./*": "./*"
},
"scripts": {
"build": "vite build",
"lint": "concurrently \"npm:lint:*(!fix)\" --names \"lint:\"",
Expand Down Expand Up @@ -80,5 +83,9 @@
},
"ember": {
"edition": "octane"
},
"ember-addon": {
"type": "app",
"version": 2
}
}
7 changes: 7 additions & 0 deletions tests/ts-app-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"doc": "doc",
"test": "tests"
},
"exports": {
"./*": "./*"
},
"scripts": {
"build": "ember build --environment=production",
"lint": "concurrently \"npm:lint:*(!fix)\" --names \"lint:\"",
Expand Down Expand Up @@ -84,5 +87,9 @@
},
"ember": {
"edition": "octane"
},
"ember-addon": {
"type": "app",
"version": 2
}
}