Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --inline-vue flag for build command to avoid externalization of Vue in lib & wc mode #4261

Merged
merged 5 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(build): Also --inline-vue flag for build when target is wc
  • Loading branch information
romansp committed Aug 16, 2019
commit 9c8b3d6a0180368fcc7c63ac0596983e1058bdaa
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/__tests__/buildLib.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ test('build as lib without --name and --filename options (default to package nam
})

test('build as lib with --inline-vue', async () => {
const project = await create('build-lib-keep-vue-instance', defaultPreset)
const project = await create('build-lib-inline-vue', defaultPreset)

await project.write('src/main-lib.js', `
import Vue from 'vue'
Expand Down
55 changes: 55 additions & 0 deletions packages/@vue/cli-service/__tests__/buildWc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,61 @@ test('build as single wc', async () => {
expect(h1Text).toMatch('Welcome to Your Vue.js App')
})

test('build as wc with --inline-vue', async () => {
const project = await create('build-wc-inline-vue', defaultPreset)

await project.write('src/main-wc.js', `
import Vue from 'vue'
import App from "./components/App.vue"
document.addEventListener("DOMContentLoaded", function() {
new Vue({
render: h => h(App),
}).$mount('body');
});
`)

await project.write('src/components/App.vue', `
<template>
<div>{{ message }}<div>
</template>
<script>
export default {
data() {
return {
message: 'Hello from Wc'
}
},
}
</script>
`)

const { stdout } = await project.run('vue-cli-service build --target wc --inline-vue --name single-wc src/main-wc.js')
expect(stdout).toMatch('Build complete.')

expect(project.has('dist/demo.html')).toBe(true)
expect(project.has('dist/single-wc.js')).toBe(true)
expect(project.has('dist/single-wc.min.js')).toBe(true)

const port = await portfinder.getPortPromise()
server = createServer({ root: path.join(project.dir, 'dist') })

await new Promise((resolve, reject) => {
server.listen(port, err => {
if (err) return reject(err)
resolve()
})
})

const launched = await launchPuppeteer(`http://localhost:${port}/demo.html`)
browser = launched.browser
page = launched.page
const divText = await page.evaluate(() => {
return document.querySelector('div').textContent
})
expect(divText).toMatch('Hello from Wc')
})

afterEach(async () => {
if (browser) {
await browser.close()
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/lib/commands/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = (api, options) => {
'--modern': `build app targeting modern browsers with auto fallback`,
'--no-unsafe-inline': `build app without introducing inline scripts`,
'--target': `app | lib | wc | wc-async (default: ${defaults.target})`,
'--inline-vue': 'disable externalization of Vue for lib mode',
'--inline-vue': 'include the Vue module in the final bundle of library or web component target',
'--formats': `list of output formats for library builds (default: ${defaults.formats})`,
'--name': `name for lib or web-component mode (default: "name" in package.json or entry filename)`,
'--filename': `file name for output, only usable for 'lib' target (default: value of --name)`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path')
const webpack = require('webpack')
const { resolveEntry, fileToComponentName } = require('./resolveWcEntry')

module.exports = (api, { target, entry, name }) => {
module.exports = (api, { target, entry, name, 'inline-vue': inlineVue }) => {
// Disable CSS extraction and turn on CSS shadow mode for vue-style-loader
process.env.VUE_CLI_CSS_SHADOW_MODE = true

Expand Down Expand Up @@ -100,7 +100,9 @@ module.exports = (api, { target, entry, name }) => {
rawConfig.externals = [
...(Array.isArray(rawConfig.externals) ? rawConfig.externals : [rawConfig.externals]),
{
vue: 'Vue'
...(!inlineVue ? {
vue: 'Vue'
} : {})
romansp marked this conversation as resolved.
Show resolved Hide resolved
}
].filter(Boolean)

Expand Down