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

vite: Fix unresolvable imports inside monorepos #716

Merged
merged 10 commits into from
Jun 5, 2022
5 changes: 5 additions & 0 deletions .changeset/brave-seals-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/vite-plugin': patch
---

Fix unresolvable imports inside monorepos
46 changes: 29 additions & 17 deletions packages/vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
let virtualExt: string;
let packageName: string;

const getAbsoluteVirtualFileId = (source: string) =>
normalizePath(path.join(config.root, source));

return {
name: 'vanilla-extract',
enforce: 'pre',
Expand Down Expand Up @@ -59,24 +62,27 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {

virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
},
resolveId(id) {
if (!id.endsWith(virtualExt)) {
resolveId(source) {
if (!source.endsWith(virtualExt)) {
return;
}

const normalizedId = id.startsWith('/') ? id.slice(1) : id;
// Absolute paths seem to occur often in monorepos, where files are
// imported from outside the config root.
const absoluteId = source.startsWith(config.root)
? source
: getAbsoluteVirtualFileId(source);

if (cssMap.has(normalizedId)) {
return normalizePath(path.join(config.root, normalizedId));
if (cssMap.has(absoluteId)) {
return absoluteId;
}
},
load(id) {
if (!id.endsWith(virtualExt)) {
if (!cssMap.has(id)) {
return;
}

const cssFileId = id.slice(config.root.length + 1);
const css = cssMap.get(cssFileId);
const css = cssMap.get(id);

if (typeof css !== 'string') {
return;
Expand All @@ -90,13 +96,13 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
import { injectStyles } from '@vanilla-extract/css/injectStyles';

const inject = (css) => injectStyles({
fileScope: ${JSON.stringify({ filePath: cssFileId })},
fileScope: ${JSON.stringify({ filePath: id })},
css
});

inject(${JSON.stringify(css)});

import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
inject(css);
});
`;
Expand Down Expand Up @@ -145,7 +151,8 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
identOption:
identifiers ?? (config.mode === 'production' ? 'short' : 'debug'),
serializeVirtualCssPath: async ({ fileScope, source }) => {
const id = `${fileScope.filePath}${virtualExt}`;
const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);

let cssSource = source;

Expand All @@ -161,25 +168,30 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
cssSource = postCssResult.css;
}

if (server && cssMap.has(id) && cssMap.get(id) !== source) {
if (
server &&
cssMap.has(absoluteId) &&
cssMap.get(absoluteId) !== source
) {
const { moduleGraph } = server;
const moduleId = normalizePath(path.join(config.root, id));
const module = moduleGraph.getModuleById(moduleId);
const module = moduleGraph.getModuleById(absoluteId);

if (module) {
moduleGraph.invalidateModule(module);
}

server.ws.send({
type: 'custom',
event: styleUpdateEvent(id),
event: styleUpdateEvent(absoluteId),
data: cssSource,
});
}

cssMap.set(id, cssSource);
cssMap.set(absoluteId, cssSource);

return `import "${id}";`;
// We use the root relative id here to ensure file contents (content-hashes)
// are consistent across build machines
return `import "${rootRelativeId}";`;
},
});

Expand Down