@marko/compiler
Advanced tools
+9
-1
@@ -57,2 +57,6 @@ import type { Config, types as t } from "@marko/compiler"; | ||
| taglibId: string; | ||
| /** Set when the taglib was discovered by resolving an installed package. */ | ||
| packageName?: string; | ||
| /** The root directory of `packageName`, which contains all files for this tag. */ | ||
| packageRoot?: string; | ||
| types?: string; | ||
@@ -307,3 +311,7 @@ template?: string; | ||
| export function resolveRelativePath(file: t.BabelFile, request: string): string; | ||
| export function resolveRelativePath( | ||
| file: t.BabelFile, | ||
| request: string, | ||
| tagDef?: TagDefinition, | ||
| ): string; | ||
| export function importDefault( | ||
@@ -310,0 +318,0 @@ file: t.BabelFile, |
@@ -8,6 +8,9 @@ "use strict";exports.__esModule = true;exports.importDefault = importDefault;exports.importNamed = importNamed;exports.importStar = importStar;exports.resolveRelativePath = resolveRelativePath;var _compiler = require("@marko/compiler"); | ||
| const FS_START = _path.default.sep === "/" ? _path.default.sep : /^(.*?:)/.exec(_modules.cwd)[1]; | ||
| const REG_NODE_MODULES = /[\\/]node_modules[\\/]/; | ||
| function resolveRelativePath(file, request) { | ||
| function resolveRelativePath(file, request, tagDef) { | ||
| if (request.startsWith(FS_START)) { | ||
| request = (0, _relativeImportPath.relativeImportPath)(file.opts.filename, request); | ||
| request = | ||
| packageImportPath(file.opts.filename, tagDef, request) || | ||
| (0, _relativeImportPath.relativeImportPath)(file.opts.filename, request); | ||
| } | ||
@@ -25,2 +28,30 @@ | ||
| /** | ||
| * A tag installed into `node_modules` is imported through the name its package was | ||
| * resolved by, since its path is a realpath which may not be importable at all. | ||
| */ | ||
| function packageImportPath(from, tagDef, request) { | ||
| if (!tagDef?.packageName || !REG_NODE_MODULES.test(request)) return; | ||
| const { packageRoot } = tagDef; | ||
| // Within the package we stay relative, both because it always resolves and | ||
| // because a self reference would only work if the package exports the tag. | ||
| if (!isWithin(packageRoot, request) || isWithin(packageRoot, from)) return; | ||
| const subPath = _path.default.relative(packageRoot, request); | ||
| return `${tagDef.packageName}/${subPath.split(_path.default.sep).join("/")}`; | ||
| } | ||
| function isWithin(dir, filename) { | ||
| const rel = _path.default.relative(dir, filename); | ||
| // `path.relative` walks out of the dir with `..`, or returns an absolute path | ||
| // when it cannot relate the two at all, eg across windows drives. | ||
| return ( | ||
| !!rel && | ||
| rel !== ".." && | ||
| !rel.startsWith(`..${_path.default.sep}`) && | ||
| !_path.default.isAbsolute(rel)); | ||
| } | ||
| function importDefault(file, request, nameHint) { | ||
@@ -27,0 +58,0 @@ const imports = getImports(file); |
@@ -350,3 +350,4 @@ "use strict";exports.__esModule = true;exports.findAttributeTags = findAttributeTags;exports.findParentTag = findParentTag;exports.getArgOrSequence = getArgOrSequence;exports.getFullyResolvedTagName = getFullyResolvedTagName;exports.getMacroIdentifier = getMacroIdentifier;exports.getMacroIdentifierForName = getMacroIdentifierForName;exports.getTagDef = getTagDef;exports.getTagTemplate = getTagTemplate;exports.getTemplateId = getTemplateId;exports.hasMacro = hasMacro;exports.isAttributeTag = isAttributeTag;exports.isDynamicTag = isDynamicTag;exports.isLoopTag = isLoopTag;exports.isMacroTag = isMacroTag;exports.isNativeTag = isNativeTag;exports.isTransparentTag = isTransparentTag;exports.loadFileForImport = loadFileForImport;exports.loadFileForTag = loadFileForTag;exports.registerMacro = registerMacro;exports.resolveTagImport = resolveTagImport;var _compiler = require("@marko/compiler"); | ||
| const tagEntry = tagDef && (tagDef.renderer || tagDef.template); | ||
| const relativePath = tagEntry && (0, _imports.resolveRelativePath)(file, tagEntry); | ||
| const relativePath = | ||
| tagEntry && (0, _imports.resolveRelativePath)(file, tagEntry, tagDef); | ||
@@ -353,0 +354,0 @@ if (!relativePath) { |
@@ -127,3 +127,5 @@ "use strict"; | ||
| if (taglibPath) { | ||
| var taglib = taglibLoader.loadTaglibFromFile(taglibPath, true); | ||
| // Resolving realpaths the taglib, which for a virtual store (eg pnpm) bears | ||
| // no relation to how it is imported, so hold onto the name it resolved by. | ||
| var taglib = taglibLoader.loadTaglibFromFile(taglibPath, true, name); | ||
| addTaglib(taglib); | ||
@@ -130,0 +132,0 @@ } |
@@ -11,4 +11,4 @@ "use strict";var cache = require("./cache"); | ||
| function loadTaglibFromFile(filePath, isFromPackageJson) { | ||
| return loaders.loadTaglibFromFile(filePath, isFromPackageJson); | ||
| function loadTaglibFromFile(filePath, isFromPackageJson, packageName) { | ||
| return loaders.loadTaglibFromFile(filePath, isFromPackageJson, packageName); | ||
| } | ||
@@ -15,0 +15,0 @@ |
@@ -7,3 +7,3 @@ "use strict";var ok = require("assert").ok; | ||
| function loadFromFile(filePath, isFromPackageJson) { | ||
| function loadFromFile(filePath, isFromPackageJson, packageName) { | ||
| ok(filePath, '"filePath" is required'); | ||
@@ -16,3 +16,3 @@ | ||
| if (!taglib) { | ||
| taglib = new types.Taglib(filePath, isFromPackageJson); | ||
| taglib = new types.Taglib(filePath, isFromPackageJson, packageName); | ||
| cache.put(filePath, taglib); | ||
@@ -22,2 +22,6 @@ | ||
| loaders.loadTaglibFromProps(taglib, taglibProps); | ||
| } else if (packageName && !taglib.packageName) { | ||
| // The taglib may have first been loaded by walking up from a file within the | ||
| // package itself, in which case we did not yet know the name it is installed as. | ||
| taglib.setPackageName(packageName); | ||
| } | ||
@@ -24,0 +28,0 @@ |
@@ -29,3 +29,3 @@ "use strict"; | ||
| class Taglib { | ||
| constructor(filePath, isFromPackageJson) { | ||
| constructor(filePath, isFromPackageJson, packageName) { | ||
| ok(filePath, '"filePath" expected'); | ||
@@ -35,2 +35,3 @@ this.filePath = this.path /* deprecated */ = this.id = filePath; | ||
| this.dirname = path.dirname(this.filePath); | ||
| this.packageName = packageName; | ||
| this.scriptLang = undefined; | ||
@@ -77,4 +78,21 @@ this.tags = {}; | ||
| tag.taglibId = this.id || this.path; | ||
| this.setTagPackage(tag); | ||
| } | ||
| setPackageName(packageName) { | ||
| this.packageName = packageName; | ||
| for (var name in this.tags) { | ||
| if (hasOwnProperty.call(this.tags, name)) { | ||
| this.setTagPackage(this.tags[name]); | ||
| } | ||
| } | ||
| } | ||
| setTagPackage(tag) { | ||
| if (this.packageName) { | ||
| tag.packageName = this.packageName; | ||
| tag.packageRoot = this.dirname; | ||
| } | ||
| } | ||
| addImport(path) { | ||
@@ -81,0 +99,0 @@ var importedTaglib = loaders.loadTaglibFromFile(path); |
+3
-3
| { | ||
| "name": "@marko/compiler", | ||
| "version": "5.40.2", | ||
| "version": "5.41.0", | ||
| "description": "Marko template to JS compiler.", | ||
@@ -80,3 +80,3 @@ "keywords": [ | ||
| "raptor-util": "^3.2.0", | ||
| "relative-import-path": "^1.0.0", | ||
| "relative-import-path": "^1.0.1", | ||
| "resolve-from": "^5.0.0", | ||
@@ -87,3 +87,3 @@ "self-closing-tags": "^1.0.1", | ||
| "devDependencies": { | ||
| "marko": "^5.39.14" | ||
| "marko": "^5.39.18" | ||
| }, | ||
@@ -90,0 +90,0 @@ "engines": { |
5108507
0.05%124498
0.05%Updated