Skip to content

Commit 6ac9f66

Browse files
authored
webpack/next: Fix webpack template strings by using a modified regex from the webpack source (#1333)
1 parent bffdabd commit 6ac9f66

File tree

8 files changed

+26
-1
lines changed

8 files changed

+26
-1
lines changed

.changeset/eleven-seals-invent.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@vanilla-extract/webpack-plugin': patch
3+
'@vanilla-extract/next-plugin': patch
4+
---
5+
6+
Use a more accurate regex for detecting [webpack template strings] in paths
7+
8+
We now use a modified version of the regex from the webpack source code to detect template strings in paths.
9+
As long as the path isn't already escaped, we should detect it.
10+
11+
[webpack template strings]: https://webpack.js.org/configuration/output/#template-strings
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { style } from '@vanilla-extract/css';
2+
3+
export const withHyphen = style({ color: 'indigo' });

fixtures/template-string-paths/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { singleSquareBracketsId } from './[id]/index.css';
33
import { doubleSquareBracketId } from './[[id]]/index.css';
44
import { catchAllSegment } from './[...slug]/index.css';
55
import { optionalCatchAllSegment } from './[[...slug]]/index.css';
6+
import { withHyphen } from './[blog-id]/index.css';
67

78
// Fixture for testing escaping of webpack template strings and Next.js dyanmic routes
89
// https://webpack.js.org/configuration/output/#template-strings
@@ -14,6 +15,7 @@ function render() {
1415
<div class="${doubleSquareBracketId}">[[id]] path</div>
1516
<div class="${catchAllSegment}">[...slug] path</div>
1617
<div class="${optionalCatchAllSegment}">[[...slug]] path</div>
18+
<div class="${withHyphen}">[blog-id] path</div>
1719
`;
1820
}
1921

packages/webpack-plugin/src/compiler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ function getRootCompilation(loader: LoaderContext) {
5555
return compilation;
5656
}
5757

58-
const templateStringRegexp = /\[([^\[\]\.]+)\]/g;
58+
// Modified version of webpack's regex for detecting template strings.
59+
// We only want to match un-escaped strings so we can escape them ourselves.
60+
// https://github.com/webpack/webpack/blob/87660921808566ef3b8796f8df61bd79fc026108/lib/TemplatedPathPlugin.js#L19
61+
const templateStringRegexp = /\[([\w:]+)\]/g;
5962

6063
export const escapeWebpackTemplateString = (s: string) =>
6164
s.replaceAll(templateStringRegexp, '[\\$1\\]');
2.18 KB
Loading
2.24 KB
Loading

tests/e2e/template-string-paths.playwright.ts-snapshots/template-string-paths-mini-css-extract--development.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
.\[\[\.\.\.slug\]\]_optionalCatchAllSegment__1kvknas0 {
1414
color: orchid;
1515
}
16+
.\[blog-id\]_withHyphen__1e83mlh0 {
17+
color: indigo;
18+
}

tests/e2e/template-string-paths.playwright.ts-snapshots/template-string-paths-mini-css-extract--production.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
._1kvknas0 {
1414
color: orchid;
1515
}
16+
._1e83mlh0 {
17+
color: indigo;
18+
}

0 commit comments

Comments
 (0)