Skip to content

Commit 1e200c5

Browse files
tibramohaoqunjiang
authored andcommitted
feat: build library with specified formats (#2583)
1 parent 6e5cf0e commit 1e200c5

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
jest.setTimeout(40000)
2+
3+
const { defaultPreset } = require('@vue/cli/lib/options')
4+
const create = require('@vue/cli-test-utils/createTestProject')
5+
6+
let project
7+
8+
beforeAll(async () => {
9+
project = await create('build-lib-formats', defaultPreset)
10+
})
11+
12+
test('build as lib with default formats', async () => {
13+
const { stdout } = await project.run('vue-cli-service build --target lib --name testLib src/components/HelloWorld.vue')
14+
expect(stdout).toMatch('Build complete.')
15+
16+
expect(project.has('dist/demo.html')).toBe(true)
17+
expect(project.has('dist/testLib.common.js')).toBe(true)
18+
expect(project.has('dist/testLib.umd.js')).toBe(true)
19+
expect(project.has('dist/testLib.umd.min.js')).toBe(true)
20+
expect(project.has('dist/testLib.css')).toBe(true)
21+
})
22+
test('build as lib with formats commonjs and umd', async () => {
23+
const { stdout } = await project.run('vue-cli-service build --target lib --formats commonjs,umd --name testLib src/components/HelloWorld.vue')
24+
expect(stdout).toMatch('Build complete.')
25+
26+
expect(project.has('dist/demo.html')).toBe(true)
27+
expect(project.has('dist/testLib.common.js')).toBe(true)
28+
expect(project.has('dist/testLib.umd.js')).toBe(true)
29+
expect(project.has('dist/testLib.umd.min.js')).toBe(false)
30+
expect(project.has('dist/testLib.css')).toBe(true)
31+
})
32+
33+
test('build as lib with format umd-min', async () => {
34+
const { stdout } = await project.run('vue-cli-service build --target lib --formats umd-min --name testLib src/components/HelloWorld.vue')
35+
expect(stdout).toMatch('Build complete.')
36+
37+
expect(project.has('dist/demo.html')).toBe(false)
38+
expect(project.has('dist/testLib.common.js')).toBe(false)
39+
expect(project.has('dist/testLib.umd.js')).toBe(false)
40+
expect(project.has('dist/testLib.umd.min.js')).toBe(true)
41+
expect(project.has('dist/testLib.css')).toBe(true)
42+
})
43+
44+
test('build as lib with unknown formats throws an error', async () => {
45+
try {
46+
await project.run('vue-cli-service build --target lib --formats umd,x,y --name testLib src/components/HelloWorld.vue')
47+
} catch (e) {
48+
expect(e.code).toBe(1)
49+
expect(e.failed).toBeTruthy()
50+
}
51+
})

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const defaults = {
22
clean: true,
33
target: 'app',
4+
formats: 'commonjs,umd,umd-min',
45
'unsafe-inline': true
56
}
67

78
const buildModes = {
8-
lib: 'library (commonjs + umd)',
9+
lib: 'library',
910
wc: 'web component',
1011
'wc-async': 'web component (async)'
1112
}
@@ -28,6 +29,7 @@ module.exports = (api, options) => {
2829
'--modern': `build app targeting modern browsers with auto fallback`,
2930
'--no-unsafe-inline': `build app without introducing inline scripts`,
3031
'--target': `app | lib | wc | wc-async (default: ${defaults.target})`,
32+
'--formats': `list of output formats for library builds (default: ${defaults.formats})`,
3133
'--name': `name for lib or web-component mode (default: "name" in package.json or entry filename)`,
3234
'--no-clean': `do not remove the dist directory before building the project`,
3335
'--report': `generate report.html to help analyze bundle content`,
@@ -104,7 +106,8 @@ async function build (args, api, options) {
104106
} else {
105107
const buildMode = buildModes[args.target]
106108
if (buildMode) {
107-
logWithSpinner(`Building for ${mode} as ${buildMode}...`)
109+
const additionalParams = buildMode === 'library' ? ` (${args.formats})` : ``
110+
logWithSpinner(`Building for ${mode} as ${buildMode}${additionalParams}...`)
108111
} else {
109112
throw new Error(`Unknown build target: ${args.target}`)
110113
}

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require('fs')
22
const path = require('path')
33

4-
module.exports = (api, { entry, name }, options) => {
4+
module.exports = (api, { entry, name, formats }, options) => {
55
const { log, error } = require('@vue/cli-shared-utils')
66
const abort = msg => {
77
log()
@@ -114,9 +114,20 @@ module.exports = (api, { entry, name }, options) => {
114114
return rawConfig
115115
}
116116

117-
return [
118-
genConfig('commonjs2', 'common'),
119-
genConfig('umd', undefined, true),
120-
genConfig('umd', 'umd.min')
121-
]
117+
const configMap = {
118+
commonjs: genConfig('commonjs2', 'common'),
119+
umd: genConfig('umd', undefined, true),
120+
'umd-min': genConfig('umd', 'umd.min')
121+
}
122+
123+
const formatArray = (formats + '').split(',')
124+
const configs = formatArray.map(format => configMap[format])
125+
if (configs.indexOf(undefined) !== -1) {
126+
const unknownFormats = formatArray.filter(f => configMap[f] === undefined).join(', ')
127+
abort(
128+
`Unknown library build formats: ${unknownFormats}`
129+
)
130+
}
131+
132+
return configs
122133
}

0 commit comments

Comments
 (0)