Skip to content

Commit 46102e8

Browse files
committed
Allow users to disable url rewriting in the PostCSS plugin
1 parent 2941a7b commit 46102e8

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

integrations/postcss/url-rewriting.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,78 @@ test(
7272
`)
7373
},
7474
)
75+
76+
test(
77+
'url rewriting can be disabled',
78+
{
79+
fs: {
80+
'package.json': json`
81+
{
82+
"dependencies": {
83+
"postcss": "^8",
84+
"postcss-cli": "^10",
85+
"tailwindcss": "workspace:^",
86+
"@tailwindcss/postcss": "workspace:^"
87+
}
88+
}
89+
`,
90+
'postcss.config.js': js`
91+
module.exports = {
92+
plugins: {
93+
'@tailwindcss/postcss': {
94+
rewriteUrls: false,
95+
},
96+
},
97+
}
98+
`,
99+
'src/index.css': css`
100+
@reference 'tailwindcss';
101+
@import './dir-1/bar.css';
102+
@import './dir-1/dir-2/baz.css';
103+
@import './dir-1/dir-2/vector.css';
104+
`,
105+
'src/dir-1/bar.css': css`
106+
.test1 {
107+
background-image: url('../../resources/image.png');
108+
}
109+
`,
110+
'src/dir-1/dir-2/baz.css': css`
111+
.test2 {
112+
background-image: url('../../../resources/image.png');
113+
}
114+
`,
115+
'src/dir-1/dir-2/vector.css': css`
116+
@import './dir-3/vector.css';
117+
.test3 {
118+
background-image: url('../../../resources/vector.svg');
119+
}
120+
`,
121+
'src/dir-1/dir-2/dir-3/vector.css': css`
122+
.test4 {
123+
background-image: url('./vector-2.svg');
124+
}
125+
`,
126+
},
127+
},
128+
async ({ fs, exec, expect }) => {
129+
await exec('pnpm postcss src/index.css --output dist/out.css')
130+
131+
expect(await fs.dumpFiles('dist/out.css')).toMatchInlineSnapshot(`
132+
"
133+
--- dist/out.css ---
134+
.test1 {
135+
background-image: url('../../resources/image.png');
136+
}
137+
.test2 {
138+
background-image: url('../../../resources/image.png');
139+
}
140+
.test4 {
141+
background-image: url('./vector-2.svg');
142+
}
143+
.test3 {
144+
background-image: url('../../../resources/vector.svg');
145+
}
146+
"
147+
`)
148+
},
149+
)

packages/@tailwindcss-postcss/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ export type PluginOptions = {
5353

5454
// Optimize and minify the output CSS.
5555
optimize?: boolean | { minify?: boolean }
56+
57+
// Whether or not we should rewrite any encountered urls
58+
// default: true
59+
rewriteUrls?: boolean
5660
}
5761

5862
function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
5963
let base = opts.base ?? process.cwd()
6064
let optimize = opts.optimize ?? process.env.NODE_ENV === 'production'
65+
let shouldRewriteUrls = opts.rewriteUrls ?? true
6166

6267
return {
6368
postcssPlugin: '@tailwindcss/postcss',
@@ -123,7 +128,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
123128
let compiler = await compileAst(ast, {
124129
from: result.opts.from,
125130
base: inputBasePath,
126-
shouldRewriteUrls: true,
131+
shouldRewriteUrls,
127132
onDependency: (path) => context.fullRebuildPaths.push(path),
128133
// In CSS Module files, we have to disable the `@property` polyfill since these will
129134
// emit global `*` rules which are considered to be non-pure and will cause builds

0 commit comments

Comments
 (0)