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 24, 2023
1 parent 3d68eeb commit 1e3cf2f
Show file tree
Hide file tree
Showing 5 changed files with 548 additions and 3 deletions.
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));

98 changes: 96 additions & 2 deletions src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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>,
Expand Down Expand Up @@ -54,3 +54,97 @@ export const babel = async (

return babelConfig;
};

export const swc = async (
swcConfig: Record<string, any>,
options: Options & AddonOptionsVite
) => {
console.log("[addon-coverage] Adding istanbul plugin to SWC config");
swcConfig.jsc.experimental.plugins ||= [];

swcConfig.jsc.experimental.plugins.push([
"swc-plugin-coverage-instrument",
{
...options.istanbul,
include: Array.from(options.istanbul?.include || []),
exclude: [
options.configDir + "/**",
...defaultExclude,
...Array.from(options.istanbul?.exclude || []),
],
extension: options.istanbul?.extension || defaultExtensions,
coverageVariable: "__coverage__",
},
]);

return swcConfig;
};

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

if(options.useSwcPlugin) {
console.log("[addon-coverage] Adding istanbul plugin to SWC config");

webpackConfig.module.rules.push({
test: extensions,
use: [
{
loader: require.resolve('swc-loader'),
options: {
parseMap: true,
jsc: {
experimental: {
plugins: [
[
"swc-plugin-coverage-instrument",
{
...options.istanbul,
coverageVariable: options.istanbul.coverageVariable || "__coverage__",
}
]
]
},
},
},
},
],
include: options.istanbul.include || [],
exclude: options.istanbul.exclude || ((modulePath: string) => {
return defaultExcludeRegexes.some((pattern) => pattern.test(modulePath));
}),
});
} else {
console.log("[addon-coverage] Adding istanbul plugin to Webpack config");

webpackConfig.module.rules.push({
test: extensions,
use: [
{
loader: require.resolve('swc-loader'),
options: {
parseMap: true,
},
},
],
exclude: options.istanbul.exclude || ((modulePath: string) => {
return defaultExcludeRegexes.some((pattern) => pattern.test(modulePath));
}),
},
{
test: extensions,
loader: '@jsdevtools/coverage-istanbul-loader',
enforce: 'post',
options: options.istanbul || {},
exclude: options.istanbul.exclude || ((modulePath: string) => {
return defaultExcludeRegexes.some((pattern) => pattern.test(modulePath));
}),
});
}

return webpackConfig;
};
37 changes: 37 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,47 @@ 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 = {
useSwcPlugin: true;
istanbul?: {
include?: string[];
exclude?: string[];
extension?: RegExp;
} & IstanbulOptionsSWC;
} | {
useSwcPlugin: false;
istanbul?: {
include?: string[];
exclude?: string[];
extension?: RegExp;
} & IstanbulOptionsWebpack;
};
Loading

0 comments on commit 1e3cf2f

Please sign in to comment.