@plaudit/webpack-extensions 2.11.3 → 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ import browserSync, { type BrowserSyncInstance } from "browser-sync";
2
+ import type { Compiler, WebpackPluginInstance } from "webpack";
3
+ export type PluginOptions = {
4
+ reload: boolean;
5
+ name: string;
6
+ callback?: (err: Error, bs: BrowserSyncInstance) => any;
7
+ injectCss: boolean;
8
+ };
9
+ export declare class BrowserSyncPlugin implements WebpackPluginInstance {
10
+ private static isBrowserSyncRunning;
11
+ private readonly browserSyncOptions;
12
+ private readonly pluginOptions;
13
+ private readonly browserSyncInstance;
14
+ private isWebpackWatching;
15
+ private mostRecentChunkHashes;
16
+ constructor(browserSyncOptions: browserSync.Options, pluginOptions?: Partial<PluginOptions>);
17
+ apply(compiler: Compiler): void;
18
+ private getCssOnlyEmittedAssetsNames;
19
+ }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BrowserSyncPlugin = void 0;
7
+ const browser_sync_1 = __importDefault(require("browser-sync"));
8
+ class BrowserSyncPlugin {
9
+ static isBrowserSyncRunning = false;
10
+ browserSyncOptions;
11
+ pluginOptions;
12
+ browserSyncInstance;
13
+ isWebpackWatching = false;
14
+ mostRecentChunkHashes = {};
15
+ constructor(browserSyncOptions, pluginOptions) {
16
+ this.browserSyncOptions = { ...browserSyncOptions };
17
+ this.pluginOptions = { reload: true, name: "plaudit-bs-plugin", injectCss: true, ...pluginOptions };
18
+ this.browserSyncInstance = browser_sync_1.default.has(this.pluginOptions.name) ? browser_sync_1.default.get(this.pluginOptions.name) : browser_sync_1.default.create(this.pluginOptions.name);
19
+ }
20
+ apply(compiler) {
21
+ compiler.hooks.watchRun.tap(BrowserSyncPlugin.name, () => {
22
+ this.isWebpackWatching = true;
23
+ });
24
+ compiler.hooks.compilation.tap(BrowserSyncPlugin.name, () => {
25
+ if (BrowserSyncPlugin.isBrowserSyncRunning && this.browserSyncOptions.notify) {
26
+ this.browserSyncInstance.notify('Rebuilding...');
27
+ }
28
+ });
29
+ compiler.hooks.done.tap(BrowserSyncPlugin.name, stats => {
30
+ if (!this.isWebpackWatching) {
31
+ return;
32
+ }
33
+ if (!BrowserSyncPlugin.isBrowserSyncRunning) {
34
+ this.browserSyncInstance.init(this.browserSyncOptions, this.pluginOptions.callback);
35
+ BrowserSyncPlugin.isBrowserSyncRunning = true;
36
+ }
37
+ if (this.pluginOptions.reload) {
38
+ if (this.pluginOptions.injectCss) {
39
+ const cssOnlyEmittedAssets = this.getCssOnlyEmittedAssetsNames(stats);
40
+ if (cssOnlyEmittedAssets !== undefined) {
41
+ this.browserSyncInstance.reload(cssOnlyEmittedAssets);
42
+ return;
43
+ }
44
+ }
45
+ this.browserSyncInstance.reload();
46
+ }
47
+ });
48
+ }
49
+ getCssOnlyEmittedAssetsNames(stats) {
50
+ const compiledStats = stats.compilation.getStats().toJson({
51
+ hash: true,
52
+ publicPath: true,
53
+ assets: true,
54
+ chunks: true,
55
+ modules: false,
56
+ source: false,
57
+ errorDetails: false,
58
+ timings: false
59
+ });
60
+ const chunks = compiledStats.chunks
61
+ ?.filter((chunk) => chunk.names !== undefined && chunk.files !== undefined);
62
+ if (!chunks) {
63
+ return undefined;
64
+ }
65
+ const mostRecentChunkHashes = this.mostRecentChunkHashes;
66
+ this.mostRecentChunkHashes = Object.fromEntries(chunks.flatMap(chunk => chunk.names.map(cn => [cn, chunk.hash])));
67
+ if (Object.keys(mostRecentChunkHashes).length !== chunks.length) {
68
+ return undefined;
69
+ }
70
+ const changedFilenames = chunks
71
+ .filter(chunk => chunk.names.some(name => mostRecentChunkHashes[name] !== chunk.hash))
72
+ .flatMap(chunk => [...chunk.files, ...(chunk.auxiliaryFiles ?? [])])
73
+ .filter(filename => !filename.endsWith(".asset.php"));
74
+ if (changedFilenames.length === 0) {
75
+ return undefined;
76
+ }
77
+ for (const filename of changedFilenames) {
78
+ if (!filename.includes(".css")) {
79
+ return undefined;
80
+ }
81
+ }
82
+ return changedFilenames;
83
+ }
84
+ }
85
+ exports.BrowserSyncPlugin = BrowserSyncPlugin;
@@ -8,8 +8,8 @@ const AdditionalDependencyInjectorPlugin_1 = __importDefault(require("./wordpres
8
8
  const BlockJSONStyleRemappingPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/BlockJSONStyleRemappingPlugin"));
9
9
  const ExtensionsConfigFileGeneratorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/ExtensionsConfigFileGeneratorPlugin"));
10
10
  const VariablesJSMonitorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/VariablesJSMonitorPlugin"));
11
+ const BrowserSyncPlugin_1 = require("./wordpress-scripts-wrapper/BrowserSyncPlugin");
11
12
  const static_configs_1 = require("./wordpress-scripts-wrapper/static-configs");
12
- const browser_sync_webpack_plugin_1 = __importDefault(require("browser-sync-webpack-plugin"));
13
13
  const copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
14
14
  const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
15
15
  const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-empty-scripts"));
@@ -25,6 +25,18 @@ function mapToRealEntrypoints(entrypoint, dir, mapper = (entrypoint) => entrypoi
25
25
  return [joinPossiblyAbsolutePaths(node_path_1.default.basename(parsedEntrypoint.dir), parsedEntrypoint.name), { import: ep }];
26
26
  });
27
27
  }
28
+ let envFileContents = undefined;
29
+ function loadEnvFile() {
30
+ if (envFileContents !== undefined) {
31
+ return envFileContents;
32
+ }
33
+ for (let envFileHolderPath = process.cwd(); envFileHolderPath.length > 5; envFileHolderPath = node_path_1.default.dirname(envFileHolderPath)) {
34
+ if (node_fs_1.default.existsSync(node_path_1.default.join(envFileHolderPath, ".env"))) {
35
+ return envFileContents = node_fs_1.default.readFileSync(node_path_1.default.join(envFileHolderPath, ".env"), { encoding: 'utf8' });
36
+ }
37
+ }
38
+ return "";
39
+ }
28
40
  function isTruthy(value) {
29
41
  return !!value;
30
42
  }
@@ -47,7 +59,7 @@ function resolveLegacyBlockScriptsInFolder(folder) {
47
59
  const fullBlockDir = node_path_1.default.join(folder, blockDir);
48
60
  const packageJSON = node_path_1.default.join(fullBlockDir, 'package.json');
49
61
  if (node_fs_1.default.existsSync(packageJSON)) {
50
- const main = JSON.parse(node_fs_1.default.readFileSync(packageJSON, 'utf-8'))['main'];
62
+ const main = JSON.parse(node_fs_1.default.readFileSync(packageJSON, 'utf8'))['main'];
51
63
  if (main && node_fs_1.default.existsSync(node_path_1.default.join(fullBlockDir, main))) {
52
64
  blockScriptEntrypoints.push(node_path_1.default.join(fullBlockDir, main));
53
65
  }
@@ -201,7 +213,6 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
201
213
  }
202
214
  disableDefaultURLProcessing(webpackConfig);
203
215
  injectPostcssConfigOverrides(webpackConfig, name => currentVariables[name], verbose);
204
- let first = true;
205
216
  const sources = Array.isArray(config.src) ? config.src.map(s => [s, s]) : Object.entries(config.src);
206
217
  return sources.map(([src, dest]) => {
207
218
  const srcRoots = typeof dest !== 'string' && dest.withLegacyBlocksIn
@@ -213,15 +224,26 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
213
224
  const copyFiles = srcIsDirectory && src !== dest;
214
225
  const plugins = webpackConfig.plugins?.filter(isTruthy) ?? [];
215
226
  if (process.env["NO_TS_CHECKER"] !== "true") {
216
- plugins.push(new fork_ts_checker_webpack_plugin_1.default({
217
- async: true,
218
- typescript: {
219
- diagnosticOptions: {
220
- semantic: true,
221
- syntactic: true,
227
+ const include = (Array.isArray(srcRoot) ? srcRoot : [srcRoot])
228
+ .filter(sr => node_path_1.default.extname(sr).length === 0 || scriptOrStyleTest(sr) === "script")
229
+ .map(sr => node_path_1.default.extname(sr).length > 0 ? sr : node_path_1.default.join(sr, "**", "*"));
230
+ if (include.length > 0) {
231
+ plugins.push(new fork_ts_checker_webpack_plugin_1.default({
232
+ async: true,
233
+ typescript: {
234
+ diagnosticOptions: {
235
+ semantic: true,
236
+ syntactic: true,
237
+ },
238
+ configOverwrite: {
239
+ include,
240
+ compilerOptions: {
241
+ skipLibCheck: true
242
+ }
243
+ }
222
244
  }
223
- }
224
- }));
245
+ }));
246
+ }
225
247
  }
226
248
  plugins.push(new webpack_remove_empty_scripts_1.default({
227
249
  stage: webpack_remove_empty_scripts_1.default.STAGE_AFTER_PROCESS_PLUGINS,
@@ -237,16 +259,14 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
237
259
  if (srcIsDirectory && (typeof dest !== 'string' && dest.directoryLayout === 'extensions')) {
238
260
  plugins.push(new ExtensionsConfigFileGeneratorPlugin_1.default(srcRoot));
239
261
  }
240
- if (first) {
241
- first = false;
242
- if (process.argv.includes('--browser-sync') || process.env['BROWSER_SYNC'] === 'true') {
243
- plugins.push(new browser_sync_webpack_plugin_1.default({
244
- host: 'localhost',
245
- port: 3000,
246
- https: true,
247
- proxy: 'https://localhost:8443'
248
- }));
249
- }
262
+ if (process.argv.includes('--browser-sync') || process.env['BROWSER_SYNC'] === 'true') {
263
+ const port = loadEnvFile().match(/WEB_PORT_SSL=(\d+)/i)?.[1] ?? "8443";
264
+ plugins.push(new BrowserSyncPlugin_1.BrowserSyncPlugin({
265
+ host: 'localhost',
266
+ port: 3000,
267
+ https: true,
268
+ proxy: port === "443" ? "https://localhost" : `https://localhost:${port}`
269
+ }));
250
270
  }
251
271
  let entry;
252
272
  if (srcIsDirectory) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.11.3",
3
+ "version": "2.12.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",
@@ -35,14 +35,13 @@
35
35
  "@plaudit/postcss-variables": "^1.0.0",
36
36
  "@wordpress/scripts": "^27.1.0",
37
37
  "autoprefixer": "^10.4.17",
38
- "browser-sync": "^2.29.3",
39
- "browser-sync-webpack-plugin": "^2.3.0",
38
+ "browser-sync": "^3.0.2",
40
39
  "clean-webpack-plugin": "^4.0.0",
41
40
  "copy-webpack-plugin": "^11.0.0",
42
41
  "cssnano": "^6.0.3",
43
42
  "eslint": "^8.56.0",
44
43
  "eslint-plugin-jsdoc": "^48.0.4",
45
- "fork-ts-checker-webpack-plugin": "^8.0.0",
44
+ "fork-ts-checker-webpack-plugin": "^9.0.2",
46
45
  "postcss": "^8.4.33",
47
46
  "postcss-calc": "^9.0.1",
48
47
  "postcss-discard-comments": "^6.0.1",