webpack.config.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Build config for the v0.5.0 FSE migration (@wordpress/scripts + webpack).
  3. *
  4. * This is now the theme's only build — it fully replaced the old gulp pipeline
  5. * (copy/mixin/build/cssf/js/jsf). It compiles the site-wide stylesheet and both
  6. * JS bundles from npm packages + the bespoke source in src/ (see
  7. * _claude/notes/upgrade-plan.md).
  8. *
  9. * Output layout (output.path = theme root so nothing escapes it):
  10. * - front → js/v4-front.min.js (homepage bundle, overwrites the enqueued path)
  11. * - main → js/v4-script.min.js (site-wide bundle, overwrites the enqueued path)
  12. * - v4-style → v4-style.min.css (theme root — keeps the `style-min` enqueue + font url()s valid)
  13. * - index / other → build/[name].{js,css} (seed + future block view scripts)
  14. */
  15. const path = require('path');
  16. const webpack = require('webpack');
  17. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  18. const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
  19. const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
  20. module.exports = {
  21. entry: {
  22. index: './src/index.js',
  23. // Reproduces gulp `build` → v4-style.min.css (concat of the 5 CSS sources, in order).
  24. 'v4-style': './src/legacy-style.js',
  25. // Reproduces gulp `jsf` → homepage JS (jquery + svg-morpheus + front-page.js).
  26. front: './src/front.js',
  27. // Reproduces gulp `js` → site-wide JS (jquery/bootstrap/fullcalendar/plugins).
  28. main: './src/main.js',
  29. },
  30. output: {
  31. path: path.resolve(__dirname), // theme root
  32. // The two bundles overwrite their committed, enqueued paths in place (like
  33. // v4-style.min.css does) so nothing needs re-pointing and they ship via git.
  34. // Everything else (the seed, future block scripts) goes under the gitignored build/.
  35. filename: (pathData) => {
  36. const n = pathData.chunk.name;
  37. if (n === 'front') return 'js/v4-front.min.js';
  38. if (n === 'main') return 'js/v4-script.min.js';
  39. return 'build/[name].js';
  40. },
  41. clean: false, // NEVER clean — output.path is the theme root
  42. },
  43. module: {
  44. rules: [
  45. {
  46. test: /\.js$/,
  47. exclude: /node_modules/,
  48. use: { loader: 'babel-loader' },
  49. },
  50. {
  51. // svg-morpheus ships a bare global `var SVGMorpheus` (no export) — surface it as a
  52. // module export so src/front.js can re-expose it on window for front-page.js.
  53. test: require.resolve('svg-morpheus/compile/minified/svg-morpheus.js'),
  54. loader: 'exports-loader',
  55. options: { type: 'commonjs', exports: 'single SVGMorpheus' },
  56. },
  57. {
  58. // fullcalendar/main.js is likewise a bare global `var FullCalendar` build.
  59. test: require.resolve('fullcalendar/main.js'),
  60. loader: 'exports-loader',
  61. options: { type: 'commonjs', exports: 'single FullCalendar' },
  62. },
  63. {
  64. test: /\.css$/,
  65. use: [
  66. MiniCssExtractPlugin.loader,
  67. // url:false / import:false → leave url() + @import untouched (gulp-concat parity,
  68. // so the relative `fonts/…` paths keep resolving from the theme root).
  69. { loader: 'css-loader', options: { url: false, import: false } },
  70. ],
  71. },
  72. {
  73. // styles.scss @imports Bootstrap + bootstrap-icons from node_modules — webpack
  74. // now compiles it (replaces gulp's `mixin` task). quietDeps silences Bootstrap's
  75. // internal sass deprecation noise; url:false keeps the bootstrap-icons font path.
  76. test: /\.scss$/,
  77. use: [
  78. MiniCssExtractPlugin.loader,
  79. { loader: 'css-loader', options: { url: false, import: false } },
  80. {
  81. loader: 'sass-loader',
  82. options: {
  83. implementation: require('sass'),
  84. sassOptions: { quietDeps: true },
  85. },
  86. },
  87. ],
  88. },
  89. ],
  90. },
  91. optimization: {
  92. // '...' keeps webpack's default JS minifier (terser); add CSS minification.
  93. minimizer: [
  94. '...',
  95. // discardComments.removeAll matches the old gulp clean-css (specialComments:0).
  96. new CssMinimizerPlugin({
  97. minimizerOptions: { preset: ['default', { discardComments: { removeAll: true } }] },
  98. }),
  99. ],
  100. },
  101. plugins: [
  102. // jQuery globals for the bundled source that assumes a global $ (front-page.js
  103. // IIFEs, and the npm jQuery plugins validation/backstretch/lazyload in the main bundle).
  104. new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }),
  105. new MiniCssExtractPlugin({
  106. filename: ({ chunk }) =>
  107. chunk.name === 'v4-style' ? 'v4-style.min.css' : 'build/[name].css',
  108. }),
  109. new BrowserSyncPlugin({
  110. host: 'daw.stu',
  111. port: 81,
  112. proxy: 'https://daw.stu',
  113. https: {
  114. key: '/opt/homebrew/etc/httpd/ssl/daw.stu-key.pem',
  115. cert: '/opt/homebrew/etc/httpd/ssl/daw.stu-cert.pem',
  116. },
  117. files: [
  118. './**/*.php',
  119. './src/**/*.js',
  120. './src/**/*.css',
  121. './templates/**/*.html',
  122. './parts/**/*.html',
  123. './patterns/**/*.php',
  124. './theme.json',
  125. './.bs-reload', // touched by save_post (functions.php) → reload after Gutenberg "Update"
  126. ],
  127. ignore: ['node_modules', 'build', 'js/v4-*', '*.min.*'],
  128. open: 'external',
  129. reloadDelay: 50,
  130. injectChanges: true,
  131. notify: false,
  132. // Serve dev responses uncached so a reload always reflects the latest edit
  133. // (no stale browser cache while iterating). Pairs with the `files` watch above,
  134. // which already auto-reloads on .php / template / src changes.
  135. middleware: function (req, res, next) {
  136. res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
  137. res.setHeader('Pragma', 'no-cache');
  138. res.setHeader('Expires', '0');
  139. next();
  140. },
  141. }),
  142. ],
  143. };