@@ -11,6 +11,22 @@ const hyphenate = str => {
11
11
return str . replace ( hyphenateRE , '-$1' ) . toLowerCase ( )
12
12
}
13
13
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
+
14
30
exports . fileToComponentName = ( prefix , file ) => {
15
31
const basename = path . basename ( file ) . replace ( / \. ( j s x ? | v u e ) $ / , '' )
16
32
const camelName = camelize ( basename )
@@ -22,8 +38,13 @@ exports.fileToComponentName = (prefix, file) => {
22
38
}
23
39
}
24
40
25
- exports . resolveEntry = ( prefix , files , async ) => {
41
+ exports . resolveEntry = ( prefix , libName , files , async ) => {
26
42
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
+
27
48
const content = `
28
49
import Vue from 'vue'
29
50
import wrap from '@vue/web-component-wrapper'
@@ -40,15 +61,7 @@ import 'vue-loader/lib/runtime/component-normalizer'
40
61
}
41
62
})()
42
63
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 ( )
52
65
fs . writeFileSync ( filePath , content )
53
66
return filePath
54
67
}
0 commit comments