Skip to content

Commit

Permalink
feat: return revert function for registers (#604)
Browse files Browse the repository at this point in the history
antfu authored Apr 11, 2021
1 parent 38b66f3 commit debd5ca
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions src/register.ts
Original file line number Diff line number Diff line change
@@ -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,
@@ -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"],
@@ -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"],
@@ -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();
});
};
}

0 comments on commit debd5ca

Please sign in to comment.