1
0

webpack.common.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. },
  114. optimization: {
  115. splitChunks: {
  116. automaticNameDelimiter: '-',
  117. cacheGroups: {
  118. vendors: {
  119. // split every dependency into one bundle
  120. test: /[\\/]node_modules[\\/]/,
  121. enforce: true,
  122. // necessary to keep this name to properly inject it
  123. // see OC_Template.php
  124. name: 'core-common',
  125. chunks: 'all',
  126. },
  127. },
  128. },
  129. },
  130. plugins: [
  131. new VueLoaderPlugin(),
  132. new webpack.ProvidePlugin({
  133. // Provide jQuery to jquery plugins as some are loaded before $ is exposed globally.
  134. jQuery: 'jquery',
  135. // Shim ICAL to prevent using the global object (window.ICAL).
  136. // The library ical.js heavily depends on instanceof checks which will
  137. // break if two separate versions of the library are used (e.g. bundled one
  138. // and global one).
  139. ICAL: 'ical.js',
  140. }),
  141. ],
  142. resolve: {
  143. alias: {
  144. // make sure to use the handlebar runtime when importing
  145. handlebars: 'handlebars/runtime',
  146. },
  147. extensions: ['*', '.js', '.vue'],
  148. symlinks: false,
  149. fallback: {
  150. stream: require.resolve('stream-browserify'),
  151. buffer: require.resolve('buffer'),
  152. },
  153. },
  154. }