Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return revert function for registers #604

Merged
merged 2 commits into from
Apr 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 33 additions & 19 deletions src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ export interface HookOptions {
ignoreNodeModules?: boolean;
}

export function addHook(extension: string, options: Options, hookOptions?: HookOptions): void {
pirates.addHook(
export type RevertFunction = () => void;

export function addHook(
extension: string,
options: Options,
hookOptions?: HookOptions,
): RevertFunction {
return pirates.addHook(
(code: string, filePath: string): string => {
const {code: transformedCode, sourceMap} = transform(code, {
...options,
Expand All @@ -23,24 +29,24 @@ export function addHook(extension: string, options: Options, hookOptions?: HookO
);
}

export function registerJS(hookOptions?: HookOptions): void {
addHook(".js", {transforms: ["imports", "flow", "jsx"]}, hookOptions);
export function registerJS(hookOptions?: HookOptions): RevertFunction {
return addHook(".js", {transforms: ["imports", "flow", "jsx"]}, hookOptions);
}

export function registerJSX(hookOptions?: HookOptions): void {
addHook(".jsx", {transforms: ["imports", "flow", "jsx"]}, hookOptions);
export function registerJSX(hookOptions?: HookOptions): RevertFunction {
return addHook(".jsx", {transforms: ["imports", "flow", "jsx"]}, hookOptions);
}

export function registerTS(hookOptions?: HookOptions): void {
addHook(".ts", {transforms: ["imports", "typescript"]}, hookOptions);
export function registerTS(hookOptions?: HookOptions): RevertFunction {
return addHook(".ts", {transforms: ["imports", "typescript"]}, hookOptions);
}

export function registerTSX(hookOptions?: HookOptions): void {
addHook(".tsx", {transforms: ["imports", "typescript", "jsx"]}, hookOptions);
export function registerTSX(hookOptions?: HookOptions): RevertFunction {
return addHook(".tsx", {transforms: ["imports", "typescript", "jsx"]}, hookOptions);
}

export function registerTSLegacyModuleInterop(hookOptions?: HookOptions): void {
addHook(
export function registerTSLegacyModuleInterop(hookOptions?: HookOptions): RevertFunction {
return addHook(
".ts",
{
transforms: ["imports", "typescript"],
Expand All @@ -50,8 +56,8 @@ export function registerTSLegacyModuleInterop(hookOptions?: HookOptions): void {
);
}

export function registerTSXLegacyModuleInterop(hookOptions?: HookOptions): void {
addHook(
export function registerTSXLegacyModuleInterop(hookOptions?: HookOptions): RevertFunction {
return addHook(
".tsx",
{
transforms: ["imports", "typescript", "jsx"],
Expand All @@ -61,9 +67,17 @@ export function registerTSXLegacyModuleInterop(hookOptions?: HookOptions): void
);
}

export function registerAll(hookOptions?: HookOptions): void {
registerJS(hookOptions);
registerJSX(hookOptions);
registerTS(hookOptions);
registerTSX(hookOptions);
export function registerAll(hookOptions?: HookOptions): RevertFunction {
const reverts = [
registerJS(hookOptions),
registerJSX(hookOptions),
registerTS(hookOptions),
registerTSX(hookOptions),
];

return () => {
reverts.forEach((fn) => {
fn();
});
};
}