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: ignore import of CSS files inside vanilla-extract files #10

Merged
merged 2 commits into from
Jul 12, 2023
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/silly-spoons-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tabula/forge': patch
---

ignore import of CSS files inside `vanilla-extract` files
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ We support the [vanilla-extract](https://vanilla-extract.style/).

This is zero-runtime CSS-in-JS solution with TypeScript support.

**IMPORTANT**: All imports of CSS files in `*.css.ts` files is ignored.

### SVGR

We support the [SVGR](https://react-svgr.com/) to allow to use SVG images not
Expand Down
24 changes: 18 additions & 6 deletions src/plugins/vanillaExtractPlugin/vanillaExtractPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stat } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';

import { vanillaExtractPlugin as officialPlugin } from '@vanilla-extract/esbuild-plugin';
Expand All @@ -20,12 +21,23 @@ export function vanillaExtractPlugin(): Plugin {
name: 'vanilla-extract-plugin/static',

setup({ onLoad, onResolve }) {
onResolve(staticOptions, ({ importer, path }) => ({
path: resolve(dirname(importer), path),
pluginData: {
importPath: path,
},
}));
onResolve(staticOptions, async ({ importer, path }) => {
const resolvedPath = resolve(dirname(importer), path);

// NOTE: Ignores imports of `vanilla-extract` files itself.
try {
await stat(resolvedPath);
} catch {
return null;
}

return {
path: resolve(dirname(importer), path),
pluginData: {
importPath: path,
},
};
});

onLoad(staticOptions, ({ pluginData }) => ({
contents: `export default ${JSON.stringify((pluginData as PluginData).importPath)};`,
Expand Down
15 changes: 15 additions & 0 deletions tests/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ it(
production: false,
},
async (t, c) => {
t.false(await c.isExists('lib/assets'));

t.snapshot(await c.read('lib/index.css'));
},
);
Expand Down Expand Up @@ -584,6 +586,19 @@ it(
},
);

it(
'ignores import of css files from `vanilla-extract` files',
{
dependencies: ['@vanilla-extract/css'],
name: 'browser-vanilla-extract-css',
platform: 'browser',
production: false,
},
async (t, c) => {
t.snapshot(await c.read('lib/index.css'));
},
);

// ----- Storybook

it(
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/browser-vanilla-extract-css/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "browser-vanilla-extract-static",
"version": "1.0.0",
"type": "module",
"module": "lib/index.js",
"exports": {
".": {
"import": "./lib/index.js"
}
},
"files": [
"lib"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eot
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
otf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ttf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
woff
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
woff2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bmp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ico
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
webp
7 changes: 7 additions & 0 deletions tests/fixtures/browser-vanilla-extract-css/src/index.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { style } from '@vanilla-extract/css';

import { color } from './vars.css';

export const root = style({
color,
});
3 changes: 3 additions & 0 deletions tests/fixtures/browser-vanilla-extract-css/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { root } from './index.css';

document.body.classList.add(root);
38 changes: 38 additions & 0 deletions tests/fixtures/browser-vanilla-extract-css/src/reboot.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@font-face {
font-family: VanillaExtract;
src: url('./assets/font.eot'), url('./assets/font.otf') format('opentype'),
url('./assets/font.ttf') format('truetype'), url('./assets/font.woff') format('woff'),
url('./assets/font.woff2') format('woff2');
}

.bmp {
background-image: url('./assets/image.bmp');
}

.gif {
background-image: url('./assets/image.gif');
}

.ico {
background-image: url('./assets/image.ico');
}

.jpeg {
background-image: url('./assets/image.jpeg');
}

.jpg {
background-image: url('./assets/image.jpg');
}

.png {
background-image: url('./assets/image.png');
}

.svg {
background-image: url('./assets/image.svg');
}

.webp {
background-image: url('./assets/image.webp');
}
3 changes: 3 additions & 0 deletions tests/fixtures/browser-vanilla-extract-css/src/vars.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './reboot.css';

export const color = 'red';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "@tabula/typescript-config/tsconfig.browser.json",

"include": ["src"]
}
43 changes: 27 additions & 16 deletions tests/snapshots/browser.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ The actual snapshot is saved in `browser.test.ts.snap`.

Generated by [AVA](https://avajs.dev).

## resolves paths in SCSS with conditional exports

> Snapshot 1

`/* src/index.module.scss?css-module */␊
.browser-scss--src-index-module__scssModule ul {␊
background-color: red;␊
}␊
/* src/index.scss */␊
.scssRoot ul {␊
background: red;␊
}␊
/*# sourceMappingURL=index.css.map */␊
`

## generates source maps

> Snapshot 1
Expand Down Expand Up @@ -274,6 +258,22 @@ Generated by [AVA](https://avajs.dev).

## resolves paths in SCSS

> Snapshot 1

`/* src/index.module.scss?css-module */␊
.browser-scss--src-index-module__scssModule ul {␊
background-color: red;␊
}␊
/* src/index.scss */␊
.scssRoot ul {␊
background: red;␊
}␊
/*# sourceMappingURL=index.css.map */␊
`

## resolves paths in SCSS with conditional exports

> Snapshot 1

`/* src/index.module.scss?css-module */␊
Expand Down Expand Up @@ -845,6 +845,17 @@ Generated by [AVA](https://avajs.dev).
/*# sourceMappingURL=index.css.map */␊
`

## ignores import of css files from `vanilla-extract` files

> Snapshot 1

`/* vanilla-extract-css-ns:src/index.css.ts.vanilla.css?source=LnNyY19yb290X18xZ2F0MzY0MCB7CiAgY29sb3I6IHJlZDsKfQ== */␊
.src_root__1gat3640 {␊
color: red;␊
}␊
/*# sourceMappingURL=index.css.map */␊
`

## doesn't generate documentation by default

> Snapshot 1
Expand Down
Binary file modified tests/snapshots/browser.test.ts.snap
Binary file not shown.