UNPKG

1.47 kBJavaScriptView Raw
1// @ts-ignore: no types available.
2import * as pirates from "pirates";
3
4import { transform} from "./index";
5
6export function addHook(extension, options) {
7 pirates.addHook(
8 (code, filePath) => {
9 const {code: transformedCode, sourceMap} = transform(code, {
10 ...options,
11 sourceMapOptions: {compiledFilename: filePath},
12 filePath,
13 });
14 const mapBase64 = Buffer.from(JSON.stringify(sourceMap)).toString("base64");
15 const suffix = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${mapBase64}`;
16 return `${transformedCode}\n${suffix}`;
17 },
18 {exts: [extension]},
19 );
20}
21
22export function registerJS() {
23 addHook(".js", {transforms: ["imports", "flow", "jsx"]});
24}
25
26export function registerJSX() {
27 addHook(".jsx", {transforms: ["imports", "flow", "jsx"]});
28}
29
30export function registerTS() {
31 addHook(".ts", {transforms: ["imports", "typescript"]});
32}
33
34export function registerTSX() {
35 addHook(".tsx", {transforms: ["imports", "typescript", "jsx"]});
36}
37
38export function registerTSLegacyModuleInterop() {
39 addHook(".ts", {
40 transforms: ["imports", "typescript"],
41 enableLegacyTypeScriptModuleInterop: true,
42 });
43}
44
45export function registerTSXLegacyModuleInterop() {
46 addHook(".tsx", {
47 transforms: ["imports", "typescript", "jsx"],
48 enableLegacyTypeScriptModuleInterop: true,
49 });
50}
51
52export function registerAll() {
53 registerJS();
54 registerJSX();
55 registerTS();
56 registerTSX();
57}