1
1
// @ts -check
2
2
import { CancelToken } from "@esfx/canceltoken" ;
3
- import assert from "assert" ;
4
3
import chalk from "chalk" ;
5
4
import chokidar from "chokidar" ;
6
5
import esbuild from "esbuild" ;
@@ -180,17 +179,15 @@ async function runDtsBundler(entrypoint, output) {
180
179
function createBundler ( entrypoint , outfile , taskOptions = { } ) {
181
180
const getOptions = memoize ( async ( ) => {
182
181
const copyright = await getCopyrightHeader ( ) ;
183
- const banner = taskOptions . exportIsTsObject ? "var ts = {}; ((module) => {" : "" ;
184
-
185
182
/** @type {esbuild.BuildOptions } */
186
183
const options = {
187
184
entryPoints : [ entrypoint ] ,
188
- banner : { js : copyright + banner } ,
185
+ banner : { js : copyright } ,
189
186
bundle : true ,
190
187
outfile,
191
188
platform : "node" ,
192
189
target : [ "es2020" , "node14.17" ] ,
193
- format : "cjs " ,
190
+ format : "esm " ,
194
191
sourcemap : "linked" ,
195
192
sourcesContent : false ,
196
193
treeShaking : taskOptions . treeShaking ,
@@ -203,63 +200,15 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
203
200
options . external = [ "./typescript.js" ] ;
204
201
options . plugins = options . plugins || [ ] ;
205
202
options . plugins . push ( {
206
- name : "remap-typescript-to-require " ,
203
+ name : "remap-typescript-to-public-api " ,
207
204
setup ( build ) {
208
205
build . onLoad ( { filter : / s r c [ \\ / ] t y p e s c r i p t [ \\ / ] t y p e s c r i p t \. t s $ / } , ( ) => {
209
- return { contents : `export * from "./typescript.js"` } ;
206
+ return { contents : `export * from "./typescript.js"` } ; // TODO(jakebailey): require(ESM) - this remapping can be fixed up
210
207
} ) ;
211
208
} ,
212
209
} ) ;
213
210
}
214
211
215
- if ( taskOptions . exportIsTsObject ) {
216
- // Monaco bundles us as ESM by wrapping our code with something that defines module.exports
217
- // but then does not use it, instead using the `ts` variable. Ensure that if we think we're CJS
218
- // that we still set `ts` to the module.exports object.
219
- options . footer = { js : `})(typeof module !== "undefined" && module.exports ? module : { exports: ts });\nif (typeof module !== "undefined" && module.exports) { ts = module.exports; }` } ;
220
-
221
- // esbuild converts calls to "require" to "__require"; this function
222
- // calls the real require if it exists, or throws if it does not (rather than
223
- // throwing an error like "require not defined"). But, since we want typescript
224
- // to be consumable by other bundlers, we need to convert these calls back to
225
- // require so our imports are visible again.
226
- //
227
- // To fix this, we redefine "require" to a name we're unlikely to use with the
228
- // same length as "require", then replace it back to "require" after bundling,
229
- // ensuring that source maps still work.
230
- //
231
- // See: https://github.com/evanw/esbuild/issues/1905
232
- const require = "require" ;
233
- const fakeName = "Q" . repeat ( require . length ) ;
234
- const fakeNameRegExp = new RegExp ( fakeName , "g" ) ;
235
- options . define = { [ require ] : fakeName } ;
236
-
237
- // For historical reasons, TypeScript does not set __esModule. Hack esbuild's __toCommonJS to be a noop.
238
- // We reference `__copyProps` to ensure the final bundle doesn't have any unreferenced code.
239
- const toCommonJsRegExp = / v a r _ _ t o C o m m o n J S .* / ;
240
- const toCommonJsRegExpReplacement = "var __toCommonJS = (mod) => (__copyProps, mod); // Modified helper to skip setting __esModule." ;
241
-
242
- options . plugins = options . plugins || [ ] ;
243
- options . plugins . push (
244
- {
245
- name : "post-process" ,
246
- setup : build => {
247
- build . onEnd ( async ( ) => {
248
- let contents = await fs . promises . readFile ( outfile , "utf-8" ) ;
249
- contents = contents . replace ( fakeNameRegExp , require ) ;
250
- let matches = 0 ;
251
- contents = contents . replace ( toCommonJsRegExp , ( ) => {
252
- matches ++ ;
253
- return toCommonJsRegExpReplacement ;
254
- } ) ;
255
- assert ( matches === 1 , "Expected exactly one match for __toCommonJS" ) ;
256
- await fs . promises . writeFile ( outfile , contents ) ;
257
- } ) ;
258
- } ,
259
- } ,
260
- ) ;
261
- }
262
-
263
212
return options ;
264
213
} ) ;
265
214
@@ -324,13 +273,13 @@ function entrypointBuildTask(options) {
324
273
} ) ;
325
274
326
275
/**
327
- * Writes a CJS module that reexports another CJS file. E.g. given
276
+ * Writes a module that reexports another file. E.g. given
328
277
* `options.builtEntrypoint = "./built/local/tsc/tsc.js"` and
329
278
* `options.output = "./built/local/tsc.js"`, this will create a file
330
279
* named "./built/local/tsc.js" containing:
331
280
*
332
281
* ```
333
- * module.exports = require( "./tsc/tsc.js")
282
+ * export * from "./tsc/tsc.js";
334
283
* ```
335
284
*/
336
285
const shim = task ( {
@@ -339,7 +288,7 @@ function entrypointBuildTask(options) {
339
288
const outDir = path . dirname ( options . output ) ;
340
289
await fs . promises . mkdir ( outDir , { recursive : true } ) ;
341
290
const moduleSpecifier = path . relative ( outDir , options . builtEntrypoint ) ;
342
- await fs . promises . writeFile ( options . output , `module.exports = require( "./${ moduleSpecifier . replace ( / [ \\ / ] / g, "/" ) } ") ` ) ;
291
+ await fs . promises . writeFile ( options . output , `export * from "./${ moduleSpecifier . replace ( / [ \\ / ] / g, "/" ) } "` ) ;
343
292
} ,
344
293
} ) ;
345
294
@@ -447,6 +396,7 @@ export const watchMin = task({
447
396
448
397
// This is technically not enough to make tsserverlibrary loadable in the
449
398
// browser, but it's unlikely that anyone has actually been doing that.
399
+ // TODO(jakebailey): require(ESM) - fix this
450
400
const lsslJs = `
451
401
if (typeof module !== "undefined" && module.exports) {
452
402
module.exports = require("./typescript.js");
@@ -504,7 +454,7 @@ const { main: tests, watch: watchTests } = entrypointBuildTask({
504
454
description : "Builds the test infrastructure" ,
505
455
buildDeps : [ generateDiagnostics ] ,
506
456
project : "src/testRunner" ,
507
- srcEntrypoint : "./src/testRunner/_namespaces/Harness .ts" ,
457
+ srcEntrypoint : "./src/testRunner/runner .ts" ,
508
458
builtEntrypoint : "./built/local/testRunner/runner.js" ,
509
459
output : testRunner ,
510
460
mainDeps : [ generateLibs ] ,
0 commit comments