@marko/vite 5.3.2 → 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;
@@ -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;
@@ -988,7 +1031,23 @@ function markoPlugin(opts = {}) {
988
1031
  optimizeExtensions.push(".marko");
989
1032
  const esbuildOptions = optimizeDeps.esbuildOptions ??= {};
990
1033
  const esbuildPlugins = esbuildOptions.plugins ??= [];
991
- 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
+ );
992
1051
  const ssr = config.ssr ??= {};
993
1052
  const { noExternal } = ssr;
994
1053
  if (noExternal !== true) {
@@ -1061,6 +1120,7 @@ function markoPlugin(opts = {}) {
1061
1120
  },
1062
1121
  configResolved(config) {
1063
1122
  basePath = config.base;
1123
+ cacheDir = config.cacheDir && normalizePath(config.cacheDir);
1064
1124
  getMarkoAssetFns = void 0;
1065
1125
  for (const plugin2 of config.plugins) {
1066
1126
  const fn = plugin2.api?.getMarkoAssetCodeForEntry;
@@ -1170,6 +1230,9 @@ function markoPlugin(opts = {}) {
1170
1230
  }
1171
1231
  let importeeQuery = getMarkoQuery(importee);
1172
1232
  if (importeeQuery) {
1233
+ if (importee[0] !== "." && importee[0] !== "\0" && importeeQuery.startsWith(virtualFileQuery)) {
1234
+ return importee;
1235
+ }
1173
1236
  importee = importee.slice(0, -importeeQuery.length);
1174
1237
  } else if (!importOpts.scan) {
1175
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.
@@ -1191,7 +1254,7 @@ function markoPlugin(opts = {}) {
1191
1254
  )
1192
1255
  } : await this.resolve(importee, importer, resolveOpts);
1193
1256
  if (resolved) {
1194
- resolved.id += importeeQuery;
1257
+ resolved.id = stripVersionAndTimeStamp(resolved.id) + importeeQuery;
1195
1258
  }
1196
1259
  return resolved;
1197
1260
  }
@@ -1219,14 +1282,26 @@ function markoPlugin(opts = {}) {
1219
1282
  return "NO_CLIENT_ASSETS";
1220
1283
  }
1221
1284
  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;
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
+ });
1230
1305
  }
1231
1306
  }
1232
1307
  return virtualFiles.get(id) || null;
@@ -1491,6 +1566,12 @@ function fileNameToURL(fileName, root) {
1491
1566
  }
1492
1567
  return `/${relativeURL}`;
1493
1568
  }
1569
+ function virtualPathToCacheFile(virtualPath, root, cacheDir) {
1570
+ return path6.join(
1571
+ cacheDir,
1572
+ normalizePath(path6.relative(root, virtualPath)).replace(/[\\/]+/g, "_")
1573
+ );
1574
+ }
1494
1575
  function getPosixBasenameWithoutExt(file) {
1495
1576
  const baseStart = file.lastIndexOf(POSIX_SEP) + 1;
1496
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.2",
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",