@nasti-toolchain/nasti 1.3.1 → 1.3.3
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/cli.cjs +160 -14
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +159 -13
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +156 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +156 -10
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -277,6 +277,13 @@ async function transformRequest(url, ctx) {
|
|
|
277
277
|
if (!filePath || !import_node_fs7.default.existsSync(filePath)) return null;
|
|
278
278
|
const mod = await moduleGraph.ensureEntryFromUrl(url);
|
|
279
279
|
moduleGraph.registerModule(mod, filePath);
|
|
280
|
+
const cleanReqUrl = url.split("?")[0];
|
|
281
|
+
if (cleanReqUrl.startsWith("/@modules/")) {
|
|
282
|
+
const code2 = await bundlePackageAsEsm(filePath);
|
|
283
|
+
const transformResult2 = { code: code2 };
|
|
284
|
+
mod.transformResult = transformResult2;
|
|
285
|
+
return transformResult2;
|
|
286
|
+
}
|
|
280
287
|
let code = import_node_fs7.default.readFileSync(filePath, "utf-8");
|
|
281
288
|
const pluginResult = await pluginContainer.transform(code, filePath);
|
|
282
289
|
if (pluginResult) {
|
|
@@ -301,6 +308,67 @@ async function transformRequest(url, ctx) {
|
|
|
301
308
|
mod.transformResult = transformResult;
|
|
302
309
|
return transformResult;
|
|
303
310
|
}
|
|
311
|
+
async function bundlePackageAsEsm(entryFile) {
|
|
312
|
+
if (!esmBundleCache.has(entryFile)) {
|
|
313
|
+
esmBundleCache.set(entryFile, doBundlePackage(entryFile));
|
|
314
|
+
}
|
|
315
|
+
return esmBundleCache.get(entryFile);
|
|
316
|
+
}
|
|
317
|
+
async function doBundlePackage(entryFile) {
|
|
318
|
+
const { rolldown: rolldown2 } = await import("rolldown");
|
|
319
|
+
const bundle = await rolldown2({
|
|
320
|
+
input: entryFile,
|
|
321
|
+
// 仅将其他 npm 包外部化;相对路径(包内部文件)全部内联打包
|
|
322
|
+
external: (id) => {
|
|
323
|
+
if (id.startsWith(".") || id.startsWith("/") || /^[A-Za-z]:\\/.test(id)) return false;
|
|
324
|
+
return true;
|
|
325
|
+
},
|
|
326
|
+
define: {
|
|
327
|
+
// CJS 包(如 react)通过 process.env.NODE_ENV 判断环境,需在打包时替换
|
|
328
|
+
"process.env.NODE_ENV": '"development"'
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
const result = await bundle.generate({ format: "esm", exports: "named" });
|
|
332
|
+
await bundle.close();
|
|
333
|
+
let code = result.output[0].code;
|
|
334
|
+
code = code.replace(/process\.env\.NODE_ENV/g, '"development"');
|
|
335
|
+
code = code.replace(
|
|
336
|
+
/^(import\b[^;'"]*?\bfrom\s+)(['"])([^'"./][^'"]*)(\2)/gm,
|
|
337
|
+
(_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
|
|
338
|
+
).replace(
|
|
339
|
+
/^(export\b[^;'"]*?\bfrom\s+)(['"])([^'"./][^'"]*)(\2)/gm,
|
|
340
|
+
(_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
|
|
341
|
+
).replace(
|
|
342
|
+
/^(import\s+)(['"])([^'"./][^'"]*)(\2)/gm,
|
|
343
|
+
(_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
|
|
344
|
+
);
|
|
345
|
+
if (code.includes("__commonJSMin")) {
|
|
346
|
+
code = await injectCjsNamedExports(code, entryFile);
|
|
347
|
+
}
|
|
348
|
+
return code;
|
|
349
|
+
}
|
|
350
|
+
async function injectCjsNamedExports(code, entryFile) {
|
|
351
|
+
try {
|
|
352
|
+
const { createRequire: createRequire2 } = await import("module");
|
|
353
|
+
const req = createRequire2(entryFile);
|
|
354
|
+
const cjsExports = req(entryFile);
|
|
355
|
+
if (!cjsExports || typeof cjsExports !== "object" || Array.isArray(cjsExports)) return code;
|
|
356
|
+
const namedKeys = Object.keys(cjsExports).filter(
|
|
357
|
+
(k) => k !== "__esModule" && k !== "default" && VALID_IDENT.test(k)
|
|
358
|
+
);
|
|
359
|
+
if (namedKeys.length === 0) return code;
|
|
360
|
+
return code.replace(
|
|
361
|
+
/^export default (\w+\(\));?\s*$/m,
|
|
362
|
+
(_, call) => [
|
|
363
|
+
`const __cjsMod = ${call};`,
|
|
364
|
+
`export default __cjsMod;`,
|
|
365
|
+
...namedKeys.map((k) => `export const ${k} = __cjsMod[${JSON.stringify(k)}];`)
|
|
366
|
+
].join("\n")
|
|
367
|
+
);
|
|
368
|
+
} catch {
|
|
369
|
+
return code;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
304
372
|
function rewriteImports(code, _config) {
|
|
305
373
|
return code.replace(
|
|
306
374
|
/\bfrom\s+(['"])([^'"./][^'"]*)\1/g,
|
|
@@ -321,16 +389,92 @@ function rewriteImports(code, _config) {
|
|
|
321
389
|
}
|
|
322
390
|
);
|
|
323
391
|
}
|
|
392
|
+
function resolveNodeModule(root, moduleName) {
|
|
393
|
+
let pkgName;
|
|
394
|
+
let subpath;
|
|
395
|
+
if (moduleName.startsWith("@")) {
|
|
396
|
+
const parts = moduleName.split("/");
|
|
397
|
+
pkgName = parts.slice(0, 2).join("/");
|
|
398
|
+
subpath = parts.slice(2).join("/");
|
|
399
|
+
} else {
|
|
400
|
+
const slash = moduleName.indexOf("/");
|
|
401
|
+
pkgName = slash === -1 ? moduleName : moduleName.slice(0, slash);
|
|
402
|
+
subpath = slash === -1 ? "" : moduleName.slice(slash + 1);
|
|
403
|
+
}
|
|
404
|
+
let pkgDir = null;
|
|
405
|
+
let dir = root;
|
|
406
|
+
for (; ; ) {
|
|
407
|
+
const candidate = import_node_path8.default.join(dir, "node_modules", pkgName);
|
|
408
|
+
if (import_node_fs7.default.existsSync(candidate)) {
|
|
409
|
+
pkgDir = candidate;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
const parent = import_node_path8.default.dirname(dir);
|
|
413
|
+
if (parent === dir) break;
|
|
414
|
+
dir = parent;
|
|
415
|
+
}
|
|
416
|
+
if (!pkgDir) return null;
|
|
417
|
+
const pkgJsonPath = import_node_path8.default.join(pkgDir, "package.json");
|
|
418
|
+
if (!import_node_fs7.default.existsSync(pkgJsonPath)) return null;
|
|
419
|
+
let pkg;
|
|
420
|
+
try {
|
|
421
|
+
pkg = JSON.parse(import_node_fs7.default.readFileSync(pkgJsonPath, "utf-8"));
|
|
422
|
+
} catch {
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
if (pkg.exports) {
|
|
426
|
+
const exportKey = subpath ? `./${subpath}` : ".";
|
|
427
|
+
const resolved = resolvePackageExports(pkg.exports, exportKey, pkgDir);
|
|
428
|
+
if (resolved) return resolved;
|
|
429
|
+
}
|
|
430
|
+
if (subpath) {
|
|
431
|
+
const direct = import_node_path8.default.join(pkgDir, subpath);
|
|
432
|
+
if (import_node_fs7.default.existsSync(direct) && import_node_fs7.default.statSync(direct).isFile()) return direct;
|
|
433
|
+
for (const ext of RESOLVE_EXTENSIONS) {
|
|
434
|
+
if (import_node_fs7.default.existsSync(direct + ext)) return direct + ext;
|
|
435
|
+
}
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
438
|
+
for (const field of ["module", "jsnext:main", "jsnext", "main"]) {
|
|
439
|
+
if (typeof pkg[field] === "string") {
|
|
440
|
+
const entry = import_node_path8.default.join(pkgDir, pkg[field]);
|
|
441
|
+
if (import_node_fs7.default.existsSync(entry)) return entry;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
function resolvePackageExports(exports2, key, pkgDir) {
|
|
447
|
+
if (typeof exports2 === "string") {
|
|
448
|
+
return key === "." ? import_node_path8.default.join(pkgDir, exports2) : null;
|
|
449
|
+
}
|
|
450
|
+
const entry = exports2[key];
|
|
451
|
+
if (entry === void 0) return null;
|
|
452
|
+
return resolveExportValue(entry, pkgDir);
|
|
453
|
+
}
|
|
454
|
+
function resolveExportValue(value, pkgDir) {
|
|
455
|
+
if (typeof value === "string") return import_node_path8.default.join(pkgDir, value);
|
|
456
|
+
if (Array.isArray(value)) {
|
|
457
|
+
for (const item of value) {
|
|
458
|
+
const r = resolveExportValue(item, pkgDir);
|
|
459
|
+
if (r) return r;
|
|
460
|
+
}
|
|
461
|
+
return null;
|
|
462
|
+
}
|
|
463
|
+
if (value && typeof value === "object") {
|
|
464
|
+
for (const cond of ESM_CONDITIONS) {
|
|
465
|
+
if (cond in value) {
|
|
466
|
+
const r = resolveExportValue(value[cond], pkgDir);
|
|
467
|
+
if (r) return r;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
324
473
|
function resolveUrlToFile(url, root) {
|
|
325
474
|
const cleanUrl = url.split("?")[0];
|
|
326
475
|
if (cleanUrl.startsWith("/@modules/")) {
|
|
327
476
|
const moduleName = cleanUrl.slice("/@modules/".length);
|
|
328
|
-
|
|
329
|
-
const req = (0, import_node_module2.createRequire)(import_node_path8.default.resolve(root, "package.json"));
|
|
330
|
-
return req.resolve(moduleName);
|
|
331
|
-
} catch {
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
477
|
+
return resolveNodeModule(root, moduleName);
|
|
334
478
|
}
|
|
335
479
|
const filePath = import_node_path8.default.resolve(root, cleanUrl.replace(/^\//, ""));
|
|
336
480
|
if (import_node_fs7.default.existsSync(filePath) && import_node_fs7.default.statSync(filePath).isFile()) {
|
|
@@ -451,17 +595,19 @@ if (!window.__nasti_hot_map) window.__nasti_hot_map = new Map();
|
|
|
451
595
|
window.__NASTI_HMR__ = { createHotContext };
|
|
452
596
|
`;
|
|
453
597
|
}
|
|
454
|
-
var import_node_path8, import_node_fs7,
|
|
598
|
+
var import_node_path8, import_node_fs7, esmBundleCache, VALID_IDENT, RESOLVE_EXTENSIONS, ESM_CONDITIONS;
|
|
455
599
|
var init_middleware = __esm({
|
|
456
600
|
"src/server/middleware.ts"() {
|
|
457
601
|
"use strict";
|
|
458
602
|
import_node_path8 = __toESM(require("path"), 1);
|
|
459
603
|
import_node_fs7 = __toESM(require("fs"), 1);
|
|
460
|
-
import_node_module2 = require("module");
|
|
461
604
|
init_transformer();
|
|
462
605
|
init_html();
|
|
463
606
|
init_env();
|
|
607
|
+
esmBundleCache = /* @__PURE__ */ new Map();
|
|
608
|
+
VALID_IDENT = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
464
609
|
RESOLVE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js", ".mjs", ".json", ".vue"];
|
|
610
|
+
ESM_CONDITIONS = ["import", "browser", "module", "default"];
|
|
465
611
|
}
|
|
466
612
|
});
|
|
467
613
|
|
|
@@ -957,7 +1103,7 @@ var import_picocolors = __toESM(require("picocolors"), 1);
|
|
|
957
1103
|
async function build(inlineConfig = {}) {
|
|
958
1104
|
const config = await resolveConfig(inlineConfig, "build");
|
|
959
1105
|
const startTime = performance.now();
|
|
960
|
-
console.log(import_picocolors.default.cyan("\n\u{1F528} nasti build") + import_picocolors.default.dim(` v${"1.3.
|
|
1106
|
+
console.log(import_picocolors.default.cyan("\n\u{1F528} nasti build") + import_picocolors.default.dim(` v${"1.3.3"}`));
|
|
961
1107
|
console.log(import_picocolors.default.dim(` root: ${config.root}`));
|
|
962
1108
|
console.log(import_picocolors.default.dim(` mode: ${config.mode}`));
|
|
963
1109
|
const outDir = import_node_path7.default.resolve(config.root, config.build.outDir);
|
|
@@ -1363,7 +1509,7 @@ async function createServer(inlineConfig = {}) {
|
|
|
1363
1509
|
const localUrl = `http://localhost:${actualPort}`;
|
|
1364
1510
|
const networkUrl = host === "0.0.0.0" ? `http://${getNetworkAddress()}:${actualPort}` : null;
|
|
1365
1511
|
console.log();
|
|
1366
|
-
console.log(import_picocolors2.default.cyan(" nasti dev server") + import_picocolors2.default.dim(` v${"1.3.
|
|
1512
|
+
console.log(import_picocolors2.default.cyan(" nasti dev server") + import_picocolors2.default.dim(` v${"1.3.3"}`));
|
|
1367
1513
|
console.log();
|
|
1368
1514
|
console.log(` ${import_picocolors2.default.green(">")} Local: ${import_picocolors2.default.cyan(localUrl)}`);
|
|
1369
1515
|
if (networkUrl) {
|