-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Rewrite urls in CSS files when using Vite #14877
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
209cfa2
Add helper for writing binary files in integration tests
thecrypticace b1d41e7
Add failing test for Vite URL rewriting
thecrypticace fc52f68
Rewrite urls in CSS files in Vite
thecrypticace 9d25aad
Update changelog
thecrypticace 78dca9a
Don’t special case `@tailwind utilities` in toCss
thecrypticace 5db1c38
Update packages/tailwindcss/src/index.ts
thecrypticace 0da9bda
Update CHANGELOG.md
thecrypticace 8b8347c
Merge branch 'next' into fix/v4-url-rewriting
thecrypticace ce0e72b
Perform removal in existing walk
thecrypticace b1ba1ad
Merge branch 'next' into fix/v4-url-rewriting
thecrypticace File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { describe, expect } from 'vitest' | ||
| import { binary, css, html, svg, test, ts, txt } from '../utils' | ||
|
|
||
| const SIMPLE_IMAGE = `iVBORw0KGgoAAAANSUhEUgAAADAAAAAlAQAAAAAsYlcCAAAACklEQVR4AWMYBQABAwABRUEDtQAAAABJRU5ErkJggg==` | ||
|
|
||
| for (let transformer of ['postcss', 'lightningcss']) { | ||
| describe(transformer, () => { | ||
| test( | ||
| 'can rewrite urls in production builds', | ||
| { | ||
| fs: { | ||
| 'package.json': txt` | ||
| { | ||
| "type": "module", | ||
| "dependencies": { | ||
| "tailwindcss": "workspace:^" | ||
| }, | ||
| "devDependencies": { | ||
| ${transformer === 'lightningcss' ? `"lightningcss": "^1.26.0",` : ''} | ||
| "@tailwindcss/vite": "workspace:^", | ||
| "vite": "^5.3.5" | ||
| } | ||
| } | ||
| `, | ||
| 'vite.config.ts': ts` | ||
| import tailwindcss from '@tailwindcss/vite' | ||
| import { defineConfig } from 'vite' | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [tailwindcss()], | ||
| build: { | ||
| assetsInlineLimit: 256, | ||
| cssMinify: false, | ||
| }, | ||
| css: ${transformer === 'postcss' ? '{}' : "{ transformer: 'lightningcss' }"}, | ||
| }) | ||
| `, | ||
| 'index.html': html` | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <link rel="stylesheet" href="./src/app.css" /> | ||
| </head> | ||
| <body> | ||
| <div id="app"></div> | ||
| <script type="module" src="./src/main.ts"></script> | ||
| </body> | ||
| </html> | ||
| `, | ||
| 'src/main.ts': ts``, | ||
| 'src/app.css': css` | ||
| @import './dir-1/bar.css'; | ||
| @import './dir-1/dir-2/baz.css'; | ||
| @import './dir-1/dir-2/vector.css'; | ||
| `, | ||
| 'src/dir-1/bar.css': css` | ||
| .bar { | ||
| background-image: url('../../resources/image.png'); | ||
| } | ||
| `, | ||
| 'src/dir-1/dir-2/baz.css': css` | ||
| .baz { | ||
| background-image: url('../../../resources/image.png'); | ||
| } | ||
| `, | ||
| 'src/dir-1/dir-2/vector.css': css` | ||
| .baz { | ||
| background-image: url('../../../resources/vector.svg'); | ||
| } | ||
| `, | ||
| 'resources/image.png': binary(SIMPLE_IMAGE), | ||
| 'resources/vector.svg': svg` | ||
| <svg width="400" height="400" xmlns="http://www.w3.org/2000/svg"> | ||
| <rect width="100%" height="100%" fill="red" /> | ||
| <circle cx="200" cy="100" r="80" fill="green" /> | ||
| <rect width="100%" height="100%" fill="red" /> | ||
| <circle cx="200" cy="100" r="80" fill="green" /> | ||
| </svg> | ||
| `, | ||
| }, | ||
| }, | ||
| async ({ fs, exec }) => { | ||
| await exec('pnpm vite build') | ||
|
|
||
| let files = await fs.glob('dist/**/*.css') | ||
| expect(files).toHaveLength(1) | ||
|
|
||
| await fs.expectFileToContain(files[0][0], [SIMPLE_IMAGE]) | ||
|
|
||
| let images = await fs.glob('dist/**/*.svg') | ||
| expect(images).toHaveLength(1) | ||
|
|
||
| await fs.expectFileToContain(files[0][0], [/\/assets\/vector-.*?\.svg/]) | ||
| }, | ||
| ) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { expect, test } from 'vitest' | ||
| import { rewriteUrls } from './urls' | ||
|
|
||
| const css = String.raw | ||
|
|
||
| test('URLs can be rewritten', async () => { | ||
| let root = '/root' | ||
|
|
||
| let result = await rewriteUrls({ | ||
| root, | ||
| base: '/root/foo/bar', | ||
| // prettier-ignore | ||
| css: css` | ||
| .foo { | ||
| /* Relative URLs: replaced */ | ||
| background: url(./image.jpg); | ||
| background: url(../image.jpg); | ||
| background: url('./image.jpg'); | ||
| background: url("./image.jpg"); | ||
|
|
||
| /* External URL: ignored */ | ||
| background: url(http://example.com/image.jpg); | ||
| background: url('http://example.com/image.jpg'); | ||
| background: url("http://example.com/image.jpg"); | ||
|
|
||
| /* Data URI: ignored */ | ||
| /* background: url(data:image/png;base64,abc==); */ | ||
| background: url('data:image/png;base64,abc=='); | ||
| background: url("data:image/png;base64,abc=="); | ||
|
|
||
| /* Function calls: ignored */ | ||
| background: url(var(--foo)); | ||
| background: url(var(--foo, './image.jpg')); | ||
| background: url(var(--foo, "./image.jpg")); | ||
|
|
||
| /* Fragments: ignored */ | ||
| background: url(#dont-touch-this); | ||
|
|
||
| /* Image Sets - Raw URL: replaced */ | ||
| background: image-set( | ||
| image1.jpg 1x, | ||
| image2.jpg 2x | ||
| ); | ||
| background: image-set( | ||
| 'image1.jpg' 1x, | ||
| 'image2.jpg' 2x | ||
| ); | ||
| background: image-set( | ||
| "image1.jpg" 1x, | ||
| "image2.jpg" 2x | ||
| ); | ||
|
|
||
| /* Image Sets - Relative URLs: replaced */ | ||
| background: image-set( | ||
| url('image1.jpg') 1x, | ||
| url('image2.jpg') 2x | ||
| ); | ||
| background: image-set( | ||
| url("image1.jpg") 1x, | ||
| url("image2.jpg") 2x | ||
| ); | ||
| background: image-set( | ||
| url('image1.avif') type('image/avif'), | ||
| url('image2.jpg') type('image/jpeg') | ||
| ); | ||
| background: image-set( | ||
| url("image1.avif") type('image/avif'), | ||
| url("image2.jpg") type('image/jpeg') | ||
| ); | ||
|
|
||
| /* Image Sets - Function calls: ignored */ | ||
| background: image-set( | ||
| linear-gradient(blue, white) 1x, | ||
| linear-gradient(blue, green) 2x | ||
| ); | ||
|
|
||
| /* Image Sets - Mixed: replaced */ | ||
| background: image-set( | ||
| linear-gradient(blue, white) 1x, | ||
| url("image2.jpg") 2x | ||
| ); | ||
| } | ||
|
|
||
| /* Fonts - Multiple URLs: replaced */ | ||
| @font-face { | ||
| font-family: "Newman"; | ||
| src: | ||
| local("Newman"), | ||
| url("newman-COLRv1.otf") format("opentype") tech(color-COLRv1), | ||
| url("newman-outline.otf") format("opentype"), | ||
| url("newman-outline.woff") format("woff"); | ||
| } | ||
| `, | ||
| }) | ||
|
|
||
| expect(result).toMatchInlineSnapshot(` | ||
| ".foo { | ||
| background: url(./foo/bar/image.jpg); | ||
| background: url(./foo/image.jpg); | ||
| background: url('./foo/bar/image.jpg'); | ||
| background: url("./foo/bar/image.jpg"); | ||
| background: url(http://example.com/image.jpg); | ||
| background: url('http://example.com/image.jpg'); | ||
| background: url("http://example.com/image.jpg"); | ||
| background: url('data:image/png;base64,abc=='); | ||
| background: url("data:image/png;base64,abc=="); | ||
| background: url(var(--foo)); | ||
| background: url(var(--foo, './image.jpg')); | ||
| background: url(var(--foo, "./image.jpg")); | ||
| background: url(#dont-touch-this); | ||
| background: image-set(url(./foo/bar/image1.jpg) 1x, url(./foo/bar/image2.jpg) 2x); | ||
| background: image-set(url('./foo/bar/image1.jpg') 1x, url('./foo/bar/image2.jpg') 2x); | ||
| background: image-set(url("./foo/bar/image1.jpg") 1x, url("./foo/bar/image2.jpg") 2x); | ||
| background: image-set(url('./foo/bar/image1.jpg') 1x, url('./foo/bar/image2.jpg') 2x); | ||
| background: image-set(url("./foo/bar/image1.jpg") 1x, url("./foo/bar/image2.jpg") 2x); | ||
| background: image-set(url('./foo/bar/image1.avif') type('image/avif'), url('./foo/bar/image2.jpg') type('image/jpeg')); | ||
| background: image-set(url("./foo/bar/image1.avif") type('image/avif'), url("./foo/bar/image2.jpg") type('image/jpeg')); | ||
| background: image-set(linear-gradient(blue, white) 1x, linear-gradient(blue, green) 2x); | ||
| background: image-set(linear-gradient(blue, white) 1x, url("./foo/bar/image2.jpg") 2x); | ||
| } | ||
| @font-face { | ||
| font-family: "Newman"; | ||
| src: local("Newman"), url("./foo/bar/newman-COLRv1.otf") format("opentype") tech(color-COLRv1), url("./foo/bar/newman-outline.otf") format("opentype"), url("./foo/bar/newman-outline.woff") format("woff"); | ||
| } | ||
| " | ||
| `) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.