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

Use realistic file paths in file scopes #647

Merged
merged 6 commits into from
May 2, 2022
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
5 changes: 5 additions & 0 deletions .changeset/friendly-geckos-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/esbuild-plugin': patch
---

Allow CSS url imports to be resolved
6 changes: 6 additions & 0 deletions .changeset/gold-chairs-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@vanilla-extract/webpack-plugin': patch
'@vanilla-extract/vite-plugin': patch
---

Use more realistic file paths for virtual CSS files
5 changes: 5 additions & 0 deletions .changeset/nine-wasps-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/integration': major
---

Update `addFileScope` to always update and only use file names
9 changes: 8 additions & 1 deletion packages/esbuild-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { dirname, join } from 'path';

import {
cssFileFilter,
virtualCssFileFilter,
Expand Down Expand Up @@ -43,15 +45,20 @@ export function vanillaExtractPlugin({
build.onLoad(
{ filter: /.*/, namespace: vanillaCssNamespace },
async ({ path }) => {
let { source } = await getSourceFromVirtualCssFile(path);
let { source, fileName } = await getSourceFromVirtualCssFile(path);

if (typeof processCss === 'function') {
source = await processCss(source);
}

const rootDir = build.initialOptions.absWorkingDir ?? process.cwd();

const resolveDir = dirname(join(rootDir, fileName));
mattcompiles marked this conversation as resolved.
Show resolved Hide resolved

return {
contents: source,
loader: 'css',
resolveDir,
};
},
);
Expand Down
108 changes: 108 additions & 0 deletions packages/integration/src/addFileScope.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { outdent } from 'outdent';
import { addFileScope } from './addFileScope';

test('should add missing fileScope', () => {
const source = outdent`
import {style} from '@vanilla-extract/css';

export const myStyle = style({});
`;

expect(
addFileScope({
source,
rootPath: '/the-root',
filePath: '/the-root/app/app.css.ts',
}),
).toMatchInlineSnapshot(`
"
import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
setFileScope(\\"app/app.css.ts\\");
import {style} from '@vanilla-extract/css';

export const myStyle = style({});
endFileScope();
"
`);
});

test('should update existing fileScope', () => {
const source = outdent`
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
setFileScope("some-weird-file", "some-weird-package");
import {style} from '@vanilla-extract/css';

export const myStyle = style({});
endFileScope();
`;

expect(
addFileScope({
source,
rootPath: '/the-root',
filePath: '/the-root/app/app.css.ts',
}),
).toMatchInlineSnapshot(`
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
setFileScope(\\"app/app.css.ts\\");
import {style} from '@vanilla-extract/css';

export const myStyle = style({});
endFileScope();"
`);
});

test('should update existing fileScope with newlines', () => {
const source = outdent`
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
setFileScope(
"some-weird-file",
"some-weird-package"
);
import {style} from '@vanilla-extract/css';

export const myStyle = style({});
endFileScope();
`;

expect(
addFileScope({
source,
rootPath: '/the-root',
filePath: '/the-root/app/app.css.ts',
}),
).toMatchInlineSnapshot(`
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
setFileScope(\\"app/app.css.ts\\");
import {style} from '@vanilla-extract/css';

export const myStyle = style({});
endFileScope();"
`);
});

test('should handle namespaced filescope calls', () => {
const source = outdent`
import * as vanillaFileScope from "@vanilla-extract/css/fileScope";
vanillaFileScope.setFileScope("some-weird-file");
import {style} from '@vanilla-extract/css';

export const myStyle = style({});
vanillaFileScope.endFileScope();
`;

expect(
addFileScope({
source,
rootPath: '/the-root',
filePath: '/the-root/app/app.css.ts',
}),
).toMatchInlineSnapshot(`
"import * as vanillaFileScope from \\"@vanilla-extract/css/fileScope\\";
vanillaFileScope.setFileScope(\\"app/app.css.ts\\");
import {style} from '@vanilla-extract/css';

export const myStyle = style({});
vanillaFileScope.endFileScope();"
`);
});
26 changes: 11 additions & 15 deletions packages/integration/src/addFileScope.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import { posix, relative, sep } from 'path';
import type { PackageInfo } from './packageInfo';

interface AddFileScopeParams {
source: string;
filePath: string;
packageInfo: PackageInfo;
rootPath: string;
}
export function addFileScope({
source,
filePath,
packageInfo,
rootPath,
}: AddFileScopeParams) {
if (source.indexOf('@vanilla-extract/css/fileScope') > -1) {
return { source, updated: false };
}

// Encode windows file paths as posix
const normalizedPath = posix.join(
...relative(packageInfo.dirname, filePath).split(sep),
);
const normalizedPath = posix.join(...relative(rootPath, filePath).split(sep));

const packageName = packageInfo.name ? `"${packageInfo.name}"` : 'undefined';
if (source.indexOf('@vanilla-extract/css/fileScope') > -1) {
return source.replace(
/setFileScope\(((\n|.)*?)\)/,
`setFileScope("${normalizedPath}")`,
);
}

const contents = `
return `
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
setFileScope("${normalizedPath}", ${packageName});
setFileScope("${normalizedPath}");
${source}
endFileScope();
`;

return { source: contents, updated: true };
}
19 changes: 7 additions & 12 deletions packages/integration/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,25 @@ import { promises as fs } from 'fs';
import { build as esbuild, Plugin } from 'esbuild';

import { cssFileFilter } from './filters';
import { getPackageInfo } from './packageInfo';
import { addFileScope } from './addFileScope';

export const vanillaExtractFilescopePlugin = (): Plugin => ({
name: 'vanilla-extract-filescope',
setup(build) {
const packageInfo = getPackageInfo(build.initialOptions.absWorkingDir);

build.onLoad({ filter: cssFileFilter }, async ({ path }) => {
const originalSource = await fs.readFile(path, 'utf-8');

const { source, updated } = addFileScope({
const source = addFileScope({
source: originalSource,
filePath: path,
packageInfo,
rootPath: build.initialOptions.absWorkingDir!,
});

if (updated) {
return {
contents: source,
loader: path.match(/\.(ts|tsx)$/i) ? 'ts' : undefined,
resolveDir: dirname(path),
};
}
return {
contents: source,
loader: path.match(/\.(ts|tsx)$/i) ? 'ts' : undefined,
resolveDir: dirname(path),
};
});
},
});
Expand Down
Loading