Skip to content

Commit

Permalink
Merge pull request #3356 from jakebailey/redo-require-removal
Browse files Browse the repository at this point in the history
Remove regex replacements from import-typescript
  • Loading branch information
alexdima authored Oct 18, 2022
2 parents 8fc2ca5 + 30a85db commit 6e995f7
Show file tree
Hide file tree
Showing 6 changed files with 1,230 additions and 129 deletions.
96 changes: 13 additions & 83 deletions build/importTypescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,73 +35,18 @@ const TYPESCRIPT_LIB_DESTINATION = path.join(REPO_ROOT, 'src/language/typescript
export const typescriptVersion = "${typeScriptDependencyVersion}";\n`
);

let tsServices = fs
.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.js'))
.toString();
let tsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescript.js')).toString();

// Ensure we never run into the node system...
// (this also removes require calls that trick webpack into shimming those modules...)
tsServices = tsServices.replace(
/\n ts\.sys =([^]*)\n \}\)\(\);/m,
`\n // MONACOCHANGE\n ts.sys = undefined;\n // END MONACOCHANGE`
);

// Eliminate more require() calls...
tsServices = tsServices.replace(
/^( +)etwModule = require\(.*$/m,
'$1// MONACOCHANGE\n$1etwModule = undefined;\n$1// END MONACOCHANGE'
);
tsServices = tsServices.replace(
/^( +)var result = ts\.sys\.require\(.*$/m,
'$1// MONACOCHANGE\n$1var result = undefined;\n$1// END MONACOCHANGE'
);
tsServices = tsServices.replace(
/^( +)fs = require\("fs"\);$/m,
'$1// MONACOCHANGE\n$1fs = undefined;\n$1// END MONACOCHANGE'
);
tsServices = tsServices.replace(
/^( +)debugger;$/m,
'$1// MONACOCHANGE\n$1// debugger;\n$1// END MONACOCHANGE'
);
tsServices = tsServices.replace(
/= require\("perf_hooks"\)/m,
'/* MONACOCHANGE */= {}/* END MONACOCHANGE */'
);
tsServices = tsServices.replace(
/typeof require === "function"/m,
'/* MONACOCHANGE */false/* END MONACOCHANGE */'
);

tsServices = tsServices.replace(
/module.exports = ts;/m,
'/* MONACOCHANGE */ /*module.exports = ts;*/ /* END MONACOCHANGE */'
);

// Flag any new require calls (outside comments) so they can be corrected preemptively.
// To avoid missing cases (or using an even more complex regex), temporarily remove comments
// about require() and then check for lines actually calling require().
// \/[*/] matches the start of a comment (single or multi-line).
// ^\s+\*[^/] matches (presumably) a later line of a multi-line comment.
const tsServicesNoCommentedRequire = tsServices.replace(
/(\/[*/]|^\s+\*[^/]).*\brequire\(.*/gm,
''
);
const linesWithRequire = tsServicesNoCommentedRequire.match(/^.*?\brequire\(.*$/gm);

// Allow error messages to include references to require() in their strings
const runtimeRequires =
linesWithRequire &&
linesWithRequire.filter((l) => !l.includes(': diag(') && !l.includes('ts.DiagnosticCategory'));

if (runtimeRequires && runtimeRequires.length && linesWithRequire) {
console.error(
'Found new require() calls on the following lines. These should be removed to avoid breaking webpack builds.\n'
);
console.error(
runtimeRequires.map((r) => `${r} (${tsServicesNoCommentedRequire.indexOf(r)})`).join('\n')
);
process.exit(1);
}
// The output from this build will only be accessible via AMD or ESM; rather than removing
// references to require/module, define them as dummy variables that bundlers will ignore.
// The TS code can figure out that it's not running under Node even with these defined.
tsServices =
`
/* MONACOCHANGE */
var require = undefined;
var module = { exports: {} };
/* END MONACOCHANGE */
` + tsServices;

const tsServices_amd =
generatedNote +
Expand All @@ -118,15 +63,6 @@ define("vs/language/typescript/lib/typescriptServices", [], function() { return
stripSourceMaps(tsServices_amd)
);

// Remove pattern that creates warnings with esbuild
// e.g.
// > /src/typescript/lib/typescriptServices.js:20:21: warning: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
// 20 │ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
// ╵ ~~~~
//

tsServices = tsServices.replace(/\nvar ([^ ]+) = \(this && this\.([^)]+)\) \|\|/gm, '\nvar $1 =');

const tsServices_esm =
generatedNote +
tsServices +
Expand All @@ -149,14 +85,8 @@ export var typescript = ts;
stripSourceMaps(tsServices_esm)
);

let dtsServices = fs
.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.d.ts'))
.toString();
dtsServices += `
// MONACOCHANGE
export = ts;
// END MONACOCHANGE
`;
let dtsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescript.d.ts')).toString();

fs.writeFileSync(
path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'),
generatedNote + dtsServices
Expand Down
3 changes: 2 additions & 1 deletion build/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"noEmit": true,
"module": "CommonJS",
"esModuleInterop": true
},
"files": ["./**/*"]
"include": ["./**/*"]
}
2 changes: 2 additions & 0 deletions build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export function buildESM(options: { base: string; entryPoints: string[]; externa
bundle: true,
target: 'esnext',
format: 'esm',
drop: ['debugger'],
define: {
AMD: 'false'
},
Expand Down Expand Up @@ -141,6 +142,7 @@ function buildOneAMD(
bundle: true,
target: 'esnext',
format: 'iife',
drop: ['debugger'],
define: {
AMD: 'true'
},
Expand Down
Loading

0 comments on commit 6e995f7

Please sign in to comment.