@marko/vite 5.0.13 → 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);
47
+ } catch {
48
+ isCJS = false;
34
49
  }
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];
57
- }
58
- return relativePath;
59
- }
60
- }
61
- function packageFilter(pkg) {
62
- if (pkg.exports) {
63
- pkg.main = exportsMainFile;
50
+ cjsModuleLookup.set(moduleId, isCJS);
64
51
  }
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,
@@ -1010,6 +989,11 @@ function markoPlugin(opts = {}) {
1010
989
  chunkFileNames: `[name]-[hash].js`
1011
990
  };
1012
991
  }
992
+ if (isSSRBuild && !config.build?.commonjsOptions?.esmExternals) {
993
+ config.build ??= {};
994
+ config.build.commonjsOptions ??= {};
995
+ config.build.commonjsOptions.esmExternals = (id) => !isCJSModule(id, rootResolveFile);
996
+ }
1013
997
  if (basePathVar) {
1014
998
  config.experimental ??= {};
1015
999
  if (config.experimental.renderBuiltUrl) {
@@ -1062,8 +1046,6 @@ function markoPlugin(opts = {}) {
1062
1046
  ...ssrConfig.babelConfig,
1063
1047
  plugins: (ssrConfig.babelConfig.plugins || []).concat(
1064
1048
  plugin({
1065
- extensions: config.resolve.extensions,
1066
- conditions: config.resolve.conditions,
1067
1049
  filter: isBuild ? void 0 : (path7) => !/^\./.test(path7)
1068
1050
  })
1069
1051
  )
@@ -1274,7 +1256,7 @@ function markoPlugin(opts = {}) {
1274
1256
  if (!isMarkoFile(id)) {
1275
1257
  if (!isBuild) {
1276
1258
  const ext = path6.extname(id);
1277
- if (ext === ".cjs" || ext === ".js" && isCJSModule(id)) {
1259
+ if (ext === ".cjs" || ext === ".js" && isCJSModule(id, rootResolveFile)) {
1278
1260
  if (cjsToEsm === void 0) {
1279
1261
  try {
1280
1262
  cjsToEsm = (await import("@chialab/cjs-to-esm")).transform;
@@ -1298,7 +1280,7 @@ function markoPlugin(opts = {}) {
1298
1280
  if (linked) {
1299
1281
  cachedSources.set(id, source);
1300
1282
  }
1301
- if (!query && isCJSModule(id)) {
1283
+ if (!query && isCJSModule(id, rootResolveFile)) {
1302
1284
  if (isBuild) {
1303
1285
  const { code: code2, map: map2, meta: meta2 } = await compiler2.compile(
1304
1286
  source,
@@ -1318,7 +1300,7 @@ function markoPlugin(opts = {}) {
1318
1300
  id,
1319
1301
  getConfigForFileSystem(
1320
1302
  info,
1321
- isSSR ? isCJSModule(id) ? ssrCjsConfig : ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
1303
+ isSSR ? isCJSModule(id, rootResolveFile) ? ssrCjsConfig : ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
1322
1304
  )
1323
1305
  );
1324
1306
  const { map, meta } = compiled;
@@ -1469,13 +1451,13 @@ function getPosixBasenameWithoutExt(file) {
1469
1451
  return file.slice(baseStart, extStart);
1470
1452
  }
1471
1453
  function createDeferredPromise() {
1472
- let resolve2;
1454
+ let resolve;
1473
1455
  let reject;
1474
1456
  const promise = new Promise((res, rej) => {
1475
- resolve2 = res;
1457
+ resolve = res;
1476
1458
  reject = rej;
1477
1459
  });
1478
- promise.resolve = resolve2;
1460
+ promise.resolve = resolve;
1479
1461
  promise.reject = reject;
1480
1462
  return promise;
1481
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.13",
3
+ "version": "5.0.15",
4
4
  "description": "A Marko plugin for Vite",
5
5
  "keywords": [
6
6
  "loader",