fix: --target for global build

This commit is contained in:
Evan You
2018-01-30 01:45:51 -05:00
parent faadadf5ad
commit 4fb4e35fe7
6 changed files with 52 additions and 33 deletions

View File

@@ -8,7 +8,7 @@ const babelPlugin = toPlugin('@vue/cli-plugin-babel')
const eslintPlugin = toPlugin('@vue/cli-plugin-eslint')
const createConfigPlugin = require('./lib/createConfigPlugin')
function createService (entry) {
function resolveEntry (entry) {
const context = process.cwd()
entry = entry || findExisting(context, [
@@ -29,6 +29,13 @@ function createService (entry) {
process.exit(1)
}
return {
context,
entry
}
}
function createService (context, entry, asLib) {
return new Service(context, {
projectOptions: {
compiler: true,
@@ -37,15 +44,21 @@ function createService (entry) {
plugins: [
babelPlugin,
eslintPlugin,
createConfigPlugin(context, entry)
createConfigPlugin(context, entry, asLib)
]
})
}
exports.serve = (entry, args) => {
createService(entry).run('serve', args)
exports.serve = (_entry, args) => {
const { context, entry } = resolveEntry(_entry)
createService(context, entry).run('serve', args)
}
exports.build = (entry, args) => {
createService(entry).run('build', args)
exports.build = (_entry, args) => {
const { context, entry } = resolveEntry(_entry)
const asLib = args.target && args.target !== 'app'
if (asLib) {
args.libEntry = entry
}
createService(context, entry, asLib).run('build', args)
}

View File

@@ -2,7 +2,7 @@ const path = require('path')
const resolve = require('resolve')
const { findExisting } = require('./util')
module.exports = function createConfigPlugin (context, entry) {
module.exports = function createConfigPlugin (context, entry, asLib) {
return {
id: '@vue/cli-service-global-config',
apply: (api, options) => {
@@ -111,17 +111,22 @@ module.exports = function createConfigPlugin (context, entry) {
}
}))
// set html plugin template
const indexFile = findExisting(context, [
'index.html',
'public/index.html'
]) || path.resolve(__dirname, '../template/index.html')
config
.plugin('html')
.tap(() => [{ template: indexFile }])
if (!asLib) {
// set html plugin template
const indexFile = findExisting(context, [
'index.html',
'public/index.html'
]) || path.resolve(__dirname, '../template/index.html')
config
.plugin('html')
.tap(args => {
args[0].template = indexFile
return args
})
}
// disable copy plugin if no public dir
if (!findExisting(context, ['public'])) {
if (asLib || !findExisting(context, ['public'])) {
config.plugins.delete('copy')
}
})

View File

@@ -1,7 +1,7 @@
const defaults = {
mode: 'production',
target: 'app',
entry: 'src/App.vue'
libEntry: 'src/App.vue'
}
module.exports = (api, options) => {
@@ -11,11 +11,13 @@ module.exports = (api, options) => {
options: {
'--mode': `specify env mode (default: ${defaults.mode})`,
'--target': `app | lib | web-component (default: ${defaults.target})`,
'--entry': `entry for lib or web-component (default: ${defaults.entry})`,
'--name': `name for lib or web-component (default: "name" in package.json)`
'--libEntry': `entry for lib or web-component (default: ${defaults.entry})`,
'--libName': `name for lib or web-component (default: "name" in package.json)`
}
}, args => {
args = Object.assign({}, defaults, args)
for (const key in defaults) {
if (args[key] == null) args[key] = defaults[key]
}
api.setMode(args.mode)
const chalk = require('chalk')

View File

@@ -1,21 +1,21 @@
module.exports = (api, { entry, name }) => {
module.exports = (api, { libEntry, libName }) => {
const genConfig = (format, postfix = format) => {
api.chainWebpack(config => {
const libName = name || api.service.pkg.name
libName = libName || api.service.pkg.name || libEntry.replace(/\.(js|vue)$/, '')
config.entryPoints.clear()
// set proxy entry for *.vue files
if (/\.vue$/.test(entry)) {
if (/\.vue$/.test(libEntry)) {
config
.entry(`${libName}.${postfix}`)
.add(require.resolve('./entry-lib.js'))
config.resolve
.alias
.set('~entry', api.resolve(entry))
.set('~entry', api.resolve(libEntry))
} else {
config
.entry(`${libName}.${postfix}`)
.add(api.resolve(entry))
.add(api.resolve(libEntry))
}
config.output

View File

@@ -1,21 +1,21 @@
module.exports = (api, { entry, name }) => {
module.exports = (api, { libEntry, libName }) => {
const genConfig = postfix => {
api.chainWebpack(config => {
const libName = name || api.service.pkg.name
libName = libName || api.service.pkg.name || libEntry.replace(/\.(js|vue)$/, '')
config.entryPoints.clear()
// set proxy entry for *.vue files
if (/\.vue$/.test(entry)) {
if (/\.vue$/.test(libEntry)) {
config
.entry(`${libName}.${postfix}`)
.add(require.resolve('./entry-web-component.js'))
config.resolve
.alias
.set('~entry', api.resolve(entry))
.set('~entry', api.resolve(libEntry))
} else {
config
.entry(`${libName}.${postfix}`)
.add(api.resolve(entry))
.add(api.resolve(libEntry))
}
config.output

View File

@@ -64,9 +64,8 @@ program
program
.command('build [entry]')
.option('-t, --target', 'Build target (app, lib, web-component). Default: app')
.option('-f, --format', 'How the lib is exposed (iife, amd, cjs, umd). Default: umd')
.option('-n, --name', 'Library name for umd export')
.option('-t, --target <target>', 'Build target (app | lib | web-component, default: app)')
.option('-n, --libName <name>', 'name for lib or web-component')
.description('build a .js or .vue file in production mode with zero config')
.action((entry, cmd) => {
loadCommand('build', '@vue/cli-service-global').build(entry, cleanArgs(cmd))