UNPKG

2.59 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }var _pirates = require('pirates'); var pirates = _interopRequireWildcard(_pirates);
2
3var _index = require('./index');
4
5
6
7
8
9
10
11
12 function addHook(
13 extension,
14 options,
15 hookOptions,
16) {
17 return pirates.addHook(
18 (code, filePath) => {
19 const {code: transformedCode, sourceMap} = _index.transform.call(void 0, code, {
20 ...options,
21 sourceMapOptions: {compiledFilename: filePath},
22 filePath,
23 });
24 const mapBase64 = Buffer.from(JSON.stringify(sourceMap)).toString("base64");
25 const suffix = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${mapBase64}`;
26 return `${transformedCode}\n${suffix}`;
27 },
28 {...hookOptions, exts: [extension]},
29 );
30} exports.addHook = addHook;
31
32 function registerJS(hookOptions) {
33 return addHook(".js", {transforms: ["imports", "flow", "jsx"]}, hookOptions);
34} exports.registerJS = registerJS;
35
36 function registerJSX(hookOptions) {
37 return addHook(".jsx", {transforms: ["imports", "flow", "jsx"]}, hookOptions);
38} exports.registerJSX = registerJSX;
39
40 function registerTS(hookOptions) {
41 return addHook(".ts", {transforms: ["imports", "typescript"]}, hookOptions);
42} exports.registerTS = registerTS;
43
44 function registerTSX(hookOptions) {
45 return addHook(".tsx", {transforms: ["imports", "typescript", "jsx"]}, hookOptions);
46} exports.registerTSX = registerTSX;
47
48 function registerTSLegacyModuleInterop(hookOptions) {
49 return addHook(
50 ".ts",
51 {
52 transforms: ["imports", "typescript"],
53 enableLegacyTypeScriptModuleInterop: true,
54 },
55 hookOptions,
56 );
57} exports.registerTSLegacyModuleInterop = registerTSLegacyModuleInterop;
58
59 function registerTSXLegacyModuleInterop(hookOptions) {
60 return addHook(
61 ".tsx",
62 {
63 transforms: ["imports", "typescript", "jsx"],
64 enableLegacyTypeScriptModuleInterop: true,
65 },
66 hookOptions,
67 );
68} exports.registerTSXLegacyModuleInterop = registerTSXLegacyModuleInterop;
69
70 function registerAll(hookOptions) {
71 const reverts = [
72 registerJS(hookOptions),
73 registerJSX(hookOptions),
74 registerTS(hookOptions),
75 registerTSX(hookOptions),
76 ];
77
78 return () => {
79 for (const fn of reverts) {
80 fn();
81 }
82 };
83} exports.registerAll = registerAll;