resolveWcConfig.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const path = require('path')
  2. const { resolveEntry, fileToComponentName } = require('./resolveWcEntry')
  3. module.exports = (api, { target, entry, name, 'inline-vue': inlineVue }) => {
  4. // Disable CSS extraction and turn on CSS shadow mode for vue-style-loader
  5. process.env.VUE_CLI_CSS_SHADOW_MODE = true
  6. const { log, error } = require('@vue/cli-shared-utils')
  7. const abort = msg => {
  8. log()
  9. error(msg)
  10. process.exit(1)
  11. }
  12. const isAsync = /async/.test(target)
  13. // generate dynamic entry based on glob files
  14. const resolvedFiles = require('globby').sync([entry], { cwd: api.resolve('.') })
  15. if (!resolvedFiles.length) {
  16. abort(`entry pattern "${entry}" did not match any files.`)
  17. }
  18. let libName
  19. let prefix
  20. if (resolvedFiles.length === 1) {
  21. // in single mode, determine the lib name from filename
  22. libName = name || fileToComponentName('', resolvedFiles[0]).kebabName
  23. prefix = ''
  24. if (libName.indexOf('-') < 0) {
  25. abort(`--name must contain a hyphen when building a single web component.`)
  26. }
  27. } else {
  28. // multi mode
  29. libName = prefix = (name || api.service.pkg.name)
  30. if (!libName) {
  31. abort(`--name is required when building multiple web components.`)
  32. }
  33. }
  34. const dynamicEntry = resolveEntry(prefix, libName, resolvedFiles, isAsync)
  35. function genConfig (minify, genHTML) {
  36. const config = api.resolveChainableWebpackConfig()
  37. // make sure not to transpile wc-wrapper
  38. config.module
  39. .rule('js')
  40. .exclude
  41. .add(/vue-wc-wrapper/)
  42. // only minify min entry
  43. if (!minify) {
  44. config.optimization.minimize(false)
  45. }
  46. config
  47. .plugin('web-component-options')
  48. .use(require('webpack/lib/DefinePlugin'), [{
  49. 'process.env': {
  50. CUSTOM_ELEMENT_NAME: JSON.stringify(libName)
  51. }
  52. }])
  53. // enable shadow mode in vue-loader
  54. config.module
  55. .rule('vue')
  56. .use('vue-loader')
  57. .tap(options => {
  58. options.shadowMode = true
  59. return options
  60. })
  61. if (genHTML) {
  62. config
  63. .plugin('demo-html')
  64. .use(require('html-webpack-plugin'), [{
  65. template: path.resolve(__dirname, `./demo-wc.html`),
  66. inject: false,
  67. filename: 'demo.html',
  68. libName,
  69. components:
  70. prefix === ''
  71. ? [libName]
  72. : resolvedFiles.map(file => {
  73. return fileToComponentName(prefix, file).kebabName
  74. })
  75. }])
  76. }
  77. // set entry/output last so it takes higher priority than user
  78. // configureWebpack hooks
  79. // set proxy entry for *.vue files
  80. config.resolve
  81. .alias
  82. .set('~root', api.resolve('.'))
  83. const rawConfig = api.resolveWebpackConfig(config)
  84. // externalize Vue in case user imports it
  85. rawConfig.externals = [
  86. ...(Array.isArray(rawConfig.externals) ? rawConfig.externals : [rawConfig.externals]),
  87. { ...(inlineVue || { vue: 'Vue' }) }
  88. ].filter(Boolean)
  89. const entryName = `${libName}${minify ? `.min` : ``}`
  90. rawConfig.entry = {
  91. [entryName]: dynamicEntry
  92. }
  93. Object.assign(rawConfig.output, {
  94. // to ensure that multiple copies of async wc bundles can co-exist
  95. // on the same page.
  96. jsonpFunction: libName.replace(/-\w/g, c => c.charAt(1).toUpperCase()) + '_jsonp',
  97. filename: `${entryName}.js`,
  98. chunkFilename: `${libName}.[name]${minify ? `.min` : ``}.js`,
  99. // use dynamic publicPath so this can be deployed anywhere
  100. // the actual path will be determined at runtime by checking
  101. // document.currentScript.src.
  102. publicPath: ''
  103. })
  104. return rawConfig
  105. }
  106. return [
  107. genConfig(false, true),
  108. genConfig(true, false)
  109. ]
  110. }