zoro-cli
Version:
https://github.com/vuejs/vue-cli
61 lines (53 loc) • 1.81 kB
JavaScript
const fs = require('fs')
const path = require('path')
module.exports = ({ api, rootOptions, Types, pkg }) => {
const { type, bundler, cssPreprocessor } = rootOptions
const { weblib } = Types
let { name, libName } = pkg
if (type === weblib) {
api.render('./template')
api.render(`./template-${bundler}`)
// render css template
const cssFolder = './template-' + cssPreprocessor
if (fs.existsSync(path.join(__dirname, cssFolder))) {
api.render(cssFolder)
}
// trim group from name
if (name.indexOf('@') === 0) {
name = name.replace(/^@.*?\//, '')
}
// libName used by bundler such as rollup/webpack
if (!libName) {
libName = name
.split('-')
.map((part, index) => {
const initial =
index !== 0 ? part.charAt(0).toUpperCase() : part.charAt(0)
return initial + part.slice(1)
})
.join('')
}
api.extendPackage({
libName,
// https://webpack.js.org/guides/author-libraries/#final-steps
main: `dist/${name}.common.js`,
// https://github.com/d3/d3/issues/3138
// https://github.com/unpkg/unpkg/issues/63
unpkg: `dist/${name}.js`,
jsdelivr: `dist/${name}.js`,
// https://webpack.js.org/configuration/resolve/#resolve-mainfields
// https://webpack.js.org/configuration/resolve/#resolve-aliasfields
// https://github.com/defunctzombie/package-browser-field-spec
// https://github.com/webpack/webpack/issues/5673#issuecomment-329713876
browser: {
// redirect (main) cjs to umd, no redirect for module (esm)
[`./dist/${name}.common.js`]: `./dist/${name}.js`,
},
})
if (bundler === 'rollup') {
api.extendPackage({
module: `dist/${name}.esm.js`,
})
}
}
}