From e02a189197dd9aa68dc8f12d1f5ff315caaa6c10 Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Wed, 28 Sep 2022 09:09:43 -0700 Subject: [PATCH] feat: use Marko 5 compiler apis if available --- package-lock.json | 2 +- src/transform/create-transform.ts | 83 ++++++++++++++++--------------- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5476c08..750fd8d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "@marko/jest", - "version": "4.1.1", + "version": "4.1.2", "license": "MIT", "dependencies": { "concat-with-sourcemaps": "^1.1.0", diff --git a/src/transform/create-transform.ts b/src/transform/create-transform.ts index 32c03a4..462afb1 100644 --- a/src/transform/create-transform.ts +++ b/src/transform/create-transform.ts @@ -2,19 +2,18 @@ import fs from "fs"; import path from "path"; import crypto from "crypto"; import { Buffer } from "buffer"; -import compiler from "marko/compiler"; import mergeMaps from "merge-source-map"; import ConcatMap from "concat-with-sourcemaps"; import type { Transformer } from "@jest/transform"; +import { version as markoVersion } from "marko/package.json"; + +const isMarko4 = /^4\./.test(markoVersion); +const htmlOutput = "html"; +const domOutput = isMarko4 ? "vdom" : "dom"; +const compiler = isMarko4 ? require("marko/compiler") : require("@marko/compiler"); +const taglib = isMarko4 ? compiler.taglibFinder : compiler.taglib; +const compileSync = isMarko4 ? compiler.compile : compiler.compileSync; const THIS_FILE = fs.readFileSync(__filename); -const MARKO_OPTIONS: Record = { - writeVersionComment: false, - requireTemplates: true, - writeToDisk: false, - sourceOnly: false, - sourceMaps: true, - modules: "cjs", -}; let configuredGlobals = false; // Allows for resolving `.marko` files during compilation. @@ -52,45 +51,47 @@ export default ({ browser }: { browser: boolean }) => { }, process(src, filename, transformOptions) { const config = transformOptions.config || transformOptions; - const markoConfig = config.globals.marko as any; - - if (!configuredGlobals && markoConfig) { - configuredGlobals = true; - - for (const key in markoConfig) { - if (key !== "taglib") { - MARKO_OPTIONS[key] = markoConfig[key]; - } - } - - const taglibConfig = markoConfig.taglib; - if (taglibConfig) { - const excludeDirs = taglibConfig.excludeDirs; - if (excludeDirs) { - for (const name of excludeDirs) { - compiler.taglibFinder.excludeDir(name); + const globalMarkoConfig = config.globals.marko as any; + const output = browser ? domOutput : htmlOutput; + const markoConfig: Record = isMarko4 ? { + requireTemplates: true, + writeToDisk: false, + sourceOnly: false, + output, + } : { + sourceMaps: true, + modules: "cjs", + output, + } + + if (globalMarkoConfig) { + const { taglib: taglibConfig, ...compilerConfig } = globalMarkoConfig; + Object.assign(markoConfig, compilerConfig); + + if (!configuredGlobals) { + configuredGlobals = true; + + if (taglibConfig) { + const excludeDirs = taglibConfig.excludeDirs; + if (excludeDirs) { + for (const name of excludeDirs) { + taglib.excludeDir(name); + } } - } - - const excludePackages = taglibConfig.excludePackages; - if (excludePackages) { - for (const name of excludePackages) { - compiler.taglibFinder.excludePackage(name); + + const excludePackages = taglibConfig.excludePackages; + if (excludePackages) { + for (const name of excludePackages) { + taglib.excludePackage(name); + } } } } } - const result = compiler[ - (browser || (config as any).browser) && - compiler.compileForBrowser /** Only Marko 4 supports compileForBrowser, otherwise use compile */ - ? "compileForBrowser" - : "compile" - ](src, filename, MARKO_OPTIONS); - - let code = typeof result === "string" ? result : result.code; // Marko 3 does not support sourceOnly: false - let map = result.map; + const result = compileSync(src, filename, markoConfig); const deps = browser && result.meta && result.meta.deps; + let { code, map } = result; if (deps && deps.length) { const concatMap = new ConcatMap(true, "", ";");