Skip to content

Commit

Permalink
Reduce size of parser-typescript.js (prettier#13431)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and medikoo committed Jan 4, 2024
1 parent f870706 commit ab24851
Show file tree
Hide file tree
Showing 2 changed files with 486 additions and 55 deletions.
57 changes: 2 additions & 55 deletions scripts/build/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "node:path";
import { createRequire } from "node:module";
import createEsmUtils from "esm-utils";
import { PROJECT_ROOT } from "../utils/index.mjs";
import modifyTypescriptModule from "./modify-typescript-module.mjs";

const { require } = createEsmUtils(import.meta);

Expand Down Expand Up @@ -101,62 +102,8 @@ const parsers = [
},
{
module: require.resolve("typescript"),
process(text) {
return text.replace(
/(?<=\n)(?<indentString>\s+)function tryGetNodePerformanceHooks\(\) {.*?\n\k<indentString>}(?=\n)/s,
"function tryGetNodePerformanceHooks() {}"
);
},
process: modifyTypescriptModule,
},

...Object.entries({
// `typescript/lib/typescript.js` expose extra global objects
// `TypeScript`, `toolsVersion`, `globalThis`
'typeof process === "undefined" || process.browser': "false",
'typeof globalThis === "object"': "true",

"_fs.realpathSync.native":
"_fs.realpathSync && _fs.realpathSync.native",
// Remove useless `ts.sys`
"ts.sys = ": "ts.sys = undefined && ",

// Remove useless language service
"ts.realizeDiagnostics = ": "ts.realizeDiagnostics = undefined && ",
"ts.TypeScriptServicesFactory = ":
"ts.TypeScriptServicesFactory = undefined && ",
"var ShimBase = ": "var ShimBase = undefined && ",
"var TypeScriptServicesFactory = ":
"var TypeScriptServicesFactory = undefined && ",
"var LanguageServiceShimObject = ":
"var LanguageServiceShimObject = undefined && ",
"var CoreServicesShimHostAdapter = ":
"var CoreServicesShimHostAdapter = undefined && ",
"var LanguageServiceShimHostAdapter = ":
"var LanguageServiceShimHostAdapter = undefined && ",
"var ScriptSnapshotShimAdapter = ":
"var ScriptSnapshotShimAdapter = undefined && ",
"var ClassifierShimObject = ":
"var ClassifierShimObject = undefined && ",
"var CoreServicesShimObject = ":
"var CoreServicesShimObject = undefined && ",
"function simpleForwardCall(": "0 && function simpleForwardCall(",
"function forwardJSONCall(": "0 && function forwardJSONCall(",
"function forwardCall(": "0 && function forwardCall(",
"function realizeDiagnostics(": "0 && function realizeDiagnostics(",
"function realizeDiagnostic(": "0 && function realizeDiagnostic(",
"function convertClassifications(":
"0 && function convertClassifications(",

// Dynamic `require()`s
"ts.sys && ts.sys.require": "false",
"require(etwModulePath)": "undefined",
'require("source-map-support").install()': "",
"require(modulePath)": "undefined",
}).map(([find, replacement]) => ({
module: require.resolve("typescript"),
find,
replacement,
})),
{
module: require.resolve("debug/src/browser.js"),
path: require.resolve("./shims/debug.cjs"),
Expand Down

0 comments on commit ab24851

Please sign in to comment.