From 32c0e75f91ef8f741e125423bfda99f0455ed77d Mon Sep 17 00:00:00 2001 From: Jono M Date: Thu, 29 Jun 2023 13:42:20 +1200 Subject: [PATCH 1/3] fix(build): style insert order for UMD builds --- packages/vite/src/node/plugins/css.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index b2465d4a1d45ba..a270b5e8db3f2f 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -683,10 +683,10 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { `${style}.textContent = ${cssString};` + `document.head.appendChild(${style});` const wrapIdx = code.indexOf('System.register') - const executeFnStart = - code.indexOf('{', code.indexOf('execute:', wrapIdx)) + 1 + const executeFnStart = wrapIdx >= 0 ? code.indexOf('execute:', wrapIdx) : 0 + const injectionPoint = code.indexOf('{', executeFnStart) + 1 const s = new MagicString(code) - s.appendRight(executeFnStart, injectCode) + s.appendRight(injectionPoint, injectCode) if (config.build.sourcemap) { // resolve public URL from CSS paths, we need to use absolute paths return { From 982c61a20cf2f9db7f9b75cad61db9152c80785b Mon Sep 17 00:00:00 2001 From: binary-koan Date: Thu, 29 Jun 2023 14:01:04 +1200 Subject: [PATCH 2/3] chore: format --- packages/vite/src/node/plugins/css.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index a270b5e8db3f2f..cf21b1a974e42a 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -683,7 +683,8 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { `${style}.textContent = ${cssString};` + `document.head.appendChild(${style});` const wrapIdx = code.indexOf('System.register') - const executeFnStart = wrapIdx >= 0 ? code.indexOf('execute:', wrapIdx) : 0 + const executeFnStart = + wrapIdx >= 0 ? code.indexOf('execute:', wrapIdx) : 0 const injectionPoint = code.indexOf('{', executeFnStart) + 1 const s = new MagicString(code) s.appendRight(injectionPoint, injectCode) From 91eadffb50965ed49505a6e56711030452977529 Mon Sep 17 00:00:00 2001 From: Jono Mingard Date: Fri, 14 Jul 2023 09:57:47 +0900 Subject: [PATCH 3/3] fix(build): inject css below use strict directive --- packages/vite/src/node/plugins/css.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index cf21b1a974e42a..9c530f9cfbf9b2 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -682,10 +682,15 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { `var ${style} = document.createElement('style');` + `${style}.textContent = ${cssString};` + `document.head.appendChild(${style});` + let injectionPoint const wrapIdx = code.indexOf('System.register') - const executeFnStart = - wrapIdx >= 0 ? code.indexOf('execute:', wrapIdx) : 0 - const injectionPoint = code.indexOf('{', executeFnStart) + 1 + if (wrapIdx >= 0) { + const executeFnStart = code.indexOf('execute:', wrapIdx) + injectionPoint = code.indexOf('{', executeFnStart) + 1 + } else { + const insertMark = "'use strict';" + injectionPoint = code.indexOf(insertMark) + insertMark.length + } const s = new MagicString(code) s.appendRight(injectionPoint, injectCode) if (config.build.sourcemap) {