webpack.config.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. // Legacy support for font icon loading, to be removed.
  187. test: /\.(woff)$/i,
  188. generator: {
  189. emit: false,
  190. filename: 'wagtailadmin/fonts/[name][ext]',
  191. },
  192. },
  193. {
  194. test: /\.(svg)$/i,
  195. type: 'asset/inline',
  196. },
  197. {
  198. test: /\.(scss|css)$/,
  199. use: [
  200. MiniCssExtractPlugin.loader,
  201. {
  202. loader: 'css-loader',
  203. options: {
  204. url: false,
  205. },
  206. },
  207. {
  208. loader: 'postcss-loader',
  209. options: {
  210. postcssOptions: {
  211. plugins: ['tailwindcss', 'autoprefixer', 'cssnano'],
  212. },
  213. },
  214. },
  215. {
  216. loader: 'sass-loader',
  217. options: {
  218. sassOptions: {
  219. // Manually set Sass output so it’s identical in production and development. See:
  220. // https://github.com/tailwindlabs/tailwindcss/issues/11027
  221. // https://github.com/webpack-contrib/sass-loader/issues/1129
  222. outputStyle: 'expanded',
  223. },
  224. },
  225. },
  226. ],
  227. },
  228. ].concat(
  229. Object.keys(exposedDependencies).map((name) => {
  230. const globalName = exposedDependencies[name];
  231. // Create expose-loader configs for each Wagtail dependency.
  232. return {
  233. test: require.resolve(name),
  234. use: [
  235. {
  236. loader: 'expose-loader',
  237. options: {
  238. exposes: {
  239. globalName,
  240. override: true,
  241. },
  242. },
  243. },
  244. ],
  245. };
  246. }),
  247. ),
  248. },
  249. optimization: {
  250. splitChunks: {
  251. cacheGroups: {
  252. vendor: {
  253. name: getOutputPath('admin', 'js', 'vendor'),
  254. chunks: 'initial',
  255. minChunks: 2,
  256. reuseExistingChunk: true,
  257. },
  258. },
  259. },
  260. },
  261. // See https://webpack.js.org/configuration/devtool/.
  262. devtool: isProduction ? false : 'eval-cheap-module-source-map',
  263. // For development mode only.
  264. watchOptions: {
  265. poll: 1000,
  266. aggregateTimeout: 300,
  267. },
  268. // Disable performance hints – currently there are much more valuable
  269. // optimizations for us to do outside of Webpack
  270. performance: {
  271. hints: false,
  272. },
  273. stats: {
  274. // Add chunk information (setting this to `false` allows for a less verbose output)
  275. chunks: false,
  276. // Add the hash of the compilation
  277. hash: false,
  278. // `webpack --colors` equivalent
  279. colors: true,
  280. // Add information about the reasons why modules are included
  281. reasons: false,
  282. // Add webpack version information
  283. version: false,
  284. },
  285. };
  286. };