Skip to content

Commit

Permalink
Merge ce826a3 into 38b66f3
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Mar 27, 2021
2 parents 38b66f3 + ce826a3 commit c5591a7
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
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();
});
};
}

0 comments on commit c5591a7

Please sign in to comment.