@plaudit/webpack-extensions 2.15.1 → 2.17.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.
@@ -5,15 +5,20 @@ export type PluginOptions = {
5
5
  name: string;
6
6
  callback?: (err: Error, bs: BrowserSyncInstance) => any;
7
7
  injectCss: boolean;
8
+ middlewareBuilder?: (options: browserSync.Options) => browserSync.Options['middleware'];
8
9
  };
9
10
  export declare class BrowserSyncPlugin implements WebpackPluginInstance {
10
11
  private static isBrowserSyncRunning;
11
- private readonly browserSyncOptions;
12
+ private static browserSyncPluginOptions;
13
+ private static envFileContents;
12
14
  private readonly pluginOptions;
13
15
  private readonly browserSyncInstance;
14
16
  private isWebpackWatching;
15
17
  private mostRecentChunkHashes;
16
- constructor(browserSyncOptions: browserSync.Options, pluginOptions?: Partial<PluginOptions>);
18
+ private shouldNotify;
19
+ constructor(pluginOptions?: Partial<PluginOptions>);
20
+ private static getBrowserSyncPluginOptions;
21
+ private static loadEnvFile;
17
22
  apply(compiler: Compiler): void;
18
23
  private getCssOnlyEmittedAssetsNames;
19
24
  }
@@ -4,34 +4,125 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.BrowserSyncPlugin = void 0;
7
+ const node_child_process_1 = __importDefault(require("node:child_process"));
8
+ const node_fs_1 = __importDefault(require("node:fs"));
9
+ const node_path_1 = __importDefault(require("node:path"));
7
10
  const browser_sync_1 = __importDefault(require("browser-sync"));
11
+ const http_proxy_middleware_1 = require("http-proxy-middleware");
12
+ const open_1 = __importDefault(require("open"));
8
13
  class BrowserSyncPlugin {
9
14
  static isBrowserSyncRunning = false;
10
- browserSyncOptions;
15
+ static browserSyncPluginOptions = undefined;
16
+ static envFileContents = undefined;
11
17
  pluginOptions;
12
18
  browserSyncInstance;
13
19
  isWebpackWatching = false;
14
20
  mostRecentChunkHashes = {};
15
- constructor(browserSyncOptions, pluginOptions) {
16
- this.browserSyncOptions = { ...browserSyncOptions };
21
+ shouldNotify = false;
22
+ constructor(pluginOptions) {
17
23
  this.pluginOptions = { reload: true, name: "plaudit-bs-plugin", injectCss: true, ...pluginOptions };
18
24
  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
25
  }
26
+ static getBrowserSyncPluginOptions() {
27
+ return BrowserSyncPlugin.browserSyncPluginOptions ?? (BrowserSyncPlugin.browserSyncPluginOptions = new Promise(resolve => {
28
+ const envFile = BrowserSyncPlugin.loadEnvFile();
29
+ const prefixes = node_child_process_1.default.spawnSync("theapp", ["info", "--for-browsersync"], {
30
+ encoding: "utf-8",
31
+ stdio: ["ignore", "pipe", "inherit"]
32
+ }).stdout.split(/\r?\n/g).map(url => url.trim()).filter(url => url.length > 0).map(url => url.substring(url.indexOf("://") + 3));
33
+ const targetPort = envFile.match(/WEB_PORT_SSL=(\d+)/i)?.[1] ?? "8443";
34
+ const targetHost = targetPort === "443" ? "localhost" : `localhost:${targetPort}`;
35
+ if (prefixes.length === 0) {
36
+ resolve([{
37
+ host: 'localhost',
38
+ port: 3000,
39
+ https: true,
40
+ proxy: `https://${targetHost}`
41
+ }, {}]);
42
+ }
43
+ else {
44
+ resolve([{
45
+ port: 3000,
46
+ https: true,
47
+ open: false,
48
+ callbacks: {
49
+ ready(err, bsi) {
50
+ // This ensures that the opened URL is actually a valid one
51
+ (0, open_1.default)(`https://${prefixes[0]}:${bsi.getOption("port") ?? 3000}`);
52
+ }
53
+ }
54
+ }, {
55
+ middlewareBuilder: (options) => {
56
+ const proxyPort = options.port?.toString() ?? "3000";
57
+ const router = prefixes.reduce((router, prefix) => {
58
+ router[`${prefix}:${proxyPort}`] = {
59
+ protocol: 'https:',
60
+ slashes: true,
61
+ auth: null,
62
+ host: prefix,
63
+ port: targetPort,
64
+ hostname: targetHost,
65
+ hash: null,
66
+ search: null,
67
+ query: null,
68
+ pathname: '/',
69
+ path: '/',
70
+ href: `https://${targetHost}/`
71
+ };
72
+ return router;
73
+ }, {});
74
+ const portInjectionPattern = new RegExp(`(${prefixes.join('|')})(?!:3000)`, "gi");
75
+ return (0, http_proxy_middleware_1.createProxyMiddleware)({
76
+ changeOrigin: true, router, secure: false, xfwd: true, selfHandleResponse: true,
77
+ on: {
78
+ proxyRes: (0, http_proxy_middleware_1.responseInterceptor)(async (responseBuffer, proxyRes) => {
79
+ const contentType = proxyRes.headers["content-type"];
80
+ if (contentType && (contentType.startsWith("text/") || contentType === "application/x-javascript")) {
81
+ return responseBuffer.toString('utf-8').replaceAll(portInjectionPattern, "$1:3000");
82
+ }
83
+ return responseBuffer;
84
+ })
85
+ }
86
+ });
87
+ }
88
+ }]);
89
+ }
90
+ }));
91
+ }
92
+ static loadEnvFile() {
93
+ if (BrowserSyncPlugin.envFileContents !== undefined) {
94
+ return BrowserSyncPlugin.envFileContents;
95
+ }
96
+ for (let envFileHolderPath = process.cwd(); envFileHolderPath.length > 5; envFileHolderPath = node_path_1.default.dirname(envFileHolderPath)) {
97
+ if (node_fs_1.default.existsSync(node_path_1.default.join(envFileHolderPath, ".env"))) {
98
+ return BrowserSyncPlugin.envFileContents = node_fs_1.default.readFileSync(node_path_1.default.join(envFileHolderPath, ".env"), { encoding: 'utf8' });
99
+ }
100
+ }
101
+ return "";
102
+ }
20
103
  apply(compiler) {
21
104
  compiler.hooks.watchRun.tap(BrowserSyncPlugin.name, () => {
22
105
  this.isWebpackWatching = true;
23
106
  });
24
107
  compiler.hooks.compilation.tap(BrowserSyncPlugin.name, () => {
25
- if (BrowserSyncPlugin.isBrowserSyncRunning && this.browserSyncOptions.notify) {
108
+ if (BrowserSyncPlugin.isBrowserSyncRunning && this.shouldNotify) {
26
109
  this.browserSyncInstance.notify('Rebuilding...');
27
110
  }
28
111
  });
29
- compiler.hooks.done.tap(BrowserSyncPlugin.name, stats => {
112
+ compiler.hooks.done.tapPromise(BrowserSyncPlugin.name, async (stats) => {
30
113
  if (!this.isWebpackWatching) {
31
114
  return;
32
115
  }
33
116
  if (!BrowserSyncPlugin.isBrowserSyncRunning) {
34
- this.browserSyncInstance.init(this.browserSyncOptions, this.pluginOptions.callback);
117
+ const [browserSyncOptions, pluginOptions] = await BrowserSyncPlugin.getBrowserSyncPluginOptions();
118
+ const actualPluginOptions = { ...this.pluginOptions, ...pluginOptions };
119
+ const middleware = actualPluginOptions.middlewareBuilder?.(browserSyncOptions);
120
+ this.browserSyncInstance.init(middleware
121
+ ? { ...browserSyncOptions, server: { middleware: Array.isArray(middleware) ? middleware : [middleware] } }
122
+ : browserSyncOptions, this.pluginOptions.callback);
123
+ if (browserSyncOptions.notify) {
124
+ this.shouldNotify = true;
125
+ }
35
126
  BrowserSyncPlugin.isBrowserSyncRunning = true;
36
127
  }
37
128
  if (this.pluginOptions.reload) {
@@ -25,18 +25,6 @@ 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
- }
40
28
  function isTruthy(value) {
41
29
  return !!value;
42
30
  }
@@ -260,13 +248,7 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
260
248
  plugins.push(new ExtensionsConfigFileGeneratorPlugin_1.default(srcRoot));
261
249
  }
262
250
  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
- }));
251
+ plugins.push(new BrowserSyncPlugin_1.BrowserSyncPlugin());
270
252
  }
271
253
  let entry;
272
254
  if (srcIsDirectory) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.15.1",
3
+ "version": "2.17.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",
@@ -18,36 +18,37 @@
18
18
  }
19
19
  },
20
20
  "devDependencies": {
21
- "@types/browser-sync-webpack-plugin": "^2.2.3",
22
- "@types/node": "^20.11.20",
21
+ "@types/browser-sync-webpack-plugin": "^2.2.5",
22
+ "@types/node": "^20.11.30",
23
23
  "@types/tapable": "^2.2.7",
24
24
  "@types/webpack": "^5.28.5",
25
25
  "@types/webpack-sources": "^3.2.3",
26
26
  "postcss-load-config": "^4.0.2",
27
27
  "postcss-loader": "^7.3.4",
28
28
  "ts-node": "^10.9.2",
29
- "typescript": "^5.3.3"
29
+ "typescript": "^5.4.3"
30
30
  },
31
31
  "dependencies": {
32
32
  "@plaudit/postcss-color-function": "^5.0.0",
33
33
  "@plaudit/postcss-silent-extend": "^3.0.0",
34
34
  "@plaudit/postcss-strip-units": "^3.0.0",
35
35
  "@plaudit/postcss-variables": "^1.0.0",
36
- "@wordpress/scripts": "^27.3.0",
37
- "autoprefixer": "^10.4.17",
36
+ "@wordpress/scripts": "^27.5.0",
37
+ "autoprefixer": "^10.4.18",
38
38
  "browser-sync": "^3.0.2",
39
39
  "clean-webpack-plugin": "^4.0.0",
40
40
  "copy-webpack-plugin": "^12.0.2",
41
- "cssnano": "^6.0.5",
41
+ "cssnano": "^6.1.1",
42
42
  "eslint": "^8.57.0",
43
- "eslint-plugin-jsdoc": "^48.2.0",
43
+ "eslint-plugin-jsdoc": "^48.2.1",
44
44
  "fork-ts-checker-webpack-plugin": "^9.0.2",
45
- "postcss": "^8.4.35",
45
+ "http-proxy-middleware": "^3.0.0-beta.1",
46
+ "postcss": "^8.4.38",
46
47
  "postcss-calc": "^9.0.1",
47
- "postcss-discard-comments": "^6.0.1",
48
+ "postcss-discard-comments": "^6.0.2",
48
49
  "postcss-fallback": "^0.1.0",
49
50
  "postcss-functions": "^4.0.2",
50
- "postcss-import": "^16.0.1",
51
+ "postcss-import": "^16.1.0",
51
52
  "postcss-inline-svg": "^6.0.0",
52
53
  "postcss-media-minmax": "^5.0.0",
53
54
  "postcss-mixins": "6.2.3",
@@ -62,7 +63,7 @@
62
63
  "postcss-url": "^10.1.3",
63
64
  "react": "^18.2.0",
64
65
  "react-dom": "^18.2.0",
65
- "webpack": "^5.90.3",
66
+ "webpack": "^5.91.0",
66
67
  "webpack-remove-empty-scripts": "^1.0.4"
67
68
  }
68
69
  }