-
Notifications
You must be signed in to change notification settings - Fork 296
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 style compilation for file paths containing webpack template strings #1247
Changes from all commits
9456e13
558714e
a3ef767
f86994f
9944c18
68cd3f7
883374b
bcf189c
6f4b38d
9c5ecbf
8860a19
f32b298
fd59ed0
0e17aac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@vanilla-extract/webpack-plugin': patch | ||
--- | ||
|
||
Fixes a bug that was causing style compilation to fail on paths containing [webpack template strings] such as `[id]` or [Next.js dynamic routes] such as `[slug]`. | ||
|
||
[webpack template strings]: https://webpack.js.org/configuration/output/#template-strings | ||
[next.js dynamic routes]: https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite App</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="./src/index.ts"></script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@fixtures/template-string-paths", | ||
"version": "0.0.1", | ||
"main": "src/index.ts", | ||
"sideEffects": true, | ||
"author": "SEEK", | ||
"private": true, | ||
"dependencies": { | ||
"@vanilla-extract/css": "1.14.1" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const catchAllSegment = style({ color: 'lime' }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const optionalCatchAllSegment = style({ color: 'orchid' }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const doubleSquareBracketId = style({ color: 'darkkhaki' }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const emptySquareBrackets = style({ color: 'blue' }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const singleSquareBracketsId = style({ color: 'tomato' }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { emptySquareBrackets } from './[]/index.css'; | ||
import { singleSquareBracketsId } from './[id]/index.css'; | ||
import { doubleSquareBracketId } from './[[id]]/index.css'; | ||
import { catchAllSegment } from './[...slug]/index.css'; | ||
import { optionalCatchAllSegment } from './[[...slug]]/index.css'; | ||
|
||
// Fixture for testing escaping of webpack template strings and Next.js dyanmic routes | ||
// https://webpack.js.org/configuration/output/#template-strings | ||
// https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes | ||
function render() { | ||
document.body.innerHTML = ` | ||
<div class="${emptySquareBrackets}">[] path</div> | ||
<div class="${singleSquareBracketsId}">[id] path</div> | ||
<div class="${doubleSquareBracketId}">[[id]] path</div> | ||
<div class="${catchAllSegment}">[...slug] path</div> | ||
<div class="${optionalCatchAllSegment}">[[...slug]] path</div> | ||
`; | ||
} | ||
|
||
render(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`escapeWebpackTemplateString() /some/path/[...slug].js pattern 1`] = `"/some/path/[...slug].js"`; | ||
|
||
exports[`escapeWebpackTemplateString() /some/path/[[...slug]]/index.js pattern 1`] = `"/some/path/[[...slug]]/index.js"`; | ||
|
||
exports[`escapeWebpackTemplateString() /some/path[]/[slug]/[[foo]]/index.js pattern 1`] = `"/some/path[]/[\\slug\\]/[[\\foo\\]]/index.js"`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { escapeWebpackTemplateString } from './compiler'; | ||
|
||
describe('escapeWebpackTemplateString()', () => { | ||
test.each([ | ||
'/some/path/[...slug].js', | ||
'/some/path/[[...slug]]/index.js', | ||
'/some/path[]/[slug]/[[foo]]/index.js', | ||
])('%s pattern', (filePath) => { | ||
expect(escapeWebpackTemplateString(filePath)).toMatchSnapshot(); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { expect } from '@playwright/test'; | ||
import { | ||
getStylesheet, | ||
startFixture, | ||
TestServer, | ||
} from '@vanilla-extract-private/test-helpers'; | ||
|
||
import test from './fixture'; | ||
import { webpack as testCases } from './testCases'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chose to restrict this fixture to only run using webpack. |
||
|
||
testCases.forEach(({ type, mode, snapshotCss = true }) => { | ||
test.describe(`template-string-paths - ${type} (${mode})`, () => { | ||
let server: TestServer; | ||
|
||
test.beforeAll(async ({ port }) => { | ||
server = await startFixture('template-string-paths', { | ||
type, | ||
mode, | ||
basePort: port, | ||
}); | ||
}); | ||
|
||
test('screenshot', async ({ page }) => { | ||
await page.goto(server.url); | ||
|
||
expect(await page.screenshot()).toMatchSnapshot( | ||
'template-string-paths.png', | ||
); | ||
}); | ||
|
||
if (snapshotCss) { | ||
test('CSS @agnostic', async () => { | ||
expect( | ||
await getStylesheet(server.url, server.stylesheet), | ||
).toMatchSnapshot(`template-string-paths-${type}--${mode}.css`); | ||
}); | ||
} | ||
|
||
test.afterAll(async () => { | ||
await server.close(); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.\[\]_emptySquareBrackets__13abg9g0 { | ||
color: blue; | ||
} | ||
.\[id\]_singleSquareBracketsId__1d2wsrw0 { | ||
color: tomato; | ||
} | ||
.\[\[id\]\]_doubleSquareBracketId__1aosxxv0 { | ||
color: darkkhaki; | ||
} | ||
.\[\.\.\.slug\]_catchAllSegment__169etlp0 { | ||
color: lime; | ||
} | ||
.\[\[\.\.\.slug\]\]_optionalCatchAllSegment__1kvknas0 { | ||
color: orchid; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
._13abg9g0 { | ||
color: blue; | ||
} | ||
._1d2wsrw0 { | ||
color: tomato; | ||
} | ||
._1aosxxv0 { | ||
color: darkkhaki; | ||
} | ||
._169etlp0 { | ||
color: lime; | ||
} | ||
._1kvknas0 { | ||
color: orchid; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"compilerOptions": { | ||
"target": "ESNEXT" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */, | ||
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, | ||
"lib": ["es2019", "es2017", "dom"], | ||
"lib": ["es2021", "dom"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Required for |
||
"noEmit": true, | ||
"noImplicitAny": true, | ||
"noUnusedLocals": true, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with the
$1
syntax as my assumption is that it's less overhead compared to creating a replacement function.