Skip to content

Commit e1de8a8

Browse files
committed
feat: support ?inline css query to avoid css insertion
1 parent 3a3af6e commit e1de8a8

File tree

5 files changed

+55
-30
lines changed

5 files changed

+55
-30
lines changed

packages/playground/css/__tests__/css.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,8 @@ test('Url separation', async () => {
266266
)
267267
}
268268
})
269+
270+
test('inlined', async () => {
271+
// should not insert css
272+
expect(await getColor('.inlined')).toBe('black')
273+
})

packages/playground/css/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ <h1>CSS</h1>
8686
<p class="url-separated">
8787
Url separation preservation: should have valid background-image
8888
</p>
89+
90+
<p class="inlined">Inlined import - this should NOT be red.</p>
91+
<pre class="inlined-code"></pre>
8992
</div>
9093

9194
<script type="module" src="./main.js"></script>

packages/playground/css/inlined.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.inlined {
2+
color: green;
3+
}

packages/playground/css/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ import('./async')
4747
if (import.meta.env.DEV) {
4848
import('./async-treeshaken')
4949
}
50+
51+
// inlined
52+
import inlined from './inlined.css?inline'
53+
text('.inlined-code', inlined)

packages/vite/src/node/plugins/css.ts

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export const cssLangRE = new RegExp(cssLangs)
8686
const cssModuleRE = new RegExp(`\\.module${cssLangs}`)
8787
const directRequestRE = /(\?|&)direct\b/
8888
const commonjsProxyRE = /\?commonjs-proxy/
89+
const inlineRE = /(\?|&)inline\b/
8990

9091
const enum PreprocessLang {
9192
less = 'less',
@@ -190,36 +191,37 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
190191
if (server) {
191192
// server only logic for handling CSS @import dependency hmr
192193
const { moduleGraph } = server
193-
const thisModule = moduleGraph.getModuleById(id)!
194-
195-
// CSS modules cannot self-accept since it exports values
196-
const isSelfAccepting = !modules
197-
if (deps) {
198-
// record deps in the module graph so edits to @import css can trigger
199-
// main import to hot update
200-
const depModules = new Set<string | ModuleNode>()
201-
for (const file of deps) {
202-
depModules.add(
203-
cssLangRE.test(file)
204-
? moduleGraph.createFileOnlyEntry(file)
205-
: await moduleGraph.ensureEntryFromUrl(
206-
await fileToUrl(file, config, this)
207-
)
194+
const thisModule = moduleGraph.getModuleById(id)
195+
if (thisModule) {
196+
// CSS modules cannot self-accept since it exports values
197+
const isSelfAccepting = !modules
198+
if (deps) {
199+
// record deps in the module graph so edits to @import css can trigger
200+
// main import to hot update
201+
const depModules = new Set<string | ModuleNode>()
202+
for (const file of deps) {
203+
depModules.add(
204+
cssLangRE.test(file)
205+
? moduleGraph.createFileOnlyEntry(file)
206+
: await moduleGraph.ensureEntryFromUrl(
207+
await fileToUrl(file, config, this)
208+
)
209+
)
210+
}
211+
moduleGraph.updateModuleInfo(
212+
thisModule,
213+
depModules,
214+
// The root CSS proxy module is self-accepting and should not
215+
// have an explicit accept list
216+
new Set(),
217+
isSelfAccepting
208218
)
219+
for (const file of deps) {
220+
this.addWatchFile(file)
221+
}
222+
} else {
223+
thisModule.isSelfAccepting = isSelfAccepting
209224
}
210-
moduleGraph.updateModuleInfo(
211-
thisModule,
212-
depModules,
213-
// The root CSS proxy module is self-accepting and should not
214-
// have an explicit accept list
215-
new Set(),
216-
isSelfAccepting
217-
)
218-
for (const file of deps) {
219-
this.addWatchFile(file)
220-
}
221-
} else {
222-
thisModule.isSelfAccepting = isSelfAccepting
223225
}
224226
}
225227

@@ -255,11 +257,12 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
255257
hasEmitted = false
256258
},
257259

258-
transform(css, id, ssr) {
260+
async transform(css, id, ssr) {
259261
if (!cssLangRE.test(id) || commonjsProxyRE.test(id)) {
260262
return
261263
}
262264

265+
const inlined = inlineRE.test(id)
263266
const modules = cssModulesCache.get(config)!.get(id)
264267
const modulesCode =
265268
modules && dataToEsm(modules, { namedExports: true, preferConst: true })
@@ -272,6 +275,9 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
272275
if (ssr) {
273276
return modulesCode || `export default ${JSON.stringify(css)}`
274277
}
278+
if (inlined) {
279+
return `export default ${JSON.stringify(css)}`
280+
}
275281
return [
276282
`import { updateStyle, removeStyle } from ${JSON.stringify(
277283
path.posix.join(config.base, CLIENT_PUBLIC_PATH)
@@ -289,7 +295,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
289295
// build CSS handling ----------------------------------------------------
290296

291297
// record css
292-
styles.set(id, css)
298+
if (!inlined) {
299+
styles.set(id, css)
300+
} else {
301+
css = await minifyCSS(css, config)
302+
}
293303

294304
return {
295305
code: modulesCode || `export default ${JSON.stringify(css)}`,

0 commit comments

Comments
 (0)