@marko/vite 5.3.1 → 5.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.
@@ -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;
@@ -717,12 +759,13 @@ var render_assets_transform_default = (tag, t3) => {
717
759
  };
718
760
  function renderAssetsCall(t3, slot) {
719
761
  return t3.markoPlaceholder(
720
- t3.callExpression(
762
+ t3.optionalCallExpression(
721
763
  t3.memberExpression(
722
764
  t3.identifier("$global"),
723
765
  t3.identifier("___viteRenderAssets")
724
766
  ),
725
- [t3.stringLiteral(slot)]
767
+ [t3.stringLiteral(slot)],
768
+ true
726
769
  ),
727
770
  false
728
771
  );
@@ -820,6 +863,7 @@ function markoPlugin(opts = {}) {
820
863
  return `./${path6.posix.basename(normalizedFrom) + query}`;
821
864
  };
822
865
  let root;
866
+ let cacheDir;
823
867
  let rootResolveFile;
824
868
  let devEntryFile;
825
869
  let devEntryFilePosix;
@@ -987,7 +1031,23 @@ function markoPlugin(opts = {}) {
987
1031
  optimizeExtensions.push(".marko");
988
1032
  const esbuildOptions = optimizeDeps.esbuildOptions ??= {};
989
1033
  const esbuildPlugins = esbuildOptions.plugins ??= [];
990
- esbuildPlugins.push(esbuildPlugin(baseConfig));
1034
+ let cacheDirPromise;
1035
+ esbuildPlugins.push(
1036
+ esbuildPlugin(baseConfig, virtualFiles, async (resolved) => {
1037
+ if (cacheDir) {
1038
+ const file = virtualFiles.get(resolved);
1039
+ if (file) {
1040
+ await (cacheDirPromise ||= fs4.promises.mkdir(cacheDir, {
1041
+ recursive: true
1042
+ }));
1043
+ await fs4.promises.writeFile(
1044
+ virtualPathToCacheFile(resolved, root, cacheDir),
1045
+ (await file).code
1046
+ );
1047
+ }
1048
+ }
1049
+ })
1050
+ );
991
1051
  const ssr = config.ssr ??= {};
992
1052
  const { noExternal } = ssr;
993
1053
  if (noExternal !== true) {
@@ -1060,6 +1120,7 @@ function markoPlugin(opts = {}) {
1060
1120
  },
1061
1121
  configResolved(config) {
1062
1122
  basePath = config.base;
1123
+ cacheDir = config.cacheDir && normalizePath(config.cacheDir);
1063
1124
  getMarkoAssetFns = void 0;
1064
1125
  for (const plugin2 of config.plugins) {
1065
1126
  const fn = plugin2.api?.getMarkoAssetCodeForEntry;
@@ -1169,6 +1230,9 @@ function markoPlugin(opts = {}) {
1169
1230
  }
1170
1231
  let importeeQuery = getMarkoQuery(importee);
1171
1232
  if (importeeQuery) {
1233
+ if (importee[0] !== "." && importee[0] !== "\0" && importeeQuery.startsWith(virtualFileQuery)) {
1234
+ return importee;
1235
+ }
1172
1236
  importee = importee.slice(0, -importeeQuery.length);
1173
1237
  } else if (!importOpts.scan) {
1174
1238
  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.
@@ -1190,7 +1254,7 @@ function markoPlugin(opts = {}) {
1190
1254
  )
1191
1255
  } : await this.resolve(importee, importer, resolveOpts);
1192
1256
  if (resolved) {
1193
- resolved.id += importeeQuery;
1257
+ resolved.id = stripVersionAndTimeStamp(resolved.id) + importeeQuery;
1194
1258
  }
1195
1259
  return resolved;
1196
1260
  }
@@ -1218,14 +1282,26 @@ function markoPlugin(opts = {}) {
1218
1282
  return "NO_CLIENT_ASSETS";
1219
1283
  }
1220
1284
  const query = getMarkoQuery(id);
1221
- switch (query) {
1222
- case serverEntryQuery: {
1223
- entryIds.add(id.slice(0, -query.length));
1224
- return null;
1225
- }
1226
- case browserEntryQuery:
1227
- case browserQuery: {
1228
- return cachedSources.get(id.slice(0, -query.length)) || null;
1285
+ if (query) {
1286
+ switch (query) {
1287
+ case serverEntryQuery: {
1288
+ entryIds.add(id.slice(0, -query.length));
1289
+ return null;
1290
+ }
1291
+ case browserEntryQuery:
1292
+ case browserQuery: {
1293
+ return cachedSources.get(id.slice(0, -query.length)) || null;
1294
+ }
1295
+ default:
1296
+ return virtualFiles.get(id) || cacheDir && fs4.promises.readFile(
1297
+ virtualPathToCacheFile(id, root, cacheDir),
1298
+ "utf8"
1299
+ ).then((code) => {
1300
+ virtualFiles.set(id, { code });
1301
+ return code;
1302
+ }).catch(() => {
1303
+ return null;
1304
+ });
1229
1305
  }
1230
1306
  }
1231
1307
  return virtualFiles.get(id) || null;
@@ -1490,6 +1566,12 @@ function fileNameToURL(fileName, root) {
1490
1566
  }
1491
1567
  return `/${relativeURL}`;
1492
1568
  }
1569
+ function virtualPathToCacheFile(virtualPath, root, cacheDir) {
1570
+ return path6.join(
1571
+ cacheDir,
1572
+ normalizePath(path6.relative(root, virtualPath)).replace(/[\\/]+/g, "_")
1573
+ );
1574
+ }
1493
1575
  function getPosixBasenameWithoutExt(file) {
1494
1576
  const baseStart = file.lastIndexOf(POSIX_SEP) + 1;
1495
1577
  const extStart = file.indexOf(".", baseStart + 1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marko/vite",
3
- "version": "5.3.1",
3
+ "version": "5.3.3",
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",