@utoo/pack 1.2.13 → 1.3.0-alpha.1
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/cjs/binding.d.ts +5 -31
- package/cjs/commands/build.js +6 -11
- package/cjs/commands/dev-legacy.d.ts +44 -0
- package/cjs/commands/dev-legacy.js +458 -0
- package/cjs/commands/dev.d.ts +6 -31
- package/cjs/commands/dev.js +142 -365
- package/cjs/core/hmr.d.ts +14 -0
- package/cjs/core/hmr.js +72 -7
- package/cjs/utils/common.d.ts +1 -1
- package/cjs/utils/common.js +1 -2
- package/config_schema.json +14 -0
- package/esm/binding.d.ts +5 -31
- package/esm/commands/build.js +7 -12
- package/esm/commands/dev-legacy.d.ts +44 -0
- package/esm/commands/dev-legacy.js +442 -0
- package/esm/commands/dev.d.ts +6 -31
- package/esm/commands/dev.js +143 -356
- package/esm/core/hmr.d.ts +14 -0
- package/esm/core/hmr.js +73 -8
- package/esm/utils/common.d.ts +1 -1
- package/esm/utils/common.js +1 -1
- package/package.json +13 -9
package/cjs/binding.d.ts
CHANGED
|
@@ -9,22 +9,6 @@ export declare class ExternalObject<T> {
|
|
|
9
9
|
[K: symbol]: T
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
/** Arguments for `NapiTurbopackCallbacks::throw_turbopack_internal_error`. */
|
|
13
|
-
export interface TurbopackInternalErrorOpts {
|
|
14
|
-
message: string
|
|
15
|
-
anonymizedLocation?: string
|
|
16
|
-
}
|
|
17
|
-
export interface NapiTurbopackCallbacksJsObject {
|
|
18
|
-
/**
|
|
19
|
-
* Called when we've encountered a bug in Turbopack and not in the user's code. Constructs and
|
|
20
|
-
* throws a `TurbopackInternalError` type. Logs to anonymized telemetry.
|
|
21
|
-
*
|
|
22
|
-
* As a result of the use of `ErrorStrategy::CalleeHandled`, the first argument is an error if
|
|
23
|
-
* there's a runtime conversion error. This should never happen, but if it does, the function
|
|
24
|
-
* can throw it instead.
|
|
25
|
-
*/
|
|
26
|
-
throwTurbopackInternalError: (conversionError: Error | null, opts: TurbopackInternalErrorOpts) => never
|
|
27
|
-
}
|
|
28
12
|
export declare function registerWorkerScheduler(creator: (arg: NapiWorkerCreation) => any, terminator: (arg: NapiWorkerTermination) => any): void
|
|
29
13
|
export declare function workerCreated(workerId: number): void
|
|
30
14
|
export interface NapiWorkerCreation {
|
|
@@ -93,11 +77,6 @@ export interface NapiProjectOptions {
|
|
|
93
77
|
config: string
|
|
94
78
|
/** A map of environment variables to use when compiling code. */
|
|
95
79
|
processEnv: Array<NapiEnvVar>
|
|
96
|
-
/**
|
|
97
|
-
* A map of environment variables which should get injected at compile
|
|
98
|
-
* time.
|
|
99
|
-
*/
|
|
100
|
-
defineEnv: NapiDefineEnv
|
|
101
80
|
/** The mode in which Next.js is running. */
|
|
102
81
|
dev: boolean
|
|
103
82
|
/** The build id. */
|
|
@@ -119,11 +98,6 @@ export interface NapiPartialProjectOptions {
|
|
|
119
98
|
config?: string
|
|
120
99
|
/** A map of environment variables to use when compiling code. */
|
|
121
100
|
processEnv?: Array<NapiEnvVar>
|
|
122
|
-
/**
|
|
123
|
-
* A map of environment variables which should get injected at compile
|
|
124
|
-
* time.
|
|
125
|
-
*/
|
|
126
|
-
defineEnv?: NapiDefineEnv
|
|
127
101
|
/** The mode in which Next.js is running. */
|
|
128
102
|
dev?: boolean
|
|
129
103
|
/** The build id. */
|
|
@@ -136,11 +110,6 @@ export interface NapiPartialProjectOptions {
|
|
|
136
110
|
noMangling?: boolean
|
|
137
111
|
packPath?: string
|
|
138
112
|
}
|
|
139
|
-
export interface NapiDefineEnv {
|
|
140
|
-
client: Array<NapiEnvVar>
|
|
141
|
-
edge: Array<NapiEnvVar>
|
|
142
|
-
nodejs: Array<NapiEnvVar>
|
|
143
|
-
}
|
|
144
113
|
export interface NapiTurboEngineOptions {
|
|
145
114
|
/** Use the new backend with persistent caching enabled. */
|
|
146
115
|
persistentCaching?: boolean
|
|
@@ -214,6 +183,11 @@ export declare function projectTraceSource(project: { __napiType: "Project" }, f
|
|
|
214
183
|
export declare function projectGetSourceForAsset(project: { __napiType: "Project" }, filePath: string): Promise<string | null>
|
|
215
184
|
export declare function projectGetSourceMap(project: { __napiType: "Project" }, filePath: RcStr): Promise<string | null>
|
|
216
185
|
export declare function projectGetSourceMapSync(project: { __napiType: "Project" }, filePath: RcStr): string | null
|
|
186
|
+
/** Arguments for `NapiTurbopackCallbacks::throw_turbopack_internal_error`. */
|
|
187
|
+
export interface TurbopackInternalErrorOpts {
|
|
188
|
+
message: string
|
|
189
|
+
anonymizedLocation?: string
|
|
190
|
+
}
|
|
217
191
|
/**
|
|
218
192
|
* A version of [`NapiTurbopackCallbacks`] that can accepted as an argument to a napi function.
|
|
219
193
|
*
|
package/cjs/commands/build.js
CHANGED
|
@@ -28,7 +28,7 @@ function build(options, projectPath, rootPath) {
|
|
|
28
28
|
return buildInternal(bundleOptions, projectPath, rootPath);
|
|
29
29
|
}
|
|
30
30
|
async function buildInternal(bundleOptions, projectPath, rootPath) {
|
|
31
|
-
var _a, _b, _c, _d, _e, _f
|
|
31
|
+
var _a, _b, _c, _d, _e, _f;
|
|
32
32
|
(0, common_1.blockStdout)();
|
|
33
33
|
if (process.env.XCODE_PROFILE) {
|
|
34
34
|
await (0, xcodeProfile_1.xcodeProfilingReady)();
|
|
@@ -39,15 +39,10 @@ async function buildInternal(bundleOptions, projectPath, rootPath) {
|
|
|
39
39
|
const createProject = (0, project_1.projectFactory)();
|
|
40
40
|
const project = await createProject({
|
|
41
41
|
processEnv: (_a = bundleOptions.processEnv) !== null && _a !== void 0 ? _a : {},
|
|
42
|
-
defineEnv: (0, common_1.createDefineEnv)({
|
|
43
|
-
config: bundleOptions.config,
|
|
44
|
-
dev: (_b = bundleOptions.dev) !== null && _b !== void 0 ? _b : false,
|
|
45
|
-
optionDefineEnv: bundleOptions.defineEnv,
|
|
46
|
-
}),
|
|
47
42
|
watch: {
|
|
48
43
|
enable: false,
|
|
49
44
|
},
|
|
50
|
-
dev: (
|
|
45
|
+
dev: (_b = bundleOptions.dev) !== null && _b !== void 0 ? _b : false,
|
|
51
46
|
buildId: bundleOptions.buildId || (0, nanoid_1.nanoid)(),
|
|
52
47
|
config: {
|
|
53
48
|
...bundleOptions.config,
|
|
@@ -59,7 +54,7 @@ async function buildInternal(bundleOptions, projectPath, rootPath) {
|
|
|
59
54
|
rootPath: rootPath || projectPath || process.cwd(),
|
|
60
55
|
packPath: (0, common_1.getPackPath)(),
|
|
61
56
|
}, {
|
|
62
|
-
persistentCaching: (
|
|
57
|
+
persistentCaching: (_c = bundleOptions.config.persistentCaching) !== null && _c !== void 0 ? _c : false,
|
|
63
58
|
});
|
|
64
59
|
const entrypoints = await project.writeAllEntrypointsToDisk();
|
|
65
60
|
(0, pack_shared_1.handleIssues)(entrypoints.issues);
|
|
@@ -75,20 +70,20 @@ async function buildInternal(bundleOptions, projectPath, rootPath) {
|
|
|
75
70
|
];
|
|
76
71
|
if (htmlConfigs.length > 0) {
|
|
77
72
|
const assets = { js: [], css: [] };
|
|
78
|
-
const outputDir = ((
|
|
73
|
+
const outputDir = ((_d = bundleOptions.config.output) === null || _d === void 0 ? void 0 : _d.path) || path_1.default.join(process.cwd(), "dist");
|
|
79
74
|
if (assets.js.length === 0 && assets.css.length === 0) {
|
|
80
75
|
const discovered = (0, getInitialAssets_1.getInitialAssetsFromStats)(outputDir);
|
|
81
76
|
assets.js.push(...discovered.js);
|
|
82
77
|
assets.css.push(...discovered.css);
|
|
83
78
|
}
|
|
84
|
-
const publicPath = (
|
|
79
|
+
const publicPath = (_e = bundleOptions.config.output) === null || _e === void 0 ? void 0 : _e.publicPath;
|
|
85
80
|
for (const config of htmlConfigs) {
|
|
86
81
|
const plugin = new HtmlPlugin_1.HtmlPlugin(config);
|
|
87
82
|
await plugin.generate(outputDir, assets, publicPath);
|
|
88
83
|
}
|
|
89
84
|
}
|
|
90
85
|
if (process.env.ANALYZE) {
|
|
91
|
-
await analyzeBundle(((
|
|
86
|
+
await analyzeBundle(((_f = bundleOptions.config.output) === null || _f === void 0 ? void 0 : _f.path) || "dist");
|
|
92
87
|
}
|
|
93
88
|
await project.shutdown();
|
|
94
89
|
// TODO: Maybe run tasks in worker is a better way, see
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from "http";
|
|
2
|
+
import send from "send";
|
|
3
|
+
import { Duplex, Writable } from "stream";
|
|
4
|
+
import { BundleOptions } from "../config/types";
|
|
5
|
+
import { WebpackConfig } from "../config/webpackCompat";
|
|
6
|
+
export declare function serve(options: BundleOptions | WebpackConfig, projectPath?: string, rootPath?: string, serverOptions?: StartServerOptions): Promise<void>;
|
|
7
|
+
export interface SelfSignedCertificate {
|
|
8
|
+
key: string;
|
|
9
|
+
cert: string;
|
|
10
|
+
rootCA?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface StartServerOptions {
|
|
13
|
+
port: number;
|
|
14
|
+
https?: boolean;
|
|
15
|
+
hostname?: string;
|
|
16
|
+
logServerInfo?: boolean;
|
|
17
|
+
selfSignedCertificate?: SelfSignedCertificate;
|
|
18
|
+
}
|
|
19
|
+
export type RequestHandler = (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
20
|
+
export type UpgradeHandler = (req: IncomingMessage, socket: Duplex, head: Buffer) => Promise<void>;
|
|
21
|
+
export type ServerInitResult = {
|
|
22
|
+
requestHandler: RequestHandler;
|
|
23
|
+
upgradeHandler: UpgradeHandler;
|
|
24
|
+
closeUpgraded: () => void;
|
|
25
|
+
};
|
|
26
|
+
export declare function startServer(serverOptions: StartServerOptions, bundleOptions: BundleOptions, projectPath: string, rootPath?: string): Promise<void>;
|
|
27
|
+
export declare function initialize(bundleOptions: BundleOptions, projectPath: string, rootPath?: string): Promise<ServerInitResult>;
|
|
28
|
+
export declare function pipeToNodeResponse(readable: ReadableStream<Uint8Array>, res: ServerResponse, waitUntilForEnd?: Promise<unknown>): Promise<void>;
|
|
29
|
+
export declare function createAbortController(response: Writable): AbortController;
|
|
30
|
+
export declare function isAbortError(e: any): e is Error & {
|
|
31
|
+
name: "AbortError";
|
|
32
|
+
};
|
|
33
|
+
export declare const ResponseAbortedName = "ResponseAborted";
|
|
34
|
+
export declare class ResponseAborted extends Error {
|
|
35
|
+
readonly name = "ResponseAborted";
|
|
36
|
+
}
|
|
37
|
+
export declare class DetachedPromise<T = any> {
|
|
38
|
+
readonly resolve: (value: T | PromiseLike<T>) => void;
|
|
39
|
+
readonly reject: (reason: any) => void;
|
|
40
|
+
readonly promise: Promise<T>;
|
|
41
|
+
constructor();
|
|
42
|
+
}
|
|
43
|
+
export declare function serveStatic(req: IncomingMessage, res: ServerResponse, path: string, opts?: Parameters<typeof send>[2]): Promise<void>;
|
|
44
|
+
export declare function formatHostname(hostname: string): string;
|
|
@@ -0,0 +1,458 @@
|
|
|
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
|
+
exports.DetachedPromise = exports.ResponseAborted = exports.ResponseAbortedName = void 0;
|
|
7
|
+
exports.serve = serve;
|
|
8
|
+
exports.startServer = startServer;
|
|
9
|
+
exports.initialize = initialize;
|
|
10
|
+
exports.pipeToNodeResponse = pipeToNodeResponse;
|
|
11
|
+
exports.createAbortController = createAbortController;
|
|
12
|
+
exports.isAbortError = isAbortError;
|
|
13
|
+
exports.serveStatic = serveStatic;
|
|
14
|
+
exports.formatHostname = formatHostname;
|
|
15
|
+
const fs_1 = __importDefault(require("fs"));
|
|
16
|
+
const http_1 = __importDefault(require("http"));
|
|
17
|
+
const https_1 = __importDefault(require("https"));
|
|
18
|
+
const net_1 = require("net");
|
|
19
|
+
const path_1 = __importDefault(require("path"));
|
|
20
|
+
const send_1 = __importDefault(require("send"));
|
|
21
|
+
const url_1 = __importDefault(require("url"));
|
|
22
|
+
const webpackCompat_1 = require("../config/webpackCompat");
|
|
23
|
+
const hmr_1 = require("../core/hmr");
|
|
24
|
+
const common_1 = require("../utils/common");
|
|
25
|
+
const findRoot_1 = require("../utils/findRoot");
|
|
26
|
+
const mkcert_1 = require("../utils/mkcert");
|
|
27
|
+
const printServerInfo_1 = require("../utils/printServerInfo");
|
|
28
|
+
const xcodeProfile_1 = require("../utils/xcodeProfile");
|
|
29
|
+
function parsePath(pathStr) {
|
|
30
|
+
const hashIndex = pathStr.indexOf("#");
|
|
31
|
+
const queryIndex = pathStr.indexOf("?");
|
|
32
|
+
const hasQuery = queryIndex > -1 && (hashIndex < 0 || queryIndex < hashIndex);
|
|
33
|
+
if (hasQuery || hashIndex > -1) {
|
|
34
|
+
return {
|
|
35
|
+
pathname: pathStr.substring(0, hasQuery ? queryIndex : hashIndex),
|
|
36
|
+
query: hasQuery
|
|
37
|
+
? pathStr.substring(queryIndex, hashIndex > -1 ? hashIndex : undefined)
|
|
38
|
+
: "",
|
|
39
|
+
hash: hashIndex > -1 ? pathStr.slice(hashIndex) : "",
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return { pathname: pathStr, query: "", hash: "" };
|
|
43
|
+
}
|
|
44
|
+
function pathHasPrefix(pathStr, prefix) {
|
|
45
|
+
if (typeof pathStr !== "string") {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const { pathname } = parsePath(pathStr);
|
|
49
|
+
return pathname === prefix || pathname.startsWith(prefix + "/");
|
|
50
|
+
}
|
|
51
|
+
function removePathPrefix(pathStr, prefix) {
|
|
52
|
+
// If the path doesn't start with the prefix we can return it as is.
|
|
53
|
+
if (!pathHasPrefix(pathStr, prefix)) {
|
|
54
|
+
return pathStr;
|
|
55
|
+
}
|
|
56
|
+
// Remove the prefix from the path via slicing.
|
|
57
|
+
const withoutPrefix = pathStr.slice(prefix.length);
|
|
58
|
+
// If the path without the prefix starts with a `/` we can return it as is.
|
|
59
|
+
if (withoutPrefix.startsWith("/")) {
|
|
60
|
+
return withoutPrefix;
|
|
61
|
+
}
|
|
62
|
+
// If the path without the prefix doesn't start with a `/` we need to add it
|
|
63
|
+
// back to the path to make sure it's a valid path.
|
|
64
|
+
return `/${withoutPrefix}`;
|
|
65
|
+
}
|
|
66
|
+
function normalizedPublicPath(publicPath) {
|
|
67
|
+
const escapedPublicPath = (publicPath === null || publicPath === void 0 ? void 0 : publicPath.replace(/^\/+|\/+$/g, "")) || false;
|
|
68
|
+
if (!escapedPublicPath) {
|
|
69
|
+
return "";
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
if (URL.canParse(escapedPublicPath)) {
|
|
73
|
+
const url = new URL(escapedPublicPath).toString();
|
|
74
|
+
return url.endsWith("/") ? url.slice(0, -1) : url;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (_a) { }
|
|
78
|
+
return `/${escapedPublicPath}`;
|
|
79
|
+
}
|
|
80
|
+
function serve(options, projectPath, rootPath, serverOptions) {
|
|
81
|
+
const bundleOptions = (0, webpackCompat_1.resolveBundleOptions)(options, projectPath, rootPath);
|
|
82
|
+
if (!rootPath) {
|
|
83
|
+
rootPath = (0, findRoot_1.findRootDir)(projectPath || process.cwd());
|
|
84
|
+
}
|
|
85
|
+
return serveInternal(bundleOptions, projectPath, rootPath, serverOptions);
|
|
86
|
+
}
|
|
87
|
+
async function serveInternal(options, projectPath, rootPath, serverOptions) {
|
|
88
|
+
var _a;
|
|
89
|
+
(0, common_1.blockStdout)();
|
|
90
|
+
if (process.env.XCODE_PROFILE) {
|
|
91
|
+
await (0, xcodeProfile_1.xcodeProfilingReady)();
|
|
92
|
+
}
|
|
93
|
+
// FIXME: fix any type
|
|
94
|
+
const cfgDevServer = (((_a = options.config) === null || _a === void 0 ? void 0 : _a.devServer) || {});
|
|
95
|
+
const serverOpts = {
|
|
96
|
+
hostname: (serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.hostname) || cfgDevServer.host || "localhost",
|
|
97
|
+
port: typeof (serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.port) !== "undefined"
|
|
98
|
+
? serverOptions.port
|
|
99
|
+
: cfgDevServer.port || 3000,
|
|
100
|
+
https: typeof (serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.https) !== "undefined"
|
|
101
|
+
? serverOptions.https
|
|
102
|
+
: cfgDevServer.https,
|
|
103
|
+
logServerInfo: serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.logServerInfo,
|
|
104
|
+
selfSignedCertificate: serverOptions === null || serverOptions === void 0 ? void 0 : serverOptions.selfSignedCertificate,
|
|
105
|
+
};
|
|
106
|
+
// If HTTPS is requested and no certificate provided, attempt to generate one.
|
|
107
|
+
if (serverOpts.https && !serverOpts.selfSignedCertificate) {
|
|
108
|
+
try {
|
|
109
|
+
// createSelfSignedCertificate may return undefined on failure
|
|
110
|
+
const cert = await (0, mkcert_1.createSelfSignedCertificate)(serverOpts.hostname);
|
|
111
|
+
if (cert)
|
|
112
|
+
serverOpts.selfSignedCertificate = cert;
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
// ignore and fall back to http if certificate generation fails
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
await startServer(serverOpts, {
|
|
119
|
+
...options,
|
|
120
|
+
config: {
|
|
121
|
+
...options.config,
|
|
122
|
+
devServer: {
|
|
123
|
+
hot: true,
|
|
124
|
+
...(options.config.devServer || {}),
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
packPath: (0, common_1.getPackPath)(),
|
|
128
|
+
}, projectPath || process.cwd(), rootPath);
|
|
129
|
+
}
|
|
130
|
+
async function startServer(serverOptions, bundleOptions, projectPath, rootPath) {
|
|
131
|
+
let { port, hostname, selfSignedCertificate } = serverOptions;
|
|
132
|
+
process.title = "utoopack-dev-server";
|
|
133
|
+
let handlersReady = () => { };
|
|
134
|
+
let handlersError = () => { };
|
|
135
|
+
let handlersPromise = new Promise((resolve, reject) => {
|
|
136
|
+
handlersReady = resolve;
|
|
137
|
+
handlersError = reject;
|
|
138
|
+
});
|
|
139
|
+
let requestHandler = async (req, res) => {
|
|
140
|
+
if (handlersPromise) {
|
|
141
|
+
await handlersPromise;
|
|
142
|
+
return requestHandler(req, res);
|
|
143
|
+
}
|
|
144
|
+
throw new Error("Invariant request handler was not setup");
|
|
145
|
+
};
|
|
146
|
+
let upgradeHandler = async (req, socket, head) => {
|
|
147
|
+
if (handlersPromise) {
|
|
148
|
+
await handlersPromise;
|
|
149
|
+
return upgradeHandler(req, socket, head);
|
|
150
|
+
}
|
|
151
|
+
throw new Error("Invariant upgrade handler was not setup");
|
|
152
|
+
};
|
|
153
|
+
async function requestListener(req, res) {
|
|
154
|
+
try {
|
|
155
|
+
if (handlersPromise) {
|
|
156
|
+
await handlersPromise;
|
|
157
|
+
handlersPromise = undefined;
|
|
158
|
+
}
|
|
159
|
+
await requestHandler(req, res);
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
res.statusCode = 500;
|
|
163
|
+
res.end("Internal Server Error");
|
|
164
|
+
console.error(`Failed to handle request for ${req.url}`);
|
|
165
|
+
console.error(err);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const server = selfSignedCertificate
|
|
169
|
+
? https_1.default.createServer({
|
|
170
|
+
key: fs_1.default.readFileSync(selfSignedCertificate.key),
|
|
171
|
+
cert: fs_1.default.readFileSync(selfSignedCertificate.cert),
|
|
172
|
+
}, requestListener)
|
|
173
|
+
: http_1.default.createServer(requestListener);
|
|
174
|
+
server.on("upgrade", async (req, socket, head) => {
|
|
175
|
+
try {
|
|
176
|
+
await upgradeHandler(req, socket, head);
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
socket.destroy();
|
|
180
|
+
console.error(`Failed to handle request for ${req.url}`);
|
|
181
|
+
console.error(err);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
let portRetryCount = 0;
|
|
185
|
+
const originalPort = port;
|
|
186
|
+
server.on("error", (err) => {
|
|
187
|
+
if (port && err.code === "EADDRINUSE" && portRetryCount < 10) {
|
|
188
|
+
port += 1;
|
|
189
|
+
portRetryCount += 1;
|
|
190
|
+
server.listen(port, hostname);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
console.error(`Failed to start server`);
|
|
194
|
+
console.error(err);
|
|
195
|
+
process.exit(1);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
await new Promise((resolve) => {
|
|
199
|
+
server.on("listening", async () => {
|
|
200
|
+
const addr = server.address();
|
|
201
|
+
const actualHostname = formatHostname(typeof addr === "object"
|
|
202
|
+
? (addr === null || addr === void 0 ? void 0 : addr.address) || hostname || "localhost"
|
|
203
|
+
: addr);
|
|
204
|
+
const formattedHostname = !hostname || actualHostname === "0.0.0.0"
|
|
205
|
+
? "localhost"
|
|
206
|
+
: actualHostname === "[::]"
|
|
207
|
+
? "[::1]"
|
|
208
|
+
: formatHostname(hostname);
|
|
209
|
+
port = typeof addr === "object" ? (addr === null || addr === void 0 ? void 0 : addr.port) || port : port;
|
|
210
|
+
if (portRetryCount) {
|
|
211
|
+
console.warn(`Port ${originalPort} is in use, using available port ${port} instead.`);
|
|
212
|
+
}
|
|
213
|
+
if (serverOptions.logServerInfo !== false) {
|
|
214
|
+
(0, printServerInfo_1.printServerInfo)(serverOptions.https ? "https" : "http", formattedHostname, port);
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
let cleanupStarted = false;
|
|
218
|
+
let closeUpgraded = null;
|
|
219
|
+
const cleanup = () => {
|
|
220
|
+
if (cleanupStarted) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
cleanupStarted = true;
|
|
224
|
+
(async () => {
|
|
225
|
+
console.debug("start-server process cleanup");
|
|
226
|
+
await new Promise((res) => {
|
|
227
|
+
server.close((err) => {
|
|
228
|
+
if (err)
|
|
229
|
+
console.error(err);
|
|
230
|
+
res();
|
|
231
|
+
});
|
|
232
|
+
server.closeAllConnections();
|
|
233
|
+
closeUpgraded === null || closeUpgraded === void 0 ? void 0 : closeUpgraded();
|
|
234
|
+
});
|
|
235
|
+
console.debug("start-server process cleanup finished");
|
|
236
|
+
process.exit(0);
|
|
237
|
+
})();
|
|
238
|
+
};
|
|
239
|
+
const exception = (err) => {
|
|
240
|
+
console.error(err);
|
|
241
|
+
};
|
|
242
|
+
process.on("SIGINT", cleanup);
|
|
243
|
+
process.on("SIGTERM", cleanup);
|
|
244
|
+
process.on("rejectionHandled", () => { });
|
|
245
|
+
process.on("uncaughtException", exception);
|
|
246
|
+
process.on("unhandledRejection", exception);
|
|
247
|
+
const initResult = await initialize(bundleOptions, projectPath, rootPath);
|
|
248
|
+
requestHandler = initResult.requestHandler;
|
|
249
|
+
upgradeHandler = initResult.upgradeHandler;
|
|
250
|
+
closeUpgraded = initResult.closeUpgraded;
|
|
251
|
+
handlersReady();
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
handlersError();
|
|
255
|
+
console.error(err);
|
|
256
|
+
process.exit(1);
|
|
257
|
+
}
|
|
258
|
+
resolve();
|
|
259
|
+
});
|
|
260
|
+
server.listen(port, hostname);
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
async function initialize(bundleOptions, projectPath, rootPath) {
|
|
264
|
+
process.env.NODE_ENV = "development";
|
|
265
|
+
const hotReloader = await (0, hmr_1.createHotReloader)(bundleOptions, projectPath, rootPath);
|
|
266
|
+
await hotReloader.start();
|
|
267
|
+
const requestHandlerImpl = async (req, res) => {
|
|
268
|
+
req.on("error", console.error);
|
|
269
|
+
res.on("error", console.error);
|
|
270
|
+
const handleRequest = async () => {
|
|
271
|
+
var _a, _b;
|
|
272
|
+
if (!(req.method === "GET" || req.method === "HEAD")) {
|
|
273
|
+
res.setHeader("Allow", ["GET", "HEAD"]);
|
|
274
|
+
res.statusCode = 405;
|
|
275
|
+
res.end();
|
|
276
|
+
}
|
|
277
|
+
const distRoot = path_1.default.resolve(projectPath, ((_a = bundleOptions.config.output) === null || _a === void 0 ? void 0 : _a.path) || "./dist");
|
|
278
|
+
const publicPath = (_b = bundleOptions.config.output) === null || _b === void 0 ? void 0 : _b.publicPath;
|
|
279
|
+
try {
|
|
280
|
+
const reqUrl = req.url || "";
|
|
281
|
+
let requestPath = url_1.default.parse(reqUrl).pathname || "";
|
|
282
|
+
if (publicPath && publicPath !== "runtime") {
|
|
283
|
+
const normalizedPrefix = normalizedPublicPath(publicPath);
|
|
284
|
+
const isAbsoluteUrl = normalizedPrefix.startsWith("http://") ||
|
|
285
|
+
normalizedPrefix.startsWith("https://");
|
|
286
|
+
if (!isAbsoluteUrl && normalizedPrefix) {
|
|
287
|
+
if (pathHasPrefix(requestPath, normalizedPrefix)) {
|
|
288
|
+
requestPath = removePathPrefix(requestPath, normalizedPrefix);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return await serveStatic(req, res, requestPath, { root: distRoot });
|
|
293
|
+
}
|
|
294
|
+
catch (err) {
|
|
295
|
+
res.setHeader("Cache-Control", "private, no-cache, no-store, max-age=0, must-revalidate");
|
|
296
|
+
res.statusCode = 404;
|
|
297
|
+
res.end();
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
try {
|
|
301
|
+
await handleRequest();
|
|
302
|
+
}
|
|
303
|
+
catch (err) {
|
|
304
|
+
res.statusCode = 500;
|
|
305
|
+
res.end("Internal Server Error");
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
let requestHandler = requestHandlerImpl;
|
|
309
|
+
const logError = async (type, err) => {
|
|
310
|
+
if (type === "unhandledRejection") {
|
|
311
|
+
console.error("unhandledRejection: ", err);
|
|
312
|
+
}
|
|
313
|
+
else if (type === "uncaughtException") {
|
|
314
|
+
console.error("uncaughtException: ", err);
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
process.on("uncaughtException", logError.bind(null, "uncaughtException"));
|
|
318
|
+
process.on("unhandledRejection", logError.bind(null, "unhandledRejection"));
|
|
319
|
+
const upgradeHandler = async (req, socket, head) => {
|
|
320
|
+
var _a;
|
|
321
|
+
try {
|
|
322
|
+
const isHMRRequest = (_a = req.url) === null || _a === void 0 ? void 0 : _a.includes("turbopack-hmr");
|
|
323
|
+
if (isHMRRequest) {
|
|
324
|
+
hotReloader.onHMR(req, socket, head);
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
socket.end();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
catch (err) {
|
|
331
|
+
console.error("Error handling upgrade request", err);
|
|
332
|
+
socket.end();
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
return {
|
|
336
|
+
requestHandler,
|
|
337
|
+
upgradeHandler,
|
|
338
|
+
closeUpgraded() {
|
|
339
|
+
hotReloader.close();
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
async function pipeToNodeResponse(readable, res, waitUntilForEnd) {
|
|
344
|
+
try {
|
|
345
|
+
const { errored, destroyed } = res;
|
|
346
|
+
if (errored || destroyed)
|
|
347
|
+
return;
|
|
348
|
+
const controller = createAbortController(res);
|
|
349
|
+
const writer = createWriterFromResponse(res, waitUntilForEnd);
|
|
350
|
+
await readable.pipeTo(writer, { signal: controller.signal });
|
|
351
|
+
}
|
|
352
|
+
catch (err) {
|
|
353
|
+
if (isAbortError(err))
|
|
354
|
+
return;
|
|
355
|
+
throw new Error("failed to pipe response", { cause: err });
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
function createAbortController(response) {
|
|
359
|
+
const controller = new AbortController();
|
|
360
|
+
response.once("close", () => {
|
|
361
|
+
if (response.writableFinished)
|
|
362
|
+
return;
|
|
363
|
+
controller.abort(new ResponseAborted());
|
|
364
|
+
});
|
|
365
|
+
return controller;
|
|
366
|
+
}
|
|
367
|
+
function isAbortError(e) {
|
|
368
|
+
return (e === null || e === void 0 ? void 0 : e.name) === "AbortError" || (e === null || e === void 0 ? void 0 : e.name) === exports.ResponseAbortedName;
|
|
369
|
+
}
|
|
370
|
+
exports.ResponseAbortedName = "ResponseAborted";
|
|
371
|
+
class ResponseAborted extends Error {
|
|
372
|
+
constructor() {
|
|
373
|
+
super(...arguments);
|
|
374
|
+
this.name = exports.ResponseAbortedName;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
exports.ResponseAborted = ResponseAborted;
|
|
378
|
+
function createWriterFromResponse(res, waitUntilForEnd) {
|
|
379
|
+
let started = false;
|
|
380
|
+
let drained = new DetachedPromise();
|
|
381
|
+
function onDrain() {
|
|
382
|
+
drained.resolve();
|
|
383
|
+
}
|
|
384
|
+
res.on("drain", onDrain);
|
|
385
|
+
res.once("close", () => {
|
|
386
|
+
res.off("drain", onDrain);
|
|
387
|
+
drained.resolve();
|
|
388
|
+
});
|
|
389
|
+
const finished = new DetachedPromise();
|
|
390
|
+
res.once("finish", () => {
|
|
391
|
+
finished.resolve();
|
|
392
|
+
});
|
|
393
|
+
return new WritableStream({
|
|
394
|
+
write: async (chunk) => {
|
|
395
|
+
if (!started) {
|
|
396
|
+
started = true;
|
|
397
|
+
res.flushHeaders();
|
|
398
|
+
}
|
|
399
|
+
try {
|
|
400
|
+
const ok = res.write(chunk);
|
|
401
|
+
if ("flush" in res && typeof res.flush === "function") {
|
|
402
|
+
res.flush();
|
|
403
|
+
}
|
|
404
|
+
if (!ok) {
|
|
405
|
+
await drained.promise;
|
|
406
|
+
drained = new DetachedPromise();
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
catch (err) {
|
|
410
|
+
res.end();
|
|
411
|
+
throw new Error("failed to write chunk to response", { cause: err });
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
abort: (err) => {
|
|
415
|
+
if (res.writableFinished)
|
|
416
|
+
return;
|
|
417
|
+
res.destroy(err);
|
|
418
|
+
},
|
|
419
|
+
close: async () => {
|
|
420
|
+
if (waitUntilForEnd) {
|
|
421
|
+
await waitUntilForEnd;
|
|
422
|
+
}
|
|
423
|
+
if (res.writableFinished)
|
|
424
|
+
return;
|
|
425
|
+
res.end();
|
|
426
|
+
return finished.promise;
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
class DetachedPromise {
|
|
431
|
+
constructor() {
|
|
432
|
+
let resolve;
|
|
433
|
+
let reject;
|
|
434
|
+
this.promise = new Promise((res, rej) => {
|
|
435
|
+
resolve = res;
|
|
436
|
+
reject = rej;
|
|
437
|
+
});
|
|
438
|
+
this.resolve = resolve;
|
|
439
|
+
this.reject = reject;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
exports.DetachedPromise = DetachedPromise;
|
|
443
|
+
function serveStatic(req, res, path, opts) {
|
|
444
|
+
return new Promise((resolve, reject) => {
|
|
445
|
+
(0, send_1.default)(req, path, opts)
|
|
446
|
+
.on("directory", () => {
|
|
447
|
+
const err = new Error("No directory access");
|
|
448
|
+
err.code = "ENOENT";
|
|
449
|
+
reject(err);
|
|
450
|
+
})
|
|
451
|
+
.on("error", reject)
|
|
452
|
+
.pipe(res)
|
|
453
|
+
.on("finish", resolve);
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
function formatHostname(hostname) {
|
|
457
|
+
return (0, net_1.isIPv6)(hostname) ? `[${hostname}]` : hostname;
|
|
458
|
+
}
|
package/cjs/commands/dev.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Dev server implementation using Hono + @hono/node-server + @hono/node-ws.
|
|
3
|
+
* Keeps the same public API as dev.ts; do not remove dev.ts until this is verified.
|
|
4
|
+
*/
|
|
4
5
|
import { BundleOptions } from "../config/types";
|
|
5
|
-
import { WebpackConfig } from "../config/webpackCompat";
|
|
6
|
-
export declare function serve(options: BundleOptions | WebpackConfig, projectPath?: string, rootPath?: string, serverOptions?: StartServerOptions): Promise<void>;
|
|
6
|
+
import { type WebpackConfig } from "../config/webpackCompat";
|
|
7
7
|
export interface SelfSignedCertificate {
|
|
8
8
|
key: string;
|
|
9
9
|
cert: string;
|
|
@@ -16,29 +16,4 @@ export interface StartServerOptions {
|
|
|
16
16
|
logServerInfo?: boolean;
|
|
17
17
|
selfSignedCertificate?: SelfSignedCertificate;
|
|
18
18
|
}
|
|
19
|
-
export
|
|
20
|
-
export type UpgradeHandler = (req: IncomingMessage, socket: Duplex, head: Buffer) => Promise<void>;
|
|
21
|
-
export type ServerInitResult = {
|
|
22
|
-
requestHandler: RequestHandler;
|
|
23
|
-
upgradeHandler: UpgradeHandler;
|
|
24
|
-
closeUpgraded: () => void;
|
|
25
|
-
};
|
|
26
|
-
export declare function startServer(serverOptions: StartServerOptions, bundleOptions: BundleOptions, projectPath: string, rootPath?: string): Promise<void>;
|
|
27
|
-
export declare function initialize(bundleOptions: BundleOptions, projectPath: string, rootPath?: string): Promise<ServerInitResult>;
|
|
28
|
-
export declare function pipeToNodeResponse(readable: ReadableStream<Uint8Array>, res: ServerResponse, waitUntilForEnd?: Promise<unknown>): Promise<void>;
|
|
29
|
-
export declare function createAbortController(response: Writable): AbortController;
|
|
30
|
-
export declare function isAbortError(e: any): e is Error & {
|
|
31
|
-
name: "AbortError";
|
|
32
|
-
};
|
|
33
|
-
export declare const ResponseAbortedName = "ResponseAborted";
|
|
34
|
-
export declare class ResponseAborted extends Error {
|
|
35
|
-
readonly name = "ResponseAborted";
|
|
36
|
-
}
|
|
37
|
-
export declare class DetachedPromise<T = any> {
|
|
38
|
-
readonly resolve: (value: T | PromiseLike<T>) => void;
|
|
39
|
-
readonly reject: (reason: any) => void;
|
|
40
|
-
readonly promise: Promise<T>;
|
|
41
|
-
constructor();
|
|
42
|
-
}
|
|
43
|
-
export declare function serveStatic(req: IncomingMessage, res: ServerResponse, path: string, opts?: Parameters<typeof send>[2]): Promise<void>;
|
|
44
|
-
export declare function formatHostname(hostname: string): string;
|
|
19
|
+
export declare function serve(options: BundleOptions | WebpackConfig, projectPath?: string, rootPath?: string, serverOptions?: StartServerOptions): Promise<void>;
|