commandkit 0.1.7 → 0.1.8
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/bin/build.mjs +30 -10
- package/bin/development.mjs +4 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -1
- package/dist/index.mjs +2 -1
- package/package.json +1 -1
package/bin/build.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { build } from 'tsup';
|
|
6
6
|
import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs';
|
|
@@ -14,6 +14,7 @@ export async function bootstrapProductionBuild(config) {
|
|
|
14
14
|
antiCrash = true,
|
|
15
15
|
src,
|
|
16
16
|
main,
|
|
17
|
+
requirePolyfill: polyfillRequire,
|
|
17
18
|
} = await findCommandKitConfig(config);
|
|
18
19
|
|
|
19
20
|
const status = ora('Creating optimized production build...\n').start();
|
|
@@ -39,7 +40,7 @@ export async function bootstrapProductionBuild(config) {
|
|
|
39
40
|
entry: [src, '!dist', '!.commandkit', `!${outDir}`],
|
|
40
41
|
});
|
|
41
42
|
|
|
42
|
-
await injectShims(outDir, main, antiCrash);
|
|
43
|
+
await injectShims(outDir, main, antiCrash, polyfillRequire);
|
|
43
44
|
|
|
44
45
|
status.succeed(
|
|
45
46
|
Colors.green(`Build completed in ${(performance.now() - start).toFixed(2)}ms!`),
|
|
@@ -55,14 +56,33 @@ export async function bootstrapProductionBuild(config) {
|
|
|
55
56
|
}
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
async function injectShims(outDir, main, antiCrash) {
|
|
59
|
+
export async function injectShims(outDir, main, antiCrash, polyfillRequire) {
|
|
59
60
|
const path = join(process.cwd(), outDir, main);
|
|
60
61
|
|
|
62
|
+
const head = ['\n\n;await (async()=>{', " 'use strict';"].join('\n');
|
|
63
|
+
const tail = '\n})();';
|
|
64
|
+
const requireScript = polyfillRequire
|
|
65
|
+
? [
|
|
66
|
+
'// --- CommandKit require() polyfill ---',
|
|
67
|
+
' if (typeof require === "undefined") {',
|
|
68
|
+
' const { createRequire } = await import("node:module");',
|
|
69
|
+
' const __require = createRequire(import.meta.url);',
|
|
70
|
+
' Object.defineProperty(globalThis, "require", {',
|
|
71
|
+
' value: (id) => {',
|
|
72
|
+
' return __require(id);',
|
|
73
|
+
' },',
|
|
74
|
+
' configurable: true,',
|
|
75
|
+
' enumerable: false,',
|
|
76
|
+
' writable: true,',
|
|
77
|
+
' });',
|
|
78
|
+
' }',
|
|
79
|
+
'// --- CommandKit require() polyfill ---',
|
|
80
|
+
].join('\n')
|
|
81
|
+
: '';
|
|
82
|
+
|
|
61
83
|
const antiCrashScript = antiCrash
|
|
62
84
|
? [
|
|
63
|
-
'
|
|
64
|
-
';(()=>{',
|
|
65
|
-
" 'use strict';",
|
|
85
|
+
'// --- CommandKit Anti-Crash Monitor ---',
|
|
66
86
|
" // 'uncaughtException' event is supposed to be used to perform synchronous cleanup before shutting down the process",
|
|
67
87
|
' // instead of using it as a means to resume operation.',
|
|
68
88
|
' // But it exists here due to compatibility reasons with discord bot ecosystem.',
|
|
@@ -75,12 +95,12 @@ async function injectShims(outDir, main, antiCrash) {
|
|
|
75
95
|
' process.on(e2, (r) => {',
|
|
76
96
|
' l(p(`${b} Unhandled promise rejection`)); l(p(`${b} ${r.stack || r}`));',
|
|
77
97
|
' });',
|
|
78
|
-
'
|
|
79
|
-
'// --- CommandKit Anti-Crash Monitor ---\n',
|
|
98
|
+
'// --- CommandKit Anti-Crash Monitor ---',
|
|
80
99
|
].join('\n')
|
|
81
100
|
: '';
|
|
82
101
|
|
|
83
|
-
const
|
|
102
|
+
const contents = await readFile(path, 'utf-8');
|
|
103
|
+
const finalScript = [head, requireScript, antiCrashScript, tail, '\n\n', contents].join('\n');
|
|
84
104
|
|
|
85
|
-
return
|
|
105
|
+
return writeFile(path, finalScript);
|
|
86
106
|
}
|
package/bin/development.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs'
|
|
|
6
6
|
import { parseEnv } from './parse-env.mjs';
|
|
7
7
|
import child_process from 'node:child_process';
|
|
8
8
|
import ora from 'ora';
|
|
9
|
+
import { injectShims } from './build.mjs';
|
|
9
10
|
|
|
10
11
|
const RESTARTING_MSG_PATTERN = /^Restarting '|".+'|"\n?$/;
|
|
11
12
|
const FAILED_RUNNING_PATTERN = /^Failed running '.+'|"\n?$/;
|
|
@@ -19,6 +20,7 @@ export async function bootstrapDevelopmentServer(opts) {
|
|
|
19
20
|
envExtra = true,
|
|
20
21
|
clearRestartLogs = true,
|
|
21
22
|
outDir,
|
|
23
|
+
requirePolyfill,
|
|
22
24
|
} = await findCommandKitConfig(opts.config);
|
|
23
25
|
|
|
24
26
|
if (!src) {
|
|
@@ -61,6 +63,8 @@ export async function bootstrapDevelopmentServer(opts) {
|
|
|
61
63
|
watch: watchMode,
|
|
62
64
|
});
|
|
63
65
|
|
|
66
|
+
await injectShims('.commandkit', main, false, requirePolyfill);
|
|
67
|
+
|
|
64
68
|
status.succeed(
|
|
65
69
|
Colors.green(`Dev server started in ${(performance.now() - start).toFixed(2)}ms!\n`),
|
|
66
70
|
);
|
package/dist/index.d.mts
CHANGED
|
@@ -344,6 +344,10 @@ interface CommandKitConfig {
|
|
|
344
344
|
* Whether or not to include anti-crash handler in production. Defaults to `true`.
|
|
345
345
|
*/
|
|
346
346
|
antiCrash: boolean;
|
|
347
|
+
/**
|
|
348
|
+
* Whether or not to polyfill `require` function. Defaults to `true`.
|
|
349
|
+
*/
|
|
350
|
+
requirePolyfill: boolean;
|
|
347
351
|
}
|
|
348
352
|
declare function getConfig(): CommandKitConfig;
|
|
349
353
|
declare const requiredProps: readonly ["src", "main"];
|
package/dist/index.d.ts
CHANGED
|
@@ -344,6 +344,10 @@ interface CommandKitConfig {
|
|
|
344
344
|
* Whether or not to include anti-crash handler in production. Defaults to `true`.
|
|
345
345
|
*/
|
|
346
346
|
antiCrash: boolean;
|
|
347
|
+
/**
|
|
348
|
+
* Whether or not to polyfill `require` function. Defaults to `true`.
|
|
349
|
+
*/
|
|
350
|
+
requirePolyfill: boolean;
|
|
347
351
|
}
|
|
348
352
|
declare function getConfig(): CommandKitConfig;
|
|
349
353
|
declare const requiredProps: readonly ["src", "main"];
|
package/dist/index.js
CHANGED
package/dist/index.mjs
CHANGED