Skip to content

Commit

Permalink
fix: improve compiler loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jul 25, 2023
1 parent 8db7346 commit 6c97a4a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
8 changes: 8 additions & 0 deletions .changeset/odd-insects-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@marko/language-tools": patch
"@marko/language-server": patch
"@marko/type-check": patch
"marko-vscode": patch
---

Fallback to loading compiler relative to the Marko runtime if not hoisted.
8 changes: 8 additions & 0 deletions .changeset/three-humans-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@marko/language-tools": patch
"@marko/language-server": patch
"@marko/type-check": patch
"marko-vscode": patch
---

When no default compiler registered, avoid swallowing original error when unable to load compiler.
63 changes: 41 additions & 22 deletions packages/language-tools/src/util/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,50 @@ function getMeta(dir?: string): Meta {
return defaultMeta;
}

if (defaultMeta) {
try {
return loadMeta(dir);
} catch {
metaByDir.set(dir, defaultMeta);
return defaultMeta;
}
}

return loadMeta(dir);
}

function loadMeta(dir: string): Meta {
let cached = metaByDir.get(dir);
if (!cached) {
let require = createRequire(dir);
let configPath: string;

try {
const require = createRequire(dir);
const configPath = require.resolve("@marko/compiler/config");
cached = metaByCompiler.get(configPath);
if (!cached) {
const compiler = require(path.join(
configPath,
".."
)) as typeof Compiler;
const config = interopDefault(require(configPath)) as Compiler.Config;
cached = {
compiler,
config: {
...config,
cache: new Map(),
translator: require(config.translator),
},
};
compiler.configure(cached.config);
metaByCompiler.set(configPath, cached);
}
} catch (err) {
cached = getMeta();
// Try loading compiler directly.
configPath = require.resolve("@marko/compiler/config");
} catch {
// Fallback to checking if compiler is a installed relative to the Marko package.
require = createRequire(
path.dirname(require.resolve("marko/package.json"))
);
configPath = require.resolve("@marko/compiler/config");
}

cached = metaByCompiler.get(configPath);

if (!cached) {
const compiler = require(path.dirname(configPath)) as typeof Compiler;
const config = interopDefault(require(configPath)) as Compiler.Config;
cached = {
compiler,
config: {
...config,
cache: new Map(),
translator: require(config.translator),
},
};
compiler.configure(cached.config);
metaByCompiler.set(configPath, cached);
}

metaByDir.set(dir, cached);
Expand Down

0 comments on commit 6c97a4a

Please sign in to comment.