Skip to content

Commit ff62895

Browse files
NickeyLinhaoqunjiang
authored andcommitted
feat: add --filename option to specify the output (lib) file name (#3703)
1 parent f69339e commit ff62895

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

packages/@vue/cli-service/__tests__/buildLib.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,45 @@ test('build as lib with webpackConfiguration depending on target (js)', async ()
132132
const commonContent = await project.read('dist/testLib.common.js')
133133
expect(commonContent).not.toContain(`foo: 'bar'`)
134134
})
135+
136+
test('build as lib with --filename option', async () => {
137+
const project = await create('build-lib-filename-option', defaultPreset)
138+
await project.write('src/main.js', `
139+
export default { foo: 1 }
140+
export const bar = 2
141+
`)
142+
const { stdout } = await project.run('vue-cli-service build --target lib --name testLib --filename test-lib src/main.js')
143+
expect(stdout).toMatch('Build complete.')
144+
145+
expect(project.has('dist/demo.html')).toBe(true)
146+
expect(project.has('dist/test-lib.common.js')).toBe(true)
147+
expect(project.has('dist/test-lib.umd.js')).toBe(true)
148+
expect(project.has('dist/test-lib.umd.min.js')).toBe(true)
149+
150+
const port = await portfinder.getPortPromise()
151+
server = createServer({ root: path.join(project.dir, 'dist') })
152+
153+
await new Promise((resolve, reject) => {
154+
server.listen(port, err => {
155+
if (err) return reject(err)
156+
resolve()
157+
})
158+
})
159+
160+
const launched = await launchPuppeteer(`http://localhost:${port}/demo.html`)
161+
browser = launched.browser
162+
page = launched.page
163+
164+
expect(await page.evaluate(() => {
165+
return window.document.title
166+
})).toBe('testLib demo')
167+
168+
// should expose a module with default and named exports
169+
expect(await page.evaluate(() => {
170+
return window.testLib.default.foo
171+
})).toBe(1)
172+
173+
expect(await page.evaluate(() => {
174+
return window.testLib.bar
175+
})).toBe(2)
176+
})

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<meta charset="utf-8">
22
<title><%- htmlWebpackPlugin.options.libName %> demo</title>
3-
<script src="./<%- htmlWebpackPlugin.options.libName %>.umd.js"></script>
3+
<script src="./<%- htmlWebpackPlugin.options.assetsFileName %>.umd.js"></script>
44
<% if (htmlWebpackPlugin.options.cssExtract) { %>
5-
<link rel="stylesheet" href="./<%- htmlWebpackPlugin.options.libName %>.css">
5+
<link rel="stylesheet" href="./<%- htmlWebpackPlugin.options.assetsFileName %>.css">
66
<% } %>
77

88
<script>

packages/@vue/cli-service/lib/commands/build/demo-lib.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<meta charset="utf-8">
22
<title><%- htmlWebpackPlugin.options.libName %> demo</title>
33
<script src="https://unpkg.com/vue"></script>
4-
<script src="./<%- htmlWebpackPlugin.options.libName %>.umd.js"></script>
4+
<script src="./<%- htmlWebpackPlugin.options.assetsFileName %>.umd.js"></script>
55
<% if (htmlWebpackPlugin.options.cssExtract) { %>
6-
<link rel="stylesheet" href="./<%- htmlWebpackPlugin.options.libName %>.css">
6+
<link rel="stylesheet" href="./<%- htmlWebpackPlugin.options.assetsFileName %>.css">
77
<% } %>
88

99
<div id="app">

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = (api, options) => {
3131
'--target': `app | lib | wc | wc-async (default: ${defaults.target})`,
3232
'--formats': `list of output formats for library builds (default: ${defaults.formats})`,
3333
'--name': `name for lib or web-component mode (default: "name" in package.json or entry filename)`,
34+
'--filename': `file name for output, only usable for 'lib' target (default: value of --name)`,
3435
'--no-clean': `do not remove the dist directory before building the project`,
3536
'--report': `generate report.html to help analyze bundle content`,
3637
'--report-json': 'generate report.json to help analyze bundle content',

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

Lines changed: 5 additions & 4 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, formats }, options) => {
4+
module.exports = (api, { entry, name, formats, filename }, options) => {
55
const { log, error } = require('@vue/cli-shared-utils')
66
const abort = msg => {
77
log()
@@ -24,7 +24,7 @@ module.exports = (api, { entry, name, formats }, options) => {
2424
api.service.pkg.name ||
2525
path.basename(entry).replace(/\.(jsx?|vue)$/, '')
2626
)
27-
27+
filename = filename || libName
2828
function genConfig (format, postfix = format, genHTML) {
2929
const config = api.resolveChainableWebpackConfig()
3030

@@ -43,7 +43,7 @@ module.exports = (api, { entry, name, formats }, options) => {
4343
config
4444
.plugin('extract-css')
4545
.tap(args => {
46-
args[0].filename = `${libName}.css`
46+
args[0].filename = `${filename}.css`
4747
return args
4848
})
4949
}
@@ -74,12 +74,13 @@ module.exports = (api, { entry, name, formats }, options) => {
7474
inject: false,
7575
filename: 'demo.html',
7676
libName,
77+
assetsFileName: filename,
7778
cssExtract: config.plugins.has('extract-css')
7879
}])
7980
}
8081

8182
// resolve entry/output
82-
const entryName = `${libName}.${postfix}`
83+
const entryName = `${filename}.${postfix}`
8384
config.resolve
8485
.alias
8586
.set('~entry', fullEntryPath)

0 commit comments

Comments
 (0)