@atls/webpack-start-server-plugin 0.0.5 → 0.0.7

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,2 @@
1
+ export * from './start-server.plugin';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA"}
package/dist/index.js CHANGED
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./start-server.plugin"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wDAAqC"}
@@ -0,0 +1,13 @@
1
+ /// <reference types="node" />
2
+ import type { Writable } from 'node:stream';
3
+ export interface StartServerLoggerOptions {
4
+ stdout?: Writable;
5
+ stderr?: Writable;
6
+ }
7
+ export declare class StartServerLogger {
8
+ private readonly options;
9
+ constructor(options?: StartServerLoggerOptions);
10
+ info(body: string): void;
11
+ error(error: Error): void;
12
+ }
13
+ //# sourceMappingURL=start-server.logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start-server.logger.d.ts","sourceRoot":"","sources":["../src/start-server.logger.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE3C,MAAM,WAAW,wBAAwB;IACvC,MAAM,CAAC,EAAE,QAAQ,CAAA;IACjB,MAAM,CAAC,EAAE,QAAQ,CAAA;CAClB;AAED,qBAAa,iBAAiB;IAChB,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,GAAE,wBAA6B;IAEnE,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAgBxB,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;CAgB1B"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StartServerLogger = void 0;
4
+ class StartServerLogger {
5
+ options;
6
+ constructor(options = {}) {
7
+ this.options = options;
8
+ }
9
+ info(body) {
10
+ if (this.options.stdout) {
11
+ this.options.stdout.write(Buffer.from(JSON.stringify({
12
+ body,
13
+ severityNumber: 9,
14
+ attributes: {
15
+ '@namespace': 'webpack:start-server',
16
+ },
17
+ })));
18
+ }
19
+ }
20
+ error(error) {
21
+ if (this.options.stderr) {
22
+ this.options.stderr.write(Buffer.from(JSON.stringify({
23
+ body: error.message,
24
+ severityNumber: 17,
25
+ attributes: {
26
+ '@namespace': 'webpack:start-server',
27
+ '@stack': error.stack,
28
+ },
29
+ })));
30
+ }
31
+ }
32
+ }
33
+ exports.StartServerLogger = StartServerLogger;
34
+ //# sourceMappingURL=start-server.logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start-server.logger.js","sourceRoot":"","sources":["../src/start-server.logger.ts"],"names":[],"mappings":";;;AAOA,MAAa,iBAAiB;IACC;IAA7B,YAA6B,UAAoC,EAAE;QAAtC,YAAO,GAAP,OAAO,CAA+B;IAAG,CAAC;IAEvE,IAAI,CAAC,IAAY;QACf,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;YACvB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CACvB,MAAM,CAAC,IAAI,CACT,IAAI,CAAC,SAAS,CAAC;gBACb,IAAI;gBACJ,cAAc,EAAE,CAAC;gBACjB,UAAU,EAAE;oBACV,YAAY,EAAE,sBAAsB;iBACrC;aACF,CAAC,CACH,CACF,CAAA;SACF;IACH,CAAC;IAED,KAAK,CAAC,KAAY;QAChB,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;YACvB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CACvB,MAAM,CAAC,IAAI,CACT,IAAI,CAAC,SAAS,CAAC;gBACb,IAAI,EAAE,KAAK,CAAC,OAAO;gBACnB,cAAc,EAAE,EAAE;gBAClB,UAAU,EAAE;oBACV,YAAY,EAAE,sBAAsB;oBACpC,QAAQ,EAAE,KAAK,CAAC,KAAK;iBACtB;aACF,CAAC,CACH,CACF,CAAA;SACF;IACH,CAAC;CACF;AAnCD,8CAmCC"}
@@ -0,0 +1,23 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import type { ChildProcess } from 'node:child_process';
4
+ import type { Writable } from 'node:stream';
5
+ import type webpack from 'webpack';
6
+ import { StartServerLogger } from './start-server.logger';
7
+ export interface StartServerPluginOptions {
8
+ stdout?: Writable;
9
+ stderr?: Writable;
10
+ }
11
+ export declare class StartServerPlugin {
12
+ options: StartServerPluginOptions;
13
+ entryFile: string | null;
14
+ worker: ChildProcess | null;
15
+ initialized: boolean;
16
+ logger: StartServerLogger;
17
+ constructor(options?: Partial<StartServerPluginOptions>);
18
+ apply: (compiler: webpack.Compiler) => void;
19
+ private afterEmit;
20
+ private startServer;
21
+ private runWorker;
22
+ }
23
+ //# sourceMappingURL=start-server.plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start-server.plugin.d.ts","sourceRoot":"","sources":["../src/start-server.plugin.ts"],"names":[],"mappings":";;AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAGtD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAU,aAAa,CAAA;AAE/C,OAAO,KAAK,OAAO,MAAe,SAAS,CAAA;AAE3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAEzD,MAAM,WAAW,wBAAwB;IACvC,MAAM,CAAC,EAAE,QAAQ,CAAA;IACjB,MAAM,CAAC,EAAE,QAAQ,CAAA;CAClB;AAED,qBAAa,iBAAiB;IAC5B,OAAO,EAAE,wBAAwB,CAAA;IAEjC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAO;IAE/B,MAAM,EAAE,YAAY,GAAG,IAAI,CAAO;IAElC,WAAW,EAAE,OAAO,CAAQ;IAE5B,MAAM,EAAE,iBAAiB,CAAA;gBAEb,OAAO,GAAE,OAAO,CAAC,wBAAwB,CAAM;IAK3D,KAAK,aAAc,QAAQ,QAAQ,KAAG,IAAI,CAEzC;IAED,OAAO,CAAC,SAAS,CAYhB;IAED,OAAO,CAAC,WAAW,CAUlB;IAED,OAAO,CAAC,SAAS;CAiBlB"}
@@ -1,154 +1,56 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.StartServerPlugin = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const webpack_1 = require("webpack");
9
- const child_process_1 = require("child_process");
4
+ const node_child_process_1 = require("node:child_process");
5
+ const node_path_1 = require("node:path");
6
+ const start_server_logger_1 = require("./start-server.logger");
10
7
  class StartServerPlugin {
8
+ options;
9
+ entryFile = null;
10
+ worker = null;
11
+ initialized = false;
12
+ logger;
11
13
  constructor(options = {}) {
12
- this.entryFile = null;
13
- this.worker = null;
14
- this.workerLoaded = false;
15
- this.handleWorkerExit = (code, signal) => {
16
- if (code) {
17
- this.error(`script exited with code: ${code}`);
18
- }
19
- if (signal && signal !== 'SIGTERM') {
20
- this.error(`script exited after signal ${signal}`);
21
- }
22
- this.worker = null;
23
- if (this.options.onWorkerExit) {
24
- this.options.onWorkerExit();
25
- }
26
- if (!this.workerLoaded) {
27
- this.error('Script did not load, or HMR failed; not restarting');
28
- return;
29
- }
30
- this.workerLoaded = false;
31
- if (this.entryFile) {
32
- this.runWorker(this.entryFile);
33
- }
34
- };
35
- this.handleWorkerError = (err) => {
36
- this.error(err);
37
- this.worker = null;
38
- if (this.options.onWorkerExit) {
39
- this.options.onWorkerExit();
40
- }
41
- };
42
- this.handleWorkerMessage = (message) => {
43
- if (message === 'SSWP_LOADED') {
44
- this.workerLoaded = true;
45
- this.info('Script loaded');
46
- }
47
- else if (message === 'SSWP_HMR_FAIL') {
48
- this.workerLoaded = false;
49
- }
50
- };
51
- this.afterEmit = (compilation, callback) => {
52
- const entryFile = this.getEntryFile(compilation);
53
- if (entryFile) {
54
- this.entryFile = entryFile;
55
- }
56
- if (this.worker) {
57
- this.hmrWorker(compilation, callback);
58
- }
59
- else if (this.entryFile) {
60
- this.runWorker(this.entryFile, callback);
61
- }
62
- };
63
- this.apply = (compiler) => {
64
- const plugin = { name: 'StartServerPlugin' };
65
- compiler.hooks.make.tap(plugin, (compilation) => {
66
- compilation.addEntry(compilation.compiler.context, webpack_1.EntryPlugin.createDependency(this.getMonitor(), {
67
- name: this.options.entryName,
68
- }), this.options.entryName, () => { });
69
- });
70
- compiler.hooks.afterEmit.tapAsync(plugin, this.afterEmit);
71
- };
72
- this.options = {
73
- verbose: true,
74
- entryName: 'index',
75
- ...options,
76
- };
14
+ this.logger = new start_server_logger_1.StartServerLogger(options);
15
+ this.options = options;
77
16
  }
78
- getEntryFile(compilation) {
79
- const { entryName } = this.options;
80
- const { entrypoints } = compilation;
81
- const entry = entrypoints.get ? entrypoints.get(entryName) : entrypoints[entryName];
82
- if (!entry) {
83
- throw new Error(`Requested entry "${entryName}" does not exist, try one of: ${(entrypoints.keys
84
- ? entrypoints.keys()
85
- : Object.keys(entrypoints)).join(' ')}`);
17
+ apply = (compiler) => {
18
+ compiler.hooks.afterEmit.tapAsync({ name: 'StartServerPlugin' }, this.afterEmit);
19
+ };
20
+ afterEmit = (compilation, callback) => {
21
+ if (!this.initialized) {
22
+ this.initialized = true;
23
+ callback();
86
24
  }
87
- const entryScript = webpack_1.EntryPlugin
88
- ? entry._runtimeChunk.files.values().next().value
89
- : entry.chunks[0].files[0];
90
- if (!entryScript) {
91
- this.error(`Entry chunk not outputted: ${entry}`);
92
- return null;
25
+ else {
26
+ if (this.worker?.connected && this.worker.pid) {
27
+ process.kill(this.worker.pid);
28
+ }
29
+ this.startServer(compilation, callback);
93
30
  }
94
- const { path } = compilation.outputOptions;
95
- return path_1.default.resolve(path, entryScript);
96
- }
31
+ };
32
+ startServer = (compilation, callback) => {
33
+ this.logger.info('Starting server...');
34
+ this.entryFile = (0, node_path_1.join)(compilation.compiler.options.output.path, 'index.js');
35
+ this.runWorker(this.entryFile, (worker) => {
36
+ this.worker = worker;
37
+ callback();
38
+ });
39
+ };
97
40
  runWorker(entryFile, callback) {
98
- if (this.worker)
99
- return;
100
- if (this.options.verbose) {
101
- this.info(`running \`node ${entryFile}\``);
102
- }
103
- const worker = (0, child_process_1.fork)(entryFile, [], {
41
+ const worker = (0, node_child_process_1.fork)(entryFile, [], {
104
42
  silent: true,
105
43
  });
106
- worker.once('exit', this.handleWorkerExit);
107
- worker.once('error', this.handleWorkerError);
108
- worker.on('message', this.handleWorkerMessage);
109
44
  if (this.options.stdout) {
110
45
  worker.stdout?.pipe(this.options.stdout, { end: false });
111
46
  }
112
47
  if (this.options.stderr) {
113
48
  worker.stderr?.pipe(this.options.stderr, { end: false });
114
49
  }
115
- this.worker = worker;
116
- if (this.options.onWorkerStart) {
117
- this.options.onWorkerStart(worker);
118
- }
119
- if (callback)
120
- callback();
121
- }
122
- hmrWorker(compilation, callback) {
123
- if (this.worker?.send) {
124
- this.worker.send('SSWP_HMR');
125
- }
126
- else {
127
- this.error('hot reloaded but no way to tell the worker');
128
- }
129
- callback();
130
- }
131
- getMonitor() {
132
- const loaderPath = require.resolve('./monitor.loader');
133
- return `!!${loaderPath}!${loaderPath}`;
134
- }
135
- info(body) {
136
- if (this.options.stdout) {
137
- this.options.stdout.write(Buffer.from(JSON.stringify({
138
- severityText: 'INFO',
139
- name: 'start-server',
140
- body,
141
- })));
142
- }
143
- }
144
- error(body) {
145
- if (this.options.stderr) {
146
- this.options.stderr.write(Buffer.from(JSON.stringify({
147
- severityText: 'ERROR',
148
- name: 'start-server',
149
- body,
150
- })));
151
- }
50
+ setTimeout(() => {
51
+ callback(worker);
52
+ }, 0);
152
53
  }
153
54
  }
154
55
  exports.StartServerPlugin = StartServerPlugin;
56
+ //# sourceMappingURL=start-server.plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start-server.plugin.js","sourceRoot":"","sources":["../src/start-server.plugin.ts"],"names":[],"mappings":";;;AACA,2DAAsD;AACtD,yCAA6C;AAK7C,+DAAyD;AAOzD,MAAa,iBAAiB;IAC5B,OAAO,CAA0B;IAEjC,SAAS,GAAkB,IAAI,CAAA;IAE/B,MAAM,GAAwB,IAAI,CAAA;IAElC,WAAW,GAAY,KAAK,CAAA;IAE5B,MAAM,CAAmB;IAEzB,YAAY,UAA6C,EAAE;QACzD,IAAI,CAAC,MAAM,GAAG,IAAI,uCAAiB,CAAC,OAAO,CAAC,CAAA;QAC5C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;IAED,KAAK,GAAG,CAAC,QAA0B,EAAQ,EAAE;QAC3C,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;IAClF,CAAC,CAAA;IAEO,SAAS,GAAG,CAAC,WAAgC,EAAE,QAAoB,EAAQ,EAAE;QACnF,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;YACrB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAA;YAEvB,QAAQ,EAAE,CAAA;SACX;aAAM;YACL,IAAI,IAAI,CAAC,MAAM,EAAE,SAAS,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;gBAC7C,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;aAC9B;YAED,IAAI,CAAC,WAAW,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAA;SACxC;IACH,CAAC,CAAA;IAEO,WAAW,GAAG,CAAC,WAAgC,EAAE,QAAoB,EAAQ,EAAE;QACrF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAA;QAEtC,IAAI,CAAC,SAAS,GAAG,IAAA,gBAAI,EAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,IAAK,EAAE,UAAU,CAAC,CAAA;QAE5E,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,EAAE;YACxC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;YAEpB,QAAQ,EAAE,CAAA;QACZ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;IAEO,SAAS,CAAC,SAAiB,EAAE,QAAsC;QACzE,MAAM,MAAM,GAAG,IAAA,yBAAI,EAAC,SAAS,EAAE,EAAE,EAAE;YACjC,MAAM,EAAE,IAAI;SACb,CAAC,CAAA;QAEF,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;YACvB,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAA;SACzD;QAED,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;YACvB,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAA;SACzD;QAED,UAAU,CAAC,GAAG,EAAE;YACd,QAAQ,CAAC,MAAM,CAAC,CAAA;QAClB,CAAC,EAAE,CAAC,CAAC,CAAA;IACP,CAAC;CACF;AA/DD,8CA+DC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atls/webpack-start-server-plugin",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "license": "BSD-3-Clause",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -12,10 +12,10 @@
12
12
  "postpack": "rm -rf dist"
13
13
  },
14
14
  "dependencies": {
15
- "webpack": "^5.70.0"
15
+ "webpack": "5.91.0"
16
16
  },
17
17
  "devDependencies": {
18
- "@types/node": "^17.0.21"
18
+ "@types/node": "20"
19
19
  },
20
20
  "publishConfig": {
21
21
  "access": "public",
package/dist/monitor.js DELETED
@@ -1,98 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const monitorFn = () => {
4
- if (module.hot) {
5
- const log = (type, msg) => {
6
- console.log(JSON.stringify({
7
- severityText: type.toUpperCase(),
8
- name: 'start-server:monitor',
9
- body: msg,
10
- }));
11
- };
12
- function logApplyRecult(updatedModules, renewedModules) {
13
- var unacceptedModules = updatedModules.filter(function (moduleId) {
14
- return renewedModules && renewedModules.indexOf(moduleId) < 0;
15
- });
16
- if (unacceptedModules.length > 0) {
17
- log('warn', "[HMR] The following modules couldn't be hot updated: (They would need a full reload!)");
18
- unacceptedModules.forEach(function (moduleId) {
19
- log('warn', '[HMR] - ' + moduleId);
20
- });
21
- }
22
- if (!renewedModules || renewedModules.length === 0) {
23
- log('info', '[HMR] Nothing hot updated.');
24
- }
25
- else {
26
- log('info', '[HMR] Updated modules:');
27
- renewedModules.forEach(function (moduleId) {
28
- if (typeof moduleId === 'string' && moduleId.indexOf('!') !== -1) {
29
- var parts = moduleId.split('!');
30
- log('info', '[HMR] - ' + moduleId);
31
- }
32
- else {
33
- log('info', '[HMR] - ' + moduleId);
34
- }
35
- });
36
- var numberIds = renewedModules.every(function (moduleId) {
37
- return typeof moduleId === 'number';
38
- });
39
- if (numberIds)
40
- log('info', '[HMR] Consider using the optimization.moduleIds: "named" for module names.');
41
- }
42
- }
43
- log('info', 'Handling Hot Module Reloading');
44
- var checkForUpdate = function checkForUpdate(fromUpdate) {
45
- module.hot
46
- .check()
47
- .then(function (updatedModules) {
48
- if (!updatedModules) {
49
- if (fromUpdate)
50
- log('info', 'Update applied.');
51
- else
52
- log('warn', 'Cannot find update.');
53
- return;
54
- }
55
- return module.hot
56
- .apply({
57
- ignoreUnaccepted: true,
58
- onUnaccepted: function (data) {
59
- log('warn', '\u0007Ignored an update to unaccepted module ' + data.chain.join(' -> '));
60
- },
61
- })
62
- .then(function (renewedModules) {
63
- logApplyRecult(updatedModules, renewedModules);
64
- checkForUpdate(true);
65
- });
66
- })
67
- .catch(function (err) {
68
- var status = module.hot.status();
69
- if (['abort', 'fail'].indexOf(status) >= 0) {
70
- if (process.send) {
71
- process.send('SSWP_HMR_FAIL');
72
- }
73
- log('warn', 'Cannot apply update.');
74
- log('warn', err);
75
- log('error', 'Quitting process - will reload on next file change\u0007\n\u0007\n\u0007');
76
- process.exit(222);
77
- }
78
- else {
79
- log('warn', 'Update failed: ' + err.stack || err.message);
80
- }
81
- });
82
- };
83
- process.on('message', function (message) {
84
- if (message !== 'SSWP_HMR')
85
- return;
86
- if (module.hot.status() !== 'idle') {
87
- log('warn', 'Got signal but currently in ' + module.hot.status() + ' state.');
88
- log('warn', 'Need to be in idle state to start hot update.');
89
- return;
90
- }
91
- checkForUpdate();
92
- });
93
- }
94
- if (process.send) {
95
- process.send('SSWP_LOADED');
96
- }
97
- };
98
- exports.default = monitorFn;
@@ -1,11 +0,0 @@
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
- const monitor_1 = __importDefault(require("./monitor"));
7
- const monitorSrc = `(${monitor_1.default.toString()})()`;
8
- const loader = function () {
9
- return monitorSrc;
10
- };
11
- exports.default = loader;