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

feat(build): honor custom component name for single file wc builds #1182

Merged
merged 1 commit into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 8 additions & 4 deletions packages/@vue/cli-service/lib/commands/build/resolveWcConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = (api, { target, entry, name }) => {

// generate dynamic entry based on glob files
const resolvedFiles = require('globby').sync([entry], { cwd: api.resolve('.') })

if (!resolvedFiles.length) {
abort(`entry pattern "${entry}" did not match any files.`)
}
Expand All @@ -38,7 +39,7 @@ module.exports = (api, { target, entry, name }) => {
}
}

const dynamicEntry = resolveEntry(prefix, resolvedFiles, isAsync)
const dynamicEntry = resolveEntry(prefix, libName, resolvedFiles, isAsync)

function genConfig (minify, genHTML) {
const config = api.resolveChainableWebpackConfig()
Expand Down Expand Up @@ -104,9 +105,12 @@ module.exports = (api, { target, entry, name }) => {
inject: false,
filename: 'demo.html',
libName,
components: resolvedFiles.map(file => {
return fileToComponentName(prefix, file).kebabName
})
components:
prefix === ''
? [libName]
: resolvedFiles.map(file => {
return fileToComponentName(prefix, file).kebabName
})
}])
}

Expand Down
33 changes: 23 additions & 10 deletions packages/@vue/cli-service/lib/commands/build/resolveWcEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ const hyphenate = str => {
return str.replace(hyphenateRE, '-$1').toLowerCase()
}

/**
* Creates the script to add the component to the custom elements
* @param {string} prefix The prefix for the component library
* @param {string} component The component name for single entry builds, component file for multi-entry builds
* @param {string} file The file for the component
* @param {boolean} async Whether to load component async or not
*/
const createElement = (prefix, component, file, async) => {
const { camelName, kebabName } = exports.fileToComponentName(prefix, component)

return async
? `window.customElements.define('${kebabName}', wrap(Vue, () => import('~root/${file}')))\n`
: `import ${camelName} from '~root/${file}'\n` +
`window.customElements.define('${kebabName}', wrap(Vue, ${camelName}))\n`
}

exports.fileToComponentName = (prefix, file) => {
const basename = path.basename(file).replace(/\.(jsx?|vue)$/, '')
const camelName = camelize(basename)
Expand All @@ -22,8 +38,13 @@ exports.fileToComponentName = (prefix, file) => {
}
}

exports.resolveEntry = (prefix, files, async) => {
exports.resolveEntry = (prefix, libName, files, async) => {
const filePath = path.resolve(__dirname, 'entry-wc.js')
const elements =
prefix === ''
? [createElement('', libName, files[0])]
: files.map(file => createElement(prefix, file, file, async)).join('\n')

const content = `
import Vue from 'vue'
import wrap from '@vue/web-component-wrapper'
Expand All @@ -40,15 +61,7 @@ import 'vue-loader/lib/runtime/component-normalizer'
}
})()

${files.map(file => {
const { camelName, kebabName } = exports.fileToComponentName(prefix, file)
return async
? `window.customElements.define('${kebabName}', wrap(Vue, () => import('~root/${file}')))\n`
: (
`import ${camelName} from '~root/${file}'\n` +
`window.customElements.define('${kebabName}', wrap(Vue, ${camelName}))\n`
)
}).join('\n')}`.trim()
${elements}`.trim()
fs.writeFileSync(filePath, content)
return filePath
}