Skip to content

Commit

Permalink
optimize new added imports
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Sep 12, 2024
1 parent f7656a3 commit 67afc0c
Show file tree
Hide file tree
Showing 9 changed files with 599 additions and 27 deletions.
29 changes: 25 additions & 4 deletions packages/compat/src/compat-addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { locateEmbroiderWorkingDir, RewrittenPackageCache, WaitForTrees } from '
import TreeSync from 'tree-sync';
import type CompatApp from './compat-app';
import { convertLegacyAddons } from './standalone-addon-build';
import { ensureSymlinkSync, writeFileSync, existsSync } from 'fs-extra';

// This build stage expects to be run with broccoli memoization enabled in order
// to get good rebuild performance. We turn it on by default here, but you can
Expand Down Expand Up @@ -47,11 +48,9 @@ export default class CompatAddons implements Stage {
},
changedMap: Map<string, boolean>
) {
let rewrittenPackages = resolve(locateEmbroiderWorkingDir(this.compatApp.root), 'rewritten-packages');
if (!this.treeSync) {
this.treeSync = new TreeSync(
addons,
resolve(locateEmbroiderWorkingDir(this.compatApp.root), 'rewritten-packages')
);
this.treeSync = new TreeSync(addons, rewrittenPackages);
}

if (
Expand All @@ -61,6 +60,28 @@ export default class CompatAddons implements Stage {
this.treeSync.sync();
RewrittenPackageCache.shared('embroider', this.compatApp.root).invalidateIndex();
}
const resolvableRewrittenPackages = resolve(
locateEmbroiderWorkingDir(this.compatApp.root),
'..',
'@embroider',
'rewritten-packages'
);
const embroiderDir = resolve(locateEmbroiderWorkingDir(this.compatApp.root), 'rewritten-packages');
console.log('link', embroiderDir, resolvableRewrittenPackages, existsSync(embroiderDir));
if (existsSync(embroiderDir)) {
ensureSymlinkSync(embroiderDir, resolvableRewrittenPackages, 'dir');
writeFileSync(
resolve(resolvableRewrittenPackages, 'package.json'),
JSON.stringify(
{
name: '@embroider/rewritten-packages',
main: 'moved-package-target.js',
},
null,
2
)
);
}
this.didBuild = true;
}
}
65 changes: 51 additions & 14 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
packageName as getPackageName,
packageName,
} from '@embroider/shared-internals';
import { dirname, resolve, posix } from 'path';
import { dirname, resolve, posix, extname } from 'path';
import type { Package, V2Package } from '@embroider/shared-internals';
import { explicitRelative, RewrittenPackageCache } from '@embroider/shared-internals';
import makeDebug from 'debug';
Expand Down Expand Up @@ -594,6 +594,7 @@ export class Resolver {

let hbsModule: Resolution | null = null;
let jsModule: Resolution | null = null;
let jsSpecifier: string | null = null;

// first, the various places our template might be.
for (let candidate of this.componentTemplateCandidates(target.packageName)) {
Expand Down Expand Up @@ -629,6 +630,7 @@ export class Resolver {
// It matches as a priority lower than .js, so finding an .hbs means
// there's definitely not a .js.
if (resolution.type === 'found' && !resolution.filename.endsWith('.hbs')) {
jsSpecifier = candidateSpecifier;
jsModule = resolution;
break;
}
Expand All @@ -640,8 +642,20 @@ export class Resolver {
request,
request.virtualize(virtualPairComponent(hbsModule.filename, jsModule?.filename))
);
} else if (jsModule) {
return logTransition(`resolving to resolveComponent found only JS`, request, request.resolveTo(jsModule));
} else if (jsSpecifier && jsModule) {
let newRequest = request.alias(jsSpecifier);
let fromPkg = this.packageCache.ownerOfFile(target.from)!;
let renamedRequest = this.handleRenaming(newRequest);
if (!renamedRequest.specifier.startsWith('.')) {
newRequest = renamedRequest;
}
let targetPkgName = getPackageName(newRequest.specifier);
if (targetPkgName && fromPkg.name !== targetPkgName) {
newRequest = this.makeResolvable(newRequest);
} else {
newRequest = renamedRequest;
}
return logTransition(`resolving to resolveComponent found only JS`, request, newRequest);
} else {
return logTransition(`resolveComponent failed`, request);
}
Expand All @@ -662,11 +676,12 @@ export class Resolver {
})
);

// for the case of 'ignored' that means that esbuild found this helper in an external
// package so it should be considered found in this case and we should not look for a
// component with this name
if (helperMatch.type === 'found' || helperMatch.type === 'ignored') {
return logTransition('resolve to ambiguous case matched a helper', request, request.resolveTo(helperMatch));
if (helperMatch.type === 'found') {
return logTransition(
'resolve to ambiguous case matched a helper',
request,
request.alias(helperCandidate.specifier)
);
}

// unlike resolveHelper, resolveComponent already does pre-resolution in
Expand Down Expand Up @@ -874,6 +889,12 @@ export class Resolver {
if (isTerminal(request)) {
return request;
}
if (request.meta?.skipHandleRewrittenPackages) {
return request;
}
if (request.specifier.startsWith('@embroider/rewritten-packages')) {
return request;
}
let requestingPkg = this.packageCache.ownerOfFile(request.fromFile);
if (!requestingPkg) {
return request;
Expand Down Expand Up @@ -1042,12 +1063,20 @@ export class Resolver {
}

private resolveWithinMovedPackage<R extends ModuleRequest>(request: R, pkg: Package): R {
let levels = ['..'];
if (pkg.name.startsWith('@')) {
levels.push('..');
}
let originalFromFile = request.fromFile;
let newRequest = request.rehome(resolve(pkg.root, ...levels, 'moved-package-target.js'));
if (!pkg.isV2Addon()) {
let levels = ['..'];
if (pkg.name.startsWith('@')) {
levels.push('..');
}
let newRequest = request.rehome(resolve(pkg.root, ...levels, 'moved-package-target.js'));

// setting meta because if this fails, we want the fallback to pick up back
// in the original requesting package.
return newRequest.withMeta({ originalFromFile });
}

let newRequest = this.makeResolvable(request);

if (newRequest === request) {
return request;
Expand Down Expand Up @@ -1228,6 +1257,10 @@ export class Resolver {
);
}

if (request.specifier.startsWith('@embroider/rewritten-packages')) {
return request;
}

if (request.specifier === '@embroider/macros') {
// the macros package is always handled directly within babel (not
// necessarily as a real resolvable package), so we should not mess with it.
Expand Down Expand Up @@ -1289,7 +1322,11 @@ export class Resolver {
if (pkg.needsLooseResolving()) {
let activeAddon = this.maybeFallbackToActiveAddon(request, packageName);
if (activeAddon) {
return activeAddon;
const rehomed = request.rehome(activeAddon.canResolveFromFile);
if (rehomed !== request) {
let newRequest = this.makeResolvable(rehomed);
return logTransition(`activeAddons`, request, newRequest);
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/shared-internals/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ export default class Package {

@Memoize()
protected get internalPackageJSON() {
return JSON.parse(readFileSync(join(this.root, 'package.json'), 'utf8'));
try {
return JSON.parse(readFileSync(join(this.root, 'package.json'), 'utf8'));
} catch {
return {};
}
}

@Memoize()
Expand Down Expand Up @@ -91,7 +95,7 @@ export default class Package {
}

get meta(): AddonMeta | AppMeta | undefined {
let m = this.packageJSON['ember-addon'];
let m = this.packageJSON['ember-addon'] || {};
if (this.isV2App()) {
return m as unknown as AppMeta;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/optimize-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ export interface OptimizeDeps {
[key: string]: unknown;
}

export function optimizeDeps(): OptimizeDeps {
export function optimizeDeps(options?: OptimizeDeps): OptimizeDeps {
return {
exclude: ['@embroider/macros'],
...options,
exclude: ['@embroider/macros', ...(options?.exclude || [])],
extensions: ['.hbs', '.gjs', '.gts'],
esbuildOptions: {
plugins: [esBuildResolver()],
Expand Down
20 changes: 18 additions & 2 deletions packages/vite/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { virtualContent, ResolverLoader } from '@embroider/core';
import { RollupModuleRequest, virtualPrefix } from './request';
import assertNever from 'assert-never';
import makeDebug from 'debug';
import { resolve } from 'path';
import { resolve, join } from 'path';
import { writeStatus } from './esbuild-request';
import type { PluginContext } from 'rollup';
import type { PartialResolvedId, PluginContext } from 'rollup';

const debug = makeDebug('embroider:vite');

export function resolver(): Plugin {
let resolverLoader = new ResolverLoader(process.cwd());
let server: ViteDevServer;
let virtualDeps: Map<string, string[]> = new Map();
let optimizedDeps: Map<string, string> = new Map();

return {
name: 'embroider-resolver',
Expand Down Expand Up @@ -46,10 +47,25 @@ export function resolver(): Plugin {
// fallthrough to other rollup plugins
return null;
}
let alias = await resolverLoader.resolver.resolveAlias(request);
if (request.fromFile && optimizedDeps.has(request.fromFile)) {
request = request.rehome(join(optimizedDeps.get(request.fromFile)!, 'package.json'));
}

let resolution = await resolverLoader.resolver.resolve(request);
switch (resolution.type) {
case 'found':
case 'ignored':
let res = resolution.result as PartialResolvedId;
if (res.id.includes('.vite/deps')) {
let pkg = resolverLoader.resolver.packageCache.ownerOfFile(
resolve('node_modules', alias.specifier.replace('@embroider', '.embroider'))
);
if (pkg) {
pkg = resolverLoader.resolver.packageCache.maybeMoved(pkg) || pkg;
optimizedDeps.set(res.id.split('?')[0], pkg.root);
}
}
return resolution.result;
case 'not_found':
return null;
Expand Down
Loading

0 comments on commit 67afc0c

Please sign in to comment.