@marko/vite 5.0.14 → 5.1.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/babel-plugin-cjs-interop.d.ts +0 -2
- package/dist/index.mjs +50 -76
- package/dist/resolve.d.ts +1 -2
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -14,55 +14,42 @@ import * as t from "@babel/types";
|
|
|
14
14
|
import fs from "fs";
|
|
15
15
|
import path from "path";
|
|
16
16
|
import Resolve from "resolve";
|
|
17
|
-
|
|
18
|
-
var
|
|
19
|
-
var modulePathReg = /^.*[/\\]node_modules[/\\](?:@[^/\\]+[/\\])?[^/\\]+[/\\]/;
|
|
17
|
+
var moduleNameReg = /^(?:@[^/\\]+[/\\])?[^/\\]+/;
|
|
18
|
+
var modulePathReg = /^.*[/\\]node_modules[/\\]((?:@[^/\\]+[/\\])?[^/\\]+[/\\])/;
|
|
20
19
|
var cjsModuleLookup = /* @__PURE__ */ new Map();
|
|
21
|
-
function isCJSModule(id) {
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
function isCJSModule(id, fromFile) {
|
|
21
|
+
if (/\.cjs$/.test(id)) return true;
|
|
22
|
+
if (/\.mjs$/.test(id)) return false;
|
|
23
|
+
if (id[0] === ".") return isCJSModule(fromFile, fromFile);
|
|
24
|
+
const isAbsolute = path.isAbsolute(id);
|
|
25
|
+
const moduleId = moduleNameReg.exec(
|
|
26
|
+
isAbsolute ? id.replace(modulePathReg, "$1").replace(/\\/g, "/") : id
|
|
27
|
+
)?.[0];
|
|
28
|
+
if (!moduleId) return false;
|
|
29
|
+
let isCJS = cjsModuleLookup.get(moduleId);
|
|
30
|
+
if (isCJS === void 0) {
|
|
31
|
+
try {
|
|
32
|
+
if (isAbsolute) {
|
|
33
|
+
const pkgPath = modulePathReg.exec(id)[0] + "/package.json";
|
|
28
34
|
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
29
35
|
isCJS = pkg.type !== "module" && !pkg.exports;
|
|
30
|
-
}
|
|
31
|
-
|
|
36
|
+
} else {
|
|
37
|
+
Resolve.sync(moduleId + "/package.json", {
|
|
38
|
+
basedir: path.dirname(fromFile),
|
|
39
|
+
filename: fromFile,
|
|
40
|
+
pathFilter(pkg, _pkgFile, relativePath) {
|
|
41
|
+
isCJS = pkg.type !== "module" && !pkg.exports;
|
|
42
|
+
return relativePath;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
isCJS ??= false;
|
|
32
46
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return isCJS;
|
|
36
|
-
}
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
function resolve(id, from, extensions, conditions) {
|
|
40
|
-
return Resolve.sync(id, {
|
|
41
|
-
basedir: path.dirname(from),
|
|
42
|
-
filename: from,
|
|
43
|
-
pathFilter,
|
|
44
|
-
packageFilter,
|
|
45
|
-
extensions
|
|
46
|
-
});
|
|
47
|
-
function pathFilter(pkg, pkgFile, relativePath) {
|
|
48
|
-
cjsModuleLookup.set(pkgFile, pkg.type !== "module" && !pkg.exports);
|
|
49
|
-
if (pkg.exports) {
|
|
50
|
-
return exports(
|
|
51
|
-
pkg,
|
|
52
|
-
relativePath === exportsMainFile ? "." : relativePath,
|
|
53
|
-
{
|
|
54
|
-
conditions
|
|
55
|
-
}
|
|
56
|
-
)?.[0];
|
|
47
|
+
} catch {
|
|
48
|
+
isCJS = false;
|
|
57
49
|
}
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
function packageFilter(pkg) {
|
|
62
|
-
if (pkg.exports) {
|
|
63
|
-
pkg.main = exportsMainFile;
|
|
50
|
+
cjsModuleLookup.set(moduleId, isCJS);
|
|
64
51
|
}
|
|
65
|
-
return
|
|
52
|
+
return isCJS;
|
|
66
53
|
}
|
|
67
54
|
|
|
68
55
|
// src/babel-plugin-cjs-interop.ts
|
|
@@ -71,20 +58,10 @@ function plugin(options) {
|
|
|
71
58
|
name: "marko-import-interop",
|
|
72
59
|
visitor: {
|
|
73
60
|
ImportDeclaration(path7) {
|
|
74
|
-
if (!path7.node.specifiers.length || /\.(?:mjs|marko)$|\?/.test(path7.node.source.value) || options.filter?.(path7.node.source.value) === false
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const resolved = resolve(
|
|
79
|
-
path7.node.source.value,
|
|
80
|
-
path7.hub.file.opts.filename,
|
|
81
|
-
options.extensions,
|
|
82
|
-
options.conditions
|
|
83
|
-
);
|
|
84
|
-
if (!/\.c?js$/.test(resolved) || !isCJSModule(resolved)) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
} catch (_) {
|
|
61
|
+
if (!path7.node.specifiers.length || /\.(?:mjs|marko)$|\?/.test(path7.node.source.value) || options.filter?.(path7.node.source.value) === false || !isCJSModule(
|
|
62
|
+
path7.node.source.value,
|
|
63
|
+
path7.hub.file.opts.filename
|
|
64
|
+
)) {
|
|
88
65
|
return;
|
|
89
66
|
}
|
|
90
67
|
let namespaceId;
|
|
@@ -455,7 +432,7 @@ function stripBasePath(basePath, path7) {
|
|
|
455
432
|
// src/manifest-generator.ts
|
|
456
433
|
var MARKER_COMMENT = "MARKO_VITE";
|
|
457
434
|
function generateDocManifest(basePath, rawHtml) {
|
|
458
|
-
return new Promise((
|
|
435
|
+
return new Promise((resolve, reject) => {
|
|
459
436
|
const parser = new Parser(
|
|
460
437
|
new DomHandler(function(err, dom) {
|
|
461
438
|
if (err) {
|
|
@@ -481,7 +458,7 @@ function generateDocManifest(basePath, rawHtml) {
|
|
|
481
458
|
bodyPrepend,
|
|
482
459
|
body
|
|
483
460
|
);
|
|
484
|
-
|
|
461
|
+
resolve({
|
|
485
462
|
preload,
|
|
486
463
|
"head-prepend": serializeOrNull(basePath, headPrepend, preload),
|
|
487
464
|
head: serializeOrNull(basePath, head, preload),
|
|
@@ -660,13 +637,10 @@ function renderAssets(slot) {
|
|
|
660
637
|
const slotWrittenEntriesKey = \`___viteWrittenEntries-\${slot}\`;
|
|
661
638
|
const lastWrittenEntry = this[slotWrittenEntriesKey] || 0;
|
|
662
639
|
const writtenEntries = (this[slotWrittenEntriesKey] = entries.length);
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
html += \`<script\${this.___viteInjectAttrs}>${opts.runtimeId ? `$mbp_${opts.runtimeId}` : "$mbp"}=\${JSON.stringify(base)}</script>\`
|
|
668
|
-
}
|
|
669
|
-
|
|
640
|
+
${opts.basePathVar ? `if (!this.___flushedMBP && slot !== "head-prepend") {
|
|
641
|
+
this.___flushedMBP = true;
|
|
642
|
+
html += \`<script\${this.___viteInjectAttrs}>${opts.runtimeId ? `$mbp_${opts.runtimeId}` : "$mbp"}=\${JSON.stringify(base)}</script>\`
|
|
643
|
+
}` : ""}
|
|
670
644
|
for (let i = lastWrittenEntry; i < writtenEntries; i++) {
|
|
671
645
|
let entry = entries[i];
|
|
672
646
|
|
|
@@ -828,7 +802,7 @@ function markoPlugin(opts = {}) {
|
|
|
828
802
|
let hydrateConfig;
|
|
829
803
|
const resolveVirtualDependency = (from, dep) => {
|
|
830
804
|
const normalizedFrom = normalizePath(from);
|
|
831
|
-
const query = `${virtualFileQuery}&id=${
|
|
805
|
+
const query = `${virtualFileQuery}&id=${encodeURIComponent(dep.virtualPath)}`;
|
|
832
806
|
const id = normalizedFrom + query;
|
|
833
807
|
if (devServer) {
|
|
834
808
|
const prev = virtualFiles.get(id);
|
|
@@ -840,6 +814,7 @@ function markoPlugin(opts = {}) {
|
|
|
840
814
|
return `./${path6.posix.basename(normalizedFrom) + query}`;
|
|
841
815
|
};
|
|
842
816
|
let root;
|
|
817
|
+
let rootResolveFile;
|
|
843
818
|
let devEntryFile;
|
|
844
819
|
let devEntryFilePosix;
|
|
845
820
|
let renderAssetsRuntimeCode;
|
|
@@ -901,6 +876,7 @@ function markoPlugin(opts = {}) {
|
|
|
901
876
|
config.base = process.env.BASE_URL;
|
|
902
877
|
}
|
|
903
878
|
root = normalizePath(config.root || process.cwd());
|
|
879
|
+
rootResolveFile = path6.join(root, "_.js");
|
|
904
880
|
baseConfig = {
|
|
905
881
|
cache,
|
|
906
882
|
optimize,
|
|
@@ -1013,7 +989,7 @@ function markoPlugin(opts = {}) {
|
|
|
1013
989
|
if (isSSRBuild && !config.build?.commonjsOptions?.esmExternals) {
|
|
1014
990
|
config.build ??= {};
|
|
1015
991
|
config.build.commonjsOptions ??= {};
|
|
1016
|
-
config.build.commonjsOptions.esmExternals = (id) => !isCJSModule(id);
|
|
992
|
+
config.build.commonjsOptions.esmExternals = (id) => !isCJSModule(id, rootResolveFile);
|
|
1017
993
|
}
|
|
1018
994
|
if (basePathVar) {
|
|
1019
995
|
config.experimental ??= {};
|
|
@@ -1067,8 +1043,6 @@ function markoPlugin(opts = {}) {
|
|
|
1067
1043
|
...ssrConfig.babelConfig,
|
|
1068
1044
|
plugins: (ssrConfig.babelConfig.plugins || []).concat(
|
|
1069
1045
|
plugin({
|
|
1070
|
-
extensions: config.resolve.extensions,
|
|
1071
|
-
conditions: config.resolve.conditions,
|
|
1072
1046
|
filter: isBuild ? void 0 : (path7) => !/^\./.test(path7)
|
|
1073
1047
|
})
|
|
1074
1048
|
)
|
|
@@ -1279,7 +1253,7 @@ function markoPlugin(opts = {}) {
|
|
|
1279
1253
|
if (!isMarkoFile(id)) {
|
|
1280
1254
|
if (!isBuild) {
|
|
1281
1255
|
const ext = path6.extname(id);
|
|
1282
|
-
if (ext === ".cjs" || ext === ".js" && isCJSModule(id)) {
|
|
1256
|
+
if (ext === ".cjs" || ext === ".js" && isCJSModule(id, rootResolveFile)) {
|
|
1283
1257
|
if (cjsToEsm === void 0) {
|
|
1284
1258
|
try {
|
|
1285
1259
|
cjsToEsm = (await import("@chialab/cjs-to-esm")).transform;
|
|
@@ -1303,7 +1277,7 @@ function markoPlugin(opts = {}) {
|
|
|
1303
1277
|
if (linked) {
|
|
1304
1278
|
cachedSources.set(id, source);
|
|
1305
1279
|
}
|
|
1306
|
-
if (!query && isCJSModule(id)) {
|
|
1280
|
+
if (!query && isCJSModule(id, rootResolveFile)) {
|
|
1307
1281
|
if (isBuild) {
|
|
1308
1282
|
const { code: code2, map: map2, meta: meta2 } = await compiler2.compile(
|
|
1309
1283
|
source,
|
|
@@ -1323,7 +1297,7 @@ function markoPlugin(opts = {}) {
|
|
|
1323
1297
|
id,
|
|
1324
1298
|
getConfigForFileSystem(
|
|
1325
1299
|
info,
|
|
1326
|
-
isSSR ? isCJSModule(id) ? ssrCjsConfig : ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
|
|
1300
|
+
isSSR ? isCJSModule(id, rootResolveFile) ? ssrCjsConfig : ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
|
|
1327
1301
|
)
|
|
1328
1302
|
);
|
|
1329
1303
|
const { map, meta } = compiled;
|
|
@@ -1474,13 +1448,13 @@ function getPosixBasenameWithoutExt(file) {
|
|
|
1474
1448
|
return file.slice(baseStart, extStart);
|
|
1475
1449
|
}
|
|
1476
1450
|
function createDeferredPromise() {
|
|
1477
|
-
let
|
|
1451
|
+
let resolve;
|
|
1478
1452
|
let reject;
|
|
1479
1453
|
const promise = new Promise((res, rej) => {
|
|
1480
|
-
|
|
1454
|
+
resolve = res;
|
|
1481
1455
|
reject = rej;
|
|
1482
1456
|
});
|
|
1483
|
-
promise.resolve =
|
|
1457
|
+
promise.resolve = resolve;
|
|
1484
1458
|
promise.reject = reject;
|
|
1485
1459
|
return promise;
|
|
1486
1460
|
}
|
package/dist/resolve.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export declare function isCJSModule(id: string): boolean;
|
|
2
|
-
export declare function resolve(id: string, from: string, extensions: string[], conditions: string[]): string;
|
|
1
|
+
export declare function isCJSModule(id: string, fromFile: string): boolean;
|