Skip to content

Commit

Permalink
feat: expose a way to ignore tags and customize Marko config
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 25, 2021
1 parent f837537 commit 616674c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 31 additions & 1 deletion src/transform/create-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ 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<string, any> = {
writeVersionComment: false,
requireTemplates: true,
writeToDisk: false,
sourceOnly: false,
sourceMaps: true,
modules: "cjs",
};
let configuredGlobals = false;

// Allows for resolving `.marko` files during compilation.
if (!(".marko" in require.extensions)) {
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 616674c

Please sign in to comment.