1
0

webpack.common.js 4.4 KB

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