docsify/build/build.js

157 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

const rollup = require('rollup')
const buble = require('rollup-plugin-buble')
const commonjs = require('rollup-plugin-commonjs')
const nodeResolve = require('rollup-plugin-node-resolve')
2019-10-16 21:20:54 +08:00
const { uglify } = require('rollup-plugin-uglify')
const replace = require('rollup-plugin-replace')
const isProd = process.env.NODE_ENV === 'production'
const version = process.env.VERSION || require('../package.json').version
const chokidar = require('chokidar')
const path = require('path')
2016-11-20 22:29:36 +08:00
/**
* @param {{
* input: string,
* output?: string,
* globalName?: string,
* plugins?: Array<import('rollup').Plugin>
* }} opts
*/
async function build(opts) {
await rollup
2016-11-20 22:29:36 +08:00
.rollup({
input: opts.input,
2017-03-09 20:30:38 +08:00
plugins: (opts.plugins || []).concat([
buble({
transforms: {
dangerousForOf: true
}}),
2017-03-09 20:30:38 +08:00
commonjs(),
2017-05-31 08:25:00 +08:00
nodeResolve(),
replace({
2018-02-10 16:24:34 +08:00
__VERSION__: version,
'process.env.SSR': false
2017-05-31 08:25:00 +08:00
})
]),
onwarn: function (message) {
if (message.code === 'UNRESOLVED_IMPORT') {
throw new Error(
`Could not resolve module ` +
message.source +
`. Try running 'npm install' or using rollup's 'external' option if this is an external dependency. ` +
`Module ${message.source} is imported in ${message.importer}`
)
}
}
2016-11-20 22:29:36 +08:00
})
2018-10-31 21:11:06 +08:00
.then(function (bundle) {
var dest = 'lib/' + (opts.output || opts.input)
2016-11-22 22:46:44 +08:00
console.log(dest)
return bundle.write({
format: 'iife',
output: opts.globalName ? {name: opts.globalName} : {},
2018-01-21 13:59:53 +08:00
file: dest,
strict: false
2016-11-20 22:29:36 +08:00
})
})
}
async function buildCore() {
const promises = []
promises.push(build({
input: 'src/core/index.js',
output: 'docsify.js',
}))
2017-02-17 23:09:09 +08:00
if (isProd) {
promises.push(build({
input: 'src/core/index.js',
output: 'docsify.min.js',
plugins: [uglify()]
}))
}
await Promise.all(promises)
}
async function buildAllPlugin() {
var plugins = [
{name: 'search', input: 'search/index.js'},
{name: 'ga', input: 'ga.js'},
2019-05-05 19:24:20 +08:00
{name: 'matomo', input: 'matomo.js'},
{name: 'emoji', input: 'emoji.js'},
{name: 'external-script', input: 'external-script.js'},
{name: 'front-matter', input: 'front-matter/index.js'},
{name: 'zoom-image', input: 'zoom-image.js'},
{name: 'disqus', input: 'disqus.js'},
{name: 'gitalk', input: 'gitalk.js'}
]
const promises = plugins.map(item => {
return build({
input: 'src/plugins/' + item.input,
output: 'plugins/' + item.name + '.js'
})
2017-02-17 23:09:09 +08:00
})
if (isProd) {
plugins.forEach(item => {
promises.push(build({
input: 'src/plugins/' + item.input,
output: 'plugins/' + item.name + '.min.js',
plugins: [uglify()]
}))
})
}
await Promise.all(promises)
}
async function main() {
if (!isProd) {
chokidar
.watch(['src/core', 'src/plugins'], {
atomic: true,
awaitWriteFinish: {
stabilityThreshold: 1000,
pollInterval: 100
}
})
.on('change', p => {
console.log('[watch] ', p)
const dirs = p.split(path.sep)
if (dirs[1] === 'core') {
buildCore()
} else if (dirs[2]) {
const name = path.basename(dirs[2], '.js')
const input = `src/plugins/${name}${
/\.js/.test(dirs[2]) ? '' : '/index'
}.js`
build({
input,
output: 'plugins/' + name + '.js'
})
}
})
.on('ready', () => {
console.log('[start]')
buildCore()
buildAllPlugin()
})
} else {
await Promise.all([
buildCore(),
buildAllPlugin()
])
}
2017-02-17 23:09:09 +08:00
}
main().catch((e) => {
console.error(e)
process.exit(1)
})