webpack.config.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. };
  12. const appLabel = exceptions[app] || `wagtail${app}`;
  13. return path.join('wagtail', app, 'static', appLabel, folder, filename);
  14. };
  15. // Mapping from package name to exposed global variable.
  16. const exposedDependencies = {
  17. 'focus-trap-react': 'FocusTrapReact',
  18. 'react': 'React',
  19. 'react-dom': 'ReactDOM',
  20. 'react-transition-group/CSSTransitionGroup': 'CSSTransitionGroup',
  21. 'draft-js': 'DraftJS',
  22. };
  23. module.exports = function exports(env, argv) {
  24. const isProduction = argv.mode === 'production';
  25. const entrypoints = {
  26. 'admin': [
  27. 'chooser-modal',
  28. 'chooser-widget',
  29. 'chooser-widget-telepath',
  30. 'comments',
  31. 'core',
  32. 'date-time-chooser',
  33. 'draftail',
  34. 'expanding-formset',
  35. 'filtered-select',
  36. 'icons',
  37. 'modal-workflow',
  38. 'page-chooser-modal',
  39. 'page-chooser',
  40. 'page-chooser-telepath',
  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. '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[
  107. getOutputPath('contrib/typed_table_block', 'css', 'typed_table_block')
  108. ] = path.resolve(
  109. 'wagtail',
  110. 'contrib',
  111. 'typed_table_block',
  112. 'static_src',
  113. 'typed_table_block',
  114. 'scss',
  115. 'typed_table_block.scss',
  116. );
  117. return {
  118. entry: {
  119. ...entry,
  120. ...sassEntry,
  121. },
  122. output: {
  123. path: path.resolve('.'),
  124. publicPath: '/static/',
  125. },
  126. resolve: {
  127. extensions: ['.ts', '.tsx', '.js'],
  128. // Some libraries import Node modules but don't use them in the browser.
  129. // Tell Webpack to provide empty mocks for them so importing them works.
  130. fallback: {
  131. fs: false,
  132. net: false,
  133. tls: false,
  134. },
  135. },
  136. externals: {
  137. jquery: 'jQuery',
  138. },
  139. plugins: [
  140. new MiniCssExtractPlugin({
  141. filename: '[name].css',
  142. }),
  143. new CopyPlugin({
  144. patterns: [
  145. {
  146. from: 'wagtail/admin/static_src/',
  147. to: 'wagtail/admin/static/',
  148. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  149. },
  150. {
  151. from: 'wagtail/documents/static_src/',
  152. to: 'wagtail/documents/static/',
  153. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  154. },
  155. {
  156. from: 'wagtail/embeds/static_src/',
  157. to: 'wagtail/embeds/static/',
  158. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  159. },
  160. {
  161. from: 'wagtail/images/static_src/',
  162. to: 'wagtail/images/static/',
  163. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  164. },
  165. {
  166. from: 'wagtail/contrib/search_promotions/static_src/',
  167. to: 'wagtail/contrib/search_promotions/static/',
  168. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  169. },
  170. {
  171. from: 'wagtail/users/static_src/',
  172. to: 'wagtail/users/static/',
  173. globOptions: { ignore: ['**/{app,scss}/**', '*.{css,txt}'] },
  174. },
  175. ],
  176. }),
  177. ],
  178. module: {
  179. rules: [
  180. {
  181. test: /\.(js|ts)x?$/,
  182. loader: 'ts-loader',
  183. exclude: /node_modules/,
  184. },
  185. {
  186. test: /\.(svg)$/i,
  187. type: 'asset/inline',
  188. },
  189. {
  190. test: /\.(scss|css)$/,
  191. use: [
  192. MiniCssExtractPlugin.loader,
  193. {
  194. loader: 'css-loader',
  195. options: {
  196. url: false,
  197. },
  198. },
  199. {
  200. loader: 'postcss-loader',
  201. options: {
  202. postcssOptions: {
  203. plugins: ['tailwindcss', 'autoprefixer', 'cssnano'],
  204. },
  205. },
  206. },
  207. {
  208. loader: 'sass-loader',
  209. options: {
  210. sassOptions: {
  211. // Manually set Sass output so it’s identical in production and development. See:
  212. // https://github.com/tailwindlabs/tailwindcss/issues/11027
  213. // https://github.com/webpack-contrib/sass-loader/issues/1129
  214. outputStyle: 'expanded',
  215. },
  216. },
  217. },
  218. ],
  219. },
  220. ].concat(
  221. Object.keys(exposedDependencies).map((name) => {
  222. const globalName = exposedDependencies[name];
  223. // Create expose-loader configs for each Wagtail dependency.
  224. return {
  225. test: require.resolve(name),
  226. use: [
  227. {
  228. loader: 'expose-loader',
  229. options: {
  230. exposes: {
  231. globalName,
  232. override: true,
  233. },
  234. },
  235. },
  236. ],
  237. };
  238. }),
  239. ),
  240. },
  241. optimization: {
  242. splitChunks: {
  243. cacheGroups: {
  244. vendor: {
  245. name: getOutputPath('admin', 'js', 'vendor'),
  246. chunks: 'initial',
  247. minChunks: 2,
  248. reuseExistingChunk: true,
  249. },
  250. },
  251. },
  252. },
  253. // See https://webpack.js.org/configuration/devtool/.
  254. devtool: isProduction ? false : 'eval-cheap-module-source-map',
  255. // For development mode only.
  256. watchOptions: {
  257. poll: 1000,
  258. aggregateTimeout: 300,
  259. },
  260. // Disable performance hints – currently there are much more valuable
  261. // optimizations for us to do outside of Webpack
  262. performance: {
  263. hints: false,
  264. },
  265. stats: {
  266. // Add chunk information (setting this to `false` allows for a less verbose output)
  267. chunks: false,
  268. // Add the hash of the compilation
  269. hash: false,
  270. // `webpack --colors` equivalent
  271. colors: true,
  272. // Add information about the reasons why modules are included
  273. reasons: false,
  274. // Add webpack version information
  275. version: false,
  276. },
  277. };
  278. };