Skip to content

Commit 6c966f4

Browse files
hiendvyyx990803
authored andcommitted
feat(build): set output target before configureWebpack, close vuejs#1941 (vuejs#1943)
1 parent cb92491 commit 6c966f4

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,43 @@ test('build as lib (js)', async () => {
8888
return window.testLib.bar
8989
})).toBe(2)
9090
})
91+
92+
test('build as lib with webpackConfiguration depending on target (js)', async () => {
93+
const project = await create('build-lib-js-webpack-target', defaultPreset)
94+
await project.write('src/a-library.js', `
95+
export default {
96+
foo: 'bar'
97+
}
98+
`)
99+
100+
await project.write('src/main.js', `
101+
export * from 'a-library'
102+
`)
103+
104+
await project.write('vue.config.js', `
105+
const path = require('path')
106+
module.exports = {
107+
configureWebpack: config => {
108+
config.resolve.alias['a-library'] = path.resolve(__dirname, 'src', 'a-library.js')
109+
110+
if (config.output.libraryTarget === 'umd') {
111+
return
112+
}
113+
114+
config.externals = ['a-library']
115+
}
116+
}
117+
`)
118+
119+
const { stdout } = await project.run('vue-cli-service build --target lib --name testLib src/main.js')
120+
expect(stdout).toMatch('Build complete.')
121+
122+
expect(project.has('dist/testLib.umd.js')).toBe(true)
123+
expect(project.has('dist/testLib.common.js')).toBe(true)
124+
125+
const umdContent = await project.read('dist/testLib.umd.js')
126+
expect(umdContent).toContain(`foo: 'bar'`)
127+
128+
const commonContent = await project.read('dist/testLib.common.js')
129+
expect(commonContent).not.toContain(`foo: 'bar'`)
130+
})

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ module.exports = (api, { entry, name }, options) => {
7575
.alias
7676
.set('~entry', fullEntryPath)
7777

78+
// set output target before user configureWebpack hooks are applied
79+
config.output.libraryTarget(format)
80+
7881
// set entry/output after user configureWebpack hooks are applied
7982
const rawConfig = api.resolveWebpackConfig(config)
8083

0 commit comments

Comments
 (0)