diff --git a/README.md b/README.md index 0e9050c..729e64a 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,27 @@ module.exports = { }; ``` +## Customizing the Marko compiler + +You can override the default [Marko compiler options](https://markojs.com/docs/compiler/#options) by adding a Jest "globals" config with a `marko` property. +An additional `taglib` property can be set which supports an `excludeDirs` and `excludePackages` array which will prevent Marko from discovering taglibs in a directory or package respectively. + +**jest.config.js** + +```javascript +module.exports = { + preset: "@marko/jest/preset/browser", + globals: { + marko: { + ignoreUnrecognizedTags: true, + taglib: { + excludePackages: ["@scope/package-name"] + } + } + } +}; +``` + ## Test both server & browser For many Marko projects you may have a mix of server and browser components. You can test all of these with Jest by using the [projects configuration](https://jestjs.io/docs/en/configuration#projects-array-string-projectconfig) [like this project does](./blob/master/jest.config.js)! Simply make sure to use `@marko/jest/preset/node` and `@marko/jest/preset/browser` according to the test environment. diff --git a/src/transform/create-transform.ts b/src/transform/create-transform.ts index 50090e4..ba237c8 100644 --- a/src/transform/create-transform.ts +++ b/src/transform/create-transform.ts @@ -7,7 +7,7 @@ import mergeMaps from "merge-source-map"; import ConcatMap from "concat-with-sourcemaps"; import type { Transformer } from "@jest/transform"; const THIS_FILE = fs.readFileSync(__filename); -const MARKO_OPTIONS = { +const MARKO_OPTIONS: Record = { writeVersionComment: false, requireTemplates: true, writeToDisk: false, @@ -15,6 +15,7 @@ const MARKO_OPTIONS = { sourceMaps: true, modules: "cjs", }; +let configuredGlobals = false; // Allows for resolving `.marko` files during compilation. if (!(".marko" in require.extensions)) { @@ -51,6 +52,35 @@ 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 excludePackages = taglibConfig.excludePackages; + if (excludePackages) { + for (const name of excludePackages) { + compiler.taglibFinder.excludePackage(name); + } + } + } + } + const result = compiler[ (browser || (config as any).browser) && compiler.compileForBrowser /** Only Marko 4 supports compileForBrowser, otherwise use compile */