rollup-plugin-data-qa
Version:
[](https://www.npmjs.com/package/rollup-plugin-data-qa) [](https://github.com/semantic-releas
59 lines (58 loc) • 2.04 kB
JavaScript
import { DEFAULT_STYLED_COMPONENT_NAMES, TRANSFORM_HOOK_ID_FILTER } from "./pluginConstants.js";
import createModuleFilter from "./utils/createModuleFilter/index.js";
import getParseOptions from "./utils/getParseOptions/index.js";
import mightNeedTransform from "./utils/mightNeedTransform/index.js";
import shouldProcessModule from "./utils/shouldProcessModule/index.js";
import transformAst from "./utils/transformAst/index.js";
import MagicString from "magic-string";
//#region src/index.ts
const getModuleId = (id) => id.split("?")[0];
const matchesTransformHookId = (id) => TRANSFORM_HOOK_ID_FILTER.some((pattern) => pattern.test(getModuleId(id)));
const injectDataQa = ({ format = "paramCase", include = [], exclude = [], childOverrideParent, options: { disabledReactFunctionComponent, disabledStyledComponent, styledComponentNames = DEFAULT_STYLED_COMPONENT_NAMES } = {} } = {}) => {
const moduleFilter = createModuleFilter({
include,
exclude
});
const transformModule = function transformModule(code, id) {
const moduleId = getModuleId(id);
if (!shouldProcessModule(moduleId)) return null;
if (!moduleFilter(id)) return null;
if (!mightNeedTransform(code, {
disabledReactFunctionComponent,
disabledStyledComponent
})) return null;
try {
const ast = this.parse(code, getParseOptions(moduleId));
const magicString = new MagicString(code);
transformAst({
ast,
code: magicString,
format,
childOverrideParent,
disabledReactFunctionComponent,
disabledStyledComponent,
styledComponentNames
});
if (!magicString.hasChanged()) return null;
return {
code: magicString.toString(),
map: magicString.generateMap({
file: id,
includeContent: true,
hires: true
})
};
} catch (error) {
this.warn(`${id} - ${error}`);
}
};
return {
name: "rollup-plugin-data-qa",
transform(code, id) {
if (!matchesTransformHookId(id)) return null;
return transformModule.call(this, code, id) || null;
}
};
};
//#endregion
export { injectDataQa };