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 ESLintPlugin = require('eslint-webpack-plugin')
  6. const webpack = require('webpack')
  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: true,
  52. },
  53. module: {
  54. rules: [
  55. {
  56. test: /davclient/,
  57. loader: 'exports-loader',
  58. options: {
  59. type: 'commonjs',
  60. exports: 'dav',
  61. },
  62. },
  63. {
  64. test: /\.css$/,
  65. use: ['style-loader', 'css-loader'],
  66. },
  67. {
  68. test: /\.scss$/,
  69. use: ['style-loader', 'css-loader', 'sass-loader'],
  70. },
  71. {
  72. test: /\.vue$/,
  73. loader: 'vue-loader',
  74. exclude: BabelLoaderExcludeNodeModulesExcept([
  75. 'vue-material-design-icons',
  76. 'emoji-mart-vue-fast',
  77. ]),
  78. },
  79. {
  80. test: /\.js$/,
  81. loader: 'babel-loader',
  82. // automatically detect necessary packages to
  83. // transpile in the node_modules folder
  84. exclude: BabelLoaderExcludeNodeModulesExcept([
  85. '@nextcloud/dialogs',
  86. '@nextcloud/event-bus',
  87. '@nextcloud/vue-dashboard',
  88. 'davclient.js',
  89. 'nextcloud-vue-collections',
  90. 'p-finally',
  91. 'p-limit',
  92. 'p-locate',
  93. 'p-queue',
  94. 'p-timeout',
  95. 'p-try',
  96. 'semver',
  97. 'striptags',
  98. 'toastify-js',
  99. 'v-tooltip',
  100. 'yocto-queue',
  101. ]),
  102. },
  103. {
  104. test: /\.(png|jpe?g|gif|svg|woff2?|eot|ttf)$/,
  105. type: 'asset/inline',
  106. },
  107. {
  108. test: /\.handlebars/,
  109. loader: 'handlebars-loader',
  110. },
  111. ],
  112. },
  113. optimization: {
  114. splitChunks: {
  115. automaticNameDelimiter: '-',
  116. cacheGroups: {
  117. vendors: {
  118. // split every dependency into one bundle
  119. test: /[\\/]node_modules[\\/]/,
  120. enforce: true,
  121. // necessary to keep this name to properly inject it
  122. // see OC_Template.php
  123. name: 'core-common',
  124. chunks: 'all',
  125. },
  126. },
  127. },
  128. },
  129. plugins: [
  130. new VueLoaderPlugin(),
  131. new ESLintPlugin(),
  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. }