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

Fix fastboot test #1902

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
16 changes: 5 additions & 11 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class Resolver {
return this.external('early require', request, request.specifier);
}

request = this.handleFastbootSwitch(request);
request = await this.handleFastbootSwitch(request);
request = await this.handleGlobalsCompat(request);
request = this.handleImplicitModules(request);
request = this.handleImplicitTestScripts(request);
Expand Down Expand Up @@ -335,7 +335,7 @@ export class Resolver {
return request;
}

private handleFastbootSwitch<R extends ModuleRequest>(request: R): R {
private async handleFastbootSwitch<R extends ModuleRequest>(request: R): Promise<R> {
if (isTerminal(request)) {
return request;
}
Expand Down Expand Up @@ -369,15 +369,9 @@ export class Resolver {
} else {
targetFile = fastbootFile.localFilename;
}
return logTransition(
'matched app entry',
request,
// deliberately not using rehome because we want
// generateFastbootSwitch to see that this request is coming *from*
// a fastboot switch so it won't cycle back around. Instead we make
// the targetFile relative to the fromFile that we already have.
request.alias(explicitRelative(dirname(request.fromFile), resolve(pkg.root, targetFile)))
);

let resolution = await request.alias(targetFile).rehome(resolve(pkg.root, 'package.json')).defaultResolve();
return logTransition('matched app entry', request, request.resolveTo(resolution));
}
}

Expand Down
19 changes: 17 additions & 2 deletions packages/vite/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import type { Plugin, ViteDevServer } from 'vite';
import { virtualContent, ResolverLoader } from '@embroider/core';
import { type PluginItem, transform } from '@babel/core';
import { locateEmbroiderWorkingDir, virtualContent, ResolverLoader } from '@embroider/core';
import { RollupModuleRequest, virtualPrefix } from './request';
import assertNever from 'assert-never';
import makeDebug from 'debug';
import { resolve } from 'path';
import { readJSONSync } from 'fs-extra';

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 macrosConfig: PluginItem | undefined;

return {
name: 'embroider-resolver',
Expand Down Expand Up @@ -57,7 +60,12 @@ export function resolver(): Plugin {
let { src, watches } = virtualContent(pathname.slice(virtualPrefix.length + 1), resolverLoader.resolver);
virtualDeps.set(id, watches);
server?.watcher.add(watches);
return src;
if (!macrosConfig) {
macrosConfig = readJSONSync(
resolve(locateEmbroiderWorkingDir(process.cwd()), 'rewritten-app', 'macros-config.json')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To change after rebase, relates to #1935:

resolve(locateEmbroiderWorkingDir(process.cwd()), 'macros-config.json')

) as PluginItem;
}
return pathname.endsWith('.css') ? src : runMacros(src, id, macrosConfig);
}
},
buildEnd() {
Expand All @@ -80,3 +88,10 @@ export function resolver(): Plugin {
},
};
}

function runMacros(src: string, filename: string, macrosConfig: PluginItem): string {
return transform(src, {
filename,
plugins: [macrosConfig],
})!.code!;
}
Loading