11// @ts -check
2- import path from "path" ;
3- import fs from "fs" ;
2+ import { CancelToken } from "@esfx/canceltoken" ;
3+ import chalk from "chalk" ;
4+ import chokidar from "chokidar" ;
45import del from "del" ;
5- import { task } from "hereby" ;
6+ import esbuild from "esbuild" ;
7+ import { EventEmitter } from "events" ;
8+ import fs from "fs" ;
9+ import fsExtra from "fs-extra" ;
610import _glob from "glob" ;
11+ import { task } from "hereby" ;
12+ import path from "path" ;
713import util from "util" ;
8- import chalk from "chalk" ;
9- import fsExtra from "fs-extra" ;
10- import { Debouncer , Deferred , exec , getDiffTool , getDirSize , memoize , needsUpdate , readJson } from "./scripts/build/utils.mjs" ;
11- import { localBaseline , localRwcBaseline , refBaseline , refRwcBaseline , runConsoleTests } from "./scripts/build/tests.mjs" ;
12- import { buildProject , cleanProject , watchProject } from "./scripts/build/projects.mjs" ;
14+
1315import { localizationDirectories } from "./scripts/build/localization.mjs" ;
1416import cmdLineOptions from "./scripts/build/options.mjs" ;
15- import esbuild from "esbuild" ;
16- import chokidar from "chokidar" ;
17- import { EventEmitter } from "events" ;
18- import { CancelToken } from "@esfx/canceltoken" ;
17+ import { buildProject , cleanProject , watchProject } from "./scripts/build/projects.mjs" ;
18+ import { localBaseline , localRwcBaseline , refBaseline , refRwcBaseline , runConsoleTests } from "./scripts/build/tests.mjs" ;
19+ import { Debouncer , Deferred , exec , getDiffTool , getDirSize , memoize , needsUpdate , readJson } from "./scripts/build/utils.mjs" ;
1920
2021const glob = util . promisify ( _glob ) ;
2122
@@ -59,10 +60,7 @@ export const generateLibs = task({
5960
6061 for ( const source of lib . sources ) {
6162 const contents = await fs . promises . readFile ( source , "utf-8" ) ;
62- // TODO(jakebailey): "\n\n" is for compatibility with our current tests; our test baselines
63- // are sensitive to the positions of things in the lib files. Eventually remove this,
64- // or remove lib.d.ts line numbers from our baselines.
65- output += "\n\n" + contents . replace ( / \r \n / g, "\n" ) ;
63+ output += "\n" + contents . replace ( / \r \n / g, "\n" ) ;
6664 }
6765
6866 await fs . promises . writeFile ( lib . target , output ) ;
@@ -182,28 +180,6 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
182180 packages : "external" ,
183181 logLevel : "warning" ,
184182 // legalComments: "none", // If we add copyright headers to the source files, uncomment.
185- plugins : [
186- {
187- name : "fix-require" ,
188- setup : ( build ) => {
189- build . onEnd ( async ( ) => {
190- // esbuild converts calls to "require" to "__require"; this function
191- // calls the real require if it exists, or throws if it does not (rather than
192- // throwing an error like "require not defined"). But, since we want typescript
193- // to be consumable by other bundlers, we need to convert these calls back to
194- // require so our imports are visible again.
195- //
196- // The leading spaces are to keep the offsets the same within the files to keep
197- // source maps working (though this only really matters for the line the require is on).
198- //
199- // See: https://github.com/evanw/esbuild/issues/1905
200- let contents = await fs . promises . readFile ( outfile , "utf-8" ) ;
201- contents = contents . replace ( / _ _ r e q u i r e \( / g, " require(" ) ;
202- await fs . promises . writeFile ( outfile , contents ) ;
203- } ) ;
204- } ,
205- }
206- ]
207183 } ;
208184
209185 if ( taskOptions . exportIsTsObject ) {
@@ -213,6 +189,30 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
213189 options . globalName = "ts" ;
214190 // If we are in a CJS context, export the ts namespace.
215191 options . footer = { js : `\nif (typeof module !== "undefined" && module.exports) { module.exports = ts; }` } ;
192+
193+ // esbuild converts calls to "require" to "__require"; this function
194+ // calls the real require if it exists, or throws if it does not (rather than
195+ // throwing an error like "require not defined"). But, since we want typescript
196+ // to be consumable by other bundlers, we need to convert these calls back to
197+ // require so our imports are visible again.
198+ //
199+ // The leading spaces are to keep the offsets the same within the files to keep
200+ // source maps working (though this only really matters for the line the require is on).
201+ //
202+ // See: https://github.com/evanw/esbuild/issues/1905
203+ options . define = { require : "$$require" } ;
204+ options . plugins = [
205+ {
206+ name : "fix-require" ,
207+ setup : ( build ) => {
208+ build . onEnd ( async ( ) => {
209+ let contents = await fs . promises . readFile ( outfile , "utf-8" ) ;
210+ contents = contents . replace ( / \$ \$ r e q u i r e / g, " require" ) ;
211+ await fs . promises . writeFile ( outfile , contents ) ;
212+ } ) ;
213+ } ,
214+ }
215+ ] ;
216216 }
217217
218218 return options ;
@@ -515,6 +515,7 @@ const { main: watchGuard, watch: watchWatchGuard } = entrypointBuildTask({
515515export const generateTypesMap = task ( {
516516 name : "generate-types-map" ,
517517 run : async ( ) => {
518+ await fs . promises . mkdir ( "./built/local" , { recursive : true } ) ;
518519 const source = "src/server/typesMap.json" ;
519520 const target = "built/local/typesMap.json" ;
520521 const contents = await fs . promises . readFile ( source , "utf-8" ) ;
0 commit comments