Skip to content

Commit

Permalink
support swc and webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Oct 25, 2023
1 parent 3d68eeb commit e56d027
Show file tree
Hide file tree
Showing 6 changed files with 519 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
- name: Prepare repository
run: git fetch --unshallow --tags

- name: Use Node.js 14.x
- name: Use Node.js 18.x
uses: actions/setup-node@v1
with:
node-version: 14.x
node-version: 18.x

- name: Install dependencies
uses: bahmutov/npm-install@v1
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@
"icon": "https://user-images.githubusercontent.com/321738/63501763-88dbf600-c4cc-11e9-96cd-94adadc2fd72.png"
},
"dependencies": {
"@jsdevtools/coverage-istanbul-loader": "^3.0.5",
"@types/babel__core": "^7.1.19",
"@types/istanbul-lib-coverage": "^2.0.4",
"babel-plugin-istanbul": "^6.1.1",
"swc-plugin-coverage-instrument": "^0.0.20",
"vite-plugin-istanbul": "^3.0.1"
}
}
25 changes: 25 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,28 @@ export const defaultExclude = [
"**/{karma,rollup,webpack}.config.js",
"**/.{eslint,mocha}rc.{js,cjs}",
];

export const defaultExcludeRegexes = [
"node_modules",
"\\.storybook/.*",
"coverage/.*",
"packages/[^/]+/test(s?)/.*",
".*\\.d\\.ts$",
"test(s?)/.*",
`test(-[^.]+)?\\.(${testFileExtensions})$`,
`.*(-|\\.)((spec|stories|types)\\.(${testFileExtensions}))$`,
"__tests__/.*",
".*-entry\\.js",

/* Exclude common development tool configuration files */
`.*\\/(ava|babel|nyc)\\.config\\.(js|cjs|mjs)$`,
`.*\\/jest\\.config\\.(js|cjs|mjs|ts)$`,
`.*\\/(karma|rollup|webpack)\\.config\\.js$`,
`.*\\/.(eslint|mocha)rc\\.(js|cjs)$`,

// angular
"\.(e2e|spec|stories)\.ts$",
"(ngfactory|ngstyle)\.js",
"polyfills.ts"
].map(pattern => new RegExp(pattern));

65 changes: 61 additions & 4 deletions src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import type { TransformOptions } from "@babel/core";
import type { Options } from "@storybook/core-common";
import { defaultExclude, defaultExtensions } from "./constants";
import type { AddonOptionsBabel, AddonOptionsVite } from "./types";
import {
defaultExclude,
defaultExcludeRegexes,
defaultExtensions,
} from "./constants";
import type {
AddonOptionsBabel,
AddonOptionsVite,
AddonOptionsWebpack,
} from "./types";

export const viteFinal = async (
viteConfig: Record<string, any>,
options: Options & AddonOptionsVite
) => {
const istanbul = require("vite-plugin-istanbul");

console.log("[addon-coverage] Adding istanbul plugin to vite config");
console.log("[addon-coverage] Adding istanbul plugin to Vite config");
viteConfig.build = viteConfig.build || {};
viteConfig.build.sourcemap = true;

Expand All @@ -35,7 +43,7 @@ export const babel = async (
babelConfig: TransformOptions,
options: Options & AddonOptionsBabel
) => {
console.log("[addon-coverage] Adding istanbul plugin to babel config");
console.log("[addon-coverage] Adding istanbul plugin to Babel config");
babelConfig.plugins ||= [];
babelConfig.plugins.push([
"istanbul",
Expand All @@ -54,3 +62,52 @@ export const babel = async (

return babelConfig;
};

export const swc = async (
swcConfig: Record<string, any>,
options: Options & AddonOptionsWebpack
) => {
swcConfig.parseMap = true;

return swcConfig;
};

export const webpackFinal = async (
webpackConfig: Record<string, any>,
options: Options & AddonOptionsWebpack
) => {
if (options.useWebpackConfig !== true) return webpackConfig;
webpackConfig.module.rules ||= [];
const extensions = options.istanbul?.extension || /\.(mjs|cjs|tsx?|jsx?)$/;

console.log("[addon-coverage] Adding istanbul loader to Webpack config");

webpackConfig.module.rules.push({
test: extensions,
loader: "@jsdevtools/coverage-istanbul-loader",
enforce: "post",
options: options.istanbul || {},
include: (modulePath: string) => {
if (options.istanbul?.include) {
const includeRegexes = options.istanbul.include.map(
(pattern) => new RegExp(pattern)
);
return includeRegexes.some((pattern) => pattern.test(modulePath));
} else {
return true;
}
},
exclude: (modulePath: string) => {
const excludeRegexes = defaultExcludeRegexes;
if (options.istanbul?.exclude) {
excludeRegexes.push(
...options.istanbul.exclude.map((pattern) => new RegExp(pattern))
);
}

return excludeRegexes.some((pattern) => pattern.test(modulePath));
},
});

return webpackConfig;
};
41 changes: 41 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,51 @@ interface IstanbulOptionsBabel {
fileName?: string;
}

interface IstanbulOptionsSWC {
coverageVariable?: string;
compact?: boolean;
reportLogic?: boolean;
ignoreClassMethods?: Array<string>;
inputSourceMap?: Record<string, any>;
instrumentLog: {
level: "trace" | "warn" | "error" | "info";
enableTrace: boolean;
};
}

interface IstanbulOptionsWebpack {
coverageVariable?: string;
preserveComments?: boolean;
compact?: boolean;
produceSourceMap?: boolean;
ignoreClassMethods: [];
debug?: boolean;
}

export interface AddonOptionsBabel {
istanbul?: IstanbulOptionsBabel;
}

export interface AddonOptionsVite {
istanbul?: IstanbulOptionsVite;
}

export type AddonOptionsWebpack = {
useWebpackConfig?: boolean;
useSwcConfig?: boolean;
useSwcPlugin: true;
istanbul?: {
include?: string[];
exclude?: string[];
extension?: RegExp;
} & IstanbulOptionsSWC;
} | {
useWebpackConfig?: boolean;
useSwcConfig?: boolean;
useSwcPlugin: false;
istanbul?: {
include?: string[];
exclude?: string[];
extension?: RegExp;
} & IstanbulOptionsWebpack;
};
Loading

0 comments on commit e56d027

Please sign in to comment.