Skip to content

Commit 7322589

Browse files
committed
feat!: auto import css + split editor css from main component
This avoids including Monaco Editor css when using CodeMirror, and vice-versa. In addition, CSS will be auto imported when the relevant JS entries are imported. This is breaking because it makes the dist files require a build system that can handle CSS imports from JS.
1 parent fbfaa44 commit 7322589

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Basic editing experience with no intellisense. Lighter weight, fewer network req
1414
<script setup>
1515
import { Repl } from '@vue/repl'
1616
import CodeMirror from '@vue/repl/codemirror-editor'
17-
import '@vue/repl/style.css'
17+
// import '@vue/repl/style.css'
18+
// ^ no longer needed after 3.0
1819
</script>
1920
2021
<template>
@@ -30,7 +31,8 @@ With Volar support, autocomplete, type inference, and semantic highlighting. Hea
3031
<script setup>
3132
import { Repl } from '@vue/repl'
3233
import Monaco from '@vue/repl/monaco-editor'
33-
import '@vue/repl/style.css'
34+
// import '@vue/repl/style.css'
35+
// ^ no longer needed after 3.0
3436
</script>
3537
3638
<template>

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"import": "./dist/codemirror-editor.js",
2727
"require": null
2828
},
29-
"./style.css": "./dist/style.css",
30-
"./dist/style.css": "./dist/style.css"
29+
"./style.css": "./dist/vue-repl.css",
30+
"./dist/style.css": "./dist/vue-repl.css"
3131
},
3232
"typesVersions": {
3333
"*": {

vite.config.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Plugin, mergeConfig } from 'vite'
22
import dts from 'vite-plugin-dts'
33
import base from './vite.preview.config'
4+
import fs from 'node:fs'
5+
import path from 'node:path'
46

57
const genStub: Plugin = {
68
name: 'gen-stub',
@@ -14,12 +16,37 @@ const genStub: Plugin = {
1416
},
1517
}
1618

19+
/**
20+
* Patch generated entries and import their corresponding CSS files.
21+
* Also normalize MonacoEditor.css
22+
*/
23+
const patchCssFiles: Plugin = {
24+
name: 'patch-css',
25+
apply: 'build',
26+
writeBundle() {
27+
// 1. MonacoEditor.css -> monaco-editor.css
28+
const outDir = path.resolve('dist')
29+
fs.renameSync(
30+
path.resolve(outDir, 'MonacoEditor.css'),
31+
path.resolve(outDir, 'monaco-editor.css')
32+
)
33+
34+
// 2. inject css imports to the files
35+
;['vue-repl', 'monaco-editor', 'codemirror-editor'].forEach((file) => {
36+
const filePath = path.resolve(outDir, file + '.js')
37+
const content = fs.readFileSync(filePath, 'utf-8')
38+
fs.writeFileSync(filePath, `import './${file}.css'\n${content}`)
39+
})
40+
},
41+
}
42+
1743
export default mergeConfig(base, {
1844
plugins: [
1945
dts({
2046
rollupTypes: true,
2147
}),
2248
genStub,
49+
patchCssFiles,
2350
],
2451
optimizeDeps: {
2552
// avoid late discovered deps
@@ -43,6 +70,7 @@ export default mergeConfig(base, {
4370
formats: ['es'],
4471
fileName: () => '[name].js',
4572
},
73+
cssCodeSplit: true,
4674
rollupOptions: {
4775
output: {
4876
chunkFileNames: 'chunks/[name]-[hash].js',

0 commit comments

Comments
 (0)