Skip to content

Commit

Permalink
feat: move @marko/compiler to a peerDependency (#41)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: @marko/compiler is no longer installed automatically.
  • Loading branch information
DylanPiercey authored Jul 17, 2023
1 parent bc01cd6 commit 486a9f6
Show file tree
Hide file tree
Showing 7 changed files with 701 additions and 683 deletions.
1,297 changes: 656 additions & 641 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
"version": "1.0.0",
"author": "Dylan Piercey <[email protected]>",
"bugs": "https://github.com/marko-js/prettier/issues",
"dependencies": {
"@marko/compiler": "^5",
"@marko/translator-default": "^5"
},
"devDependencies": {
"@babel/generator": "^7.20.14",
"@commitlint/cli": "^17.4.4",
Expand Down Expand Up @@ -58,6 +54,7 @@
"main": "dist/index.js",
"module": "dist/index.mjs",
"peerDependencies": {
"@marko/compiler": "^5",
"prettier": "^2"
},
"publishConfig": {
Expand Down
6 changes: 4 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { types as t } from "@marko/compiler";
import type * as Compiler from "@marko/compiler";

declare module "prettier" {
interface Options {
Expand All @@ -7,6 +7,8 @@ declare module "prettier" {
}

interface ParserOptions {
markoCompiler?: typeof Compiler;
markoCompilerConfig?: Compiler.Config;
markoSyntax?: "auto" | "html" | "concise";
markoAttrParen?: boolean;
// @internal
Expand All @@ -25,7 +27,7 @@ export type MarkoEmbedNode = {
loc: undefined;
};

export type Node = t.Node | MarkoEmbedNode;
export type Node = Compiler.types.Node | MarkoEmbedNode;

export const enclosedNodeTypeReg =
/^(?:Identifier|.*Literal|(?:Object|Array|Parenthesized|Record|Tuple)Expression)$/;
Expand Down
70 changes: 37 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import {
CustomParser,
ParserOptions,
} from "prettier";
import * as defaultCompiler from "@marko/compiler";
import type { types } from "@marko/compiler";
import defaultConfig from "@marko/compiler/config";
import * as defaultTranslator from "@marko/translator-default";
import type * as Compiler from "@marko/compiler";
import type { types, Config } from "@marko/compiler";
import {
Node,
shorthandIdOrClassReg,
Expand Down Expand Up @@ -56,25 +54,6 @@ const expressionParser: CustomParser = (code, parsers, options) => {
};
};

const [{ compileSync, types: t }, config] = (() => {
try {
return [
rootRequire("@marko/compiler"),
rootRequire("@marko/compiler/config"),
];
} catch {
return [defaultCompiler, defaultConfig];
}
})() as [typeof defaultCompiler, typeof defaultConfig];

const translator = (() => {
try {
return rootRequire(config.translator);
} catch {
return defaultTranslator;
}
})();

export const languages: SupportLanguage[] = [
{
name: "marko",
Expand Down Expand Up @@ -120,16 +99,9 @@ export const options: SupportOptions = {
default: (() => {
// By default we check if the installed parser supported unenclosed whitespace for all attrs.
try {
let compilerRequire: NodeRequire;

try {
compilerRequire = createRequire(
rootRequire.resolve("@marko/compiler")
);
} catch {
compilerRequire = createRequire(rootRequire.resolve("marko"));
}

const compilerRequire = createRequire(
rootRequire.resolve("@marko/compiler")
);
const [major, minor] = (
compilerRequire("htmljs-parser/package.json") as { version: string }
).version
Expand All @@ -151,6 +123,36 @@ export const parsers: Record<string, Parser<Node>> = {
astFormat: "marko-ast",
parse(text, _parsers, opts) {
const { filepath = defaultFilePath } = opts;

const [{ compileSync, types: t }, config] = (() => {
try {
return [
(opts.markoCompiler ||= rootRequire("@marko/compiler")),
(opts.markoCompilerConfig ||= rootRequire(
"@marko/compiler/config"
).default),
];
} catch (cause) {
throw new Error(
"You must have @marko/compiler installed to use prettier-plugin-marko.",
{ cause }
);
}
})() as [typeof Compiler, Config];

const translator = (() => {
try {
return rootRequire(config.translator);
} catch (cause) {
throw new Error(
`Unable to load Marko translator at ${JSON.stringify(
config.translator
)}. Please install the Marko runtime.`,
{ cause }
);
}
})();

const { ast } = compileSync(`${text}\n`, filepath, {
ast: true,
code: false,
Expand Down Expand Up @@ -224,6 +226,7 @@ export const printers: Record<string, Printer<Node>> = {
"marko-ast": {
print(path, opts, print) {
const node = path.getValue();
const t = opts.markoCompiler!.types;

switch (node.type) {
case "File":
Expand Down Expand Up @@ -719,6 +722,7 @@ export const printers: Record<string, Printer<Node>> = {
},
embed(path, print, toDoc, opts) {
const node = path.getValue();
const t = opts.markoCompiler!.types;

switch (node.type) {
case "_MarkoEmbed":
Expand Down
2 changes: 1 addition & 1 deletion src/utils/get-sibling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { types as t } from "@marko/compiler";
import type { types as t } from "@marko/compiler";
import { AstPath } from "prettier";
import { Node } from "../constants";

Expand Down
2 changes: 1 addition & 1 deletion src/utils/is-text-like.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { types as t } from "@marko/compiler";
import type { types as t } from "@marko/compiler";
import { Node } from "../constants";

export default function isTextLike(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/with-block-if-needed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { types as t } from "@marko/compiler";
import type { types as t } from "@marko/compiler";
import { doc, Doc, format, ParserOptions } from "prettier";
import { enclosedNodeTypeReg } from "../constants";
import outerCodeMatches from "./outer-code-matches";
Expand Down

0 comments on commit 486a9f6

Please sign in to comment.