@marko/vite 4.1.3 → 4.1.5

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.
package/README.md CHANGED
@@ -49,6 +49,12 @@ export default defineConfig({
49
49
  });
50
50
  ```
51
51
 
52
+ # Base paths
53
+
54
+ When deploying an application under a nested public path, use Vite's [`base` option](https://vitejs.dev/config/shared-options.html#base) to specify a path to prefix all assets. This option can also be specified with the `BASE_URL` environment variable.
55
+
56
+ If the base path is not static, see the [basePathVar](#optionsbasepathvar) option for handling more advanced cases.
57
+
52
58
  # Browser asset references
53
59
 
54
60
  With @marko/vite when a _static relative path_ is used for certain native tag attributes, the relative asset will be imported and processed by Vite.
@@ -62,7 +68,7 @@ As an example, with the following template, the `logo.svg` will be imported and
62
68
  <img src="/assets/logo-TwEWmgMb.svg">
63
69
  ```
64
70
 
65
- You can see the list of elements and their attributes which are processed [here](./src/relative-assets-transform.ts).
71
+ Most common image, media, and font filetypes are transformed automatically but some file types such as `.js` and `.css` files will not be. Generally these should be imported directly and not rendered as markup. To force the transformation of a path, add `?url` to it. You can see the list of elements and their attributes which are processed [here](./src/relative-assets-transform.ts).
66
72
 
67
73
  # Linked Mode
68
74
 
@@ -28,4 +28,5 @@ import type { PluginObj } from "@babel/core";
28
28
  export default function plugin(options: {
29
29
  extensions: string[];
30
30
  conditions: string[];
31
+ filter?: (path: string) => boolean;
31
32
  }): PluginObj;
package/dist/index.mjs CHANGED
@@ -4,6 +4,7 @@ import path5 from "path";
4
4
  import crypto from "crypto";
5
5
  import anyMatch from "anymatch";
6
6
  import { pathToFileURL } from "url";
7
+ import { transform as cjsToEsm } from "@chialab/cjs-to-esm";
7
8
 
8
9
  // src/server-entry-template.ts
9
10
  import path from "path";
@@ -447,7 +448,7 @@ function plugin(options) {
447
448
  name: "marko-import-interop",
448
449
  visitor: {
449
450
  ImportDeclaration(path6) {
450
- if (!path6.node.specifiers.length || /\.(?:mjs|marko)$|\?/.test(path6.node.source.value)) {
451
+ if (!path6.node.specifiers.length || /\.(?:mjs|marko)$|\?/.test(path6.node.source.value) || options.filter?.(path6.node.source.value) === false) {
451
452
  return;
452
453
  }
453
454
  try {
@@ -527,10 +528,12 @@ function plugin(options) {
527
528
 
528
529
  // src/render-assets-transform.ts
529
530
  var render_assets_transform_default = (tag, t2) => {
530
- const body = tag.get("body");
531
- const tagName = tag.get("name").node.value;
532
- body.unshiftContainer("body", renderAssetsCall(t2, `${tagName}-prepend`));
533
- body.pushContainer("body", renderAssetsCall(t2, tagName));
531
+ if (tag.hub.file.markoOpts.markoViteLinked) {
532
+ const body = tag.get("body");
533
+ const tagName = tag.get("name").node.value;
534
+ body.unshiftContainer("body", renderAssetsCall(t2, `${tagName}-prepend`));
535
+ body.pushContainer("body", renderAssetsCall(t2, tagName));
536
+ }
534
537
  };
535
538
  function renderAssetsCall(t2, slot) {
536
539
  return t2.markoPlaceholder(
@@ -561,8 +564,10 @@ var assetAttrsByTag = /* @__PURE__ */ new Map([
561
564
  ["area", attrHref],
562
565
  ["link", attrHref],
563
566
  ["object", /* @__PURE__ */ new Set(["data"])],
564
- ["body", /* @__PURE__ */ new Set(["background"])]
567
+ ["body", /* @__PURE__ */ new Set(["background"])],
568
+ ["script", /* @__PURE__ */ new Set(["src"])]
565
569
  ]);
570
+ var assetFileReg = /(?:^\..*\.(?:a?png|jpe?g|jfif|pipeg|pjp|gif|svg|ico|web[pm]|avif|mp4|ogg|mp3|wav|flac|aac|opus|woff2?|eot|[ot]tf|webmanifest|pdf|txt)(\?|$)|\?url\b)/;
566
571
  function transform(tag, t2) {
567
572
  const { name, attributes } = tag.node;
568
573
  if (name.type !== "StringLiteral") {
@@ -575,9 +580,7 @@ function transform(tag, t2) {
575
580
  for (const attr of attributes) {
576
581
  if (attr.type === "MarkoAttribute" && attr.value.type === "StringLiteral" && assetAttrs.has(attr.name)) {
577
582
  const { value } = attr.value;
578
- if (!(value[0] === "/" || // Ignore absolute paths.
579
- !/\.[^.]+$/.test(value) || // Ignore paths without a file extension.
580
- /^[a-z]{2,}:/i.test(value))) {
583
+ if (assetFileReg.test(value)) {
581
584
  const importedId = tag.scope.generateUid(value);
582
585
  attr.value = t2.identifier(importedId);
583
586
  tag.hub.file.path.unshiftContainer(
@@ -669,8 +672,7 @@ function markoPlugin(opts = {}) {
669
672
  let basePathVar;
670
673
  let baseConfig;
671
674
  let ssrConfig;
672
- let ssrCjsBuildConfig;
673
- let ssrCjsServeConfig;
675
+ let ssrCjsConfig;
674
676
  let domConfig;
675
677
  let hydrateConfig;
676
678
  const resolveVirtualDependency = (from, dep) => {
@@ -719,6 +721,9 @@ function markoPlugin(opts = {}) {
719
721
  compiler ??= await import(opts.compiler || "@marko/compiler");
720
722
  runtimeId = opts.runtimeId;
721
723
  basePathVar = opts.basePathVar;
724
+ if ("BASE_URL" in process.env && config.base == null) {
725
+ config.base = process.env.BASE_URL;
726
+ }
722
727
  baseConfig = {
723
728
  cache,
724
729
  optimize,
@@ -747,12 +752,9 @@ function markoPlugin(opts = {}) {
747
752
  resolveVirtualDependency,
748
753
  output: "html"
749
754
  };
750
- ssrCjsServeConfig = {
751
- ...ssrConfig,
752
- ast: true,
753
- code: false,
754
- sourceMaps: false
755
- };
755
+ if (linked) {
756
+ ssrConfig.markoViteLinked = linked;
757
+ }
756
758
  domConfig = {
757
759
  ...baseConfig,
758
760
  resolveVirtualDependency,
@@ -857,18 +859,28 @@ function markoPlugin(opts = {}) {
857
859
  }
858
860
  };
859
861
  }
862
+ return {
863
+ resolve: {
864
+ alias: [
865
+ {
866
+ find: /^~/,
867
+ replacement: ""
868
+ }
869
+ ]
870
+ }
871
+ };
860
872
  },
861
873
  configResolved(config) {
862
874
  basePath = config.base;
863
- ssrCjsBuildConfig = {
875
+ ssrCjsConfig = {
864
876
  ...ssrConfig,
865
- //modules: 'cjs'
866
877
  babelConfig: {
867
878
  ...ssrConfig.babelConfig,
868
879
  plugins: (ssrConfig.babelConfig.plugins || []).concat(
869
880
  plugin({
870
881
  extensions: config.resolve.extensions,
871
- conditions: config.resolve.conditions
882
+ conditions: config.resolve.conditions,
883
+ filter: isBuild ? void 0 : (path6) => !/^\./.test(path6)
872
884
  })
873
885
  )
874
886
  }
@@ -1065,6 +1077,12 @@ function markoPlugin(opts = {}) {
1065
1077
  }
1066
1078
  }
1067
1079
  if (!isMarkoFile(id)) {
1080
+ if (!isBuild) {
1081
+ const ext = path5.extname(id);
1082
+ if (ext === ".cjs" || ext === ".js" && isCJSModule(id)) {
1083
+ return cjsToEsm(source);
1084
+ }
1085
+ }
1068
1086
  return null;
1069
1087
  }
1070
1088
  if (isSSR) {
@@ -1076,74 +1094,13 @@ function markoPlugin(opts = {}) {
1076
1094
  const { code: code2, map: map2, meta: meta2 } = await compiler.compile(
1077
1095
  source,
1078
1096
  id,
1079
- getConfigForFileSystem(info, ssrCjsBuildConfig)
1097
+ getConfigForFileSystem(info, ssrCjsConfig)
1080
1098
  );
1081
1099
  return {
1082
1100
  code: code2,
1083
1101
  map: map2,
1084
1102
  meta: { arcSourceCode: source, arcScanIds: meta2.analyzedTags }
1085
1103
  };
1086
- } else {
1087
- const { ast } = await compiler.compile(
1088
- source,
1089
- id,
1090
- ssrCjsServeConfig
1091
- );
1092
- let namedExports = "";
1093
- let code2 = `import { createRequire } from "module";
1094
- `;
1095
- code2 += `import "@marko/compiler/register.js";
1096
- `;
1097
- code2 += `const mod = createRequire(import.meta.url)(${JSON.stringify(
1098
- id
1099
- )});
1100
- `;
1101
- for (const child of ast.program.body) {
1102
- switch (child.type) {
1103
- case "ExportAllDeclaration":
1104
- code2 += `export * from ${JSON.stringify(
1105
- child.source.value
1106
- )};
1107
- `;
1108
- break;
1109
- case "ExportNamedDeclaration":
1110
- if (child.specifiers) {
1111
- for (const specifier of child.specifiers) {
1112
- if (specifier.exported.type === "Identifier") {
1113
- namedExports += `${specifier.exported.name},`;
1114
- } else {
1115
- namedExports += `mod[${JSON.stringify(
1116
- specifier.exported.value
1117
- )}] as ${specifier.exported.value},`;
1118
- }
1119
- }
1120
- }
1121
- if (child.declaration) {
1122
- if ("id" in child.declaration && child.declaration.id) {
1123
- if (child.declaration.id.type === "Identifier") {
1124
- namedExports += `${child.declaration.id.name},`;
1125
- } else {
1126
- namedExports += `mod[${JSON.stringify(
1127
- child.declaration.id.value
1128
- )}] as ${child.declaration.id.value},`;
1129
- }
1130
- }
1131
- if ("declarations" in child.declaration) {
1132
- for (const declaration of child.declaration.declarations) {
1133
- if (declaration.id.type === "Identifier") {
1134
- namedExports += `${declaration.id.name},`;
1135
- }
1136
- }
1137
- }
1138
- }
1139
- break;
1140
- }
1141
- }
1142
- code2 += `export const { ${namedExports} } = mod;
1143
- `;
1144
- code2 += `export default mod.default;
1145
- `;
1146
- return code2;
1147
1104
  }
1148
1105
  }
1149
1106
  }
@@ -1152,7 +1109,7 @@ function markoPlugin(opts = {}) {
1152
1109
  id,
1153
1110
  getConfigForFileSystem(
1154
1111
  info,
1155
- isSSR ? ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
1112
+ isSSR ? isCJSModule(id) ? ssrCjsConfig : ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
1156
1113
  )
1157
1114
  );
1158
1115
  const { map, meta } = compiled;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@marko/vite",
3
3
  "description": "A Marko plugin for Vite",
4
- "version": "4.1.3",
4
+ "version": "4.1.5",
5
5
  "author": "Dylan Piercey <dpiercey@ebay.com>",
6
6
  "bugs": "https://github.com/marko-js/vite/issues",
7
7
  "dependencies": {
@@ -15,6 +15,7 @@
15
15
  "devDependencies": {
16
16
  "@changesets/changelog-github": "^0.5.0",
17
17
  "@changesets/cli": "^2.27.1",
18
+ "@chialab/cjs-to-esm": "^0.18.0",
18
19
  "@marko/compiler": "^5.34.7",
19
20
  "@marko/fixture-snapshots": "^2.2.1",
20
21
  "@marko/testing-library": "^6.2.0",
@@ -33,6 +34,7 @@
33
34
  "fixpack": "^4.0.0",
34
35
  "husky": "^9.0.11",
35
36
  "jsdom": "^24.0.0",
37
+ "less": "^4.2.0",
36
38
  "lint-staged": "^15.2.2",
37
39
  "marko": "^5.32.13",
38
40
  "mocha": "^10.3.0",