@atls/webpack-start-server-plugin 0.0.6 → 1.0.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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -17
- package/dist/start-server.logger.d.ts +12 -0
- package/dist/start-server.logger.js +28 -0
- package/dist/start-server.plugin.d.ts +12 -19
- package/dist/start-server.plugin.js +27 -132
- package/package.json +22 -5
- package/dist/monitor.d.ts +0 -2
- package/dist/monitor.js +0 -98
- package/dist/monitor.loader.d.ts +0 -2
- package/dist/monitor.loader.js +0 -11
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './start-server.plugin';
|
|
1
|
+
export * from './start-server.plugin.js';
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./start-server.plugin"), exports);
|
|
1
|
+
export * from "./start-server.plugin.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
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
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export class StartServerLogger {
|
|
2
|
+
constructor(options = {}) {
|
|
3
|
+
this.options = options;
|
|
4
|
+
}
|
|
5
|
+
info(body) {
|
|
6
|
+
if (this.options.stdout) {
|
|
7
|
+
this.options.stdout.write(Buffer.from(JSON.stringify({
|
|
8
|
+
body,
|
|
9
|
+
severityNumber: 9,
|
|
10
|
+
attributes: {
|
|
11
|
+
'@namespace': 'webpack:start-server',
|
|
12
|
+
},
|
|
13
|
+
})));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
error(error) {
|
|
17
|
+
if (this.options.stderr) {
|
|
18
|
+
this.options.stderr.write(Buffer.from(JSON.stringify({
|
|
19
|
+
body: error.message,
|
|
20
|
+
severityNumber: 17,
|
|
21
|
+
attributes: {
|
|
22
|
+
'@namespace': 'webpack:start-server',
|
|
23
|
+
'@stack': error.stack,
|
|
24
|
+
},
|
|
25
|
+
})));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -1,29 +1,22 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
|
|
3
|
-
import {
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
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.js';
|
|
4
7
|
export interface StartServerPluginOptions {
|
|
5
|
-
verbose: boolean;
|
|
6
|
-
entryName: string;
|
|
7
8
|
stdout?: Writable;
|
|
8
9
|
stderr?: Writable;
|
|
9
|
-
onWorkerStart?: (workeer: ChildProcess) => void;
|
|
10
|
-
onWorkerExit?: () => void;
|
|
11
10
|
}
|
|
12
11
|
export declare class StartServerPlugin {
|
|
13
12
|
options: StartServerPluginOptions;
|
|
14
13
|
entryFile: string | null;
|
|
15
14
|
worker: ChildProcess | null;
|
|
16
|
-
|
|
15
|
+
initialized: boolean;
|
|
16
|
+
logger: StartServerLogger;
|
|
17
17
|
constructor(options?: Partial<StartServerPluginOptions>);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
runWorker(entryFile: string, callback?: any): void;
|
|
23
|
-
hmrWorker(compilation: any, callback: any): void;
|
|
24
|
-
afterEmit: (compilation: any, callback: any) => void;
|
|
25
|
-
getMonitor(): string;
|
|
26
|
-
apply: (compiler: any) => void;
|
|
27
|
-
info(body: any): void;
|
|
28
|
-
error(body: any): void;
|
|
18
|
+
apply: (compiler: webpack.Compiler) => void;
|
|
19
|
+
private afterEmit;
|
|
20
|
+
private startServer;
|
|
21
|
+
private runWorker;
|
|
29
22
|
}
|
|
@@ -1,154 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
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");
|
|
10
|
-
class StartServerPlugin {
|
|
1
|
+
import { fork } from 'node:child_process';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { StartServerLogger } from './start-server.logger.js';
|
|
4
|
+
export class StartServerPlugin {
|
|
11
5
|
constructor(options = {}) {
|
|
12
6
|
this.entryFile = null;
|
|
13
7
|
this.worker = null;
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
16
|
-
|
|
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
|
-
}
|
|
8
|
+
this.initialized = false;
|
|
9
|
+
this.apply = (compiler) => {
|
|
10
|
+
compiler.hooks.afterEmit.tapAsync({ name: 'StartServerPlugin' }, this.afterEmit);
|
|
50
11
|
};
|
|
51
12
|
this.afterEmit = (compilation, callback) => {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
if (this.worker) {
|
|
57
|
-
this.hmrWorker(compilation, callback);
|
|
13
|
+
if (!this.initialized) {
|
|
14
|
+
this.initialized = true;
|
|
15
|
+
callback();
|
|
58
16
|
}
|
|
59
|
-
else
|
|
60
|
-
this.
|
|
17
|
+
else {
|
|
18
|
+
if (this.worker?.connected && this.worker.pid) {
|
|
19
|
+
process.kill(this.worker.pid);
|
|
20
|
+
}
|
|
21
|
+
this.startServer(compilation, callback);
|
|
61
22
|
}
|
|
62
23
|
};
|
|
63
|
-
this.
|
|
64
|
-
|
|
65
|
-
compiler.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
24
|
+
this.startServer = (compilation, callback) => {
|
|
25
|
+
this.logger.info('Starting server...');
|
|
26
|
+
this.entryFile = join(compilation.compiler.options.output.path, 'index.js');
|
|
27
|
+
this.runWorker(this.entryFile, (worker) => {
|
|
28
|
+
this.worker = worker;
|
|
29
|
+
callback();
|
|
69
30
|
});
|
|
70
|
-
compiler.hooks.afterEmit.tapAsync(plugin, this.afterEmit);
|
|
71
|
-
};
|
|
72
|
-
this.options = {
|
|
73
|
-
verbose: true,
|
|
74
|
-
entryName: 'index',
|
|
75
|
-
...options,
|
|
76
31
|
};
|
|
77
|
-
|
|
78
|
-
|
|
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(' ')}`);
|
|
86
|
-
}
|
|
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;
|
|
93
|
-
}
|
|
94
|
-
const { path } = compilation.outputOptions;
|
|
95
|
-
return path_1.default.resolve(path, entryScript);
|
|
32
|
+
this.logger = new StartServerLogger(options);
|
|
33
|
+
this.options = options;
|
|
96
34
|
}
|
|
97
35
|
runWorker(entryFile, callback) {
|
|
98
|
-
|
|
99
|
-
return;
|
|
100
|
-
if (this.options.verbose) {
|
|
101
|
-
this.info(`running \`node ${entryFile}\``);
|
|
102
|
-
}
|
|
103
|
-
const worker = (0, child_process_1.fork)(entryFile, [], {
|
|
36
|
+
const worker = fork(entryFile, [], {
|
|
104
37
|
silent: true,
|
|
105
38
|
});
|
|
106
|
-
worker.once('exit', this.handleWorkerExit);
|
|
107
|
-
worker.once('error', this.handleWorkerError);
|
|
108
|
-
worker.on('message', this.handleWorkerMessage);
|
|
109
39
|
if (this.options.stdout) {
|
|
110
40
|
worker.stdout?.pipe(this.options.stdout, { end: false });
|
|
111
41
|
}
|
|
112
42
|
if (this.options.stderr) {
|
|
113
43
|
worker.stderr?.pipe(this.options.stderr, { end: false });
|
|
114
44
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
}
|
|
45
|
+
setTimeout(() => {
|
|
46
|
+
callback(worker);
|
|
47
|
+
}, 0);
|
|
152
48
|
}
|
|
153
49
|
}
|
|
154
|
-
exports.StartServerPlugin = StartServerPlugin;
|
package/package.json
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atls/webpack-start-server-plugin",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"license": "BSD-3-Clause",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./package.json": "./package.json",
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
5
14
|
"main": "dist/index.js",
|
|
6
15
|
"files": [
|
|
7
16
|
"dist"
|
|
@@ -12,15 +21,23 @@
|
|
|
12
21
|
"postpack": "rm -rf dist"
|
|
13
22
|
},
|
|
14
23
|
"dependencies": {
|
|
15
|
-
"webpack": "^5.
|
|
24
|
+
"webpack": "^5.88.1"
|
|
16
25
|
},
|
|
17
26
|
"devDependencies": {
|
|
18
|
-
"@types/node": "^
|
|
27
|
+
"@types/node": "^20.14.9"
|
|
19
28
|
},
|
|
20
29
|
"publishConfig": {
|
|
21
30
|
"access": "public",
|
|
31
|
+
"exports": {
|
|
32
|
+
"./package.json": "./package.json",
|
|
33
|
+
".": {
|
|
34
|
+
"import": "./dist/index.js",
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"default": "./dist/index.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
22
39
|
"main": "dist/index.js",
|
|
23
|
-
"
|
|
40
|
+
"types": "dist/index.d.ts"
|
|
24
41
|
},
|
|
25
|
-
"
|
|
42
|
+
"types": "dist/index.d.ts"
|
|
26
43
|
}
|
package/dist/monitor.d.ts
DELETED
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;
|
package/dist/monitor.loader.d.ts
DELETED
package/dist/monitor.loader.js
DELETED
|
@@ -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;
|