-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
supports vite css.extract=false. credits: @ruofee
- Loading branch information
Showing
3 changed files
with
54 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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,44 @@ | ||
// supports vite css.extract=false. | ||
// credits: @ruofee | ||
// https://github.com/vitejs/vite/issues/4345#issuecomment-1073734133 | ||
// https://github.com/ruofee/vue-dynamic-form-component/blob/vite/build/ViteSingleCssPlugin.js | ||
|
||
let packageNames = [] | ||
let viteConfig | ||
let IIFEcss | ||
|
||
let extMap = { | ||
cjs: '.js', | ||
es: '.mjs', | ||
umd: '.umd.js', | ||
} | ||
|
||
// 将 css 打包到 js 文件中 | ||
export default function () { | ||
return { | ||
apply: 'build', | ||
enforce: 'post', | ||
name: 'pack-css', | ||
configResolved (config) { | ||
viteConfig = config | ||
packageNames = viteConfig.build.lib.formats.map(format => { | ||
return viteConfig.build.lib.fileName + extMap[format] | ||
}) | ||
}, | ||
generateBundle (_, bundle) { | ||
const cssFileName = 'style.css' | ||
const { [cssFileName]: cssBundle } = bundle | ||
if (cssBundle) { | ||
IIFEcss = `(function() {try {var elementStyle = document.createElement('style');elementStyle.textContent = ${JSON.stringify( | ||
cssBundle.source | ||
)};document.head.appendChild(elementStyle);} catch(error) {console.error(error, 'unable to concat style inside the bundled file')}})()` | ||
delete bundle[cssFileName] | ||
} | ||
packageNames.forEach(packageName => { | ||
if (bundle[packageName]) { | ||
bundle[packageName].code += ';;' + IIFEcss + ';;' | ||
} | ||
}) | ||
} | ||
} | ||
} |
This file contains 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