webpack.config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/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. test: /\.css$/,
  59. use: [
  60. MiniCssExtractPlugin.loader,
  61. // url:false / import:false → leave url() + @import untouched (gulp-concat parity,
  62. // so the relative `fonts/…` paths keep resolving from the theme root).
  63. { loader: 'css-loader', options: { url: false, import: false } },
  64. ],
  65. },
  66. {
  67. // styles.scss @imports Bootstrap + bootstrap-icons from node_modules — webpack
  68. // now compiles it (replaces gulp's `mixin` task). quietDeps silences Bootstrap's
  69. // internal sass deprecation noise; url:false keeps the bootstrap-icons font path.
  70. test: /\.scss$/,
  71. use: [
  72. MiniCssExtractPlugin.loader,
  73. { loader: 'css-loader', options: { url: false, import: false } },
  74. {
  75. loader: 'sass-loader',
  76. options: {
  77. implementation: require('sass'),
  78. sassOptions: { quietDeps: true },
  79. },
  80. },
  81. ],
  82. },
  83. ],
  84. },
  85. optimization: {
  86. // '...' keeps webpack's default JS minifier (terser); add CSS minification.
  87. minimizer: [
  88. '...',
  89. // discardComments.removeAll matches the old gulp clean-css (specialComments:0).
  90. new CssMinimizerPlugin({
  91. minimizerOptions: { preset: ['default', { discardComments: { removeAll: true } }] },
  92. }),
  93. ],
  94. },
  95. plugins: [
  96. // jQuery globals for the bundled source that assumes a global $ (front-page.js
  97. // IIFEs, and the npm jQuery plugins validation/backstretch/lazyload in the main bundle).
  98. new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }),
  99. new MiniCssExtractPlugin({
  100. filename: ({ chunk }) =>
  101. chunk.name === 'v4-style' ? 'v4-style.min.css' : 'build/[name].css',
  102. }),
  103. new BrowserSyncPlugin({
  104. host: 'daw.stu',
  105. port: 81,
  106. proxy: 'https://daw.stu',
  107. https: {
  108. key: '/opt/homebrew/etc/httpd/ssl/daw.stu-key.pem',
  109. cert: '/opt/homebrew/etc/httpd/ssl/daw.stu-cert.pem',
  110. },
  111. files: [
  112. './**/*.php',
  113. './src/**/*.js',
  114. './src/**/*.css',
  115. './templates/**/*.html',
  116. './parts/**/*.html',
  117. './patterns/**/*.php',
  118. './theme.json',
  119. ],
  120. ignore: ['node_modules', 'build', 'js/v4-*', '*.min.*'],
  121. open: 'external',
  122. reloadDelay: 50,
  123. injectChanges: true,
  124. notify: false,
  125. // Serve dev responses uncached so a reload always reflects the latest edit
  126. // (no stale browser cache while iterating). Pairs with the `files` watch above,
  127. // which already auto-reloads on .php / template / src changes.
  128. middleware: function (req, res, next) {
  129. res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
  130. res.setHeader('Pragma', 'no-cache');
  131. res.setHeader('Expires', '0');
  132. next();
  133. },
  134. }),
  135. ],
  136. };