Skip to content

Commit de9748a

Browse files
MewesKhaoqunjiang
authored andcommitted
fix: do not introduce extra level of directory when building lib for scope packages (#4320)
This fixes references to static assets in the bundled script. Fixes #4311
1 parent e2e3469 commit de9748a

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

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

+40
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,43 @@ test('build as lib with --filename option', async () => {
174174
return window.testLib.bar
175175
})).toBe(2)
176176
})
177+
178+
test('build as lib without --name and --filename options (default to package name)', async () => {
179+
const project = await create('build-lib-no-name-and-filename-option', defaultPreset)
180+
await project.write('package.json', `
181+
{
182+
"name": "test-lib"
183+
}
184+
`)
185+
await project.write('src/main.js', `
186+
export default { foo: 1 }
187+
export const bar = 2
188+
`)
189+
const { stdout } = await project.run('vue-cli-service build --target lib src/main.js')
190+
expect(stdout).toMatch('Build complete.')
191+
192+
expect(project.has('dist/demo.html')).toBe(true)
193+
expect(project.has('dist/test-lib.common.js')).toBe(true)
194+
expect(project.has('dist/test-lib.umd.js')).toBe(true)
195+
expect(project.has('dist/test-lib.umd.min.js')).toBe(true)
196+
})
197+
198+
test('build as lib without --name and --filename options (default to package name, minus scope)', async () => {
199+
const project = await create('build-lib-no-name-and-filename-option-with-scope', defaultPreset)
200+
await project.write('package.json', `
201+
{
202+
"name": "@foo/test-lib"
203+
}
204+
`)
205+
await project.write('src/main.js', `
206+
export default { foo: 1 }
207+
export const bar = 2
208+
`)
209+
const { stdout } = await project.run('vue-cli-service build --target lib src/main.js')
210+
expect(stdout).toMatch('Build complete.')
211+
212+
expect(project.has('dist/demo.html')).toBe(true)
213+
expect(project.has('dist/test-lib.common.js')).toBe(true)
214+
expect(project.has('dist/test-lib.umd.js')).toBe(true)
215+
expect(project.has('dist/test-lib.umd.min.js')).toBe(true)
216+
})

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ module.exports = (api, { entry, name, formats, filename }, options) => {
2121
const isVueEntry = /\.vue$/.test(entry)
2222
const libName = (
2323
name ||
24-
api.service.pkg.name ||
25-
path.basename(entry).replace(/\.(jsx?|vue)$/, '')
24+
(
25+
api.service.pkg.name
26+
? api.service.pkg.name.replace(/^@.+\//, '')
27+
: path.basename(entry).replace(/\.(jsx?|vue)$/, '')
28+
)
2629
)
2730
filename = filename || libName
2831
function genConfig (format, postfix = format, genHTML) {

0 commit comments

Comments
 (0)