webpack.config.js 8.3 KB

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