Skip to content

Commit 4fb4e35

Browse files
committed
fix: --target for global build
1 parent faadadf commit 4fb4e35

File tree

6 files changed

+52
-33
lines changed

6 files changed

+52
-33
lines changed

packages/@vue/cli-service-global/index.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const babelPlugin = toPlugin('@vue/cli-plugin-babel')
88
const eslintPlugin = toPlugin('@vue/cli-plugin-eslint')
99
const createConfigPlugin = require('./lib/createConfigPlugin')
1010

11-
function createService (entry) {
11+
function resolveEntry (entry) {
1212
const context = process.cwd()
1313

1414
entry = entry || findExisting(context, [
@@ -29,6 +29,13 @@ function createService (entry) {
2929
process.exit(1)
3030
}
3131

32+
return {
33+
context,
34+
entry
35+
}
36+
}
37+
38+
function createService (context, entry, asLib) {
3239
return new Service(context, {
3340
projectOptions: {
3441
compiler: true,
@@ -37,15 +44,21 @@ function createService (entry) {
3744
plugins: [
3845
babelPlugin,
3946
eslintPlugin,
40-
createConfigPlugin(context, entry)
47+
createConfigPlugin(context, entry, asLib)
4148
]
4249
})
4350
}
4451

45-
exports.serve = (entry, args) => {
46-
createService(entry).run('serve', args)
52+
exports.serve = (_entry, args) => {
53+
const { context, entry } = resolveEntry(_entry)
54+
createService(context, entry).run('serve', args)
4755
}
4856

49-
exports.build = (entry, args) => {
50-
createService(entry).run('build', args)
57+
exports.build = (_entry, args) => {
58+
const { context, entry } = resolveEntry(_entry)
59+
const asLib = args.target && args.target !== 'app'
60+
if (asLib) {
61+
args.libEntry = entry
62+
}
63+
createService(context, entry, asLib).run('build', args)
5164
}

packages/@vue/cli-service-global/lib/createConfigPlugin.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path')
22
const resolve = require('resolve')
33
const { findExisting } = require('./util')
44

5-
module.exports = function createConfigPlugin (context, entry) {
5+
module.exports = function createConfigPlugin (context, entry, asLib) {
66
return {
77
id: '@vue/cli-service-global-config',
88
apply: (api, options) => {
@@ -111,17 +111,22 @@ module.exports = function createConfigPlugin (context, entry) {
111111
}
112112
}))
113113

114-
// set html plugin template
115-
const indexFile = findExisting(context, [
116-
'index.html',
117-
'public/index.html'
118-
]) || path.resolve(__dirname, '../template/index.html')
119-
config
120-
.plugin('html')
121-
.tap(() => [{ template: indexFile }])
114+
if (!asLib) {
115+
// set html plugin template
116+
const indexFile = findExisting(context, [
117+
'index.html',
118+
'public/index.html'
119+
]) || path.resolve(__dirname, '../template/index.html')
120+
config
121+
.plugin('html')
122+
.tap(args => {
123+
args[0].template = indexFile
124+
return args
125+
})
126+
}
122127

123128
// disable copy plugin if no public dir
124-
if (!findExisting(context, ['public'])) {
129+
if (asLib || !findExisting(context, ['public'])) {
125130
config.plugins.delete('copy')
126131
}
127132
})

packages/@vue/cli-service/lib/commands/build/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const defaults = {
22
mode: 'production',
33
target: 'app',
4-
entry: 'src/App.vue'
4+
libEntry: 'src/App.vue'
55
}
66

77
module.exports = (api, options) => {
@@ -11,11 +11,13 @@ module.exports = (api, options) => {
1111
options: {
1212
'--mode': `specify env mode (default: ${defaults.mode})`,
1313
'--target': `app | lib | web-component (default: ${defaults.target})`,
14-
'--entry': `entry for lib or web-component (default: ${defaults.entry})`,
15-
'--name': `name for lib or web-component (default: "name" in package.json)`
14+
'--libEntry': `entry for lib or web-component (default: ${defaults.entry})`,
15+
'--libName': `name for lib or web-component (default: "name" in package.json)`
1616
}
1717
}, args => {
18-
args = Object.assign({}, defaults, args)
18+
for (const key in defaults) {
19+
if (args[key] == null) args[key] = defaults[key]
20+
}
1921
api.setMode(args.mode)
2022

2123
const chalk = require('chalk')

packages/@vue/cli-service/lib/commands/build/resolveLibConfig.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
module.exports = (api, { entry, name }) => {
1+
module.exports = (api, { libEntry, libName }) => {
22
const genConfig = (format, postfix = format) => {
33
api.chainWebpack(config => {
4-
const libName = name || api.service.pkg.name
4+
libName = libName || api.service.pkg.name || libEntry.replace(/\.(js|vue)$/, '')
55

66
config.entryPoints.clear()
77
// set proxy entry for *.vue files
8-
if (/\.vue$/.test(entry)) {
8+
if (/\.vue$/.test(libEntry)) {
99
config
1010
.entry(`${libName}.${postfix}`)
1111
.add(require.resolve('./entry-lib.js'))
1212
config.resolve
1313
.alias
14-
.set('~entry', api.resolve(entry))
14+
.set('~entry', api.resolve(libEntry))
1515
} else {
1616
config
1717
.entry(`${libName}.${postfix}`)
18-
.add(api.resolve(entry))
18+
.add(api.resolve(libEntry))
1919
}
2020

2121
config.output

packages/@vue/cli-service/lib/commands/build/resolveWebComponentConfig.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
module.exports = (api, { entry, name }) => {
1+
module.exports = (api, { libEntry, libName }) => {
22
const genConfig = postfix => {
33
api.chainWebpack(config => {
4-
const libName = name || api.service.pkg.name
4+
libName = libName || api.service.pkg.name || libEntry.replace(/\.(js|vue)$/, '')
55

66
config.entryPoints.clear()
77
// set proxy entry for *.vue files
8-
if (/\.vue$/.test(entry)) {
8+
if (/\.vue$/.test(libEntry)) {
99
config
1010
.entry(`${libName}.${postfix}`)
1111
.add(require.resolve('./entry-web-component.js'))
1212
config.resolve
1313
.alias
14-
.set('~entry', api.resolve(entry))
14+
.set('~entry', api.resolve(libEntry))
1515
} else {
1616
config
1717
.entry(`${libName}.${postfix}`)
18-
.add(api.resolve(entry))
18+
.add(api.resolve(libEntry))
1919
}
2020

2121
config.output

packages/@vue/cli/bin/vue.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ program
6464

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

0 commit comments

Comments
 (0)