webpack.config.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. const path = require('path');
  2. const CopyPlugin = require('copy-webpack-plugin');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  4. // Generates a path to the output bundle to be loaded in the browser.
  5. const getOutputPath = (app, folder, filename) => {
  6. const exceptions = {
  7. 'documents': 'wagtaildocs',
  8. 'contrib/table_block': 'table_block',
  9. 'contrib/typed_table_block': 'typed_table_block',
  10. 'contrib/styleguide': 'wagtailstyleguide',
  11. 'contrib/modeladmin': 'wagtailmodeladmin',
  12. }
  13. const appLabel = exceptions[app] || `wagtail${app}`;
  14. return path.join('wagtail', app, 'static', appLabel, folder, filename);
  15. };
  16. // Mapping from package name to exposed global variable.
  17. const exposedDependencies = {
  18. 'focus-trap-react': 'FocusTrapReact',
  19. 'react': 'React',
  20. 'react-dom': 'ReactDOM',
  21. 'react-transition-group/CSSTransitionGroup': 'CSSTransitionGroup',
  22. 'draft-js': 'DraftJS',
  23. };
  24. module.exports = function exports(env, argv) {
  25. const isProduction = argv.mode === 'production';
  26. const entrypoints = {
  27. 'admin': [
  28. 'collapsible',
  29. 'comments',
  30. 'core',
  31. 'date-time-chooser',
  32. 'draftail',
  33. 'expanding-formset',
  34. 'filtered-select',
  35. 'hallo-bootstrap',
  36. 'hallo-plugins/hallo-hr',
  37. 'hallo-plugins/hallo-requireparagraphs',
  38. 'hallo-plugins/hallo-wagtaillink',
  39. 'lock-unlock-action',
  40. 'modal-workflow',
  41. 'page-chooser-modal',
  42. 'page-chooser',
  43. 'page-editor',
  44. 'privacy-switch',
  45. 'sidebar',
  46. 'sidebar-legacy',
  47. 'task-chooser-modal',
  48. 'task-chooser',
  49. 'telepath/blocks',
  50. 'telepath/telepath',
  51. 'telepath/widgets',
  52. 'userbar',
  53. 'wagtailadmin',
  54. 'workflow-action',
  55. 'workflow-status',
  56. 'bulk-actions'
  57. ],
  58. 'images': [
  59. 'image-chooser',
  60. 'image-chooser-telepath',
  61. ],
  62. 'documents': [
  63. 'document-chooser',
  64. 'document-chooser-telepath',
  65. ],
  66. 'snippets': [
  67. 'snippet-chooser',
  68. 'snippet-chooser-telepath',
  69. ],
  70. 'contrib/table_block': [
  71. 'table',
  72. ],
  73. 'contrib/typed_table_block': [
  74. 'typed_table_block',
  75. ],
  76. };
  77. const entry = {};
  78. for (const [appName, moduleNames] of Object.entries(entrypoints)) {
  79. moduleNames.forEach(moduleName => {
  80. entry[moduleName] = {
  81. import: [`./client/src/entrypoints/${appName}/${moduleName}.js`],
  82. filename: getOutputPath(appName, 'js', moduleName) + '.js',
  83. };
  84. // Add polyfills to all bundles except userbar
  85. // polyfills.js imports from node_modules, which adds a dependency on vendor.js (produced by splitChunks)
  86. // Because userbar is supposed to run on peoples frontends, we code it using portable JS so we don't need
  87. // to pull in all the additional JS that the vendor bundle has (such as React).
  88. if (moduleName !== 'userbar') {
  89. entry[moduleName].import.push('./client/src/utils/polyfills.js');
  90. }
  91. });
  92. }
  93. const sassEntry = {};
  94. sassEntry[getOutputPath('admin', 'css', 'core')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'core.scss');
  95. sassEntry[getOutputPath('admin', 'css', 'layouts/404')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'layouts', '404.scss');
  96. sassEntry[getOutputPath('admin', 'css', 'layouts/account')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'layouts', 'account.scss');
  97. sassEntry[getOutputPath('admin', 'css', 'layouts/compare-revisions')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'layouts', 'compare-revisions.scss');
  98. sassEntry[getOutputPath('admin', 'css', 'layouts/home')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'layouts', 'home.scss');
  99. sassEntry[getOutputPath('admin', 'css', 'layouts/login')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'layouts', 'login.scss');
  100. sassEntry[getOutputPath('admin', 'css', 'layouts/page-editor')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'layouts', 'page-editor.scss');
  101. sassEntry[getOutputPath('admin', 'css', 'layouts/report')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'layouts', 'report.scss');
  102. sassEntry[getOutputPath('admin', 'css', 'layouts/workflow-edit')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'layouts', 'workflow-edit.scss');
  103. sassEntry[getOutputPath('admin', 'css', 'layouts/workflow-progress')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'layouts', 'workflow-progress.scss');
  104. // sassEntry[getOutputPath('admin', 'css', 'normalize')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'css', 'normalize.css');
  105. sassEntry[getOutputPath('admin', 'css', 'panels/draftail')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'panels', 'draftail.scss');
  106. sassEntry[getOutputPath('admin', 'css', 'panels/hallo')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'panels', 'hallo.scss');
  107. sassEntry[getOutputPath('admin', 'css', 'panels/streamfield')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'panels', 'streamfield.scss');
  108. sassEntry[getOutputPath('admin', 'css', 'sidebar')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'sidebar.scss');
  109. sassEntry[getOutputPath('admin', 'css', 'userbar')] = path.resolve('wagtail', 'admin', 'static_src', 'wagtailadmin', 'scss', 'userbar.scss');
  110. sassEntry[getOutputPath('documents', 'css', 'add-multiple')] = path.resolve('wagtail', 'documents', 'static_src', 'wagtaildocs', 'scss', 'add-multiple.scss');
  111. sassEntry[getOutputPath('images', 'css', 'add-multiple')] = path.resolve('wagtail', 'images', 'static_src', 'wagtailimages', 'scss', 'add-multiple.scss');
  112. sassEntry[getOutputPath('images', 'css', 'focal-point-chooser')] = path.resolve('wagtail', 'images', 'static_src', 'wagtailimages', 'scss', 'focal-point-chooser.scss');
  113. sassEntry[getOutputPath('users', 'css', 'groups_edit')] = path.resolve('wagtail', 'users', 'static_src', 'wagtailusers', 'scss', 'groups_edit.scss');
  114. sassEntry[getOutputPath('contrib/styleguide', 'css', 'styleguide')] = path.resolve('wagtail', 'contrib', 'styleguide', 'static_src', 'wagtailstyleguide', 'scss', 'styleguide.scss');
  115. sassEntry[getOutputPath('contrib/modeladmin', 'css', 'index')] = path.resolve('wagtail', 'contrib', 'modeladmin', 'static_src', 'wagtailmodeladmin', 'scss', 'index.scss');
  116. sassEntry[getOutputPath('contrib/modeladmin', 'css', 'breadcrumbs_page')] = path.resolve('wagtail', 'contrib', 'modeladmin', 'static_src', 'wagtailmodeladmin', 'scss', 'breadcrumbs_page.scss');
  117. sassEntry[getOutputPath('contrib/modeladmin', 'css', 'choose_parent_page')] = path.resolve('wagtail', 'contrib', 'modeladmin', 'static_src', 'wagtailmodeladmin', 'scss', 'choose_parent_page.scss');
  118. sassEntry[getOutputPath('contrib/typed_table_block', 'css', 'typed_table_block')] = path.resolve('wagtail', 'contrib', 'typed_table_block', 'static_src', 'typed_table_block', 'scss', 'typed_table_block.scss');
  119. return {
  120. entry: {
  121. ...entry,
  122. ...sassEntry,
  123. },
  124. output: {
  125. path: path.resolve('.'),
  126. publicPath: '/static/',
  127. },
  128. resolve: {
  129. extensions: ['.ts', '.tsx', '.js'],
  130. // Some libraries import Node modules but don't use them in the browser.
  131. // Tell Webpack to provide empty mocks for them so importing them works.
  132. fallback: {
  133. fs: false,
  134. net: false,
  135. tls: false,
  136. },
  137. },
  138. externals: {
  139. jquery: 'jQuery',
  140. },
  141. plugins: [
  142. new MiniCssExtractPlugin({
  143. filename: '[name].css',
  144. }),
  145. new CopyPlugin({
  146. patterns: [
  147. { from: 'wagtail/admin/static_src/', to: 'wagtail/admin/static/', globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] } },
  148. { from: 'wagtail/documents/static_src/', to: 'wagtail/documents/static/', globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] } },
  149. { from: 'wagtail/embeds/static_src/', to: 'wagtail/embeds/static/', globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] } },
  150. { from: 'wagtail/images/static_src/', to: 'wagtail/images/static/', globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] } },
  151. { from: 'wagtail/search/static_src/', to: 'wagtail/search/static/', globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] } },
  152. { from: 'wagtail/snippets/static_src/', to: 'wagtail/snippets/static/', globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] } },
  153. { from: 'wagtail/users/static_src/', to: 'wagtail/users/static/', globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] } },
  154. { from: 'wagtail/contrib/settings/static_src/', to: 'wagtail/contrib/settings/static/', globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] } },
  155. { from: 'wagtail/contrib/modeladmin/static_src/', to: 'wagtail/contrib/modeladmin/static/', globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] } },
  156. ],
  157. }),
  158. ],
  159. module: {
  160. rules: [
  161. {
  162. test: /\.(js|ts)x?$/,
  163. loader: 'ts-loader',
  164. exclude: /node_modules/,
  165. },
  166. {
  167. // Legacy support for font icon loading, to be removed.
  168. test: /\.(woff)$/i,
  169. generator: {
  170. emit: false,
  171. filename: 'wagtailadmin/fonts/[name][ext]',
  172. },
  173. },
  174. {
  175. test: /\.(svg)$/i,
  176. type: 'asset/inline',
  177. },
  178. {
  179. test: /\.(scss|css)$/,
  180. use: [
  181. MiniCssExtractPlugin.loader,
  182. 'css-loader',
  183. {
  184. loader: 'postcss-loader',
  185. options: {
  186. postcssOptions: {
  187. plugins: [
  188. "autoprefixer",
  189. "cssnano",
  190. ]
  191. }
  192. },
  193. },
  194. 'sass-loader'
  195. ],
  196. },
  197. ].concat(Object.keys(exposedDependencies).map((name) => {
  198. const globalName = exposedDependencies[name];
  199. // Create expose-loader configs for each Wagtail dependency.
  200. return {
  201. test: require.resolve(name),
  202. use: [
  203. {
  204. loader: 'expose-loader',
  205. options: {
  206. exposes: {
  207. globalName,
  208. override: true
  209. }
  210. },
  211. },
  212. ],
  213. };
  214. }))
  215. },
  216. optimization: {
  217. splitChunks: {
  218. cacheGroups: {
  219. vendor: {
  220. name: getOutputPath('admin', 'js', 'vendor'),
  221. chunks: 'initial',
  222. minChunks: 2,
  223. reuseExistingChunk: true,
  224. },
  225. },
  226. },
  227. },
  228. // See https://webpack.js.org/configuration/devtool/.
  229. devtool: isProduction ? false : 'eval-cheap-module-source-map',
  230. // For development mode only.
  231. watchOptions: {
  232. poll: 1000,
  233. aggregateTimeout: 300,
  234. },
  235. // Disable performance hints – currently there are much more valuable
  236. // optimizations for us to do outside of Webpack
  237. performance: {
  238. hints: false
  239. },
  240. stats: {
  241. // Add chunk information (setting this to `false` allows for a less verbose output)
  242. chunks: false,
  243. // Add the hash of the compilation
  244. hash: false,
  245. // `webpack --colors` equivalent
  246. colors: true,
  247. // Add information about the reasons why modules are included
  248. reasons: false,
  249. // Add webpack version information
  250. version: false,
  251. },
  252. };
  253. };