-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lib, register): use esbuild for register. Implement fragments
- Loading branch information
1 parent
f518465
commit b220a8e
Showing
12 changed files
with
104 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,80 @@ | ||
const { addHook } = require("pirates"); | ||
const { transform } = require("sucrase"); | ||
|
||
const jsxOptions = { | ||
production: true, | ||
transforms: ["imports", "typescript", "jsx"], | ||
jsxPragma: "v", | ||
jsxFragmentPragma: "v" | ||
}; | ||
const { transformSync } = require("esbuild"); | ||
const fs = require("fs"); | ||
|
||
addHook( | ||
(code, filePath) => { | ||
let { code: transformed, sourceMap } = transform(code, { | ||
sourceMapOptions: { compiledFilename: filePath }, | ||
filePath, | ||
...jsxOptions | ||
}); | ||
let mapBase64 = Buffer.from(JSON.stringify(sourceMap)).toString("base64"); | ||
let suffix = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${mapBase64}`; | ||
return `${transformed}\n${suffix}`; | ||
let fileName = filePath.split("/").pop(); | ||
let extension = fileName.split(".").pop(); | ||
|
||
let loader = "default"; | ||
if (["js", "jsx", "ts", "tsx", "css", "json", "txt"].includes(extension)) { | ||
if (["js", "jsx", "mjs"].includes(extension)) { | ||
loader = "jsx"; | ||
} else if (["ts", "tsx"].includes(extension)) { | ||
loader = "tsx"; | ||
} else if (extension === "txt") { | ||
loader = "text"; | ||
} else { | ||
loader = extension; | ||
} | ||
} else if (["jpeg", "jpg", "png", "gif", "webp", "svg"].includes(extension)) { | ||
loader = "dataurl"; | ||
} | ||
|
||
let options = { | ||
tsconfigRaw: { | ||
compilerOptions: { | ||
target: "ESNEXT", | ||
module: "ESNEXT", | ||
strict: true, | ||
allowSyntheticDefaultImports: true, | ||
allowJs: true, | ||
esModuleInterop: true, | ||
resolveJsonModule: true | ||
} | ||
}, | ||
loader, | ||
format: "cjs", | ||
target: "esnext", | ||
logLevel: "warning", | ||
jsxFactory: "v", | ||
jsxFragment: "v.fragment" | ||
}; | ||
|
||
// Check if tsconfig.json exists with fs module | ||
if ((extension === "ts" || extension === "tsx") && fs.existsSync(process.cwd() + "/tsconfig.json")) { | ||
let tsconfig = fs.readFileSync(process.cwd() + "/tsconfig.json", "utf8"); | ||
|
||
let tsconfigRaw = JSON.parse(tsconfig); | ||
let compilerOptions = tsconfigRaw.compilerOptions || {}; | ||
|
||
options.tsconfigRaw = { ...options.tsconfigRaw, ...tsconfigRaw }; | ||
options.tsconfigRaw.compilerOptions = { ...options.tsconfigRaw.compilerOptions, ...compilerOptions }; | ||
|
||
if (compilerOptions.target) { | ||
options.target = compilerOptions.target.toLowerCase(); | ||
} | ||
|
||
if (compilerOptions.module) { | ||
let format = compilerOptions.module.toLowerCase(); | ||
if (format === "commonjs") { | ||
options.format = "cjs"; | ||
} else if (format.startsWith("es")) { | ||
options.format = "esm"; | ||
} | ||
} | ||
} | ||
|
||
let { code: transformed } = transformSync(code, options); | ||
if (/"use strict"\;/gi.test(code) === false) { | ||
transformed = '"use strict";\n' + transformed; | ||
} | ||
|
||
return transformed; | ||
}, | ||
{ exts: [".js", ".jsx", ".ts", ".tsx", ".mjs"] } | ||
{ | ||
exts: [".js", ".jsx", ".ts", ".tsx", ".mjs", ".css", ".json", ".text", ".jpeg", ".jpg", ".png", ".gif", ".webp", ".svg", ".html"], | ||
ignoreNodeModules: true | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.