Skip to content

Commit 50fdd9b

Browse files
committed
feat: build --target wc-async
1 parent 2c61d23 commit 50fdd9b

File tree

14 files changed

+148
-130
lines changed

14 files changed

+148
-130
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
template
33
packages/test
44
temp
5+
entry-wc.js

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
jest.setTimeout(30000)
1+
jest.setTimeout(15000)
22

33
const path = require('path')
44
const portfinder = require('portfinder')
@@ -8,10 +8,10 @@ const create = require('@vue/cli-test-utils/createTestProject')
88
const launchPuppeteer = require('@vue/cli-test-utils/launchPuppeteer')
99

1010
let server, browser, page
11-
test('build as single wc', async () => {
11+
test('build as wc', async () => {
1212
const project = await create('build-wc', defaultPreset)
1313

14-
const { stdout } = await project.run('vue-cli-service build --target wc src/components/HelloWorld.vue')
14+
const { stdout } = await project.run(`vue-cli-service build --target wc **/*.vue`)
1515
expect(stdout).toMatch('Build complete.')
1616

1717
expect(project.has('dist/demo.html')).toBe(true)
@@ -33,17 +33,24 @@ test('build as single wc', async () => {
3333
page = launched.page
3434

3535
const styleCount = await page.evaluate(() => {
36-
return document.querySelector('build-wc').shadowRoot.querySelectorAll('style').length
36+
return document.querySelector('build-wc-app').shadowRoot.querySelectorAll('style').length
3737
})
38-
expect(styleCount).toBe(1)
38+
expect(styleCount).toBe(2) // should contain styles from both app and child
3939

40-
await page.evaluate(() => {
41-
document.querySelector('build-wc').msg = 'hello'
42-
})
4340
const h1Text = await page.evaluate(() => {
44-
return document.querySelector('build-wc').shadowRoot.querySelector('h1').textContent
41+
return document.querySelector('build-wc-app').shadowRoot.querySelector('h1').textContent
42+
})
43+
expect(h1Text).toMatch('Welcome to Your Vue.js App')
44+
45+
const childStyleCount = await page.evaluate(() => {
46+
return document.querySelector('build-wc-hello-world').shadowRoot.querySelectorAll('style').length
47+
})
48+
expect(childStyleCount).toBe(1)
49+
50+
const h2Text = await page.evaluate(() => {
51+
return document.querySelector('build-wc-hello-world').shadowRoot.querySelector('h2').textContent
4552
})
46-
expect(h1Text).toBe('hello')
53+
expect(h2Text).toMatch('Essential Links')
4754
})
4855

4956
afterAll(async () => {

packages/@vue/cli-service/__tests__/buildMultiWc.spec.js renamed to packages/@vue/cli-service/__tests__/buildWcAsync.spec.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
jest.setTimeout(30000)
1+
jest.setTimeout(15000)
22

33
const path = require('path')
44
const portfinder = require('portfinder')
@@ -8,15 +8,21 @@ const create = require('@vue/cli-test-utils/createTestProject')
88
const launchPuppeteer = require('@vue/cli-test-utils/launchPuppeteer')
99

1010
let server, browser, page
11-
test('build as single wc', async () => {
12-
const project = await create('build-multi-wc', defaultPreset)
11+
test('build as wc in async mode', async () => {
12+
const project = await create('build-wc-async', defaultPreset)
1313

14-
const { stdout } = await project.run(`vue-cli-service build --target multi-wc **/*.vue`)
14+
const { stdout } = await project.run(`vue-cli-service build --target wc-async **/*.vue`)
1515
expect(stdout).toMatch('Build complete.')
1616

1717
expect(project.has('dist/demo.html')).toBe(true)
18-
expect(project.has('dist/build-multi-wc.js')).toBe(true)
19-
expect(project.has('dist/build-multi-wc.min.js')).toBe(true)
18+
expect(project.has('dist/build-wc-async.js')).toBe(true)
19+
expect(project.has('dist/build-wc-async.min.js')).toBe(true)
20+
21+
// code-split chunks
22+
expect(project.has('dist/build-wc-async.0.js')).toBe(true)
23+
expect(project.has('dist/build-wc-async.0.min.js')).toBe(true)
24+
expect(project.has('dist/build-wc-async.1.js')).toBe(true)
25+
expect(project.has('dist/build-wc-async.1.min.js')).toBe(true)
2026

2127
const port = await portfinder.getPortPromise()
2228
server = createServer({ root: path.join(project.dir, 'dist') })
@@ -33,22 +39,22 @@ test('build as single wc', async () => {
3339
page = launched.page
3440

3541
const styleCount = await page.evaluate(() => {
36-
return document.querySelector('build-multi-wc-app').shadowRoot.querySelectorAll('style').length
42+
return document.querySelector('build-wc-async-app').shadowRoot.querySelectorAll('style').length
3743
})
3844
expect(styleCount).toBe(2) // should contain styles from both app and child
3945

4046
const h1Text = await page.evaluate(() => {
41-
return document.querySelector('build-multi-wc-app').shadowRoot.querySelector('h1').textContent
47+
return document.querySelector('build-wc-async-app').shadowRoot.querySelector('h1').textContent
4248
})
4349
expect(h1Text).toMatch('Welcome to Your Vue.js App')
4450

4551
const childStyleCount = await page.evaluate(() => {
46-
return document.querySelector('build-multi-wc-hello-world').shadowRoot.querySelectorAll('style').length
52+
return document.querySelector('build-wc-async-hello-world').shadowRoot.querySelectorAll('style').length
4753
})
4854
expect(childStyleCount).toBe(1)
4955

5056
const h2Text = await page.evaluate(() => {
51-
return document.querySelector('build-multi-wc-hello-world').shadowRoot.querySelector('h2').textContent
57+
return document.querySelector('build-wc-async-hello-world').shadowRoot.querySelector('h2').textContent
5258
})
5359
expect(h2Text).toMatch('Essential Links')
5460
})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
entry-multi-wc.js
1+
entry-wc.js

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

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
<script src="https://unpkg.com/vue"></script>
33
<script src="./<%- htmlWebpackPlugin.options.libName %>.js"></script>
44

5-
<<%= htmlWebpackPlugin.options.libName %>></<%= htmlWebpackPlugin.options.libName %>>
5+
<% for (const comp of htmlWebpackPlugin.options.components) { %>
6+
<<%= comp %>></<%= comp %>>
7+
<% } %>
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Vue from 'vue'
22
import wrap from '@vue/web-component-wrapper'
3-
import Component from '~entry'
43

5-
window.customElements.define(
6-
process.env.CUSTOM_ELEMENT_NAME,
7-
wrap(Vue, Component)
8-
)
4+
// runtime shared by every component chunk
5+
import 'css-loader/lib/css-base'
6+
import 'vue-style-loader/lib/addStylesShadow'
7+
import 'vue-loader/lib/runtime/component-normalizer'
8+
9+
import myWc from '~root/my-wc.vue'
10+
window.customElements.define('my-wc', wrap(Vue, myWc))

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

Lines changed: 0 additions & 42 deletions
This file was deleted.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = (api, options) => {
1111
options: {
1212
'--mode': `specify env mode (default: ${defaults.mode})`,
1313
'--dest': `specify output directory (default: ${options.outputDir})`,
14-
'--target': `app | lib | wc | multi-wc (default: ${defaults.target})`,
14+
'--target': `app | lib | wc | wc-async (default: ${defaults.target})`,
1515
'--name': `name for lib or (multi-)web-component mode (default: "name" in package.json or entry filename)`
1616
}
1717
}, args => {
@@ -56,8 +56,9 @@ module.exports = (api, options) => {
5656
webpackConfig = require('./resolveLibConfig')(api, args, options)
5757
} else if (
5858
args.target === 'web-component' ||
59+
args.target === 'web-component-async' ||
5960
args.target === 'wc' ||
60-
args.target === 'multi-wc'
61+
args.target === 'wc-async'
6162
) {
6263
webpackConfig = require('./resolveWebComponentConfig')(api, args, options)
6364
} else {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,30 @@ module.exports = (api, { entry, name, dest }, options) => {
1515
const config = api.resolveChainableWebpackConfig()
1616

1717
config.entryPoints.clear()
18+
const entryName = `${libName}.${postfix}`
1819
// set proxy entry for *.vue files
1920
if (/\.vue$/.test(entry)) {
2021
config
21-
.entry(`${libName}.${postfix}`)
22+
.entry(entryName)
2223
.add(require.resolve('./entry-lib.js'))
2324
config.resolve
2425
.alias
2526
.set('~entry', api.resolve(entry))
2627
} else {
2728
config
28-
.entry(`${libName}.${postfix}`)
29+
.entry(entryName)
2930
.add(api.resolve(entry))
3031
}
3132

3233
config.output
3334
.path(api.resolve(dest))
34-
.filename(`[name].js`)
35+
.filename(`${entryName}.js`)
36+
.chunkFilename(`${entryName}.[id].js`)
3537
.library(libName)
3638
.libraryExport('default')
3739
.libraryTarget(format)
40+
// use relative publicPath so this can be deployed anywhere
41+
.publicPath('./')
3842

3943
// adjust css output name so they write to the same file
4044
if (options.css.extract !== false) {

0 commit comments

Comments
 (0)