anypack-plugin-serve 0.1.0-beta.2 → 0.1.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.
package/client.js CHANGED
@@ -33,16 +33,16 @@
33
33
  }
34
34
 
35
35
  // Initialize overlay singleton
36
- if (!window.anypackOverlay) {
36
+ if (!globalThis.anypackOverlay) {
37
37
  const { init } = require('anypack-overlay');
38
38
 
39
- window.anypackOverlay = init();
40
- window.anypackOverlay.silent = !!options?.client?.silent;
39
+ globalThis.anypackOverlay = init();
40
+ globalThis.anypackOverlay.silent = !!options?.client?.silent;
41
41
  }
42
42
 
43
43
  // Start client
44
44
  const Compiler = require('./lib/client/Compiler.js');
45
45
  const compiler = new Compiler(options, hash);
46
46
 
47
- window.anypackOverlay.addCompiler(compiler);
47
+ globalThis.anypackOverlay.addCompiler(compiler);
48
48
  })();
@@ -11,7 +11,7 @@
11
11
  const { error, refresh, warn } = require('./log.js')();
12
12
 
13
13
  // ignore 1008 (HTTP 400 equivalent) and 1011 (HTTP 500 equivalent)
14
- const ignoreCodes = [1008, 1011];
14
+ const ignoreCodes = new Set([1008, 1011]);
15
15
  const maxAttempts = 10;
16
16
 
17
17
  class ClientSocket {
@@ -50,7 +50,7 @@ class ClientSocket {
50
50
 
51
51
  if (this.options.retry) {
52
52
  this.socket.addEventListener('close', (event) => {
53
- if (ignoreCodes.includes(event.code)) {
53
+ if (ignoreCodes.has(event.code)) {
54
54
  return;
55
55
  }
56
56
 
package/lib/client/hmr.js CHANGED
@@ -49,8 +49,8 @@ const replace = async (buildHash, hash, refreshOnFailure) => {
49
49
 
50
50
  try {
51
51
  modules = await check(false);
52
- } catch (_e) {
53
- // noop. this typically happens when a MultiCompiler has more than one compiler that includes
52
+ } catch {
53
+ // This typically happens when a MultiCompiler has more than one compiler that includes
54
54
  // this script, and an update happens with a hash that isn't part of the compiler/module this
55
55
  // instance was loaded for.
56
56
  return;
package/lib/client/log.js CHANGED
@@ -26,4 +26,6 @@ const silent = {
26
26
  refresh,
27
27
  };
28
28
 
29
- module.exports = () => (window.anypackOverlay.silent ? silent : log);
29
+ module.exports = function getLogger() {
30
+ return globalThis.anypackOverlay.silent ? silent : log;
31
+ };
package/lib/index.js CHANGED
@@ -49,7 +49,8 @@ const defaults = {
49
49
  const key = 'anypack-plugin-serve';
50
50
  const nanoid = customAlphabet('1234567890abcdef', 7);
51
51
 
52
- let instance = null;
52
+ let activeInstanceId = null;
53
+ let activeLog = null;
53
54
 
54
55
  class AnypackPluginServe extends EventEmitter {
55
56
  constructor(opts = {}) {
@@ -68,16 +69,18 @@ class AnypackPluginServe extends EventEmitter {
68
69
  // of the plugin to be tested within the same context. If you find this, use this at your own
69
70
  // peril.
70
71
  /* istanbul ignore if */
71
- if (!opts.allowMany && instance) {
72
- instance.log.error(
72
+ if (!opts.allowMany && activeInstanceId !== null) {
73
+ activeLog.error(
73
74
  'Duplicate instances created. Only the first instance of this plugin will be active.',
74
75
  );
75
76
  return;
76
77
  }
77
78
 
78
- instance = this;
79
+ const instanceId = Symbol();
80
+ this._instanceId = instanceId;
81
+ activeInstanceId = instanceId;
79
82
 
80
- const options = Object.assign({}, defaults, opts);
83
+ const options = { ...defaults, ...opts };
81
84
 
82
85
  if (options.compress === true) {
83
86
  options.compress = {};
@@ -110,6 +113,7 @@ class AnypackPluginServe extends EventEmitter {
110
113
  }
111
114
 
112
115
  this.log = getLogger(options.log || {});
116
+ activeLog = this.log;
113
117
  this.options = options;
114
118
  this.compilers = [];
115
119
  this.state = {};
@@ -120,7 +124,7 @@ class AnypackPluginServe extends EventEmitter {
120
124
 
121
125
  // only allow once instance of the plugin to run for a build
122
126
  /* istanbul ignore if */
123
- if (instance !== this) {
127
+ if (activeInstanceId !== this._instanceId) {
124
128
  return;
125
129
  }
126
130
 
@@ -128,13 +132,9 @@ class AnypackPluginServe extends EventEmitter {
128
132
  }
129
133
 
130
134
  attach() {
131
- const self = this;
132
- const result = {
133
- apply(compiler) {
134
- return self.hook(compiler);
135
- },
135
+ return {
136
+ apply: (compiler) => this.hook(compiler),
136
137
  };
137
- return result;
138
138
  }
139
139
 
140
140
  // #138. handle emitted events that don't have a listener registered so they can be sent via WebSocket
@@ -239,7 +239,7 @@ class AnypackPluginServe extends EventEmitter {
239
239
  wpsId: compiler.wpsId,
240
240
  };
241
241
 
242
- const defineObject = Object.assign({}, this.options, compilerData);
242
+ const defineObject = { ...this.options, ...compilerData };
243
243
  const defineData = { ʎɐɹɔosǝʌɹǝs: JSON.stringify(defineObject) };
244
244
  const definePlugin = new compiler.webpack.DefinePlugin(defineData);
245
245
 
package/lib/middleware.js CHANGED
@@ -126,11 +126,11 @@ const getBuiltins = (app, options) => {
126
126
  }
127
127
  };
128
128
 
129
- const statik = (root, opts = { dev: true }) => {
130
- const paths = [].concat(root || options.static);
129
+ const statik = (root, opts) => {
130
+ const paths = [root || options.static].flat();
131
131
  staticPaths = paths;
132
132
  for (const path of paths) {
133
- app.use(sirv(path, opts));
133
+ app.use(sirv(path, opts ?? { dev: true }));
134
134
  }
135
135
  };
136
136
 
@@ -27,15 +27,15 @@ const init = function init(compiler, log) {
27
27
  );
28
28
 
29
29
  /* istanbul ignore else */
30
- if (!hasHMRPlugin) {
31
- addPlugin(compiler);
32
- } else {
30
+ if (hasHMRPlugin) {
33
31
  log.error(
34
32
  'anypack-plugin-serve adds HotModuleReplacementPlugin automatically. Please remove it from your config.',
35
33
  );
36
34
  throw new PluginExistsError(
37
35
  'HotModuleReplacementPlugin exists in the specified configuration.',
38
36
  );
37
+ } else {
38
+ addPlugin(compiler);
39
39
  }
40
40
  };
41
41
 
@@ -29,8 +29,9 @@ const readPkgName = () => {
29
29
  try {
30
30
  const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
31
31
  return pkg.name;
32
- } catch (_error) {
32
+ } catch (error) {
33
33
  // istanbul ignore next
34
+ console.warn(`Could not read package name: ${error.message}`);
34
35
  return null;
35
36
  }
36
37
  };
@@ -102,18 +102,17 @@ function run(command, ...args) {
102
102
 
103
103
  class Ramdisk {
104
104
  constructor(opts = {}) {
105
- const partialOpts = Object.assign({}, defaults, opts);
106
- const options = Object.assign({}, partialOpts, {
105
+ const partialOpts = { ...defaults, ...opts };
106
+ const options = {
107
+ ...partialOpts,
107
108
  blocks: partialOpts.bytes / partialOpts.blockSize,
108
- });
109
+ };
109
110
 
110
111
  this.options = options;
111
112
 
112
113
  this.commands = getCommands();
113
114
  this.options.diskPath = this.commands?.root(this.options);
114
115
 
115
- // const options = Object.assign({}, opts, { root: commands.root });
116
-
117
116
  this.init();
118
117
  }
119
118
 
package/lib/routes.js CHANGED
@@ -41,14 +41,9 @@ const setupRoutes = function setupRoutes() {
41
41
  socket.send(stringify({ action, data }));
42
42
  };
43
43
 
44
- registerEvent(
45
- this,
46
- socket,
47
- 'build',
48
- (compilerName = '<unknown>', { wpsId }) => {
49
- send('build', { compilerName, wpsId });
50
- },
51
- );
44
+ registerEvent(this, socket, 'build', (compilerName, { wpsId }) => {
45
+ send('build', { compilerName: compilerName ?? '<unknown>', wpsId });
46
+ });
52
47
 
53
48
  registerEvent(this, socket, 'done', (stats, { wpsId }) => {
54
49
  const { hash } = stats;
@@ -87,19 +82,15 @@ const setupRoutes = function setupRoutes() {
87
82
  }
88
83
  });
89
84
 
90
- registerEvent(
91
- this,
92
- socket,
93
- 'invalid',
94
- (filePath = '<unknown>', compiler) => {
95
- const context =
96
- compiler.context || compiler.options.context || process.cwd();
97
- const fileName = filePath?.replace?.(context, '') || filePath;
98
- const { wpsId } = compiler;
99
-
100
- send('invalid', { fileName, wpsId });
101
- },
102
- );
85
+ registerEvent(this, socket, 'invalid', (filePath, compiler) => {
86
+ const path = filePath ?? '<unknown>';
87
+ const context =
88
+ compiler.context || compiler.options.context || process.cwd();
89
+ const fileName = path?.replace?.(context, '') || path;
90
+ const { wpsId } = compiler;
91
+
92
+ send('invalid', { fileName, wpsId });
93
+ });
103
94
 
104
95
  registerEvent(this, socket, 'progress', (data) => {
105
96
  send('progress', data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anypack-plugin-serve",
3
- "version": "0.1.0-beta.2",
3
+ "version": "0.1.0",
4
4
  "description": "A Development Server in a Webpack Plugin",
5
5
  "license": "MPL-2.0",
6
6
  "repository": "onigoetz/anypack-plugin-serve",
@@ -33,7 +33,7 @@
33
33
  "dependencies": {
34
34
  "@polka/compression": "^1.0.0-next.25",
35
35
  "ansi-colors": "^4.1.3",
36
- "anypack-overlay": "0.1.0-beta.1",
36
+ "anypack-overlay": "0.1.0",
37
37
  "connect-history-api-fallback": "^2.0.0",
38
38
  "empathic": "^2.0.0",
39
39
  "http-proxy-middleware": "^3.0.5",
@@ -48,14 +48,15 @@
48
48
  "ws": "^8.18.3"
49
49
  },
50
50
  "devDependencies": {
51
- "@biomejs/biome": "2.3.11",
52
- "@rstest/core": "^0.8.1",
53
- "@rstest/coverage-istanbul": "^0.2.0",
51
+ "@biomejs/biome": "2.4.10",
52
+ "@rstest/core": "^0.9.0",
53
+ "@rstest/coverage-istanbul": "^0.3.0",
54
54
  "del-cli": "7.0.0",
55
55
  "get-port": "^7.1.0",
56
56
  "node-fetch": "^3.3.2",
57
57
  "puppeteer": "^24.34.0",
58
58
  "random-js": "^2.1.0",
59
+ "rstest-sonar-reporter": "1.0.0",
59
60
  "webpack": "^5.0.0",
60
61
  "webpack-nano": "^1.0.0"
61
62
  },