Skip to content

Commit

Permalink
fix: issue with loading multiple marko compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jul 17, 2023
1 parent 1a77dad commit ab2da8c
Show file tree
Hide file tree
Showing 11 changed files with 557 additions and 665 deletions.
8 changes: 8 additions & 0 deletions .changeset/dirty-parrots-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@marko/language-server": patch
"@marko/language-tools": patch
"@marko/type-check": patch
"marko-vscode": patch
---

Lazily load the default compiler and translator.
8 changes: 8 additions & 0 deletions .changeset/plenty-nails-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@marko/language-server": patch
"@marko/language-tools": patch
"@marko/type-check": patch
"marko-vscode": patch
---

Move @marko/compiler to a peerDependency of @marko/type-check to avoid conflicts.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"plugins": ["import"],
"parserOptions": {
"sourceType": "module",
"impliedStrict": true
"impliedStrict": true,
"warnOnUnsupportedTypeScriptVersion": false
},
"env": {
"node": true,
Expand Down
1,101 changes: 491 additions & 610 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
"name": "marko-language-server-repo",
"devDependencies": {
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.1",
"@changesets/cli": "^2.26.2",
"@types/mocha": "^10.0.1",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.60.0",
"@typescript-eslint/parser": "^5.60.0",
"esbuild": "^0.18.6",
"eslint": "^8.43.0",
"@types/node": "^20.4.2",
"@typescript-eslint/eslint-plugin": "^6.1.0",
"@typescript-eslint/parser": "^6.1.0",
"esbuild": "^0.18.13",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-import": "^2.27.5",
"fast-glob": "^3.2.12",
"fast-glob": "^3.3.0",
"husky": "^8.0.3",
"lint-staged": "^13.2.2",
"lint-staged": "^13.2.3",
"mocha": "^10.2.0",
"mocha-snap": "^5.0.0",
"prettier": "^2.8.8",
"tsx": "^3.12.7",
"typescript": "^5.1.6",
"@types/jsdom": "^21.1.1",
"axe-core": "^4.6.3",
"jsdom": "^21.1.1"
"axe-core": "^4.7.2",
"jsdom": "^22.1.0"
},
"private": true,
"scripts": {
Expand Down
10 changes: 5 additions & 5 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
"dependencies": {
"@babel/helper-validator-identifier": "^7.22.5",
"@marko/language-tools": "^2.1.0",
"@marko/babel-utils": "^5.22.1",
"@marko/compiler": "^5.28.5",
"@marko/translator-default": "^5.26.5",
"@marko/babel-utils": "^6.2.0",
"@marko/compiler": "^5.31.1",
"@marko/translator-default": "^5.29.1",
"htmljs-parser": "^5.5.0",
"marko": "^5.26.5",
"marko": "^5.29.1",
"prettier": "^2.8.8",
"prettier-plugin-marko": "^1.5.2",
"prettier-plugin-marko": "^2.0.1",
"relative-import-path": "^1.0.0",
"strip-json-comments": "^3.1.1",
"typescript": "^5.1.6",
Expand Down
6 changes: 5 additions & 1 deletion packages/language-server/src/service/marko/format.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { TextEdit } from "vscode-languageserver";
import * as prettier from "prettier";
import * as markoPrettier from "prettier-plugin-marko";
import { Project } from "@marko/language-tools";

import { START_POSITION } from "../../utils/constants";
import { displayError } from "../../utils/messages";
import { getFSPath } from "../../utils/file";
import { getFSDir, getFSPath } from "../../utils/file";
import type { Plugin } from "../types";

export const format: Plugin["format"] = async (doc, params, cancel) => {
try {
const dir = getFSDir(doc);
const filepath = getFSPath(doc);
const text = doc.getText();
const options: prettier.Options = {
parser: "marko",
filepath,
markoCompiler: Project.getCompiler(dir),
markoCompilerConfig: Project.getConfig(dir),
plugins: [markoPrettier],
tabWidth: params.options.tabSize,
useTabs: params.options.insertSpaces === false,
Expand Down
12 changes: 7 additions & 5 deletions packages/language-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
"description": "Marko Language Tools",
"version": "2.1.0",
"bugs": "https://github.com/marko-js/language-server/issues/new?template=Bug_report.md",
"peerDependencies": {
"@marko/compiler": "^5.28.4"
},
"dependencies": {
"@babel/helper-validator-identifier": "^7.22.5",
"@babel/parser": "^7.22.5",
"@marko/compiler": "5.28.4",
"@marko/translator-default": "5.26.5",
"@babel/parser": "^7.22.7",
"htmljs-parser": "^5.5.0",
"relative-import-path": "^1.0.0"
},
"devDependencies": {
"@babel/code-frame": "^7.22.5",
"@marko/compiler": "^5.28.5",
"@marko/compiler": "^5.31.1",
"@marko/translator-default": "^5.29.1",
"@types/babel__code-frame": "^7.0.3",
"@types/babel__helper-validator-identifier": "^7.15.0",
"@typescript/vfs": "^1.4.0",
"marko": "^5.26.5",
"marko": "^5.29.1",
"mitata": "^0.1.6",
"tsx": "^3.12.7"
},
Expand Down
50 changes: 19 additions & 31 deletions packages/language-tools/src/util/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import path from "path";
import { createRequire } from "module";
import type TS from "typescript/lib/tsserverlibrary";
import type { TaglibLookup } from "@marko/babel-utils";
import * as defaultCompiler from "@marko/compiler";
import * as defaultConfig from "@marko/compiler/config";
import * as defaultTranslator from "@marko/translator-default";
import type * as Compiler from "@marko/compiler";
import stripJSONComments from "strip-json-comments";
import { ScriptLang } from "../extractors/script";

export interface Meta {
compiler: typeof defaultCompiler;
config: Omit<defaultCompiler.Config, "cache" | "translator"> & {
fallback: boolean;
compiler: typeof Compiler;
config: Omit<Compiler.Config, "cache" | "translator"> & {
cache: Map<any, any>;
translator: {
runtimeTypes?: string;
Expand All @@ -31,16 +30,6 @@ const defaultTypeLibs: Partial<TypeLibs> = {};
const ignoreErrors = (_err: Error) => {};
const metaByDir = new Map<string, Meta>();
const metaByCompiler = new Map<string, Meta>();
const defaultMeta: Meta = {
compiler: defaultCompiler,
config: {
...defaultConfig,
cache: new Map(),
translator: defaultTranslator,
},
};

defaultCompiler.configure(defaultMeta.config);

export function getCompiler(dir?: string) {
return getMeta(dir).compiler;
Expand Down Expand Up @@ -189,7 +178,6 @@ export function getScriptLang(
}

export function clearCaches() {
clearCacheForMeta(defaultMeta);
for (const project of metaByCompiler.values()) {
clearCacheForMeta(project);
}
Expand All @@ -199,24 +187,23 @@ export function setDefaultTypePaths(defaults: typeof defaultTypeLibs) {
Object.assign(defaultTypeLibs, defaults);
}

function getMeta(dir?: string): Meta {
if (!dir) return defaultMeta;

function getMeta(dir = __dirname): Meta {
let cached = metaByDir.get(dir);
if (!cached) {
const fallback = dir === __dirname;

try {
const require = createRequire(dir);
const compilerConfigPath = require.resolve("@marko/compiler/config");
cached = metaByCompiler.get(compilerConfigPath);
const configPath = require.resolve("@marko/compiler/config");
cached = metaByCompiler.get(configPath);
if (!cached) {
const compiler = require(path.join(
compilerConfigPath,
configPath,
".."
)) as typeof defaultCompiler;
const config = interopDefault(
require(compilerConfigPath)
) as defaultCompiler.Config;
)) as typeof Compiler;
const config = interopDefault(require(configPath)) as Compiler.Config;
cached = {
fallback,
compiler,
config: {
...config,
Expand All @@ -225,10 +212,11 @@ function getMeta(dir?: string): Meta {
},
};
compiler.configure(cached.config);
metaByCompiler.set(compilerConfigPath, cached);
metaByCompiler.set(configPath, cached);
}
} catch {
cached = defaultMeta;
} catch (err) {
if (fallback) throw err;
cached = getMeta();
}

metaByDir.set(dir, cached);
Expand All @@ -252,8 +240,8 @@ function getTagLookupForProject(meta: Meta, dir: string): TaglibLookup {
ignoreErrors
);
} catch {
if (meta !== defaultMeta) {
lookup = getTagLookupForProject(defaultMeta, dir);
if (!meta.fallback) {
lookup = getTagLookupForProject(getMeta(), dir);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/type-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"bugs": "https://github.com/marko-js/language-server/issues/new?template=Bug_report.md",
"dependencies": {
"@babel/code-frame": "^7.22.5",
"@marko/language-tools": "^2.0.11",
"@marko/language-tools": "^2.1.0",
"arg": "^5.0.2",
"kleur": "^4.1.5",
"strip-json-comments": "^3.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"@types/vscode": "^1.74.2",
"@vscode/test-electron": "^2.3.3",
"@vscode/vsce": "^2.19.0",
"ovsx": "^0.8.1",
"ovsx": "^0.8.2",
"tsx": "^3.12.7",
"vscode-languageclient": "^8.1.0"
},
Expand Down

0 comments on commit ab2da8c

Please sign in to comment.