@storybook/addon-coverage
Version:
Tools to support code coverage in Storybook
153 lines (146 loc) • 5.5 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/preset.ts
var preset_exports = {};
__export(preset_exports, {
viteFinal: () => viteFinal,
webpackFinal: () => webpackFinal
});
module.exports = __toCommonJS(preset_exports);
// src/constants.ts
var commonExtensions = [".js", ".cjs", ".mjs", ".ts", ".cts", ".mts"];
var defaultExtensions = [
...commonExtensions,
".tsx",
".jsx",
".vue",
".svelte"
];
var testFileExtensions = defaultExtensions.map((extension) => extension.slice(1)).join(",");
var configFileExtensions = commonExtensions.map((extension) => extension.slice(1)).join(",");
var defaultExclude = [
"**/node_modules/**",
".storybook/**",
"coverage/**",
"packages/*/test{,s}/**",
"**/*.d.ts",
"**/*.mock.*",
"test{,s}/**",
`test{,-*}.{${testFileExtensions}}`,
`**/*{.,-}{spec,story,stories,types}.{${testFileExtensions}}`,
"**/__tests__/**",
"**/*-entry.js",
/* Exclude common development tool configuration files */
`**/{ava,babel,nyc}.config.{${configFileExtensions}}`,
`**/{jest,vitest}.config.{${configFileExtensions}}`,
`**/{karma,rollup,webpack,vite}.config.{${configFileExtensions}}`,
`**/.{eslint,mocha}rc.{${configFileExtensions}}`
];
// src/webpack5-exclude.ts
var import_test_exclude = __toESM(require("test-exclude"));
// src/nyc-config.ts
var import_load_nyc_config = require("@istanbuljs/load-nyc-config");
async function getNycConfig(opts = {}) {
const cwd = opts.cwd ?? process.cwd();
return (0, import_load_nyc_config.loadNycConfig)({
cwd,
nycrcPath: opts.nycrcPath
});
}
// src/webpack5-exclude.ts
async function createTestExclude(opts = {}) {
const { nycrcPath, include, exclude, extension } = opts;
const cwd = opts.cwd ?? process.cwd();
const nycConfig = await getNycConfig({
cwd,
nycrcPath
});
return new import_test_exclude.default({
cwd,
include: include ?? nycConfig.include,
exclude: exclude ?? nycConfig.exclude ?? defaultExclude,
extension: extension ?? nycConfig.extension ?? defaultExtensions,
excludeNodeModules: true
});
}
// src/preset.ts
var import_istanbul_lib_instrument = require("istanbul-lib-instrument");
var viteFinal = async (viteConfig, options) => {
const viteIstanbulPlugin = (await import("vite-plugin-istanbul")).default;
const istanbul = viteIstanbulPlugin ?? viteIstanbulPlugin.default;
console.log("[addon-coverage] Adding istanbul plugin to Vite config");
viteConfig.build = viteConfig.build || {};
viteConfig.build.sourcemap = true;
viteConfig.plugins ||= [];
viteConfig.plugins.push(
istanbul({
forceBuildInstrument: options.configType === "PRODUCTION",
...options.istanbul,
include: Array.from(options.istanbul?.include || []),
exclude: [
options.configDir + "/**",
...defaultExclude,
...Array.from(options.istanbul?.exclude || [])
],
extension: options.istanbul?.extension || defaultExtensions
})
);
return viteConfig;
};
var defaultOptions = {
preserveComments: true,
produceSourceMap: true,
autoWrap: true,
esModules: true,
compact: false
};
var webpackFinal = async (webpackConfig, options) => {
webpackConfig.module.rules ||= [];
const nycConfig = await getNycConfig(options.istanbul);
const extensions = options.istanbul?.extension ?? nycConfig.extension ?? defaultExtensions;
console.log("[addon-coverage] Adding istanbul loader to Webpack config");
const testExclude = await createTestExclude(options.istanbul);
let instrumenterOptions = Object.assign(defaultOptions, options.istanbul);
let instrumenter = (0, import_istanbul_lib_instrument.createInstrumenter)(instrumenterOptions);
webpackConfig.module.rules.unshift({
test: new RegExp(extensions?.join("|").replace(/\./g, "\\.")),
loader: require.resolve("./loader/webpack5-istanbul-loader"),
enforce: "post",
options: {
...options.istanbul ?? {},
instrumenter
},
include: (modulePath) => testExclude.shouldInstrument(modulePath)
});
return webpackConfig;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
viteFinal,
webpackFinal
});