webpack.config.js 5.6 KB

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