@marko/vite 5.0.14 → 5.0.15

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.
@@ -26,7 +26,5 @@ import type { PluginObj } from "@babel/core";
26
26
  * ```
27
27
  */
28
28
  export default function plugin(options: {
29
- extensions: string[];
30
- conditions: string[];
31
29
  filter?: (path: string) => boolean;
32
30
  }): PluginObj;
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
- import { exports } from "resolve.exports";
18
- var exportsMainFile = `__package_exports__`;
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
- const modulePath = modulePathReg.exec(id)?.[0];
23
- if (modulePath) {
24
- const pkgPath = modulePath + "package.json";
25
- let isCJS = cjsModuleLookup.get(pkgPath);
26
- if (isCJS === void 0) {
27
- try {
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
- } catch {
31
- isCJS = false;
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
- cjsModuleLookup.set(pkgPath, isCJS);
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
- return relativePath;
50
+ cjsModuleLookup.set(moduleId, isCJS);
59
51
  }
60
- }
61
- function packageFilter(pkg) {
62
- if (pkg.exports) {
63
- pkg.main = exportsMainFile;
64
- }
65
- return pkg;
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
- return;
76
- }
77
- try {
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((resolve2, reject) => {
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
- resolve2({
461
+ resolve({
485
462
  preload,
486
463
  "head-prepend": serializeOrNull(basePath, headPrepend, preload),
487
464
  head: serializeOrNull(basePath, head, preload),
@@ -840,6 +817,7 @@ function markoPlugin(opts = {}) {
840
817
  return `./${path6.posix.basename(normalizedFrom) + query}`;
841
818
  };
842
819
  let root;
820
+ let rootResolveFile;
843
821
  let devEntryFile;
844
822
  let devEntryFilePosix;
845
823
  let renderAssetsRuntimeCode;
@@ -901,6 +879,7 @@ function markoPlugin(opts = {}) {
901
879
  config.base = process.env.BASE_URL;
902
880
  }
903
881
  root = normalizePath(config.root || process.cwd());
882
+ rootResolveFile = path6.join(root, "_.js");
904
883
  baseConfig = {
905
884
  cache,
906
885
  optimize,
@@ -1013,7 +992,7 @@ function markoPlugin(opts = {}) {
1013
992
  if (isSSRBuild && !config.build?.commonjsOptions?.esmExternals) {
1014
993
  config.build ??= {};
1015
994
  config.build.commonjsOptions ??= {};
1016
- config.build.commonjsOptions.esmExternals = (id) => !isCJSModule(id);
995
+ config.build.commonjsOptions.esmExternals = (id) => !isCJSModule(id, rootResolveFile);
1017
996
  }
1018
997
  if (basePathVar) {
1019
998
  config.experimental ??= {};
@@ -1067,8 +1046,6 @@ function markoPlugin(opts = {}) {
1067
1046
  ...ssrConfig.babelConfig,
1068
1047
  plugins: (ssrConfig.babelConfig.plugins || []).concat(
1069
1048
  plugin({
1070
- extensions: config.resolve.extensions,
1071
- conditions: config.resolve.conditions,
1072
1049
  filter: isBuild ? void 0 : (path7) => !/^\./.test(path7)
1073
1050
  })
1074
1051
  )
@@ -1279,7 +1256,7 @@ function markoPlugin(opts = {}) {
1279
1256
  if (!isMarkoFile(id)) {
1280
1257
  if (!isBuild) {
1281
1258
  const ext = path6.extname(id);
1282
- if (ext === ".cjs" || ext === ".js" && isCJSModule(id)) {
1259
+ if (ext === ".cjs" || ext === ".js" && isCJSModule(id, rootResolveFile)) {
1283
1260
  if (cjsToEsm === void 0) {
1284
1261
  try {
1285
1262
  cjsToEsm = (await import("@chialab/cjs-to-esm")).transform;
@@ -1303,7 +1280,7 @@ function markoPlugin(opts = {}) {
1303
1280
  if (linked) {
1304
1281
  cachedSources.set(id, source);
1305
1282
  }
1306
- if (!query && isCJSModule(id)) {
1283
+ if (!query && isCJSModule(id, rootResolveFile)) {
1307
1284
  if (isBuild) {
1308
1285
  const { code: code2, map: map2, meta: meta2 } = await compiler2.compile(
1309
1286
  source,
@@ -1323,7 +1300,7 @@ function markoPlugin(opts = {}) {
1323
1300
  id,
1324
1301
  getConfigForFileSystem(
1325
1302
  info,
1326
- isSSR ? isCJSModule(id) ? ssrCjsConfig : ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
1303
+ isSSR ? isCJSModule(id, rootResolveFile) ? ssrCjsConfig : ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
1327
1304
  )
1328
1305
  );
1329
1306
  const { map, meta } = compiled;
@@ -1474,13 +1451,13 @@ function getPosixBasenameWithoutExt(file) {
1474
1451
  return file.slice(baseStart, extStart);
1475
1452
  }
1476
1453
  function createDeferredPromise() {
1477
- let resolve2;
1454
+ let resolve;
1478
1455
  let reject;
1479
1456
  const promise = new Promise((res, rej) => {
1480
- resolve2 = res;
1457
+ resolve = res;
1481
1458
  reject = rej;
1482
1459
  });
1483
- promise.resolve = resolve2;
1460
+ promise.resolve = resolve;
1484
1461
  promise.reject = reject;
1485
1462
  return promise;
1486
1463
  }
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marko/vite",
3
- "version": "5.0.14",
3
+ "version": "5.0.15",
4
4
  "description": "A Marko plugin for Vite",
5
5
  "keywords": [
6
6
  "loader",