1 |
|
2 | import * as pirates from "pirates";
|
3 |
|
4 | import { transform} from "./index";
|
5 |
|
6 | export 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 |
|
22 | export function registerJS() {
|
23 | addHook(".js", {transforms: ["imports", "flow", "jsx"]});
|
24 | }
|
25 |
|
26 | export function registerJSX() {
|
27 | addHook(".jsx", {transforms: ["imports", "flow", "jsx"]});
|
28 | }
|
29 |
|
30 | export function registerTS() {
|
31 | addHook(".ts", {transforms: ["imports", "typescript"]});
|
32 | }
|
33 |
|
34 | export function registerTSX() {
|
35 | addHook(".tsx", {transforms: ["imports", "typescript", "jsx"]});
|
36 | }
|
37 |
|
38 | export function registerTSLegacyModuleInterop() {
|
39 | addHook(".ts", {
|
40 | transforms: ["imports", "typescript"],
|
41 | enableLegacyTypeScriptModuleInterop: true,
|
42 | });
|
43 | }
|
44 |
|
45 | export function registerTSXLegacyModuleInterop() {
|
46 | addHook(".tsx", {
|
47 | transforms: ["imports", "typescript", "jsx"],
|
48 | enableLegacyTypeScriptModuleInterop: true,
|
49 | });
|
50 | }
|
51 |
|
52 | export function registerAll() {
|
53 | registerJS();
|
54 | registerJSX();
|
55 | registerTS();
|
56 | registerTSX();
|
57 | }
|