webpack.common.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* eslint-disable camelcase */
  2. const { VueLoaderPlugin } = require('vue-loader')
  3. const path = require('path')
  4. const BabelLoaderExcludeNodeModulesExcept = require('babel-loader-exclude-node-modules-except')
  5. const webpack = require('webpack')
  6. const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
  7. const modules = require('./webpack.modules.js')
  8. const formatOutputFromModules = (modules) => {
  9. // merge all configs into one object, and use AppID to generate the fileNames
  10. // with the following format:
  11. // AppId-fileName: path/to/js-file.js
  12. const moduleEntries = Object.keys(modules).map(moduleKey => {
  13. const module = modules[moduleKey]
  14. const entries = Object.keys(module).map(entryKey => {
  15. const entry = module[entryKey]
  16. return { [`${moduleKey}-${entryKey}`]: entry }
  17. })
  18. return Object.assign({}, ...Object.values(entries))
  19. })
  20. return Object.assign({}, ...Object.values(moduleEntries))
  21. }
  22. const modulesToBuild = () => {
  23. const MODULE = process.env.MODULE
  24. if (MODULE) {
  25. if (!modules[MODULE]) {
  26. throw new Error(`No module "${MODULE}" found`)
  27. }
  28. return formatOutputFromModules({
  29. [MODULE]: modules[MODULE],
  30. })
  31. }
  32. return formatOutputFromModules(modules)
  33. }
  34. module.exports = {
  35. entry: modulesToBuild(),
  36. output: {
  37. // Step away from the src folder and extract to the js folder
  38. path: path.join(__dirname, 'dist'),
  39. // Let webpack determine automatically where it's located
  40. publicPath: 'auto',
  41. filename: '[name].js?v=[contenthash]',
  42. chunkFilename: '[name]-[id].js?v=[contenthash]',
  43. // Make sure sourcemaps have a proper path and do not
  44. // leak local paths https://github.com/webpack/webpack/issues/3603
  45. devtoolNamespace: 'nextcloud',
  46. devtoolModuleFilenameTemplate(info) {
  47. const rootDir = process.cwd()
  48. const rel = path.relative(rootDir, info.absoluteResourcePath)
  49. return `webpack:///nextcloud/${rel}`
  50. },
  51. clean: {
  52. keep: /icons\.css/, // Keep static icons css
  53. },
  54. },
  55. module: {
  56. rules: [
  57. {
  58. test: /davclient/,
  59. loader: 'exports-loader',
  60. options: {
  61. type: 'commonjs',
  62. exports: 'dav',
  63. },
  64. },
  65. {
  66. test: /\.css$/,
  67. use: ['style-loader', 'css-loader'],
  68. },
  69. {
  70. test: /\.scss$/,
  71. use: ['style-loader', 'css-loader', 'sass-loader'],
  72. },
  73. {
  74. test: /\.vue$/,
  75. loader: 'vue-loader',
  76. exclude: BabelLoaderExcludeNodeModulesExcept([
  77. 'vue-material-design-icons',
  78. 'emoji-mart-vue-fast',
  79. ]),
  80. },
  81. {
  82. test: /\.js$/,
  83. loader: 'babel-loader',
  84. // automatically detect necessary packages to
  85. // transpile in the node_modules folder
  86. exclude: BabelLoaderExcludeNodeModulesExcept([
  87. '@nextcloud/dialogs',
  88. '@nextcloud/event-bus',
  89. '@nextcloud/vue-dashboard',
  90. 'davclient.js',
  91. 'nextcloud-vue-collections',
  92. 'p-finally',
  93. 'p-limit',
  94. 'p-locate',
  95. 'p-queue',
  96. 'p-timeout',
  97. 'p-try',
  98. 'semver',
  99. 'striptags',
  100. 'toastify-js',
  101. 'v-tooltip',
  102. 'yocto-queue',
  103. ]),
  104. },
  105. {
  106. test: /\.(png|jpe?g|gif|svg|woff2?|eot|ttf)$/,
  107. type: 'asset/inline',
  108. },
  109. {
  110. test: /\.handlebars/,
  111. loader: 'handlebars-loader',
  112. },
  113. ],
  114. },
  115. optimization: {
  116. splitChunks: {
  117. automaticNameDelimiter: '-',
  118. cacheGroups: {
  119. vendors: {
  120. // split every dependency into one bundle
  121. test: /[\\/]node_modules[\\/]/,
  122. enforce: true,
  123. // necessary to keep this name to properly inject it
  124. // see OC_Template.php
  125. name: 'core-common',
  126. chunks: 'all',
  127. },
  128. },
  129. },
  130. },
  131. plugins: [
  132. new VueLoaderPlugin(),
  133. new NodePolyfillPlugin(),
  134. new webpack.ProvidePlugin({
  135. // Provide jQuery to jquery plugins as some are loaded before $ is exposed globally.
  136. // We need to provide the path to node_moduels as otherwise npm link will fail due
  137. // to tribute.js checking for jQuery in @nextcloud/vue
  138. jQuery: path.resolve(path.join(__dirname, 'node_modules/jquery')),
  139. // Shim ICAL to prevent using the global object (window.ICAL).
  140. // The library ical.js heavily depends on instanceof checks which will
  141. // break if two separate versions of the library are used (e.g. bundled one
  142. // and global one).
  143. ICAL: 'ical.js',
  144. }),
  145. ],
  146. resolve: {
  147. alias: {
  148. // make sure to use the handlebar runtime when importing
  149. handlebars: 'handlebars/runtime',
  150. },
  151. extensions: ['*', '.js', '.vue'],
  152. symlinks: true,
  153. fallback: {
  154. assert: false,
  155. },
  156. },
  157. }