webpack.config.js 8.4 KB

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