Skip to content

Commit

Permalink
fix initial dep scan
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Jun 21, 2024
1 parent 6c9f59a commit 84415eb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
14 changes: 6 additions & 8 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,9 @@ export class Resolver {
if (isTerminal(request)) {
return request;
}
if (request.meta?.skipHandleRewrittenPackages) {
return request;
}
if (request.specifier.startsWith('@embroider/rewritten-packages')) {
return request;
}
Expand Down Expand Up @@ -1286,13 +1289,7 @@ export class Resolver {
};
let isVirtual = request.isVirtual;
let path = request.specifier;
if (
path.startsWith('/@embroider/core/') ||
path.startsWith('@embroider/core/') ||
path.startsWith('.') ||
path.startsWith('#') ||
engineNames.some(packageName => path.startsWith(packageName))
) {
if (path.startsWith('.') || path.startsWith('#') || engineNames.some(packageName => path.startsWith(packageName))) {
let resolved = await this.beforeResolve(request);
if (resolved.isVirtual) {
isVirtual = true;
Expand All @@ -1316,11 +1313,12 @@ export class Resolver {
}
}
} else {
let resolved = this.handleRenaming(request);
let resolved = await this.beforeResolve(request.withMeta({ skipHandleRewrittenPackages: true }));
if (resolved.specifier) {
res.path = resolved.specifier;
res.importer = resolved.fromFile;
}
isVirtual = isVirtual || resolved.isVirtual;
}
if (!isVirtual) {
const ext = extname(res.path);
Expand Down
14 changes: 6 additions & 8 deletions packages/vite/src/esbuild-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,24 @@ export function esBuildResolver(root = process.cwd()): EsBuildPlugin {
// so it can do its import analysis
// this is something like what vite needs to do for aliases
let alias = await resolverLoader.resolver.resolveAlias(request);
console.log('alias', alias.specifier, alias.isVirtual);
if (alias.isVirtual) {
return {
namespace: 'embroider',
path: alias.specifier,
};
}
let isExcluded = false;
if (excluded && excluded.some((addon: string) => alias.specifier?.startsWith(addon))) {
// just mark directly as external and do not tell vite
return {
external: true,
path: alias.specifier,
};
isExcluded = true;
}
alias = resolverLoader.resolver.makeResolvable(alias);
args.importer = alias.fromFile || importer;
path = alias.specifier;
let res = (await build.resolve(path, args)) as any;
console.log('scan', path, res?.path, res?.namespace);

// if its excluded we want to scan its dependencies
if (res && isExcluded) {
res.external = false;
}
return res;
});

Expand Down
46 changes: 31 additions & 15 deletions tests/scenarios/vite-dep-optimizer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,42 +91,58 @@ app.forEachScenario(scenario => {
);
}

Qmodule(`initial dep scan`, function (hooks) {
Qmodule('vite esbuild dep scan', function (hooks) {
let server: CommandWatcher;
hooks.before(async () => {
server = CommandWatcher.launch('vite', ['--force', '--clearScreen', 'false'], { cwd: app.dir });
[, appURL] = await server.waitFor(/Local:\s*(.*)/);
});
let expectAudit: ReturnType<typeof setupAuditTest>;
hooks.after(async () => {
await server.shutdown();
});
let optimizedFiles: string[] = [];
test('created initial optimized deps', async function (assert) {
test('initial dep scan', async function (assert) {
// wait until deps are generated without accessing any API
let esbuildScanOptimizedDeps = [];
while (true) {
console.log('test');
console.log(existsSync(join(app.dir, 'node_modules', '.vite')));
if (existsSync(join(app.dir, 'node_modules', '.vite'))) {
const deps = readdirSync(join(app.dir, 'node_modules', '.vite'))[0];
let currentOptimizedFiles = readdirSync(join(app.dir, 'node_modules', '.vite', deps)).filter(f =>
f.endsWith('.js')
);
if (currentOptimizedFiles.length !== 0 && currentOptimizedFiles.length === optimizedFiles.length) {
console.log(currentOptimizedFiles);
if (
currentOptimizedFiles.length !== 0 &&
currentOptimizedFiles.length === esbuildScanOptimizedDeps.length
) {
break;
}
optimizedFiles.length = 0;
optimizedFiles.push(...currentOptimizedFiles);
esbuildScanOptimizedDeps.length = 0;
esbuildScanOptimizedDeps.push(...currentOptimizedFiles);
}
await new Promise(resolve => setTimeout(resolve, 500));
}
assert.equal(esbuildScanOptimizedDeps.length, 132);
});
});

Qmodule(`vite dep tests`, function (hooks) {
let server: CommandWatcher;
hooks.before(async () => {
server = CommandWatcher.launch('vite', ['--force', '--clearScreen', 'false'], { cwd: app.dir });
[, appURL] = await server.waitFor(/Local:\s*(.*)/);
});
let expectAudit = setupAuditTest(hooks, () => ({
appURL,
startingFrom: ['tests/index.html', 'index.html'],
fetch: fetch as unknown as typeof globalThis.fetch,
}));
hooks.after(async () => {
await server.shutdown();
});
let optimizedFiles: string[] = [];
test('created initial optimized deps', async function (assert) {
await waitUntilOptimizedReady(expectAudit);
optimizedFiles = readdirSync(join(app.dir, 'node_modules', '.vite', 'deps')).filter(f => f.endsWith('.js'));
assert.ok(optimizedFiles.length === 132, `should have created optimized deps: ${optimizedFiles.length}`);
expectAudit = setupAuditTest(hooks, () => ({
appURL,
startingFrom: ['tests/index.html', 'index.html'],
fetch: fetch as unknown as typeof globalThis.fetch,
}));
});

test('should use all optimized deps', function (assert) {
Expand Down

0 comments on commit 84415eb

Please sign in to comment.