webpack.config.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. 'chooser-modal',
  29. 'chooser-widget',
  30. 'chooser-widget-telepath',
  31. 'comments',
  32. 'core',
  33. 'date-time-chooser',
  34. 'draftail',
  35. 'expanding-formset',
  36. 'filtered-select',
  37. 'icons',
  38. 'lock-unlock-action',
  39. 'modal-workflow',
  40. 'page-chooser-modal',
  41. 'page-chooser',
  42. 'page-editor',
  43. 'preview-panel',
  44. 'privacy-switch',
  45. 'schedule-publishing',
  46. 'sidebar',
  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-modal',
  61. 'image-chooser-telepath',
  62. ],
  63. 'documents': [
  64. 'document-chooser',
  65. 'document-chooser-modal',
  66. 'document-chooser-telepath',
  67. ],
  68. 'snippets': ['snippet-chooser', 'snippet-chooser-telepath'],
  69. 'contrib/table_block': ['table'],
  70. 'contrib/typed_table_block': ['typed_table_block'],
  71. };
  72. const entry = {};
  73. // eslint-disable-next-line no-restricted-syntax
  74. for (const [appName, moduleNames] of Object.entries(entrypoints)) {
  75. moduleNames.forEach((moduleName) => {
  76. entry[moduleName] = {
  77. import: [`./client/src/entrypoints/${appName}/${moduleName}.js`],
  78. filename: getOutputPath(appName, 'js', moduleName) + '.js',
  79. };
  80. });
  81. }
  82. const sassEntry = {};
  83. sassEntry[getOutputPath('admin', 'css', 'core')] = path.resolve(
  84. 'wagtail',
  85. 'admin',
  86. 'static_src',
  87. 'wagtailadmin',
  88. 'scss',
  89. 'core.scss',
  90. );
  91. sassEntry[getOutputPath('admin', 'css', 'panels/draftail')] = path.resolve(
  92. 'wagtail',
  93. 'admin',
  94. 'static_src',
  95. 'wagtailadmin',
  96. 'scss',
  97. 'panels',
  98. 'draftail.scss',
  99. );
  100. sassEntry[getOutputPath('admin', 'css', 'panels/streamfield')] = path.resolve(
  101. 'wagtail',
  102. 'admin',
  103. 'static_src',
  104. 'wagtailadmin',
  105. 'scss',
  106. 'panels',
  107. 'streamfield.scss',
  108. );
  109. sassEntry[getOutputPath('contrib/styleguide', 'css', 'styleguide')] =
  110. path.resolve(
  111. 'wagtail',
  112. 'contrib',
  113. 'styleguide',
  114. 'static_src',
  115. 'wagtailstyleguide',
  116. 'scss',
  117. 'styleguide.scss',
  118. );
  119. sassEntry[
  120. getOutputPath('contrib/typed_table_block', 'css', 'typed_table_block')
  121. ] = path.resolve(
  122. 'wagtail',
  123. 'contrib',
  124. 'typed_table_block',
  125. 'static_src',
  126. 'typed_table_block',
  127. 'scss',
  128. 'typed_table_block.scss',
  129. );
  130. return {
  131. entry: {
  132. ...entry,
  133. ...sassEntry,
  134. },
  135. output: {
  136. path: path.resolve('.'),
  137. publicPath: '/static/',
  138. },
  139. resolve: {
  140. extensions: ['.ts', '.tsx', '.js'],
  141. // Some libraries import Node modules but don't use them in the browser.
  142. // Tell Webpack to provide empty mocks for them so importing them works.
  143. fallback: {
  144. fs: false,
  145. net: false,
  146. tls: false,
  147. },
  148. },
  149. externals: {
  150. jquery: 'jQuery',
  151. },
  152. plugins: [
  153. new MiniCssExtractPlugin({
  154. filename: '[name].css',
  155. }),
  156. new CopyPlugin({
  157. patterns: [
  158. {
  159. from: 'wagtail/admin/static_src/',
  160. to: 'wagtail/admin/static/',
  161. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  162. },
  163. {
  164. from: 'wagtail/documents/static_src/',
  165. to: 'wagtail/documents/static/',
  166. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  167. },
  168. {
  169. from: 'wagtail/embeds/static_src/',
  170. to: 'wagtail/embeds/static/',
  171. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  172. },
  173. {
  174. from: 'wagtail/images/static_src/',
  175. to: 'wagtail/images/static/',
  176. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  177. },
  178. {
  179. from: 'wagtail/search/static_src/',
  180. to: 'wagtail/search/static/',
  181. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  182. },
  183. {
  184. from: 'wagtail/users/static_src/',
  185. to: 'wagtail/users/static/',
  186. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  187. },
  188. {
  189. from: 'wagtail/contrib/settings/static_src/',
  190. to: 'wagtail/contrib/settings/static/',
  191. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  192. },
  193. {
  194. from: 'wagtail/contrib/modeladmin/static_src/',
  195. to: 'wagtail/contrib/modeladmin/static/',
  196. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  197. },
  198. ],
  199. }),
  200. ],
  201. module: {
  202. rules: [
  203. {
  204. test: /\.(js|ts)x?$/,
  205. loader: 'ts-loader',
  206. exclude: /node_modules/,
  207. },
  208. {
  209. // Legacy support for font icon loading, to be removed.
  210. test: /\.(woff)$/i,
  211. generator: {
  212. emit: false,
  213. filename: 'wagtailadmin/fonts/[name][ext]',
  214. },
  215. },
  216. {
  217. test: /\.(svg)$/i,
  218. type: 'asset/inline',
  219. },
  220. {
  221. test: /\.(scss|css)$/,
  222. use: [
  223. MiniCssExtractPlugin.loader,
  224. {
  225. loader: 'css-loader',
  226. options: {
  227. url: false,
  228. },
  229. },
  230. {
  231. loader: 'postcss-loader',
  232. options: {
  233. postcssOptions: {
  234. plugins: ['tailwindcss', 'autoprefixer', 'cssnano'],
  235. },
  236. },
  237. },
  238. 'sass-loader',
  239. ],
  240. },
  241. ].concat(
  242. Object.keys(exposedDependencies).map((name) => {
  243. const globalName = exposedDependencies[name];
  244. // Create expose-loader configs for each Wagtail dependency.
  245. return {
  246. test: require.resolve(name),
  247. use: [
  248. {
  249. loader: 'expose-loader',
  250. options: {
  251. exposes: {
  252. globalName,
  253. override: true,
  254. },
  255. },
  256. },
  257. ],
  258. };
  259. }),
  260. ),
  261. },
  262. optimization: {
  263. splitChunks: {
  264. cacheGroups: {
  265. vendor: {
  266. name: getOutputPath('admin', 'js', 'vendor'),
  267. chunks: 'initial',
  268. minChunks: 2,
  269. reuseExistingChunk: true,
  270. },
  271. },
  272. },
  273. },
  274. // See https://webpack.js.org/configuration/devtool/.
  275. devtool: isProduction ? false : 'eval-cheap-module-source-map',
  276. // For development mode only.
  277. watchOptions: {
  278. poll: 1000,
  279. aggregateTimeout: 300,
  280. },
  281. // Disable performance hints – currently there are much more valuable
  282. // optimizations for us to do outside of Webpack
  283. performance: {
  284. hints: false,
  285. },
  286. stats: {
  287. // Add chunk information (setting this to `false` allows for a less verbose output)
  288. chunks: false,
  289. // Add the hash of the compilation
  290. hash: false,
  291. // `webpack --colors` equivalent
  292. colors: true,
  293. // Add information about the reasons why modules are included
  294. reasons: false,
  295. // Add webpack version information
  296. version: false,
  297. },
  298. };
  299. };