@@ -11,6 +11,22 @@ const hyphenate = str => {
1111 return str . replace ( hyphenateRE , '-$1' ) . toLowerCase ( )
1212}
1313
14+ /**
15+ * Creates the script to add the component to the custom elements
16+ * @param {string } prefix The prefix for the component library
17+ * @param {string } component The component name for single entry builds, component file for multi-entry builds
18+ * @param {string } file The file for the component
19+ * @param {boolean } async Whether to load component async or not
20+ */
21+ const createElement = ( prefix , component , file , async ) => {
22+ const { camelName, kebabName } = exports . fileToComponentName ( prefix , component )
23+
24+ return async
25+ ? `window.customElements.define('${ kebabName } ', wrap(Vue, () => import('~root/${ file } ')))\n`
26+ : `import ${ camelName } from '~root/${ file } '\n` +
27+ `window.customElements.define('${ kebabName } ', wrap(Vue, ${ camelName } ))\n`
28+ }
29+
1430exports . fileToComponentName = ( prefix , file ) => {
1531 const basename = path . basename ( file ) . replace ( / \. ( j s x ? | v u e ) $ / , '' )
1632 const camelName = camelize ( basename )
@@ -22,8 +38,13 @@ exports.fileToComponentName = (prefix, file) => {
2238 }
2339}
2440
25- exports . resolveEntry = ( prefix , files , async ) => {
41+ exports . resolveEntry = ( prefix , libName , files , async ) => {
2642 const filePath = path . resolve ( __dirname , 'entry-wc.js' )
43+ const elements =
44+ prefix === ''
45+ ? [ createElement ( '' , libName , files [ 0 ] ) ]
46+ : files . map ( file => createElement ( prefix , file , file , async ) ) . join ( '\n' )
47+
2748 const content = `
2849import Vue from 'vue'
2950import wrap from '@vue/web-component-wrapper'
@@ -40,15 +61,7 @@ import 'vue-loader/lib/runtime/component-normalizer'
4061 }
4162})()
4263
43- ${ files . map ( file => {
44- const { camelName, kebabName } = exports . fileToComponentName ( prefix , file )
45- return async
46- ? `window.customElements.define('${ kebabName } ', wrap(Vue, () => import('~root/${ file } ')))\n`
47- : (
48- `import ${ camelName } from '~root/${ file } '\n` +
49- `window.customElements.define('${ kebabName } ', wrap(Vue, ${ camelName } ))\n`
50- )
51- } ) . join ( '\n' ) } `. trim ( )
64+ ${ elements } `. trim ( )
5265 fs . writeFileSync ( filePath , content )
5366 return filePath
5467}
0 commit comments