Skip to content

Commit

Permalink
chore: build
Browse files Browse the repository at this point in the history
  • Loading branch information
wangfupeng1988 committed Feb 16, 2022
1 parent 0ca1f77 commit b519a5c
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 175 deletions.
69 changes: 38 additions & 31 deletions build/config/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,42 @@ import peerDepsExternal from 'rollup-plugin-peer-deps-external'
export const extensions = ['.js', '.jsx', '.ts', '.tsx']
const isProd = process.env.NODE_ENV === 'production'

export default {
input: path.resolve(__dirname, './src/index.ts'),
output: {
// 属性有 file format name sourcemap 等
// https://www.rollupjs.com/guide/big-list-of-options
},
plugins: [
peerDepsExternal(), // 打包结果不包含 package.json 的 peerDependencies
json({
compact: true,
indent: ' ',
preferConst: true,
}),
typescript({
clean: true,
tsconfig: path.resolve(__dirname, './tsconfig.json'),
}),
nodeResolve({
browser: true, // 重要
// 声明加载 node_module package 时默认使用 package.json 的 main 属性指定的文件
// 之前加载 es module,默认不会被处理,导致最后生成的代码有箭头函数
mainFields: isProd ? ['main'] : ['module', 'main'],
extensions,
}),
commonjs(),
replace({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
preventAssignment: true,
}),
// del({ targets: 'dist/*' }),
],
/**
* 生成 common conf
* @param {string} format 'umd' 'esm'
* @returns common conf
*/
function genCommonConf(format) {
return {
input: path.resolve(__dirname, './src/index.ts'),
output: {
// 属性有 file format name sourcemap 等
// https://www.rollupjs.com/guide/big-list-of-options
},
plugins: [
peerDepsExternal(), // 打包结果不包含 package.json 的 peerDependencies
json({
compact: true,
indent: ' ',
preferConst: true,
}),
typescript({
clean: true,
tsconfig: path.resolve(__dirname, './tsconfig.json'),
}),
nodeResolve({
browser: true, // 重要
mainFields: format === 'esm' ? ['module', 'main'] : ['main'],
extensions,
}),
commonjs(),
replace({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
preventAssignment: true,
}),
// del({ targets: 'dist/*' }),
],
}
}

export default genCommonConf
34 changes: 21 additions & 13 deletions build/config/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@

import postcss from 'rollup-plugin-postcss'
import autoprefixer from 'autoprefixer'
import commonConfig from './common'
import genCommonConf from './common'

const { input, output = {}, plugins = [], external } = commonConfig
/**
* 生成 dev config
* @param {string} format 'umd' 'esm'
*/
function genDevConf(format) {
const { input, output = {}, plugins = [], external } = genCommonConf(format)

export default {
input,
output,
external,
plugins: [
...plugins,
return {
input,
output,
external,
plugins: [
...plugins,

postcss({
plugins: [autoprefixer()],
extract: 'css/style.css',
}),
],
postcss({
plugins: [autoprefixer()],
extract: 'css/style.css',
}),
],
}
}

export default genDevConf
72 changes: 40 additions & 32 deletions build/config/prd.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,48 @@ import autoprefixer from 'autoprefixer'
import cssnano from 'cssnano'
import { terser } from 'rollup-plugin-terser'
import cleanup from 'rollup-plugin-cleanup'
import commonConfig from './common'
import genCommonConf from './common'
import { extensions } from './common'

const { input, output = {}, plugins = [], external } = commonConfig
/**
* 生成 prd config
* @param {string} format 'umd' 'esm'
*/
function genPrdConf(format) {
const { input, output = {}, plugins = [], external } = genCommonConf(format)

const finalPlugins = [
...plugins,
babel({
rootMode: 'upward',
babelHelpers: 'runtime',
exclude: 'node_modules/**',
include: 'src/**',
extensions,
}),
postcss({
plugins: [
autoprefixer(),
cssnano(), // 压缩 css
],
extract: 'css/style.css',
}),
cleanup({
comments: 'none',
extensions: ['.ts', '.tsx'],
}),
terser(), // 压缩 js
]
const finalPlugins = [
...plugins,
babel({
rootMode: 'upward',
babelHelpers: 'runtime',
exclude: 'node_modules/**',
include: 'src/**',
extensions,
}),
postcss({
plugins: [
autoprefixer(),
cssnano(), // 压缩 css
],
extract: 'css/style.css',
}),
cleanup({
comments: 'none',
extensions: ['.ts', '.tsx'],
}),
terser(), // 压缩 js
]

export default {
input,
output: {
sourcemap: true,
...output,
},
external,
plugins: finalPlugins,
return {
input,
output: {
sourcemap: true,
...output,
},
external,
plugins: finalPlugins,
}
}

export default genPrdConf
9 changes: 5 additions & 4 deletions build/create-rollup-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import { merge } from 'lodash'
import { visualizer } from 'rollup-plugin-visualizer'
import devConf from './config/dev'
import prdConf from './config/prd'
import genDevConf from './config/dev'
import genPrdConf from './config/prd'

// 环境变量
const ENV = process.env.NODE_ENV || 'production'
Expand All @@ -20,12 +20,13 @@ export const IS_PRD = ENV.indexOf('production') >= 0
*/
export function createRollupConfig(customConfig = {}) {
const { input, output = {}, plugins = [] } = customConfig
const { format } = output

let baseConfig
if (IS_PRD) {
baseConfig = prdConf
baseConfig = genPrdConf(format)
} else {
baseConfig = devConf
baseConfig = genDevConf(format)
}

if (IS_SIZE_STATS) {
Expand Down
22 changes: 10 additions & 12 deletions packages/basic-modules/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const name = 'WangEditorBasicModules'

const configList = []

// esm - 开发环境不需要 CDN 引入,只需要 npm 引入,所以优先输出 esm
// esm
const esmConf = createRollupConfig({
output: {
file: pkg.module,
Expand All @@ -15,16 +15,14 @@ const esmConf = createRollupConfig({
})
configList.push(esmConf)

if (IS_PRD) {
// umd
const umdConf = createRollupConfig({
output: {
file: pkg.main,
format: 'umd',
name,
},
})
configList.push(umdConf)
}
// umd
const umdConf = createRollupConfig({
output: {
file: pkg.main,
format: 'umd',
name,
},
})
configList.push(umdConf)

export default configList
22 changes: 10 additions & 12 deletions packages/code-highlight/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const name = 'WangEditorCodeHighLight'

const configList = []

// esm - 开发环境不需要 CDN 引入,只需要 npm 引入,所以优先输出 esm
// esm
const esmConf = createRollupConfig({
output: {
file: pkg.module,
Expand All @@ -15,16 +15,14 @@ const esmConf = createRollupConfig({
})
configList.push(esmConf)

if (IS_PRD) {
// umd
const umdConf = createRollupConfig({
output: {
file: pkg.main,
format: 'umd',
name,
},
})
configList.push(umdConf)
}
// umd
const umdConf = createRollupConfig({
output: {
file: pkg.main,
format: 'umd',
name,
},
})
configList.push(umdConf)

export default configList
22 changes: 10 additions & 12 deletions packages/core/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const name = 'WangEditorCore'

const configList = []

// esm - 开发环境不需要 CDN 引入,只需要 npm 引入,所以优先输出 esm
// esm
const esmConf = createRollupConfig({
output: {
file: pkg.module,
Expand All @@ -15,16 +15,14 @@ const esmConf = createRollupConfig({
})
configList.push(esmConf)

if (IS_PRD) {
// umd
const umdConf = createRollupConfig({
output: {
file: pkg.main,
format: 'umd',
name,
},
})
configList.push(umdConf)
}
// umd
const umdConf = createRollupConfig({
output: {
file: pkg.main,
format: 'umd',
name,
},
})
configList.push(umdConf)

export default configList
22 changes: 10 additions & 12 deletions packages/editor/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const name = 'wangEditor'

const configList = []

// umd - 开发环境需要 CDN 引入方式来测试,所以优先输出 umd
// umd
const umdConf = createRollupConfig({
output: {
file: pkg.main,
Expand All @@ -15,16 +15,14 @@ const umdConf = createRollupConfig({
})
configList.push(umdConf)

if (IS_PRD) {
// esm
const esmConf = createRollupConfig({
output: {
file: pkg.module,
format: 'esm',
name,
},
})
configList.push(esmConf)
}
// esm
const esmConf = createRollupConfig({
output: {
file: pkg.module,
format: 'esm',
name,
},
})
configList.push(esmConf)

export default configList
20 changes: 9 additions & 11 deletions packages/list-module/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ const esmConf = createRollupConfig({
})
configList.push(esmConf)

if (IS_PRD) {
// umd
const umdConf = createRollupConfig({
output: {
file: pkg.main,
format: 'umd',
name,
},
})
configList.push(umdConf)
}
// umd
const umdConf = createRollupConfig({
output: {
file: pkg.main,
format: 'umd',
name,
},
})
configList.push(umdConf)

export default configList
Loading

0 comments on commit b519a5c

Please sign in to comment.