Skip to content

Commit

Permalink
feat!: auto import css + split editor css from main component
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yyx990803 committed Nov 30, 2023
1 parent fbfaa44 commit 7322589
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Basic editing experience with no intellisense. Lighter weight, fewer network req
<script setup>
import { Repl } from '@vue/repl'
import CodeMirror from '@vue/repl/codemirror-editor'
import '@vue/repl/style.css'
// import '@vue/repl/style.css'
// ^ no longer needed after 3.0
</script>
<template>
Expand All @@ -30,7 +31,8 @@ With Volar support, autocomplete, type inference, and semantic highlighting. Hea
<script setup>
import { Repl } from '@vue/repl'
import Monaco from '@vue/repl/monaco-editor'
import '@vue/repl/style.css'
// import '@vue/repl/style.css'
// ^ no longer needed after 3.0
</script>
<template>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"import": "./dist/codemirror-editor.js",
"require": null
},
"./style.css": "./dist/style.css",
"./dist/style.css": "./dist/style.css"
"./style.css": "./dist/vue-repl.css",
"./dist/style.css": "./dist/vue-repl.css"
},
"typesVersions": {
"*": {
Expand Down
28 changes: 28 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Plugin, mergeConfig } from 'vite'
import dts from 'vite-plugin-dts'
import base from './vite.preview.config'
import fs from 'node:fs'
import path from 'node:path'

const genStub: Plugin = {
name: 'gen-stub',
Expand All @@ -14,12 +16,37 @@ const genStub: Plugin = {
},
}

/**
* Patch generated entries and import their corresponding CSS files.
* Also normalize MonacoEditor.css
*/
const patchCssFiles: Plugin = {
name: 'patch-css',
apply: 'build',
writeBundle() {
// 1. MonacoEditor.css -> monaco-editor.css
const outDir = path.resolve('dist')
fs.renameSync(
path.resolve(outDir, 'MonacoEditor.css'),
path.resolve(outDir, 'monaco-editor.css')
)

// 2. inject css imports to the files
;['vue-repl', 'monaco-editor', 'codemirror-editor'].forEach((file) => {
const filePath = path.resolve(outDir, file + '.js')
const content = fs.readFileSync(filePath, 'utf-8')
fs.writeFileSync(filePath, `import './${file}.css'\n${content}`)
})
},
}

export default mergeConfig(base, {
plugins: [
dts({
rollupTypes: true,
}),
genStub,
patchCssFiles,
],
optimizeDeps: {
// avoid late discovered deps
Expand All @@ -43,6 +70,7 @@ export default mergeConfig(base, {
formats: ['es'],
fileName: () => '[name].js',
},
cssCodeSplit: true,
rollupOptions: {
output: {
chunkFileNames: 'chunks/[name]-[hash].js',
Expand Down

0 comments on commit 7322589

Please sign in to comment.