@marko/compiler 5.40.2 → 5.41.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.
package/babel-utils.d.ts CHANGED
@@ -54,6 +54,10 @@ export interface TagDefinition {
54
54
  isRepeated?: boolean;
55
55
  targetProperty?: string;
56
56
  taglibId: string;
57
+ /** Set when the taglib was discovered by resolving an installed package. */
58
+ packageName?: string;
59
+ /** The root directory of `packageName`, which contains all files for this tag. */
60
+ packageRoot?: string;
57
61
  types?: string;
58
62
  template?: string;
59
63
  renderer?: string;
@@ -303,7 +307,11 @@ export function parseTemplateLiteral(
303
307
  sourceEnd?: null | number,
304
308
  ): t.TemplateLiteral;
305
309
 
306
- export function resolveRelativePath(file: t.BabelFile, request: string): string;
310
+ export function resolveRelativePath(
311
+ file: t.BabelFile,
312
+ request: string,
313
+ tagDef?: TagDefinition,
314
+ ): string;
307
315
  export function importDefault(
308
316
  file: t.BabelFile,
309
317
  request: string,
@@ -5,10 +5,13 @@ var _relativeImportPath = require("relative-import-path");function _interopRequi
5
5
 
6
6
  const IMPORTS_KEY = Symbol();
7
7
  const FS_START = _path.default.sep === "/" ? _path.default.sep : /^(.*?:)/.exec(_modules.cwd)[1];
8
+ const REG_NODE_MODULES = /[\\/]node_modules[\\/]/;
8
9
 
9
- function resolveRelativePath(file, request) {
10
+ function resolveRelativePath(file, request, tagDef) {
10
11
  if (request.startsWith(FS_START)) {
11
- request = (0, _relativeImportPath.relativeImportPath)(file.opts.filename, request);
12
+ request =
13
+ packageImportPath(file.opts.filename, tagDef, request) ||
14
+ (0, _relativeImportPath.relativeImportPath)(file.opts.filename, request);
12
15
  }
13
16
 
14
17
  if (file.markoOpts.optimize) {
@@ -21,6 +24,34 @@ function resolveRelativePath(file, request) {
21
24
  return request;
22
25
  }
23
26
 
27
+ /**
28
+ * A tag installed into `node_modules` is imported through the name its package was
29
+ * resolved by, since its path is a realpath which may not be importable at all.
30
+ */
31
+ function packageImportPath(from, tagDef, request) {
32
+ if (!tagDef?.packageName || !REG_NODE_MODULES.test(request)) return;
33
+
34
+ const { packageRoot } = tagDef;
35
+ // Within the package we stay relative, both because it always resolves and
36
+ // because a self reference would only work if the package exports the tag.
37
+ if (!isWithin(packageRoot, request) || isWithin(packageRoot, from)) return;
38
+
39
+ const subPath = _path.default.relative(packageRoot, request);
40
+ return `${tagDef.packageName}/${subPath.split(_path.default.sep).join("/")}`;
41
+ }
42
+
43
+ function isWithin(dir, filename) {
44
+ const rel = _path.default.relative(dir, filename);
45
+ // `path.relative` walks out of the dir with `..`, or returns an absolute path
46
+ // when it cannot relate the two at all, eg across windows drives.
47
+ return (
48
+ !!rel &&
49
+ rel !== ".." &&
50
+ !rel.startsWith(`..${_path.default.sep}`) &&
51
+ !_path.default.isAbsolute(rel));
52
+
53
+ }
54
+
24
55
  function importDefault(file, request, nameHint) {
25
56
  const imports = getImports(file);
26
57
  request = resolveRelativePath(file, request);
@@ -347,7 +347,8 @@ function resolveTagImport(path, request) {
347
347
  const tagName = request.slice(1, -1);
348
348
  const tagDef = (0, _taglib.getTagDefForTagName)(file, tagName);
349
349
  const tagEntry = tagDef && (tagDef.renderer || tagDef.template);
350
- const relativePath = tagEntry && (0, _imports.resolveRelativePath)(file, tagEntry);
350
+ const relativePath =
351
+ tagEntry && (0, _imports.resolveRelativePath)(file, tagEntry, tagDef);
351
352
 
352
353
  if (!relativePath) {
353
354
  throw path.buildCodeFrameError(
@@ -124,7 +124,9 @@ function find(dirname, registeredTaglibs, tagDiscoveryDirs) {
124
124
  rootPkg.__dirname
125
125
  );
126
126
  if (taglibPath) {
127
- var taglib = taglibLoader.loadTaglibFromFile(taglibPath, true);
127
+ // Resolving realpaths the taglib, which for a virtual store (eg pnpm) bears
128
+ // no relation to how it is imported, so hold onto the name it resolved by.
129
+ var taglib = taglibLoader.loadTaglibFromFile(taglibPath, true, name);
128
130
  addTaglib(taglib);
129
131
  }
130
132
  }
@@ -26,11 +26,12 @@ function handleImport(taglib, importedTaglib) {
26
26
  }
27
27
 
28
28
  class Taglib {
29
- constructor(filePath, isFromPackageJson) {
29
+ constructor(filePath, isFromPackageJson, packageName) {
30
30
  ok(filePath, '"filePath" expected');
31
31
  this.filePath = this.path /* deprecated */ = this.id = filePath;
32
32
  this.isFromPackageJson = isFromPackageJson === true;
33
33
  this.dirname = path.dirname(this.filePath);
34
+ this.packageName = packageName;
34
35
  this.scriptLang = undefined;
35
36
  this.tags = {};
36
37
  this.migrators = [];
@@ -72,6 +73,23 @@ class Taglib {
72
73
  }
73
74
  this.tags[tag.name] = tag;
74
75
  tag.taglibId = this.id || this.path;
76
+ this.setTagPackage(tag);
77
+ }
78
+
79
+ setPackageName(packageName) {
80
+ this.packageName = packageName;
81
+ for (var name in this.tags) {
82
+ if (hasOwnProperty.call(this.tags, name)) {
83
+ this.setTagPackage(this.tags[name]);
84
+ }
85
+ }
86
+ }
87
+
88
+ setTagPackage(tag) {
89
+ if (this.packageName) {
90
+ tag.packageName = this.packageName;
91
+ tag.packageRoot = this.dirname;
92
+ }
75
93
  }
76
94
 
77
95
  addImport(path) {
@@ -8,8 +8,8 @@ function loadTaglibFromProps(taglib, taglibProps) {
8
8
  return loaders.loadTaglibFromProps(taglib, taglibProps);
9
9
  }
10
10
 
11
- function loadTaglibFromFile(filePath, isFromPackageJson) {
12
- return loaders.loadTaglibFromFile(filePath, isFromPackageJson);
11
+ function loadTaglibFromFile(filePath, isFromPackageJson, packageName) {
12
+ return loaders.loadTaglibFromFile(filePath, isFromPackageJson, packageName);
13
13
  }
14
14
 
15
15
  function loadTaglibFromDir(filePath, tagDiscoveryDir) {
@@ -4,7 +4,7 @@ var jsonFileReader = require("./json-file-reader");
4
4
  var loaders = require("./loaders");
5
5
  var types = require("./types");
6
6
 
7
- function loadFromFile(filePath, isFromPackageJson) {
7
+ function loadFromFile(filePath, isFromPackageJson, packageName) {
8
8
  ok(filePath, '"filePath" is required');
9
9
 
10
10
  var taglib = cache.get(filePath);
@@ -12,11 +12,15 @@ function loadFromFile(filePath, isFromPackageJson) {
12
12
  // Only load a taglib once by caching the loaded taglibs using the file
13
13
  // system file path as the key
14
14
  if (!taglib) {
15
- taglib = new types.Taglib(filePath, isFromPackageJson);
15
+ taglib = new types.Taglib(filePath, isFromPackageJson, packageName);
16
16
  cache.put(filePath, taglib);
17
17
 
18
18
  var taglibProps = jsonFileReader.readFileSync(filePath);
19
19
  loaders.loadTaglibFromProps(taglib, taglibProps);
20
+ } else if (packageName && !taglib.packageName) {
21
+ // The taglib may have first been loaded by walking up from a file within the
22
+ // package itself, in which case we did not yet know the name it is installed as.
23
+ taglib.setPackageName(packageName);
20
24
  }
21
25
 
22
26
  return taglib;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marko/compiler",
3
- "version": "5.40.2",
3
+ "version": "5.41.0",
4
4
  "description": "Marko template to JS compiler.",
5
5
  "keywords": [
6
6
  "babel",
@@ -77,13 +77,13 @@
77
77
  "lasso-package-root": "^1.0.1",
78
78
  "raptor-regexp": "^1.0.1",
79
79
  "raptor-util": "^3.2.0",
80
- "relative-import-path": "^1.0.0",
80
+ "relative-import-path": "^1.0.1",
81
81
  "resolve-from": "^5.0.0",
82
82
  "self-closing-tags": "^1.0.1",
83
83
  "source-map-support": "^0.5.21"
84
84
  },
85
85
  "devDependencies": {
86
- "marko": "^5.39.14"
86
+ "marko": "^5.39.18"
87
87
  },
88
88
  "engines": {
89
89
  "node": "18 || 20 || >=22"