@utoo/pack 0.0.1-alpha.1 → 0.0.1-alpha.13
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 +153 -100
- package/cjs/build.d.ts +2 -0
- package/cjs/build.js +53 -0
- package/cjs/dev.d.ts +42 -0
- package/cjs/dev.js +355 -0
- package/cjs/hmr.d.ts +80 -0
- package/cjs/hmr.js +285 -0
- package/cjs/index.d.ts +2 -1
- package/cjs/index.js +5 -73
- package/cjs/mkcert.d.ts +7 -0
- package/cjs/mkcert.js +183 -0
- package/cjs/project.d.ts +3 -3
- package/cjs/project.js +4 -11
- package/cjs/types.d.ts +16 -2
- package/cjs/util.d.ts +12 -1
- package/cjs/util.js +102 -1
- package/config_schema.json +710 -0
- package/esm/binding.d.ts +153 -100
- package/esm/build.d.ts +2 -0
- package/esm/build.js +50 -0
- package/esm/dev.d.ts +42 -0
- package/esm/dev.js +339 -0
- package/esm/hmr.d.ts +80 -0
- package/esm/hmr.js +278 -0
- package/esm/index.d.ts +2 -1
- package/esm/index.js +2 -69
- package/esm/mkcert.d.ts +7 -0
- package/esm/mkcert.js +176 -0
- package/esm/project.d.ts +3 -3
- package/esm/project.js +2 -9
- package/esm/types.d.ts +16 -2
- package/esm/util.d.ts +12 -1
- package/esm/util.js +98 -1
- package/package.json +38 -23
- package/bin/cli.js +0 -9
- package/global.d.ts +0 -10
package/cjs/util.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NapiIssue } from "./binding";
|
|
2
|
-
import { StyledString } from "./types";
|
|
2
|
+
import { ConfigComplete, DefineEnv, RustifiedEnv, StyledString } from "./types";
|
|
3
3
|
export declare class ModuleBuildError extends Error {
|
|
4
4
|
name: string;
|
|
5
5
|
}
|
|
@@ -8,3 +8,14 @@ export declare function isWellKnownError(issue: NapiIssue): boolean;
|
|
|
8
8
|
export declare function formatIssue(issue: NapiIssue): string;
|
|
9
9
|
export declare function renderStyledStringToErrorAnsi(string: StyledString): string;
|
|
10
10
|
export declare function isRelevantWarning(issue: NapiIssue): boolean;
|
|
11
|
+
export declare function rustifyEnv(env: Record<string, string>): RustifiedEnv;
|
|
12
|
+
interface DefineEnvOptions {
|
|
13
|
+
config: ConfigComplete;
|
|
14
|
+
dev: boolean;
|
|
15
|
+
optionDefineEnv?: DefineEnv;
|
|
16
|
+
}
|
|
17
|
+
export declare function createDefineEnv(options: DefineEnvOptions): DefineEnv;
|
|
18
|
+
type AnyFunc<T> = (this: T, ...args: any) => any;
|
|
19
|
+
export declare function debounce<T, F extends AnyFunc<T>>(fn: F, ms: number, maxWait?: number): (this: T, ...passedArgs: Parameters<F>) => void;
|
|
20
|
+
export declare function blockStdout(): void;
|
|
21
|
+
export {};
|
package/cjs/util.js
CHANGED
|
@@ -6,8 +6,12 @@ exports.isWellKnownError = isWellKnownError;
|
|
|
6
6
|
exports.formatIssue = formatIssue;
|
|
7
7
|
exports.renderStyledStringToErrorAnsi = renderStyledStringToErrorAnsi;
|
|
8
8
|
exports.isRelevantWarning = isRelevantWarning;
|
|
9
|
-
|
|
9
|
+
exports.rustifyEnv = rustifyEnv;
|
|
10
|
+
exports.createDefineEnv = createDefineEnv;
|
|
11
|
+
exports.debounce = debounce;
|
|
12
|
+
exports.blockStdout = blockStdout;
|
|
10
13
|
const code_frame_1 = require("@babel/code-frame");
|
|
14
|
+
const picocolors_1 = require("picocolors");
|
|
11
15
|
const magicIdentifier_1 = require("./magicIdentifier");
|
|
12
16
|
class ModuleBuildError extends Error {
|
|
13
17
|
constructor() {
|
|
@@ -137,3 +141,100 @@ function isNodeModulesIssue(issue) {
|
|
|
137
141
|
(issue.filePath.match(/^(?:.*[\\/])?node_modules(?:[\\/].*)?$/) !== null ||
|
|
138
142
|
issue.filePath.includes("@utoo/pack")));
|
|
139
143
|
}
|
|
144
|
+
function rustifyEnv(env) {
|
|
145
|
+
return Object.entries(env)
|
|
146
|
+
.filter(([_, value]) => value != null)
|
|
147
|
+
.map(([name, value]) => ({
|
|
148
|
+
name,
|
|
149
|
+
value,
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
function createDefineEnv(options) {
|
|
153
|
+
var _a;
|
|
154
|
+
let defineEnv = (_a = options.optionDefineEnv) !== null && _a !== void 0 ? _a : {
|
|
155
|
+
client: [],
|
|
156
|
+
edge: [],
|
|
157
|
+
nodejs: [],
|
|
158
|
+
};
|
|
159
|
+
function getDefineEnv() {
|
|
160
|
+
var _a;
|
|
161
|
+
const envs = {
|
|
162
|
+
"process.env.NODE_ENV": options.dev ? "development" : "production",
|
|
163
|
+
};
|
|
164
|
+
const userDefines = (_a = options.config.define) !== null && _a !== void 0 ? _a : {};
|
|
165
|
+
for (const key in userDefines) {
|
|
166
|
+
envs[key] = userDefines[key];
|
|
167
|
+
}
|
|
168
|
+
// serialize
|
|
169
|
+
const defineEnvStringified = {};
|
|
170
|
+
for (const key in defineEnv) {
|
|
171
|
+
const value = envs[key];
|
|
172
|
+
defineEnvStringified[key] = JSON.stringify(value);
|
|
173
|
+
}
|
|
174
|
+
return defineEnvStringified;
|
|
175
|
+
}
|
|
176
|
+
// TODO: future define envs need to extends for more compiler like server or edge.
|
|
177
|
+
for (const variant of Object.keys(defineEnv)) {
|
|
178
|
+
defineEnv[variant] = rustifyEnv(getDefineEnv());
|
|
179
|
+
}
|
|
180
|
+
return defineEnv;
|
|
181
|
+
}
|
|
182
|
+
function debounce(fn, ms, maxWait = Infinity) {
|
|
183
|
+
let timeoutId;
|
|
184
|
+
// The time the debouncing function was first called during this debounce queue.
|
|
185
|
+
let startTime = 0;
|
|
186
|
+
// The time the debouncing function was last called.
|
|
187
|
+
let lastCall = 0;
|
|
188
|
+
// The arguments and this context of the last call to the debouncing function.
|
|
189
|
+
let args, context;
|
|
190
|
+
// A helper used to that either invokes the debounced function, or
|
|
191
|
+
// reschedules the timer if a more recent call was made.
|
|
192
|
+
function run() {
|
|
193
|
+
const now = Date.now();
|
|
194
|
+
const diff = lastCall + ms - now;
|
|
195
|
+
// If the diff is non-positive, then we've waited at least `ms`
|
|
196
|
+
// milliseconds since the last call. Or if we've waited for longer than the
|
|
197
|
+
// max wait time, we must call the debounced function.
|
|
198
|
+
if (diff <= 0 || startTime + maxWait >= now) {
|
|
199
|
+
// It's important to clear the timeout id before invoking the debounced
|
|
200
|
+
// function, in case the function calls the debouncing function again.
|
|
201
|
+
timeoutId = undefined;
|
|
202
|
+
fn.apply(context, args);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
// Else, a new call was made after the original timer was scheduled. We
|
|
206
|
+
// didn't clear the timeout (doing so is very slow), so now we need to
|
|
207
|
+
// reschedule the timer for the time difference.
|
|
208
|
+
timeoutId = setTimeout(run, diff);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return function (...passedArgs) {
|
|
212
|
+
// The arguments and this context of the most recent call are saved so the
|
|
213
|
+
// debounced function can be invoked with them later.
|
|
214
|
+
args = passedArgs;
|
|
215
|
+
context = this;
|
|
216
|
+
// Instead of constantly clearing and scheduling a timer, we record the
|
|
217
|
+
// time of the last call. If a second call comes in before the timer fires,
|
|
218
|
+
// then we'll reschedule in the run function. Doing this is considerably
|
|
219
|
+
// faster.
|
|
220
|
+
lastCall = Date.now();
|
|
221
|
+
// Only schedule a new timer if we're not currently waiting.
|
|
222
|
+
if (timeoutId === undefined) {
|
|
223
|
+
startTime = lastCall;
|
|
224
|
+
timeoutId = setTimeout(run, ms);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
// ref:
|
|
229
|
+
// https://github.com/vercel/next.js/pull/51883
|
|
230
|
+
function blockStdout() {
|
|
231
|
+
// rust needs stdout to be blocking, otherwise it will throw an error (on macOS at least) when writing a lot of data (logs) to it
|
|
232
|
+
// see https://github.com/napi-rs/napi-rs/issues/1630
|
|
233
|
+
// and https://github.com/nodejs/node/blob/main/doc/api/process.md#a-note-on-process-io
|
|
234
|
+
if (process.stdout._handle != null) {
|
|
235
|
+
process.stdout._handle.setBlocking(true);
|
|
236
|
+
}
|
|
237
|
+
if (process.stderr._handle != null) {
|
|
238
|
+
process.stderr._handle.setBlocking(true);
|
|
239
|
+
}
|
|
240
|
+
}
|