webpack.config.js 8.4 KB

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