@marko/vite 4.1.4 → 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 {
@@ -563,8 +564,10 @@ var assetAttrsByTag = /* @__PURE__ */ new Map([
563
564
  ["area", attrHref],
564
565
  ["link", attrHref],
565
566
  ["object", /* @__PURE__ */ new Set(["data"])],
566
- ["body", /* @__PURE__ */ new Set(["background"])]
567
+ ["body", /* @__PURE__ */ new Set(["background"])],
568
+ ["script", /* @__PURE__ */ new Set(["src"])]
567
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)/;
568
571
  function transform(tag, t2) {
569
572
  const { name, attributes } = tag.node;
570
573
  if (name.type !== "StringLiteral") {
@@ -577,9 +580,7 @@ function transform(tag, t2) {
577
580
  for (const attr of attributes) {
578
581
  if (attr.type === "MarkoAttribute" && attr.value.type === "StringLiteral" && assetAttrs.has(attr.name)) {
579
582
  const { value } = attr.value;
580
- if (!(value[0] === "/" || // Ignore absolute paths.
581
- !/\.[^.]+$/.test(value) || // Ignore paths without a file extension.
582
- /^[a-z]{2,}:/i.test(value))) {
583
+ if (assetFileReg.test(value)) {
583
584
  const importedId = tag.scope.generateUid(value);
584
585
  attr.value = t2.identifier(importedId);
585
586
  tag.hub.file.path.unshiftContainer(
@@ -671,8 +672,7 @@ function markoPlugin(opts = {}) {
671
672
  let basePathVar;
672
673
  let baseConfig;
673
674
  let ssrConfig;
674
- let ssrCjsBuildConfig;
675
- let ssrCjsServeConfig;
675
+ let ssrCjsConfig;
676
676
  let domConfig;
677
677
  let hydrateConfig;
678
678
  const resolveVirtualDependency = (from, dep) => {
@@ -721,6 +721,9 @@ function markoPlugin(opts = {}) {
721
721
  compiler ??= await import(opts.compiler || "@marko/compiler");
722
722
  runtimeId = opts.runtimeId;
723
723
  basePathVar = opts.basePathVar;
724
+ if ("BASE_URL" in process.env && config.base == null) {
725
+ config.base = process.env.BASE_URL;
726
+ }
724
727
  baseConfig = {
725
728
  cache,
726
729
  optimize,
@@ -752,12 +755,6 @@ function markoPlugin(opts = {}) {
752
755
  if (linked) {
753
756
  ssrConfig.markoViteLinked = linked;
754
757
  }
755
- ssrCjsServeConfig = {
756
- ...ssrConfig,
757
- ast: true,
758
- code: false,
759
- sourceMaps: false
760
- };
761
758
  domConfig = {
762
759
  ...baseConfig,
763
760
  resolveVirtualDependency,
@@ -862,18 +859,28 @@ function markoPlugin(opts = {}) {
862
859
  }
863
860
  };
864
861
  }
862
+ return {
863
+ resolve: {
864
+ alias: [
865
+ {
866
+ find: /^~/,
867
+ replacement: ""
868
+ }
869
+ ]
870
+ }
871
+ };
865
872
  },
866
873
  configResolved(config) {
867
874
  basePath = config.base;
868
- ssrCjsBuildConfig = {
875
+ ssrCjsConfig = {
869
876
  ...ssrConfig,
870
- //modules: 'cjs'
871
877
  babelConfig: {
872
878
  ...ssrConfig.babelConfig,
873
879
  plugins: (ssrConfig.babelConfig.plugins || []).concat(
874
880
  plugin({
875
881
  extensions: config.resolve.extensions,
876
- conditions: config.resolve.conditions
882
+ conditions: config.resolve.conditions,
883
+ filter: isBuild ? void 0 : (path6) => !/^\./.test(path6)
877
884
  })
878
885
  )
879
886
  }
@@ -1070,6 +1077,12 @@ function markoPlugin(opts = {}) {
1070
1077
  }
1071
1078
  }
1072
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
+ }
1073
1086
  return null;
1074
1087
  }
1075
1088
  if (isSSR) {
@@ -1081,74 +1094,13 @@ function markoPlugin(opts = {}) {
1081
1094
  const { code: code2, map: map2, meta: meta2 } = await compiler.compile(
1082
1095
  source,
1083
1096
  id,
1084
- getConfigForFileSystem(info, ssrCjsBuildConfig)
1097
+ getConfigForFileSystem(info, ssrCjsConfig)
1085
1098
  );
1086
1099
  return {
1087
1100
  code: code2,
1088
1101
  map: map2,
1089
1102
  meta: { arcSourceCode: source, arcScanIds: meta2.analyzedTags }
1090
1103
  };
1091
- } else {
1092
- const { ast } = await compiler.compile(
1093
- source,
1094
- id,
1095
- ssrCjsServeConfig
1096
- );
1097
- let namedExports = "";
1098
- let code2 = `import { createRequire } from "module";
1099
- `;
1100
- code2 += `import "@marko/compiler/register.js";
1101
- `;
1102
- code2 += `const mod = createRequire(import.meta.url)(${JSON.stringify(
1103
- id
1104
- )});
1105
- `;
1106
- for (const child of ast.program.body) {
1107
- switch (child.type) {
1108
- case "ExportAllDeclaration":
1109
- code2 += `export * from ${JSON.stringify(
1110
- child.source.value
1111
- )};
1112
- `;
1113
- break;
1114
- case "ExportNamedDeclaration":
1115
- if (child.specifiers) {
1116
- for (const specifier of child.specifiers) {
1117
- if (specifier.exported.type === "Identifier") {
1118
- namedExports += `${specifier.exported.name},`;
1119
- } else {
1120
- namedExports += `mod[${JSON.stringify(
1121
- specifier.exported.value
1122
- )}] as ${specifier.exported.value},`;
1123
- }
1124
- }
1125
- }
1126
- if (child.declaration) {
1127
- if ("id" in child.declaration && child.declaration.id) {
1128
- if (child.declaration.id.type === "Identifier") {
1129
- namedExports += `${child.declaration.id.name},`;
1130
- } else {
1131
- namedExports += `mod[${JSON.stringify(
1132
- child.declaration.id.value
1133
- )}] as ${child.declaration.id.value},`;
1134
- }
1135
- }
1136
- if ("declarations" in child.declaration) {
1137
- for (const declaration of child.declaration.declarations) {
1138
- if (declaration.id.type === "Identifier") {
1139
- namedExports += `${declaration.id.name},`;
1140
- }
1141
- }
1142
- }
1143
- }
1144
- break;
1145
- }
1146
- }
1147
- code2 += `export const { ${namedExports} } = mod;
1148
- `;
1149
- code2 += `export default mod.default;
1150
- `;
1151
- return code2;
1152
1104
  }
1153
1105
  }
1154
1106
  }
@@ -1157,7 +1109,7 @@ function markoPlugin(opts = {}) {
1157
1109
  id,
1158
1110
  getConfigForFileSystem(
1159
1111
  info,
1160
- isSSR ? ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
1112
+ isSSR ? isCJSModule(id) ? ssrCjsConfig : ssrConfig : query === browserEntryQuery ? hydrateConfig : domConfig
1161
1113
  )
1162
1114
  );
1163
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.4",
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",