@marko/vite 5.3.2 → 5.3.4

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.
@@ -9,20 +9,21 @@ import type { PluginObj } from "@babel/core";
9
9
  * Examples
10
10
  * 1. Source: ```import { bar as baz } from 'foo';```
11
11
  * Becomes:```
12
- * import _foo from 'foo';
13
- * const { bar: baz } = _foo
12
+ * import * as _foo from 'foo';
13
+ * const { bar: baz } = _foo?.default === void 0 || _foo?.__esModule ? _foo : _foo.default;
14
14
  * ```
15
15
  *
16
16
  * 2. Source: ```import myFoo from 'foo';```
17
17
  * Becomes: ```
18
18
  * import * as _myFoo from 'foo';
19
- * const myFoo = _myFoo?.__esModule ? _myFoo.default : _myFoo;
19
+ * const { default: myFoo } = _myFoo?.__esModule ? _myFoo.default : _myFoo;
20
20
  * ```
21
21
  *
22
22
  * 3. Source: ```import foo, * as nsFoo from 'foo';```
23
23
  * Becomes: ```
24
- * import nsFoo from 'foo';
25
- * const myFoo = nsFoo?.__esModule ? nsFoo.default : nsFoo
24
+ * import * as _nsFoo from 'foo';
25
+ * const nsFoo = _nsFoo?.default === void 0 || _nsFoo?.__esModule ? _nsFoo : _nsFoo.default
26
+ * const { default: myFoo } = _nsFoo?.__esModule ? _nsFoo.default : _nsFoo
26
27
  * ```
27
28
  */
28
29
  export default function plugin(options: {
@@ -2,5 +2,9 @@ import * as compiler from "@marko/compiler";
2
2
  import type * as vite from "vite";
3
3
  type ESBuildOptions = Exclude<vite.DepOptimizationConfig["esbuildOptions"], undefined>;
4
4
  type ESBuildPlugin = Exclude<ESBuildOptions["plugins"], undefined>[number];
5
- export default function esbuildPlugin(config: compiler.Config): ESBuildPlugin;
5
+ export default function esbuildPlugin(config: compiler.Config, virtualFiles: Map<string, {
6
+ code: string;
7
+ } | Promise<{
8
+ code: string;
9
+ }>>, cacheVirtualFile: (path: string) => void): ESBuildPlugin;
6
10
  export {};
package/dist/index.mjs CHANGED
@@ -110,11 +110,24 @@ function plugin(options) {
110
110
  t.variableDeclarator(
111
111
  namespaceId,
112
112
  t.conditionalExpression(
113
- t.optionalMemberExpression(
114
- rawImport,
115
- t.identifier("__esModule"),
116
- false,
117
- true
113
+ t.logicalExpression(
114
+ "||",
115
+ t.binaryExpression(
116
+ "===",
117
+ t.optionalMemberExpression(
118
+ rawImport,
119
+ t.identifier("default"),
120
+ false,
121
+ true
122
+ ),
123
+ t.unaryExpression("void", t.numericLiteral(0), true)
124
+ ),
125
+ t.optionalMemberExpression(
126
+ rawImport,
127
+ t.identifier("__esModule"),
128
+ false,
129
+ true
130
+ )
118
131
  ),
119
132
  rawImport,
120
133
  t.memberExpression(rawImport, t.identifier("default"))
@@ -138,11 +151,24 @@ function plugin(options) {
138
151
  )
139
152
  ),
140
153
  t.conditionalExpression(
141
- t.optionalMemberExpression(
142
- rawImport,
143
- t.identifier("__esModule"),
144
- false,
145
- true
154
+ t.logicalExpression(
155
+ "||",
156
+ t.binaryExpression(
157
+ "===",
158
+ t.optionalMemberExpression(
159
+ rawImport,
160
+ t.identifier("default"),
161
+ false,
162
+ true
163
+ ),
164
+ t.unaryExpression("void", t.numericLiteral(0), true)
165
+ ),
166
+ t.optionalMemberExpression(
167
+ rawImport,
168
+ t.identifier("__esModule"),
169
+ false,
170
+ true
171
+ )
146
172
  ),
147
173
  rawImport,
148
174
  t.memberExpression(rawImport, t.identifier("default"))
@@ -161,8 +187,9 @@ import * as compiler from "@marko/compiler";
161
187
  import fs2 from "fs";
162
188
  import path2 from "path";
163
189
  var importTagReg = /<([^>]+)>/;
164
- var markoErrorRegExp = /^(.+?)(?:\((\d+)(?:\s*,\s*(\d+))?\))?: (.*)$/gm;
165
- function esbuildPlugin(config) {
190
+ var markoErrorReg = /^(.+?)(?:\((\d+)(?:\s*,\s*(\d+))?\))?: (.*)$/gm;
191
+ var virtualFileReg = /\?marko-virtual&/;
192
+ function esbuildPlugin(config, virtualFiles2, cacheVirtualFile) {
166
193
  return {
167
194
  name: "marko",
168
195
  async setup(build) {
@@ -178,12 +205,27 @@ function esbuildPlugin(config) {
178
205
  ...finalConfig,
179
206
  output: "hydrate"
180
207
  };
181
- build.onResolve({ filter: /\.marko\./ }, (args) => {
208
+ build.onResolve({ filter: virtualFileReg }, (args) => {
209
+ const resolvedPath = path2.resolve(args.resolveDir, args.path);
210
+ const isExternal = !/\.(?:[cm]?[jt]s|json)$/i.test(resolvedPath);
211
+ if (isExternal && !isScan) {
212
+ void cacheVirtualFile(resolvedPath);
213
+ }
182
214
  return {
183
- path: path2.resolve(args.resolveDir, args.path),
184
- external: true
215
+ path: resolvedPath,
216
+ external: isExternal
185
217
  };
186
218
  });
219
+ build.onLoad({ filter: virtualFileReg }, async (args) => {
220
+ const file = virtualFiles2.get(args.path);
221
+ if (file) {
222
+ return {
223
+ contents: (await file).code,
224
+ loader: path2.extname(args.path).slice(1),
225
+ resolveDir: path2.dirname(args.path)
226
+ };
227
+ }
228
+ });
187
229
  build.onResolve({ filter: importTagReg }, (args) => {
188
230
  const tagName = importTagReg.exec(args.path)?.[1];
189
231
  if (tagName) {
@@ -211,7 +253,7 @@ function esbuildPlugin(config) {
211
253
  const errors = [];
212
254
  let match;
213
255
  let lines;
214
- while (match = markoErrorRegExp.exec(text)) {
256
+ while (match = markoErrorReg.exec(text)) {
215
257
  const [, file, rawLine, rawCol, text2] = match;
216
258
  const line = parseInt(rawLine, 10) || 1;
217
259
  const column = parseInt(rawCol, 10) || 1;
@@ -821,6 +863,7 @@ function markoPlugin(opts = {}) {
821
863
  return `./${path6.posix.basename(normalizedFrom) + query}`;
822
864
  };
823
865
  let root;
866
+ let cacheDir;
824
867
  let rootResolveFile;
825
868
  let devEntryFile;
826
869
  let devEntryFilePosix;
@@ -962,6 +1005,8 @@ function markoPlugin(opts = {}) {
962
1005
  test.deps.optimizer ??= {};
963
1006
  test.deps.optimizer.web ??= {};
964
1007
  test.deps.optimizer.web.enabled ??= true;
1008
+ test.deps.optimizer.client ??= {};
1009
+ test.deps.optimizer.client.enabled ??= true;
965
1010
  }
966
1011
  }
967
1012
  if (!registeredTagLib) {
@@ -988,7 +1033,23 @@ function markoPlugin(opts = {}) {
988
1033
  optimizeExtensions.push(".marko");
989
1034
  const esbuildOptions = optimizeDeps.esbuildOptions ??= {};
990
1035
  const esbuildPlugins = esbuildOptions.plugins ??= [];
991
- esbuildPlugins.push(esbuildPlugin(baseConfig));
1036
+ let cacheDirPromise;
1037
+ esbuildPlugins.push(
1038
+ esbuildPlugin(baseConfig, virtualFiles, async (resolved) => {
1039
+ if (cacheDir) {
1040
+ const file = virtualFiles.get(resolved);
1041
+ if (file) {
1042
+ await (cacheDirPromise ||= fs4.promises.mkdir(cacheDir, {
1043
+ recursive: true
1044
+ }));
1045
+ await fs4.promises.writeFile(
1046
+ virtualPathToCacheFile(resolved, root, cacheDir),
1047
+ (await file).code
1048
+ );
1049
+ }
1050
+ }
1051
+ })
1052
+ );
992
1053
  const ssr = config.ssr ??= {};
993
1054
  const { noExternal } = ssr;
994
1055
  if (noExternal !== true) {
@@ -1061,6 +1122,7 @@ function markoPlugin(opts = {}) {
1061
1122
  },
1062
1123
  configResolved(config) {
1063
1124
  basePath = config.base;
1125
+ cacheDir = config.cacheDir && normalizePath(config.cacheDir);
1064
1126
  getMarkoAssetFns = void 0;
1065
1127
  for (const plugin2 of config.plugins) {
1066
1128
  const fn = plugin2.api?.getMarkoAssetCodeForEntry;
@@ -1074,7 +1136,9 @@ function markoPlugin(opts = {}) {
1074
1136
  }
1075
1137
  },
1076
1138
  configureServer(_server) {
1077
- ssrConfig.hot = ssrCjsConfig.hot = domConfig.hot = true;
1139
+ if (!isTest) {
1140
+ ssrConfig.hot = ssrCjsConfig.hot = domConfig.hot = true;
1141
+ }
1078
1142
  devServer = _server;
1079
1143
  devServer.watcher.on("all", (type, originalFileName) => {
1080
1144
  const fileName = normalizePath(originalFileName);
@@ -1170,6 +1234,9 @@ function markoPlugin(opts = {}) {
1170
1234
  }
1171
1235
  let importeeQuery = getMarkoQuery(importee);
1172
1236
  if (importeeQuery) {
1237
+ if (importee[0] !== "." && importee[0] !== "\0" && importeeQuery.startsWith(virtualFileQuery)) {
1238
+ return importee;
1239
+ }
1173
1240
  importee = importee.slice(0, -importeeQuery.length);
1174
1241
  } else if (!importOpts.scan) {
1175
1242
  if (ssr && linked && importer && importer[0] !== "\0" && (importer !== devEntryFile || normalizePath(importer) !== devEntryFilePosix) && // Vite tries to resolve against an `index.html` in some cases, we ignore it here.
@@ -1191,7 +1258,7 @@ function markoPlugin(opts = {}) {
1191
1258
  )
1192
1259
  } : await this.resolve(importee, importer, resolveOpts);
1193
1260
  if (resolved) {
1194
- resolved.id += importeeQuery;
1261
+ resolved.id = stripVersionAndTimeStamp(resolved.id) + importeeQuery;
1195
1262
  }
1196
1263
  return resolved;
1197
1264
  }
@@ -1219,14 +1286,26 @@ function markoPlugin(opts = {}) {
1219
1286
  return "NO_CLIENT_ASSETS";
1220
1287
  }
1221
1288
  const query = getMarkoQuery(id);
1222
- switch (query) {
1223
- case serverEntryQuery: {
1224
- entryIds.add(id.slice(0, -query.length));
1225
- return null;
1226
- }
1227
- case browserEntryQuery:
1228
- case browserQuery: {
1229
- return cachedSources.get(id.slice(0, -query.length)) || null;
1289
+ if (query) {
1290
+ switch (query) {
1291
+ case serverEntryQuery: {
1292
+ entryIds.add(id.slice(0, -query.length));
1293
+ return null;
1294
+ }
1295
+ case browserEntryQuery:
1296
+ case browserQuery: {
1297
+ return cachedSources.get(id.slice(0, -query.length)) || null;
1298
+ }
1299
+ default:
1300
+ return virtualFiles.get(id) || cacheDir && fs4.promises.readFile(
1301
+ virtualPathToCacheFile(id, root, cacheDir),
1302
+ "utf8"
1303
+ ).then((code) => {
1304
+ virtualFiles.set(id, { code });
1305
+ return code;
1306
+ }).catch(() => {
1307
+ return null;
1308
+ });
1230
1309
  }
1231
1310
  }
1232
1311
  return virtualFiles.get(id) || null;
@@ -1288,7 +1367,7 @@ function markoPlugin(opts = {}) {
1288
1367
  }
1289
1368
  }
1290
1369
  if (!isMarkoFile(id)) {
1291
- if (!isBuild) {
1370
+ if (!isBuild && isSSR) {
1292
1371
  const ext = path6.extname(id);
1293
1372
  if (ext === ".cjs" || ext === ".js" && isCJSModule(id, rootResolveFile)) {
1294
1373
  if (cjsToEsm === void 0) {
@@ -1316,14 +1395,14 @@ function markoPlugin(opts = {}) {
1316
1395
  }
1317
1396
  if (!query && isCJSModule(id, rootResolveFile)) {
1318
1397
  if (isBuild) {
1319
- const { code: code2, map: map2, meta: meta2 } = await compiler2.compile(
1398
+ const { code: code2, map, meta: meta2 } = await compiler2.compile(
1320
1399
  source,
1321
1400
  id,
1322
1401
  getConfigForFileSystem(info, ssrCjsConfig)
1323
1402
  );
1324
1403
  return {
1325
1404
  code: code2,
1326
- map: map2,
1405
+ map: toSourceMap(map),
1327
1406
  meta: { arcSourceCode: source, arcScanIds: meta2.analyzedTags }
1328
1407
  };
1329
1408
  }
@@ -1337,9 +1416,9 @@ function markoPlugin(opts = {}) {
1337
1416
  isSSR ? isCJSModule(id, rootResolveFile) ? ssrCjsConfig : ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
1338
1417
  )
1339
1418
  );
1340
- const { map, meta } = compiled;
1419
+ const { meta } = compiled;
1341
1420
  let { code } = compiled;
1342
- if (query !== browserEntryQuery && devServer && !isTagsApi()) {
1421
+ if (!isTest && query !== browserEntryQuery && devServer && !isTagsApi()) {
1343
1422
  code += `
1344
1423
  if (import.meta.hot) import.meta.hot.accept(() => {});`;
1345
1424
  }
@@ -1359,7 +1438,7 @@ if (import.meta.hot) import.meta.hot.accept(() => {});`;
1359
1438
  }
1360
1439
  return {
1361
1440
  code,
1362
- map,
1441
+ map: toSourceMap(compiled.map),
1363
1442
  meta: isBuild ? { arcSourceCode: source, arcScanIds: meta.analyzedTags } : void 0
1364
1443
  };
1365
1444
  }
@@ -1491,6 +1570,12 @@ function fileNameToURL(fileName, root) {
1491
1570
  }
1492
1571
  return `/${relativeURL}`;
1493
1572
  }
1573
+ function virtualPathToCacheFile(virtualPath, root, cacheDir) {
1574
+ return path6.join(
1575
+ cacheDir,
1576
+ normalizePath(path6.relative(root, virtualPath)).replace(/[\\/]+/g, "_")
1577
+ );
1578
+ }
1494
1579
  function getPosixBasenameWithoutExt(file) {
1495
1580
  const baseStart = file.lastIndexOf(POSIX_SEP) + 1;
1496
1581
  const extStart = file.indexOf(".", baseStart + 1);
@@ -1570,6 +1655,9 @@ function getKnownTemplates(cwd) {
1570
1655
  }
1571
1656
  return knownTemplates;
1572
1657
  }
1658
+ function toSourceMap(map) {
1659
+ return map ? { mappings: map.mappings } : null;
1660
+ }
1573
1661
  export {
1574
1662
  markoPlugin as default
1575
1663
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marko/vite",
3
- "version": "5.3.2",
3
+ "version": "5.3.4",
4
4
  "description": "A Marko plugin for Vite",
5
5
  "keywords": [
6
6
  "loader",
@@ -87,7 +87,7 @@
87
87
  "tsx": "^4.20.3",
88
88
  "typescript": "^5.8.3",
89
89
  "typescript-eslint": "^8.37.0",
90
- "vite": "^7.0.4"
90
+ "vite": "^7.1.10"
91
91
  },
92
92
  "peerDependencies": {
93
93
  "@marko/compiler": "^5",