webpack.config.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. 'modal-workflow',
  39. 'page-chooser-modal',
  40. 'page-chooser',
  41. 'page-chooser-telepath',
  42. 'page-editor',
  43. 'preview-panel',
  44. 'privacy-switch',
  45. 'sidebar',
  46. 'task-chooser-modal',
  47. 'task-chooser',
  48. 'telepath/blocks',
  49. 'telepath/telepath',
  50. 'telepath/widgets',
  51. 'userbar',
  52. 'wagtailadmin',
  53. 'workflow-action',
  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[
  108. getOutputPath('contrib/typed_table_block', 'css', 'typed_table_block')
  109. ] = path.resolve(
  110. 'wagtail',
  111. 'contrib',
  112. 'typed_table_block',
  113. 'static_src',
  114. 'typed_table_block',
  115. 'scss',
  116. 'typed_table_block.scss',
  117. );
  118. return {
  119. entry: {
  120. ...entry,
  121. ...sassEntry,
  122. },
  123. output: {
  124. path: path.resolve('.'),
  125. publicPath: '/static/',
  126. },
  127. resolve: {
  128. extensions: ['.ts', '.tsx', '.js'],
  129. // Some libraries import Node modules but don't use them in the browser.
  130. // Tell Webpack to provide empty mocks for them so importing them works.
  131. fallback: {
  132. fs: false,
  133. net: false,
  134. tls: false,
  135. },
  136. },
  137. externals: {
  138. jquery: 'jQuery',
  139. },
  140. plugins: [
  141. new MiniCssExtractPlugin({
  142. filename: '[name].css',
  143. }),
  144. new CopyPlugin({
  145. patterns: [
  146. {
  147. from: 'wagtail/admin/static_src/',
  148. to: 'wagtail/admin/static/',
  149. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  150. },
  151. {
  152. from: 'wagtail/documents/static_src/',
  153. to: 'wagtail/documents/static/',
  154. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  155. },
  156. {
  157. from: 'wagtail/embeds/static_src/',
  158. to: 'wagtail/embeds/static/',
  159. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  160. },
  161. {
  162. from: 'wagtail/images/static_src/',
  163. to: 'wagtail/images/static/',
  164. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  165. },
  166. {
  167. from: 'wagtail/contrib/search_promotions/static_src/',
  168. to: 'wagtail/contrib/search_promotions/static/',
  169. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  170. },
  171. {
  172. from: 'wagtail/users/static_src/',
  173. to: 'wagtail/users/static/',
  174. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  175. },
  176. {
  177. from: 'wagtail/contrib/modeladmin/static_src/',
  178. to: 'wagtail/contrib/modeladmin/static/',
  179. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  180. },
  181. ],
  182. }),
  183. ],
  184. module: {
  185. rules: [
  186. {
  187. test: /\.(js|ts)x?$/,
  188. loader: 'ts-loader',
  189. exclude: /node_modules/,
  190. },
  191. {
  192. // Legacy support for font icon loading, to be removed.
  193. test: /\.(woff)$/i,
  194. generator: {
  195. emit: false,
  196. filename: 'wagtailadmin/fonts/[name][ext]',
  197. },
  198. },
  199. {
  200. test: /\.(svg)$/i,
  201. type: 'asset/inline',
  202. },
  203. {
  204. test: /\.(scss|css)$/,
  205. use: [
  206. MiniCssExtractPlugin.loader,
  207. {
  208. loader: 'css-loader',
  209. options: {
  210. url: false,
  211. },
  212. },
  213. {
  214. loader: 'postcss-loader',
  215. options: {
  216. postcssOptions: {
  217. plugins: ['tailwindcss', 'autoprefixer', 'cssnano'],
  218. },
  219. },
  220. },
  221. {
  222. loader: 'sass-loader',
  223. options: {
  224. sassOptions: {
  225. // Manually set Sass output so it’s identical in production and development. See:
  226. // https://github.com/tailwindlabs/tailwindcss/issues/11027
  227. // https://github.com/webpack-contrib/sass-loader/issues/1129
  228. outputStyle: 'expanded',
  229. },
  230. },
  231. },
  232. ],
  233. },
  234. ].concat(
  235. Object.keys(exposedDependencies).map((name) => {
  236. const globalName = exposedDependencies[name];
  237. // Create expose-loader configs for each Wagtail dependency.
  238. return {
  239. test: require.resolve(name),
  240. use: [
  241. {
  242. loader: 'expose-loader',
  243. options: {
  244. exposes: {
  245. globalName,
  246. override: true,
  247. },
  248. },
  249. },
  250. ],
  251. };
  252. }),
  253. ),
  254. },
  255. optimization: {
  256. splitChunks: {
  257. cacheGroups: {
  258. vendor: {
  259. name: getOutputPath('admin', 'js', 'vendor'),
  260. chunks: 'initial',
  261. minChunks: 2,
  262. reuseExistingChunk: true,
  263. },
  264. },
  265. },
  266. },
  267. // See https://webpack.js.org/configuration/devtool/.
  268. devtool: isProduction ? false : 'eval-cheap-module-source-map',
  269. // For development mode only.
  270. watchOptions: {
  271. poll: 1000,
  272. aggregateTimeout: 300,
  273. },
  274. // Disable performance hints – currently there are much more valuable
  275. // optimizations for us to do outside of Webpack
  276. performance: {
  277. hints: false,
  278. },
  279. stats: {
  280. // Add chunk information (setting this to `false` allows for a less verbose output)
  281. chunks: false,
  282. // Add the hash of the compilation
  283. hash: false,
  284. // `webpack --colors` equivalent
  285. colors: true,
  286. // Add information about the reasons why modules are included
  287. reasons: false,
  288. // Add webpack version information
  289. version: false,
  290. },
  291. };
  292. };