@marko/vite 3.0.1 → 3.1.0

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.
@@ -6,31 +6,34 @@ function esbuildPlugin(compiler, config) {
6
6
  return {
7
7
  name: "marko",
8
8
  async setup(build) {
9
+ var _a;
9
10
  const { platform = "browser" } = build.initialOptions;
11
+ const isScan = (_a = build.initialOptions.plugins) == null ? void 0 : _a.some(
12
+ (v) => v.name === "vite:dep-scan"
13
+ );
10
14
  const virtualFiles = /* @__PURE__ */ new Map();
11
15
  const finalConfig = {
12
16
  ...config,
13
- output: platform === "browser" ? "dom" : "html",
17
+ output: isScan ? "hydrate" : platform === "browser" ? "dom" : "html",
14
18
  resolveVirtualDependency(from, dep) {
15
19
  virtualFiles.set(path.join(from, "..", dep.virtualPath), dep);
16
20
  return dep.virtualPath;
17
21
  }
18
22
  };
19
- if (platform === "browser") {
20
- build.onResolve({ filter: /\.marko\./ }, (args) => {
21
- return {
22
- namespace: "marko:virtual",
23
- path: path.resolve(args.resolveDir, args.path)
24
- };
25
- });
26
- build.onLoad(
27
- { filter: /\.marko\./, namespace: "marko:virtual" },
28
- (args) => ({
29
- contents: virtualFiles.get(args.path).code,
30
- loader: path.extname(args.path).slice(1)
31
- })
32
- );
33
- }
23
+ build.onResolve({ filter: /\.marko\./ }, (args) => {
24
+ return {
25
+ namespace: "marko:virtual",
26
+ path: path.resolve(args.resolveDir, args.path)
27
+ };
28
+ });
29
+ build.onLoad(
30
+ { filter: /\.marko\./, namespace: "marko:virtual" },
31
+ (args) => ({
32
+ contents: virtualFiles.get(args.path).code,
33
+ loader: path.extname(args.path).slice(1),
34
+ external: isScan
35
+ })
36
+ );
34
37
  build.onLoad({ filter: /\.marko$/ }, async (args) => {
35
38
  try {
36
39
  const { code, meta } = await compiler.compileFile(
@@ -1,3 +1,6 @@
1
- import type * as esbuild from "esbuild";
1
+ import type * as vite from "vite";
2
2
  import type * as Compiler from "@marko/compiler";
3
- export default function esbuildPlugin(compiler: typeof Compiler, config: Compiler.Config): esbuild.Plugin;
3
+ type ESBuildOptions = Exclude<vite.DepOptimizationConfig["esbuildOptions"], undefined>;
4
+ type ESBuildPlugin = Exclude<ESBuildOptions["plugins"], undefined>[number];
5
+ export default function esbuildPlugin(compiler: typeof Compiler, config: Compiler.Config): ESBuildPlugin;
6
+ export {};
@@ -38,31 +38,34 @@ function esbuildPlugin(compiler, config) {
38
38
  return {
39
39
  name: "marko",
40
40
  async setup(build) {
41
+ var _a;
41
42
  const { platform = "browser" } = build.initialOptions;
43
+ const isScan = (_a = build.initialOptions.plugins) == null ? void 0 : _a.some(
44
+ (v) => v.name === "vite:dep-scan"
45
+ );
42
46
  const virtualFiles = /* @__PURE__ */ new Map();
43
47
  const finalConfig = {
44
48
  ...config,
45
- output: platform === "browser" ? "dom" : "html",
49
+ output: isScan ? "hydrate" : platform === "browser" ? "dom" : "html",
46
50
  resolveVirtualDependency(from, dep) {
47
51
  virtualFiles.set(import_path.default.join(from, "..", dep.virtualPath), dep);
48
52
  return dep.virtualPath;
49
53
  }
50
54
  };
51
- if (platform === "browser") {
52
- build.onResolve({ filter: /\.marko\./ }, (args) => {
53
- return {
54
- namespace: "marko:virtual",
55
- path: import_path.default.resolve(args.resolveDir, args.path)
56
- };
57
- });
58
- build.onLoad(
59
- { filter: /\.marko\./, namespace: "marko:virtual" },
60
- (args) => ({
61
- contents: virtualFiles.get(args.path).code,
62
- loader: import_path.default.extname(args.path).slice(1)
63
- })
64
- );
65
- }
55
+ build.onResolve({ filter: /\.marko\./ }, (args) => {
56
+ return {
57
+ namespace: "marko:virtual",
58
+ path: import_path.default.resolve(args.resolveDir, args.path)
59
+ };
60
+ });
61
+ build.onLoad(
62
+ { filter: /\.marko\./, namespace: "marko:virtual" },
63
+ (args) => ({
64
+ contents: virtualFiles.get(args.path).code,
65
+ loader: import_path.default.extname(args.path).slice(1),
66
+ external: isScan
67
+ })
68
+ );
66
69
  build.onLoad({ filter: /\.marko$/ }, async (args) => {
67
70
  try {
68
71
  const { code, meta } = await compiler.compileFile(
@@ -108,5 +111,3 @@ function esbuildPlugin(compiler, config) {
108
111
  }
109
112
  };
110
113
  }
111
- // Annotate the CommonJS export names for ESM import in node:
112
- 0 && (module.exports = {});
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  esbuildPlugin
3
- } from "./chunk-KRSZ5IRT.mjs";
3
+ } from "./chunk-HR4PYNIR.mjs";
4
4
  export {
5
5
  esbuildPlugin as default
6
6
  };
package/dist/index.d.ts CHANGED
@@ -1,19 +1,8 @@
1
1
  import type * as vite from "vite";
2
2
  import type * as Compiler from "@marko/compiler";
3
- import { BuildStore } from "./store";
3
+ import { type BuildStore } from "./store";
4
4
  export * from "./store";
5
5
  export type { BuildStore } from "./store";
6
- declare module "@marko/babel-utils" {
7
- interface Taglib {
8
- id: string;
9
- dirname: string;
10
- path: string;
11
- tags: TagDefinition[];
12
- }
13
- interface TaglibLookup {
14
- taglibsById: Record<string, Taglib>;
15
- }
16
- }
17
6
  export interface Options {
18
7
  linked?: boolean;
19
8
  compiler?: string;
package/dist/index.js CHANGED
@@ -37,7 +37,6 @@ var import_path = __toESM(require("path"));
37
37
  var import_crypto = __toESM(require("crypto"));
38
38
  var import_anymatch = __toESM(require("anymatch"));
39
39
  var import_url = require("url");
40
- var import_relative_import_path = require("relative-import-path");
41
40
  var import_server_entry_template = __toESM(require("./server-entry-template"));
42
41
  var import_manifest_generator = require("./manifest-generator");
43
42
  var import_esbuild_plugin = __toESM(require("./esbuild-plugin"));
@@ -66,6 +65,7 @@ const babelCaller = {
66
65
  supportsTopLevelAwait: true,
67
66
  supportsExportNamespaceFrom: true
68
67
  };
68
+ let registeredTag = false;
69
69
  function markoPlugin(opts = {}) {
70
70
  let compiler;
71
71
  let { linked = true } = opts;
@@ -97,10 +97,8 @@ function markoPlugin(opts = {}) {
97
97
  let isBuild = false;
98
98
  let isSSRBuild = false;
99
99
  let devServer;
100
- let registeredTag = false;
101
100
  let serverManifest;
102
101
  let store;
103
- let CJSTemplates;
104
102
  let basePath = "/";
105
103
  const entryIds = /* @__PURE__ */ new Set();
106
104
  const cachedSources = /* @__PURE__ */ new Map();
@@ -173,7 +171,7 @@ function markoPlugin(opts = {}) {
173
171
  config.resolve.conditions.push("browser");
174
172
  }
175
173
  }
176
- if (linked && !registeredTag) {
174
+ if (!registeredTag) {
177
175
  const transformer = import_path.default.resolve(
178
176
  thisFile,
179
177
  "../render-assets-transform"
@@ -187,49 +185,33 @@ function markoPlugin(opts = {}) {
187
185
  "<body>": { transformer }
188
186
  });
189
187
  }
190
- const lookup = compiler.taglib.buildLookup(root);
191
- const taglibDeps = [];
192
- const optimizeTaglibDeps = [];
193
- for (const name in lookup.taglibsById) {
194
- const taglib = lookup.taglibsById[name];
195
- if (!/^marko-(.+-)?core$/.test(taglib.id) && /[\\/]node_modules[\\/]/.test(taglib.dirname)) {
196
- let isEsm;
197
- for (const tagName in taglib.tags) {
198
- const tag = taglib.tags[tagName];
199
- const entry = tag.template || tag.renderer;
200
- if (entry) {
201
- const relativePath = (0, import_relative_import_path.relativeImportPath)(devEntryFile, entry);
202
- taglibDeps.push(relativePath);
203
- if (isTest || isBuild || (isEsm ?? (isEsm = getModuleType(taglib.path) === "esm"))) {
204
- optimizeTaglibDeps.push(relativePath);
205
- } else {
206
- (CJSTemplates ?? (CJSTemplates = /* @__PURE__ */ new Set())).add(normalizePath(entry));
207
- }
208
- }
209
- }
210
- }
211
- }
212
188
  const optimizeDeps = config.optimizeDeps ?? (config.optimizeDeps = {});
213
- optimizeDeps.include = Array.from(
214
- /* @__PURE__ */ new Set([
215
- ...optimizeDeps.include || [],
216
- ...compiler.getRuntimeEntryFiles("dom", opts.translator),
217
- ...compiler.getRuntimeEntryFiles("html", opts.translator),
218
- ...optimizeTaglibDeps
219
- ])
220
- );
189
+ optimizeDeps.entries ?? (optimizeDeps.entries = [
190
+ "**/*.marko",
191
+ "!**/__snapshots__/**",
192
+ `!**/__tests__/**`,
193
+ `!**/coverage/**`
194
+ ]);
195
+ const domDeps = compiler.getRuntimeEntryFiles("dom", opts.translator);
196
+ optimizeDeps.include = optimizeDeps.include ? [...optimizeDeps.include, ...domDeps] : domDeps;
221
197
  const optimizeExtensions = optimizeDeps.extensions ?? (optimizeDeps.extensions = []);
222
198
  optimizeExtensions.push(".marko");
223
199
  const esbuildOptions = optimizeDeps.esbuildOptions ?? (optimizeDeps.esbuildOptions = {});
224
200
  const esbuildPlugins = esbuildOptions.plugins ?? (esbuildOptions.plugins = []);
225
201
  esbuildPlugins.push((0, import_esbuild_plugin.default)(compiler, baseConfig));
226
202
  const ssr = config.ssr ?? (config.ssr = {});
227
- if (ssr.noExternal !== true) {
228
- ssr.noExternal = Array.from(
229
- new Set(
230
- taglibDeps.concat(ssr.noExternal || [])
231
- )
232
- );
203
+ let { noExternal } = ssr;
204
+ if (noExternal !== true) {
205
+ const noExternalReg = /\.marko$/;
206
+ if (noExternal) {
207
+ if (Array.isArray(noExternal)) {
208
+ noExternal.push(noExternalReg);
209
+ } else {
210
+ noExternal = [noExternal, noExternalReg];
211
+ }
212
+ } else {
213
+ noExternal = noExternalReg;
214
+ }
233
215
  }
234
216
  if (basePathVar) {
235
217
  config.experimental ?? (config.experimental = {});
@@ -331,13 +313,15 @@ function markoPlugin(opts = {}) {
331
313
  let importeeQuery = getMarkoQuery(importee);
332
314
  if (importeeQuery) {
333
315
  importee = importee.slice(0, -importeeQuery.length);
334
- } else if (ssr && linked && importer && (importer !== devEntryFile || normalizePath(importer) !== devEntryFilePosix) && // Vite tries to resolve against an `index.html` in some cases, we ignore it here.
335
- isMarkoFile(importee) && !isMarkoFile(importer.replace(queryReg, ""))) {
336
- importeeQuery = serverEntryQuery;
337
- } else if (!ssr && isBuild && importer && isMarkoFile(importee) && ((_a = this.getModuleInfo(importer)) == null ? void 0 : _a.isEntry)) {
338
- importeeQuery = browserEntryQuery;
339
- } else if (linked && !ssr && !importeeQuery && isMarkoFile(importee)) {
340
- importeeQuery = browserQuery;
316
+ } else if (!importOpts.scan) {
317
+ if (ssr && linked && importer && (importer !== devEntryFile || normalizePath(importer) !== devEntryFilePosix) && // Vite tries to resolve against an `index.html` in some cases, we ignore it here.
318
+ isMarkoFile(importee) && !isMarkoFile(importer.replace(queryReg, ""))) {
319
+ importeeQuery = serverEntryQuery;
320
+ } else if (!ssr && isBuild && importer && isMarkoFile(importee) && ((_a = this.getModuleInfo(importer)) == null ? void 0 : _a.isEntry)) {
321
+ importeeQuery = browserEntryQuery;
322
+ } else if (linked && !ssr && !importeeQuery && isMarkoFile(importee)) {
323
+ importeeQuery = browserQuery;
324
+ }
341
325
  }
342
326
  if (importeeQuery) {
343
327
  const resolved = importee[0] === "." ? {
@@ -366,7 +350,8 @@ function markoPlugin(opts = {}) {
366
350
  }
367
351
  return null;
368
352
  },
369
- async load(id) {
353
+ async load(rawId) {
354
+ const id = stripVersionAndTimeStamp(rawId);
370
355
  const query = getMarkoQuery(id);
371
356
  switch (query) {
372
357
  case serverEntryQuery: {
@@ -380,7 +365,8 @@ function markoPlugin(opts = {}) {
380
365
  }
381
366
  return virtualFiles.get(id) || null;
382
367
  },
383
- async transform(source, id, ssr) {
368
+ async transform(source, rawId, ssr) {
369
+ let id = stripVersionAndTimeStamp(rawId);
384
370
  const isSSR = typeof ssr === "object" ? ssr.ssr : ssr;
385
371
  const query = getMarkoQuery(id);
386
372
  if (query && !query.startsWith(virtualFileQuery)) {
@@ -429,19 +415,71 @@ function markoPlugin(opts = {}) {
429
415
  if (linked) {
430
416
  cachedSources.set(id, source);
431
417
  }
432
- if (CJSTemplates == null ? void 0 : CJSTemplates.has(id)) {
433
- return createEsmWrapper(
434
- id,
435
- getExportIdentifiers(
436
- (await compiler.compile(source, id, {
437
- cache,
438
- ast: true,
439
- code: false,
440
- output: "source",
441
- sourceMaps: false
442
- })).ast
443
- )
444
- );
418
+ if (!query) {
419
+ if (/[\\/]node_modules[\\/]/.test(id) && getModuleType(id) === "cjs") {
420
+ const { ast } = await compiler.compile(source, id, {
421
+ cache,
422
+ ast: true,
423
+ code: false,
424
+ output: "source",
425
+ sourceMaps: false
426
+ });
427
+ let namedExports = "";
428
+ let code2 = `import { createRequire } from "module";
429
+ `;
430
+ code2 += `import "@marko/compiler/register.js";
431
+ `;
432
+ code2 += `const mod = createRequire(import.meta.url)(${JSON.stringify(
433
+ id
434
+ )});
435
+ `;
436
+ for (const child of ast.program.body) {
437
+ switch (child.type) {
438
+ case "ExportAllDeclaration":
439
+ code2 += `export * from ${JSON.stringify(
440
+ child.source.value
441
+ )};
442
+ `;
443
+ break;
444
+ case "ExportNamedDeclaration":
445
+ if (child.specifiers) {
446
+ for (const specifier of child.specifiers) {
447
+ if (specifier.exported.type === "Identifier") {
448
+ namedExports += `${specifier.exported.name},`;
449
+ } else {
450
+ namedExports += `mod[${JSON.stringify(
451
+ specifier.exported.value
452
+ )}] as ${specifier.exported.value},`;
453
+ }
454
+ }
455
+ }
456
+ if (child.declaration) {
457
+ if ("id" in child.declaration && child.declaration.id) {
458
+ if (child.declaration.id.type === "Identifier") {
459
+ namedExports += `${child.declaration.id.name},`;
460
+ } else {
461
+ namedExports += `mod[${JSON.stringify(
462
+ child.declaration.id.value
463
+ )}] as ${child.declaration.id.value},`;
464
+ }
465
+ }
466
+ if ("declarations" in child.declaration) {
467
+ for (const declaration of child.declaration.declarations) {
468
+ if (declaration.id.type === "Identifier") {
469
+ namedExports += `${declaration.id.name},`;
470
+ }
471
+ }
472
+ }
473
+ }
474
+ break;
475
+ }
476
+ }
477
+ code2 += `export const { ${namedExports} } = mod;
478
+ `;
479
+ code2 += `export default mod.default;
480
+ `;
481
+ return code2;
482
+ }
445
483
  }
446
484
  }
447
485
  const compiled = await compiler.compile(
@@ -637,58 +675,17 @@ function getModuleType(file) {
637
675
  }
638
676
  return "esm";
639
677
  }
640
- let requireHookInstalled = false;
641
- async function createEsmWrapper(url, exports) {
642
- if (!requireHookInstalled) {
643
- await import("@marko/compiler/register.js");
644
- requireHookInstalled = true;
645
- }
646
- let code = `import { createRequire } from 'module';
647
- const mod = createRequire(import.meta.url)('${url}');
648
- `;
649
- let namedExports;
650
- for (const name of exports) {
651
- if (name === "default") {
652
- code += "export default mod.default;\n";
653
- } else if (namedExports) {
654
- namedExports += `, ${name}`;
655
- } else {
656
- namedExports = name;
657
- }
658
- }
659
- if (namedExports) {
660
- code += `export const { ${namedExports} } = mod;
661
- `;
662
- }
663
- return code;
664
- }
665
- function getExportIdentifiers(ast) {
666
- const exports = /* @__PURE__ */ new Set();
667
- for (const node of ast.program.body) {
668
- switch (node.type) {
669
- case "ExportDefaultDeclaration":
670
- case "MarkoTag":
671
- exports.add("default");
672
- break;
673
- case "ExportNamedDeclaration":
674
- if (node.declaration) {
675
- for (const declarator of node.declaration.declarations) {
676
- exports.add(declarator.id.name);
677
- }
678
- }
679
- for (const specifier of node.specifiers) {
680
- if (specifier.type !== "ExportSpecifier") {
681
- exports.add(specifier.exported.name);
682
- }
683
- }
684
- break;
685
- case "ExportAllDeclaration":
686
- throw new Error(
687
- 'Re-exporting using `export * from "..."` is not supported.'
688
- );
689
- }
690
- }
691
- return exports;
678
+ function stripVersionAndTimeStamp(id) {
679
+ const queryStart = id.indexOf("?");
680
+ if (queryStart === -1)
681
+ return id;
682
+ const url = id.slice(0, queryStart);
683
+ const query = id.slice(queryStart + 1).replace(/(?:^|[&])[vt]=[^&]/g, "");
684
+ if (query)
685
+ return `${url}?${query}`;
686
+ return url;
692
687
  }
693
688
  // Annotate the CommonJS export names for ESM import in node:
694
- 0 && (module.exports = {});
689
+ 0 && (module.exports = {
690
+ ...require("./store")
691
+ });
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  esbuildPlugin
3
- } from "./chunk-KRSZ5IRT.mjs";
3
+ } from "./chunk-HR4PYNIR.mjs";
4
4
  import {
5
5
  generateDocManifest,
6
6
  generateInputDoc
@@ -23,7 +23,6 @@ import path from "path";
23
23
  import crypto from "crypto";
24
24
  import anyMatch from "anymatch";
25
25
  import { pathToFileURL, fileURLToPath } from "url";
26
- import { relativeImportPath } from "relative-import-path";
27
26
  var POSIX_SEP = "/";
28
27
  var WINDOWS_SEP = "\\";
29
28
  var normalizePath = path.sep === WINDOWS_SEP ? (id) => id.replace(/\\/g, POSIX_SEP) : (id) => id;
@@ -46,6 +45,7 @@ var babelCaller = {
46
45
  supportsTopLevelAwait: true,
47
46
  supportsExportNamespaceFrom: true
48
47
  };
48
+ var registeredTag = false;
49
49
  function markoPlugin(opts = {}) {
50
50
  let compiler;
51
51
  let { linked = true } = opts;
@@ -77,10 +77,8 @@ function markoPlugin(opts = {}) {
77
77
  let isBuild = false;
78
78
  let isSSRBuild = false;
79
79
  let devServer;
80
- let registeredTag = false;
81
80
  let serverManifest;
82
81
  let store;
83
- let CJSTemplates;
84
82
  let basePath = "/";
85
83
  const entryIds = /* @__PURE__ */ new Set();
86
84
  const cachedSources = /* @__PURE__ */ new Map();
@@ -153,7 +151,7 @@ function markoPlugin(opts = {}) {
153
151
  config.resolve.conditions.push("browser");
154
152
  }
155
153
  }
156
- if (linked && !registeredTag) {
154
+ if (!registeredTag) {
157
155
  const transformer = path.resolve(
158
156
  thisFile,
159
157
  "../render-assets-transform"
@@ -167,49 +165,33 @@ function markoPlugin(opts = {}) {
167
165
  "<body>": { transformer }
168
166
  });
169
167
  }
170
- const lookup = compiler.taglib.buildLookup(root);
171
- const taglibDeps = [];
172
- const optimizeTaglibDeps = [];
173
- for (const name in lookup.taglibsById) {
174
- const taglib = lookup.taglibsById[name];
175
- if (!/^marko-(.+-)?core$/.test(taglib.id) && /[\\/]node_modules[\\/]/.test(taglib.dirname)) {
176
- let isEsm;
177
- for (const tagName in taglib.tags) {
178
- const tag = taglib.tags[tagName];
179
- const entry = tag.template || tag.renderer;
180
- if (entry) {
181
- const relativePath = relativeImportPath(devEntryFile, entry);
182
- taglibDeps.push(relativePath);
183
- if (isTest || isBuild || (isEsm ?? (isEsm = getModuleType(taglib.path) === "esm"))) {
184
- optimizeTaglibDeps.push(relativePath);
185
- } else {
186
- (CJSTemplates ?? (CJSTemplates = /* @__PURE__ */ new Set())).add(normalizePath(entry));
187
- }
188
- }
189
- }
190
- }
191
- }
192
168
  const optimizeDeps = config.optimizeDeps ?? (config.optimizeDeps = {});
193
- optimizeDeps.include = Array.from(
194
- /* @__PURE__ */ new Set([
195
- ...optimizeDeps.include || [],
196
- ...compiler.getRuntimeEntryFiles("dom", opts.translator),
197
- ...compiler.getRuntimeEntryFiles("html", opts.translator),
198
- ...optimizeTaglibDeps
199
- ])
200
- );
169
+ optimizeDeps.entries ?? (optimizeDeps.entries = [
170
+ "**/*.marko",
171
+ "!**/__snapshots__/**",
172
+ `!**/__tests__/**`,
173
+ `!**/coverage/**`
174
+ ]);
175
+ const domDeps = compiler.getRuntimeEntryFiles("dom", opts.translator);
176
+ optimizeDeps.include = optimizeDeps.include ? [...optimizeDeps.include, ...domDeps] : domDeps;
201
177
  const optimizeExtensions = optimizeDeps.extensions ?? (optimizeDeps.extensions = []);
202
178
  optimizeExtensions.push(".marko");
203
179
  const esbuildOptions = optimizeDeps.esbuildOptions ?? (optimizeDeps.esbuildOptions = {});
204
180
  const esbuildPlugins = esbuildOptions.plugins ?? (esbuildOptions.plugins = []);
205
181
  esbuildPlugins.push(esbuildPlugin(compiler, baseConfig));
206
182
  const ssr = config.ssr ?? (config.ssr = {});
207
- if (ssr.noExternal !== true) {
208
- ssr.noExternal = Array.from(
209
- new Set(
210
- taglibDeps.concat(ssr.noExternal || [])
211
- )
212
- );
183
+ let { noExternal } = ssr;
184
+ if (noExternal !== true) {
185
+ const noExternalReg = /\.marko$/;
186
+ if (noExternal) {
187
+ if (Array.isArray(noExternal)) {
188
+ noExternal.push(noExternalReg);
189
+ } else {
190
+ noExternal = [noExternal, noExternalReg];
191
+ }
192
+ } else {
193
+ noExternal = noExternalReg;
194
+ }
213
195
  }
214
196
  if (basePathVar) {
215
197
  config.experimental ?? (config.experimental = {});
@@ -311,13 +293,15 @@ function markoPlugin(opts = {}) {
311
293
  let importeeQuery = getMarkoQuery(importee);
312
294
  if (importeeQuery) {
313
295
  importee = importee.slice(0, -importeeQuery.length);
314
- } else if (ssr && linked && importer && (importer !== devEntryFile || normalizePath(importer) !== devEntryFilePosix) && // Vite tries to resolve against an `index.html` in some cases, we ignore it here.
315
- isMarkoFile(importee) && !isMarkoFile(importer.replace(queryReg, ""))) {
316
- importeeQuery = serverEntryQuery;
317
- } else if (!ssr && isBuild && importer && isMarkoFile(importee) && ((_a = this.getModuleInfo(importer)) == null ? void 0 : _a.isEntry)) {
318
- importeeQuery = browserEntryQuery;
319
- } else if (linked && !ssr && !importeeQuery && isMarkoFile(importee)) {
320
- importeeQuery = browserQuery;
296
+ } else if (!importOpts.scan) {
297
+ if (ssr && linked && importer && (importer !== devEntryFile || normalizePath(importer) !== devEntryFilePosix) && // Vite tries to resolve against an `index.html` in some cases, we ignore it here.
298
+ isMarkoFile(importee) && !isMarkoFile(importer.replace(queryReg, ""))) {
299
+ importeeQuery = serverEntryQuery;
300
+ } else if (!ssr && isBuild && importer && isMarkoFile(importee) && ((_a = this.getModuleInfo(importer)) == null ? void 0 : _a.isEntry)) {
301
+ importeeQuery = browserEntryQuery;
302
+ } else if (linked && !ssr && !importeeQuery && isMarkoFile(importee)) {
303
+ importeeQuery = browserQuery;
304
+ }
321
305
  }
322
306
  if (importeeQuery) {
323
307
  const resolved = importee[0] === "." ? {
@@ -346,7 +330,8 @@ function markoPlugin(opts = {}) {
346
330
  }
347
331
  return null;
348
332
  },
349
- async load(id) {
333
+ async load(rawId) {
334
+ const id = stripVersionAndTimeStamp(rawId);
350
335
  const query = getMarkoQuery(id);
351
336
  switch (query) {
352
337
  case serverEntryQuery: {
@@ -360,7 +345,8 @@ function markoPlugin(opts = {}) {
360
345
  }
361
346
  return virtualFiles.get(id) || null;
362
347
  },
363
- async transform(source, id, ssr) {
348
+ async transform(source, rawId, ssr) {
349
+ let id = stripVersionAndTimeStamp(rawId);
364
350
  const isSSR = typeof ssr === "object" ? ssr.ssr : ssr;
365
351
  const query = getMarkoQuery(id);
366
352
  if (query && !query.startsWith(virtualFileQuery)) {
@@ -409,19 +395,71 @@ function markoPlugin(opts = {}) {
409
395
  if (linked) {
410
396
  cachedSources.set(id, source);
411
397
  }
412
- if (CJSTemplates == null ? void 0 : CJSTemplates.has(id)) {
413
- return createEsmWrapper(
414
- id,
415
- getExportIdentifiers(
416
- (await compiler.compile(source, id, {
417
- cache,
418
- ast: true,
419
- code: false,
420
- output: "source",
421
- sourceMaps: false
422
- })).ast
423
- )
424
- );
398
+ if (!query) {
399
+ if (/[\\/]node_modules[\\/]/.test(id) && getModuleType(id) === "cjs") {
400
+ const { ast } = await compiler.compile(source, id, {
401
+ cache,
402
+ ast: true,
403
+ code: false,
404
+ output: "source",
405
+ sourceMaps: false
406
+ });
407
+ let namedExports = "";
408
+ let code2 = `import { createRequire } from "module";
409
+ `;
410
+ code2 += `import "@marko/compiler/register.js";
411
+ `;
412
+ code2 += `const mod = createRequire(import.meta.url)(${JSON.stringify(
413
+ id
414
+ )});
415
+ `;
416
+ for (const child of ast.program.body) {
417
+ switch (child.type) {
418
+ case "ExportAllDeclaration":
419
+ code2 += `export * from ${JSON.stringify(
420
+ child.source.value
421
+ )};
422
+ `;
423
+ break;
424
+ case "ExportNamedDeclaration":
425
+ if (child.specifiers) {
426
+ for (const specifier of child.specifiers) {
427
+ if (specifier.exported.type === "Identifier") {
428
+ namedExports += `${specifier.exported.name},`;
429
+ } else {
430
+ namedExports += `mod[${JSON.stringify(
431
+ specifier.exported.value
432
+ )}] as ${specifier.exported.value},`;
433
+ }
434
+ }
435
+ }
436
+ if (child.declaration) {
437
+ if ("id" in child.declaration && child.declaration.id) {
438
+ if (child.declaration.id.type === "Identifier") {
439
+ namedExports += `${child.declaration.id.name},`;
440
+ } else {
441
+ namedExports += `mod[${JSON.stringify(
442
+ child.declaration.id.value
443
+ )}] as ${child.declaration.id.value},`;
444
+ }
445
+ }
446
+ if ("declarations" in child.declaration) {
447
+ for (const declaration of child.declaration.declarations) {
448
+ if (declaration.id.type === "Identifier") {
449
+ namedExports += `${declaration.id.name},`;
450
+ }
451
+ }
452
+ }
453
+ }
454
+ break;
455
+ }
456
+ }
457
+ code2 += `export const { ${namedExports} } = mod;
458
+ `;
459
+ code2 += `export default mod.default;
460
+ `;
461
+ return code2;
462
+ }
425
463
  }
426
464
  }
427
465
  const compiled = await compiler.compile(
@@ -617,58 +655,15 @@ function getModuleType(file) {
617
655
  }
618
656
  return "esm";
619
657
  }
620
- var requireHookInstalled = false;
621
- async function createEsmWrapper(url, exports) {
622
- if (!requireHookInstalled) {
623
- await import("@marko/compiler/register.js");
624
- requireHookInstalled = true;
625
- }
626
- let code = `import { createRequire } from 'module';
627
- const mod = createRequire(import.meta.url)('${url}');
628
- `;
629
- let namedExports;
630
- for (const name of exports) {
631
- if (name === "default") {
632
- code += "export default mod.default;\n";
633
- } else if (namedExports) {
634
- namedExports += `, ${name}`;
635
- } else {
636
- namedExports = name;
637
- }
638
- }
639
- if (namedExports) {
640
- code += `export const { ${namedExports} } = mod;
641
- `;
642
- }
643
- return code;
644
- }
645
- function getExportIdentifiers(ast) {
646
- const exports = /* @__PURE__ */ new Set();
647
- for (const node of ast.program.body) {
648
- switch (node.type) {
649
- case "ExportDefaultDeclaration":
650
- case "MarkoTag":
651
- exports.add("default");
652
- break;
653
- case "ExportNamedDeclaration":
654
- if (node.declaration) {
655
- for (const declarator of node.declaration.declarations) {
656
- exports.add(declarator.id.name);
657
- }
658
- }
659
- for (const specifier of node.specifiers) {
660
- if (specifier.type !== "ExportSpecifier") {
661
- exports.add(specifier.exported.name);
662
- }
663
- }
664
- break;
665
- case "ExportAllDeclaration":
666
- throw new Error(
667
- 'Re-exporting using `export * from "..."` is not supported.'
668
- );
669
- }
670
- }
671
- return exports;
658
+ function stripVersionAndTimeStamp(id) {
659
+ const queryStart = id.indexOf("?");
660
+ if (queryStart === -1)
661
+ return id;
662
+ const url = id.slice(0, queryStart);
663
+ const query = id.slice(queryStart + 1).replace(/(?:^|[&])[vt]=[^&]/g, "");
664
+ if (query)
665
+ return `${url}?${query}`;
666
+ return url;
672
667
  }
673
668
  export {
674
669
  FileStore,
@@ -39,5 +39,3 @@ function renderAssetsCall(t, slot) {
39
39
  false
40
40
  );
41
41
  }
42
- // Annotate the CommonJS export names for ESM import in node:
43
- 0 && (module.exports = {});
@@ -125,5 +125,3 @@ function stripBasePath(basePath, path) {
125
125
  return path.slice(basePath.length);
126
126
  return path;
127
127
  }
128
- // Annotate the CommonJS export names for ESM import in node:
129
- 0 && (module.exports = {});
@@ -55,5 +55,3 @@ $ out.global.___viteBaseVar = ${JSON.stringify(
55
55
  <_vite${base} slot="body"/>
56
56
  `;
57
57
  };
58
- // Annotate the CommonJS export names for ESM import in node:
59
- 0 && (module.exports = {});
@@ -89,5 +89,3 @@ async function getTempDir(id) {
89
89
  }
90
90
  throw new Error("Unable to create temp directory");
91
91
  }
92
- // Annotate the CommonJS export names for ESM import in node:
93
- 0 && (module.exports = {});
@@ -36,5 +36,3 @@ class MemoryStore {
36
36
  this._store.set(key, value);
37
37
  }
38
38
  }
39
- // Annotate the CommonJS export names for ESM import in node:
40
- 0 && (module.exports = {});
package/package.json CHANGED
@@ -1,47 +1,46 @@
1
1
  {
2
2
  "name": "@marko/vite",
3
3
  "description": "A Marko plugin for Vite",
4
- "version": "3.0.1",
4
+ "version": "3.1.0",
5
5
  "author": "Dylan Piercey <dpiercey@ebay.com>",
6
6
  "bugs": "https://github.com/marko-js/vite/issues",
7
7
  "dependencies": {
8
8
  "anymatch": "^3.1.3",
9
9
  "domelementtype": "^2.3.0",
10
10
  "domhandler": "^5.0.3",
11
- "htmlparser2": "^8.0.1",
12
- "relative-import-path": "^1.0.0"
11
+ "htmlparser2": "^9.0.0"
13
12
  },
14
13
  "devDependencies": {
15
14
  "@changesets/changelog-github": "^0.4.8",
16
- "@changesets/cli": "^2.26.0",
17
- "@marko/compiler": "^5.23.0",
18
- "@marko/fixture-snapshots": "^2.1.7",
19
- "@marko/testing-library": "^6.1.2",
20
- "@types/jsdom": "^20.0.1",
21
- "@types/mocha": "^10.0.1",
22
- "@types/node": "^18.11.18",
23
- "@types/serve-handler": "^6.1.1",
24
- "@typescript-eslint/eslint-plugin": "^5.48.0",
25
- "@typescript-eslint/parser": "^5.48.0",
15
+ "@changesets/cli": "^2.26.2",
16
+ "@marko/compiler": "^5.33.2",
17
+ "@marko/fixture-snapshots": "^2.2.1",
18
+ "@marko/testing-library": "^6.1.4",
19
+ "@types/jsdom": "^21.1.3",
20
+ "@types/mocha": "^10.0.2",
21
+ "@types/node": "^20.8.2",
22
+ "@types/serve-handler": "^6.1.2",
23
+ "@typescript-eslint/eslint-plugin": "^6.7.4",
24
+ "@typescript-eslint/parser": "^6.7.4",
26
25
  "cross-env": "^7.0.3",
27
- "esbuild": "^0.16.14",
28
- "eslint": "^8.31.0",
29
- "eslint-config-prettier": "^8.6.0",
30
- "fast-glob": "^3.2.12",
26
+ "esbuild": "^0.19.4",
27
+ "eslint": "^8.50.0",
28
+ "eslint-config-prettier": "^9.0.0",
29
+ "fast-glob": "^3.3.1",
31
30
  "fixpack": "^4.0.0",
32
31
  "husky": "^8.0.3",
33
- "jsdom": "^20.0.3",
34
- "lint-staged": "^13.1.0",
35
- "marko": "^5.22.0",
32
+ "jsdom": "^22.1.0",
33
+ "lint-staged": "^14.0.1",
34
+ "marko": "^5.31.11",
36
35
  "mocha": "^10.2.0",
37
36
  "mocha-snap": "^5.0.0",
38
37
  "nyc": "^15.1.0",
39
- "playwright": "^1.29.1",
40
- "prettier": "^2.8.1",
38
+ "playwright": "^1.38.1",
39
+ "prettier": "^2.8.8",
41
40
  "serve-handler": "^6.1.5",
42
- "tsx": "^3.12.1",
43
- "typescript": "^4.9.4",
44
- "vite": "^4.0.4"
41
+ "tsx": "^3.13.0",
42
+ "typescript": "^5.2.2",
43
+ "vite": "^4.4.11"
45
44
  },
46
45
  "exports": {
47
46
  ".": {
@@ -69,7 +68,7 @@
69
68
  "module": "./dist/index.mjs",
70
69
  "peerDependencies": {
71
70
  "@marko/compiler": "^5",
72
- "vite": "^2 || ^3 || ^4"
71
+ "vite": "^2 || ^3 || ^4 || ^5"
73
72
  },
74
73
  "repository": {
75
74
  "type": "git",