build: rollup -> vite

This commit is contained in:
Rongjian Zhang 2022-02-04 23:43:01 +08:00
parent dced981d37
commit b90ac7ec1f
7 changed files with 671 additions and 775 deletions

View File

@ -3,10 +3,10 @@
"private": true,
"scripts": {
"clean": "rm -rf packages/*/{lib,dist,tsconfig.tsbuildinfo}",
"build": "tsc --build && node scripts/process.mjs && rollup -c",
"build": "tsc --build && node scripts/process.mjs && node scripts/build.mjs",
"build:ci": "UMD=1 npm run build",
"dev": "run-p dev:bundle dev:tsc dev:process",
"dev:bundle": "rollup -cw",
"dev:bundle": "node scripts/build.mjs --watch",
"dev:tsc": "tsc --build --watch",
"dev:process": "node scripts/process.mjs --watch",
"test": "vitest",
@ -24,11 +24,7 @@
"@babel/preset-env": "^7.16.4",
"@icon-park/svg": "^1.3.5",
"@primer/css": "^15.2.0",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.6",
"@rollup/plugin-replace": "^2.4.2",
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.35",
"@testing-library/jest-dom": "^5.15.0",
"@testing-library/svelte": "^3.0.3",
"chokidar": "^3.5.2",
@ -38,22 +34,16 @@
"lerna": "^4.0.0",
"lodash": "^4.17.21",
"mustache": "^4.2.0",
"node-sass": "^6.0.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.4.1",
"prettier-plugin-svelte": "^2.5.0",
"rollup": "^2.60.0",
"rollup-plugin-livereload": "^2.0.5",
"rollup-plugin-postcss": "^3.1.8",
"rollup-plugin-svelte": "^7.1.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-visualizer": "^5.5.2",
"rollup-plugin-vue": "^5.1.9",
"sass": "^1.49.0",
"svelte": "^3.44.2",
"svelte-preprocess": "^4.9.8",
"svgo": "^2.8.0",
"typescript": "^4.5.2",
"vite": "^2.7.13",
"vite-plugin-vue2": "^1.9.3",
"vitest": "^0.2.7",
"vue": "^2.6.14",
"vue-template-compiler": "^2.6.14"

View File

@ -1,9 +1,8 @@
@import '~codemirror-ssr/lib/codemirror.css';
@import '~codemirror-ssr/addon/lint/lint.css';
@import '~tippy.js/dist/tippy.css';
@import '~tippy.js/themes/light-border.css';
// @import '~tippy.js/animations/scale.css';
@import '~@primer/css/support/variables/color-system.scss';
@import 'codemirror-ssr/lib/codemirror.css';
@import 'codemirror-ssr/addon/lint/lint.css';
@import 'tippy.js/dist/tippy.css';
@import 'tippy.js/themes/light-border.css';
@import '@primer/css/support/variables/color-system.scss';
$border-color: $gray-200;
$primary: $blue;

View File

@ -1,4 +1,6 @@
/// <reference types="svelte" />
import './index.scss'
import Editor from './editor.svelte'
import Viewer from './viewer.svelte'

View File

@ -1,2 +1,4 @@
export { Editor, EditorProps } from './editor'
export { Viewer, ViewerProps } from './viewer'
export { Editor } from './editor'
export type { EditorProps } from './editor'
export { Viewer } from './viewer'
export type { ViewerProps } from './viewer'

File diff suppressed because it is too large Load Diff

View File

@ -1,183 +0,0 @@
// @ts-check
import fs from 'fs-extra'
import path from 'path'
import _ from 'lodash'
import svelte from 'rollup-plugin-svelte'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import babel from '@rollup/plugin-babel'
import vue from 'rollup-plugin-vue'
import { terser } from 'rollup-plugin-terser'
import postcss from 'rollup-plugin-postcss'
import replace from '@rollup/plugin-replace'
// import visualizer from 'rollup-plugin-visualizer';
const production = !process.env.ROLLUP_WATCH
const umd = process.env.UMD
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
const configs = packages
.map((key) => {
if (key === 'mp') {
return {
input: 'packages/mp/lib/viewer.js',
output: {
file: path.resolve(__dirname, 'packages/mp/dist/viewer.js'),
format: 'cjs',
},
plugins: [commonjs(), resolve(), json()],
watch: { clearScreen: false },
}
}
const pkg = fs.readJsonSync(`./packages/${key}/package.json`)
if (pkg.private) return []
const inputFile = path.resolve('packages', key, 'lib/index.js')
const umdName = key.startsWith('plugin-')
? _.camelCase(`bytemd-${key}`)
: 'bytemd'
/** @type {import('rollup').RollupOptions} */
const common = {
input: inputFile,
plugins: [
commonjs(),
svelte({
compilerOptions: {
dev: !production,
},
}),
vue(),
resolve({
browser: true,
dedupe: ['svelte'],
}),
json(),
replace({
preventAssignment: true, // fix warning
'process.env.NODE_ENV': JSON.stringify(
production ? 'production' : 'development'
),
}),
],
watch: {
clearScreen: false,
},
}
/** @type {import('rollup').RollupOptions} */
const config = {
...common,
output: [
{
format: 'es',
sourcemap: true,
file: path.resolve('packages', key, pkg.module),
},
{
format: 'cjs',
sourcemap: true,
file: path.resolve('packages', key, pkg.main),
exports: 'auto', // fix warning
},
],
external: [
/^codemirror-ssr/,
'hast-util-sanitize/lib/github.json',
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
],
}
/** @type {import('rollup').OutputOptions} */
const umdOutputOption = {
format: 'umd',
name: umdName,
sourcemap: true,
inlineDynamicImports: true,
}
/** @type {import('rollup').RollupOptions} */
const umdConfig = {
...common,
output: [
{
...umdOutputOption,
file: path.resolve('packages', key, 'dist/index.js'),
},
{
...umdOutputOption,
file: path.resolve('packages', key, 'dist/index.min.js'),
plugins: [terser()],
},
],
external: Object.keys(pkg.peerDependencies || {}),
}
/** @type {import('rollup').RollupOptions} */
const es5Config = {
...common,
input: path.resolve('packages', key, 'lib/index.js'),
output: [
{
...umdOutputOption,
file: path.resolve('packages', key, 'dist/index.es5.js'),
},
{
...umdOutputOption,
file: path.resolve('packages', key, 'dist/index.es5.min.js'),
plugins: [terser()],
},
],
plugins: [
...common.plugins,
babel({
babelHelpers: 'runtime',
extensions: ['.js', '.mjs', '.html', '.svelte'],
}),
],
}
// return [es5Config];
if (umd) {
return [config, umdConfig, es5Config]
} else {
return [config]
}
})
.flat()
/** @type {import('rollup').RollupOptions} */
const styleCommon = {
input: 'packages/bytemd/styles/index.scss',
output: {
file: 'style.js', // We don't need this file
},
}
/** @type {import('rollup').RollupOptions[]} */
const styleConfigs = [
{
...styleCommon,
plugins: [
postcss({
extract: path.resolve(__dirname, 'packages/bytemd/dist/index.css'),
}),
],
},
{
...styleCommon,
plugins: [
postcss({
extract: path.resolve(__dirname, 'packages/bytemd/dist/index.min.css'),
minimize: true,
}),
],
},
]
export default [...styleConfigs, ...configs]

69
scripts/build.mjs Normal file
View File

@ -0,0 +1,69 @@
// @ts-check
import fs from 'fs-extra'
import path from 'path'
import _ from 'lodash'
import { build } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { createVuePlugin } from 'vite-plugin-vue2'
import sveltePreprocess from 'svelte-preprocess'
const packages = fs.readdirSync('./packages')
const watch = process.argv[2] === '--watch'
const results = Promise.all(
packages.map((key) => {
const pkg = fs.readJsonSync(`./packages/${key}/package.json`)
if (pkg.private) return []
const umdName = key.startsWith('plugin-')
? _.camelCase(`bytemd-${key}`)
: 'bytemd'
/** @type {import('vite').UserConfig} */
return build({
root: path.resolve('./packages', key),
build: {
watch: watch ? {} : null,
lib: {
entry: 'src/index',
name: umdName,
formats: ['es', 'cjs', 'umd'],
fileName(format) {
if (format === 'es') return pkg.module.replace('dist/', '')
if (format === 'cjs') return pkg.main.replace('dist/', '')
if (format === 'umd') return pkg.unpkg.replace('dist/', '')
},
},
rollupOptions: {
external: [
/^codemirror-ssr/,
'hast-util-sanitize/lib/github.json',
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
],
},
},
plugins: [
svelte({
preprocess: [sveltePreprocess({ typescript: true })],
}),
createVuePlugin(),
key === 'bytemd' && {
name: 'copy-style-files',
closeBundle() {
console.log('copy style files...')
fs.copyFileSync(
'packages/bytemd/dist/style.css',
'packages/bytemd/dist/index.min.css'
)
fs.copyFileSync(
'packages/bytemd/dist/style.css',
'packages/bytemd/dist/index.css'
)
},
},
],
})
})
)