Skip to content

Commit a31c182

Browse files
authored
fix(css): inject CSS correctly when cssCodesplit: true and IIFE/UMD (#181)
1 parent 7f0c70a commit a31c182

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import {
107107
} from './asset'
108108
import type { ESBuildOptions } from './esbuild'
109109
import { getChunkOriginalFileName } from './manifest'
110+
import { IIFE_BEGIN_RE, UMD_BEGIN_RE } from './oxc'
110111

111112
const decoder = new TextDecoder()
112113
// const debug = createDebugger('vite:css')
@@ -915,22 +916,23 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
915916
`var ${style} = document.createElement('style');` +
916917
`${style}.textContent = ${cssString};` +
917918
`document.head.appendChild(${style});`
918-
let injectionPoint
919-
const wrapIdx = code.indexOf('System.register')
920-
const singleQuoteUseStrict = `'use strict';`
921-
const doubleQuoteUseStrict = `"use strict";`
922-
if (wrapIdx >= 0) {
923-
const executeFnStart = code.indexOf('execute:', wrapIdx)
924-
injectionPoint = code.indexOf('{', executeFnStart) + 1
925-
} else if (code.includes(singleQuoteUseStrict)) {
926-
injectionPoint =
927-
code.indexOf(singleQuoteUseStrict) + singleQuoteUseStrict.length
928-
} else if (code.includes(doubleQuoteUseStrict)) {
929-
injectionPoint =
930-
code.indexOf(doubleQuoteUseStrict) + doubleQuoteUseStrict.length
931-
} else {
932-
throw new Error('Injection point for inlined CSS not found')
919+
920+
if (opts.format === 'app')
921+
this.error('format: "app" is not supported')
922+
// TODO: system js support
923+
// const wrapIdx = code.indexOf('System.register')
924+
// if (wrapIdx >= 0) {
925+
// const executeFnStart = code.indexOf('execute:', wrapIdx)
926+
// injectionPoint = code.indexOf('{', executeFnStart) + 1
927+
// }
928+
const m = (
929+
opts.format === 'iife' ? IIFE_BEGIN_RE : UMD_BEGIN_RE
930+
).exec(code)
931+
if (!m) {
932+
this.error('Injection point for inlined CSS not found')
933+
return
933934
}
935+
const injectionPoint = m.index + m[0].length
934936
s ||= new MagicString(code)
935937
s.appendRight(injectionPoint, injectCode)
936938
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ import type { ESBuildOptions } from './esbuild'
3030
import { loadTsconfigJsonForFile } from './esbuild'
3131

3232
// IIFE content looks like `var MyLib = (function() {`.
33-
const IIFE_BEGIN_RE =
33+
export const IIFE_BEGIN_RE =
3434
/(?:(?:const|var)\s+\S+\s*=\s*|^|\n)\(?function\([^()]*\)\s*\{(?:\s*"use strict";)?/
3535
// UMD content looks like `(this, function(exports) {`.
36-
const UMD_BEGIN_RE = /\(this,\s*function\([^()]*\)\s*\{(?:\s*"use strict";)?/
36+
export const UMD_BEGIN_RE =
37+
/\(this,\s*function\([^()]*\)\s*\{(?:\s*"use strict";)?/
3738

3839
const jsxExtensionsRE = /\.(?:j|t)sx\b/
3940
const validExtensionRE = /\.\w+$/

0 commit comments

Comments
 (0)