1+ /* eslint-disable */
12// @ts -check
23
4+ /** Run via:
5+ node scripts/createPlaygroundBuild.js
6+ */
7+
38// This script does two things:
49//
510// - Listens to changes to the built version of TypeScript (via a filewatcher on `built/local/typescriptServices.js`)
@@ -20,11 +25,12 @@ const fs = require('fs');
2025const child_process = require ( 'child_process' ) ;
2126const http = require ( 'http' ) ;
2227const url = require ( 'url' ) ;
28+ const nodeFetch = require ( "node-fetch" ) . default
2329
2430function updateTSDist ( ) {
2531 // This code is a direct port of a script from monaco-typescript
2632 // https://github.com/microsoft/monaco-typescript/blob/master/scripts/importTypescript.js
27- // Currently based on 778ace1 on Apr 25 2020
33+ // Currently based on cc8da6b on June 6 2021
2834
2935 const generatedNote = `//
3036 // **NOTE**: Do not edit directly! This file is generated using \`npm run import-typescript\`
@@ -42,61 +48,94 @@ function updateTSDist() {
4248 }
4349 importLibs ( ) ;
4450
45- const npmLsOutput = JSON . parse ( child_process . execSync ( "npm ls typescript --depth=0 --json=true" ) . toString ( ) ) ;
46- const typeScriptDependencyVersion = npmLsOutput . dependencies . typescript . version ;
51+ const npmLsOutput = JSON . parse (
52+ child_process . execSync ( 'npm ls typescript --depth=0 --json=true' ) . toString ( )
53+ ) ;
54+ const typeScriptDependencyVersion = npmLsOutput . version ;
4755
4856 fs . writeFileSync (
4957 path . join ( TYPESCRIPT_LIB_DESTINATION , 'typescriptServicesMetadata.ts' ) ,
5058 `${ generatedNote }
5159 export const typescriptVersion = "${ typeScriptDependencyVersion } ";\n`
5260 ) ;
5361
54- var tsServices = fs . readFileSync ( path . join ( TYPESCRIPT_LIB_SOURCE , 'typescriptServices.js' ) ) . toString ( ) ;
62+ let tsServices = fs
63+ . readFileSync ( path . join ( TYPESCRIPT_LIB_SOURCE , 'typescriptServices.js' ) )
64+ . toString ( ) ;
5565
5666 // Ensure we never run into the node system...
5767 // (this also removes require calls that trick webpack into shimming those modules...)
58- tsServices = (
59- tsServices . replace ( / \n t s \. s y s = ( [ ^ ] * ) \n \} \) \( \) ; / m, `\n // MONACOCHANGE\n ts.sys = undefined;\n // END MONACOCHANGE` )
68+ tsServices = tsServices . replace (
69+ / \n t s \. s y s = ( [ ^ ] * ) \n \} \) \( \) ; / m,
70+ `\n // MONACOCHANGE\n ts.sys = undefined;\n // END MONACOCHANGE`
6071 ) ;
6172
6273 // Eliminate more require() calls...
63- tsServices = tsServices . replace ( / ^ ( + ) e t w M o d u l e = r e q u i r e \( .* $ / m, '$1// MONACOCHANGE\n$1etwModule = undefined;\n$1// END MONACOCHANGE' ) ;
64- tsServices = tsServices . replace ( / ^ ( + ) v a r r e s u l t = t s \. s y s \. r e q u i r e \( .* $ / m, '$1// MONACOCHANGE\n$1var result = undefined;\n$1// END MONACOCHANGE' ) ;
74+ tsServices = tsServices . replace (
75+ / ^ ( + ) e t w M o d u l e = r e q u i r e \( .* $ / m,
76+ '$1// MONACOCHANGE\n$1etwModule = undefined;\n$1// END MONACOCHANGE'
77+ ) ;
78+ tsServices = tsServices . replace (
79+ / ^ ( + ) v a r r e s u l t = t s \. s y s \. r e q u i r e \( .* $ / m,
80+ '$1// MONACOCHANGE\n$1var result = undefined;\n$1// END MONACOCHANGE'
81+ ) ;
82+ tsServices = tsServices . replace (
83+ / ^ ( + ) f s = r e q u i r e \( " f s " \) ; $ / m,
84+ '$1// MONACOCHANGE\n$1fs = undefined;\n$1// END MONACOCHANGE'
85+ ) ;
86+ tsServices = tsServices . replace (
87+ / ^ ( + ) d e b u g g e r ; $ / m,
88+ '$1// MONACOCHANGE\n$1// debugger;\n$1// END MONACOCHANGE'
89+ ) ;
90+ tsServices = tsServices . replace (
91+ / = r e q u i r e \( " p e r f _ h o o k s " \) / m,
92+ '/* MONACOCHANGE */= {}/* END MONACOCHANGE */'
93+ ) ;
6594
6695 // Flag any new require calls (outside comments) so they can be corrected preemptively.
6796 // To avoid missing cases (or using an even more complex regex), temporarily remove comments
6897 // about require() and then check for lines actually calling require().
6998 // \/[*/] matches the start of a comment (single or multi-line).
7099 // ^\s+\*[^/] matches (presumably) a later line of a multi-line comment.
71- const tsServicesNoCommentedRequire = tsServices . replace ( / ( \/ [ * / ] | ^ \s + \* [ ^ / ] ) .* \b r e q u i r e \( .* / gm, '' ) ;
72- const linesWithRequire = tsServicesNoCommentedRequire . match ( / ^ .* ?\b r e q u i r e \( .* $ / gm)
100+ const tsServicesNoCommentedRequire = tsServices . replace (
101+ / ( \/ [ * / ] | ^ \s + \* [ ^ / ] ) .* \b r e q u i r e \( .* / gm,
102+ ''
103+ ) ;
104+ const linesWithRequire = tsServicesNoCommentedRequire . match ( / ^ .* ?\b r e q u i r e \( .* $ / gm) ;
73105
74106 // Allow error messages to include references to require() in their strings
75- const runtimeRequires = linesWithRequire && linesWithRequire . filter ( l => ! l . includes ( ": diag(" ) )
76-
77- if ( runtimeRequires && runtimeRequires . length ) {
78- console . error ( 'Found new require() calls on the following lines. These should be removed to avoid breaking webpack builds.\n' ) ;
79- console . error ( linesWithRequire . join ( '\n' ) ) ;
107+ const runtimeRequires =
108+ linesWithRequire &&
109+ linesWithRequire . filter ( ( l ) => ! l . includes ( ': diag(' ) && ! l . includes ( 'ts.DiagnosticCategory' ) ) ;
110+
111+ if ( runtimeRequires && runtimeRequires . length && linesWithRequire ) {
112+ console . error (
113+ 'Found new require() calls on the following lines. These should be removed to avoid breaking webpack builds.\n'
114+ ) ;
115+ console . error (
116+ runtimeRequires . map ( ( r ) => `${ r } (${ tsServicesNoCommentedRequire . indexOf ( r ) } )` ) . join ( '\n' )
117+ ) ;
80118 process . exit ( 1 ) ;
81119 }
82120
83- // Make sure process.args don't get called in the browser, this
84- // should only happen in TS 2.6.2
85- const beforeProcess = `ts.perfLogger.logInfoEvent("Starting TypeScript v" + ts.versionMajorMinor + " with command line: " + JSON.stringify(process.argv));`
86- const afterProcess = `// MONACOCHANGE\n ts.perfLogger.logInfoEvent("Starting TypeScript v" + ts.versionMajorMinor + " with command line: " + JSON.stringify([]));\n// END MONACOCHANGE`
87- tsServices = tsServices . replace ( beforeProcess , afterProcess ) ;
88-
89- var tsServices_amd = generatedNote + tsServices +
121+ const tsServices_amd =
122+ generatedNote +
123+ tsServices +
90124 `
91125 // MONACOCHANGE
92126 // Defining the entire module name because r.js has an issue and cannot bundle this file
93127 // correctly with an anonymous define call
94128 define("vs/language/typescript/lib/typescriptServices", [], function() { return ts; });
95129 // END MONACOCHANGE
96130 ` ;
97- fs . writeFileSync ( path . join ( TYPESCRIPT_LIB_DESTINATION , 'typescriptServices-amd.js' ) , tsServices_amd ) ;
131+ fs . writeFileSync (
132+ path . join ( TYPESCRIPT_LIB_DESTINATION , 'typescriptServices-amd.js' ) ,
133+ stripSourceMaps ( tsServices_amd )
134+ ) ;
98135
99- var tsServices_esm = generatedNote + tsServices +
136+ const tsServices_esm =
137+ generatedNote +
138+ tsServices +
100139 `
101140 // MONACOCHANGE
102141 export var createClassifier = ts.createClassifier;
@@ -110,144 +149,81 @@ function updateTSDist() {
110149 export var TokenClass = ts.TokenClass;
111150 // END MONACOCHANGE
112151 ` ;
113- fs . writeFileSync ( path . join ( TYPESCRIPT_LIB_DESTINATION , 'typescriptServices.js' ) , tsServices_esm ) ;
152+ fs . writeFileSync (
153+ path . join ( TYPESCRIPT_LIB_DESTINATION , 'typescriptServices.js' ) ,
154+ stripSourceMaps ( tsServices_esm )
155+ ) ;
114156
115- var dtsServices = fs . readFileSync ( path . join ( TYPESCRIPT_LIB_SOURCE , 'typescriptServices.d.ts' ) ) . toString ( ) ;
116- dtsServices +=
117- `
157+ let dtsServices = fs
158+ . readFileSync ( path . join ( TYPESCRIPT_LIB_SOURCE , 'typescriptServices.d.ts' ) )
159+ . toString ( ) ;
160+ dtsServices += `
118161 // MONACOCHANGE
119162 export = ts;
120163 // END MONACOCHANGE
121164 ` ;
122- fs . writeFileSync ( path . join ( TYPESCRIPT_LIB_DESTINATION , 'typescriptServices.d.ts' ) , generatedNote + dtsServices ) ;
123-
165+ fs . writeFileSync (
166+ path . join ( TYPESCRIPT_LIB_DESTINATION , 'typescriptServices.d.ts' ) ,
167+ generatedNote + dtsServices
168+ ) ;
124169 } ) ( ) ;
125170
126171 function importLibs ( ) {
127- function getFileName ( name ) {
128- return ( name === '' ? 'lib.d.ts' : `lib.${ name } .d.ts` ) ;
129- }
130- function getVariableName ( name ) {
131- return ( name === '' ? 'lib_dts' : `lib_${ name . replace ( / \. / g, '_' ) } _dts` ) ;
132- }
133172 function readLibFile ( name ) {
134- var srcPath = path . join ( TYPESCRIPT_LIB_SOURCE , getFileName ( name ) ) ;
173+ const srcPath = path . join ( TYPESCRIPT_LIB_SOURCE , name ) ;
135174 return fs . readFileSync ( srcPath ) . toString ( ) ;
136175 }
137176
138- var queue = [ ] ;
139- var in_queue = { } ;
140-
141- var enqueue = function ( name ) {
142- if ( in_queue [ name ] ) {
143- return ;
144- }
145- in_queue [ name ] = true ;
146- queue . push ( name ) ;
147- } ;
148-
149- enqueue ( '' ) ;
150- enqueue ( 'es2015' ) ;
151-
152- var result = [ ] ;
153- while ( queue . length > 0 ) {
154- var name = queue . shift ( ) ;
155- var contents = readLibFile ( name ) ;
156- var lines = contents . split ( / \r \n | \r | \n / ) ;
157-
158- var output = '' ;
159- var writeOutput = function ( text ) {
160- if ( output . length === 0 ) {
161- output = text ;
162- } else {
163- output += ` + ${ text } ` ;
164- }
165- } ;
166- var outputLines = [ ] ;
167- var flushOutputLines = function ( ) {
168- writeOutput ( `"${ escapeText ( outputLines . join ( '\n' ) ) } "` ) ;
169- outputLines = [ ] ;
170- } ;
171- var deps = [ ] ;
172- for ( let i = 0 ; i < lines . length ; i ++ ) {
173- let m = lines [ i ] . match ( / \/ \/ \/ \s * < r e f e r e n c e \s * l i b = " ( [ ^ " ] + ) " / ) ;
174- if ( m ) {
175- flushOutputLines ( ) ;
176- writeOutput ( getVariableName ( m [ 1 ] ) ) ;
177- deps . push ( getVariableName ( m [ 1 ] ) ) ;
178- enqueue ( m [ 1 ] ) ;
179- continue ;
180- }
181- outputLines . push ( lines [ i ] ) ;
182- }
183- flushOutputLines ( ) ;
184-
185- result . push ( {
186- name : getVariableName ( name ) ,
187- deps : deps ,
188- output : output
189- } ) ;
190- }
191-
192- var strResult = `/*---------------------------------------------------------------------------------------------
177+ let strLibResult = `/*---------------------------------------------------------------------------------------------
193178 * Copyright (c) Microsoft Corporation. All rights reserved.
194179 * Licensed under the MIT License. See License.txt in the project root for license information.
195180 *--------------------------------------------------------------------------------------------*/
196- ${ generatedNote } ` ;
197- // Do a topological sort
198- while ( result . length > 0 ) {
199- for ( let i = result . length - 1 ; i >= 0 ; i -- ) {
200- if ( result [ i ] . deps . length === 0 ) {
201- // emit this node
202- strResult += `\nexport const ${ result [ i ] . name } : string = ${ result [ i ] . output } ;\n` ;
203-
204- // mark dep as resolved
205- for ( let j = 0 ; j < result . length ; j ++ ) {
206- for ( let k = 0 ; k < result [ j ] . deps . length ; k ++ ) {
207- if ( result [ j ] . deps [ k ] === result [ i ] . name ) {
208- result [ j ] . deps . splice ( k , 1 ) ;
209- break ;
210- }
211- }
212- }
213-
214- // remove from result
215- result . splice ( i , 1 ) ;
216- break ;
217- }
218- }
181+ ${ generatedNote }
182+ /** Contains all the lib files */
183+ export const libFileMap: Record<string, string> = {}
184+ ` ;
185+ let strIndexResult = `/*---------------------------------------------------------------------------------------------
186+ * Copyright (c) Microsoft Corporation. All rights reserved.
187+ * Licensed under the MIT License. See License.txt in the project root for license information.
188+ *--------------------------------------------------------------------------------------------*/
189+ ${ generatedNote }
190+ /** Contains all the lib files */
191+ export const libFileSet: Record<string, boolean> = {}
192+ ` ;
193+ const dtsFiles = fs . readdirSync ( TYPESCRIPT_LIB_SOURCE ) . filter ( ( f ) => f . includes ( 'lib.' ) ) ;
194+ while ( dtsFiles . length > 0 ) {
195+ const name = dtsFiles . shift ( ) ;
196+ const output = readLibFile ( name ) . replace ( / \r \n / g, '\n' ) ;
197+ strLibResult += `libFileMap['${ name } '] = "${ escapeText ( output ) } ";\n` ;
198+ strIndexResult += `libFileSet['${ name } '] = true;\n` ;
219199 }
220200
221- strResult += `
222- /** This is the DTS which is used when the target is ES6 or below */
223- export const lib_es5_bundled_dts = lib_dts;
224-
225- /** This is the DTS which is used by default in monaco-typescript, and when the target is 2015 or above */
226- export const lib_es2015_bundled_dts = lib_es2015_dts + "" + lib_dom_dts + "" + lib_webworker_importscripts_dts + "" + lib_scripthost_dts + "";
227- `
228-
229- var dstPath = path . join ( TYPESCRIPT_LIB_DESTINATION , 'lib.ts' ) ;
230- fs . writeFileSync ( dstPath , strResult ) ;
201+ fs . writeFileSync ( path . join ( TYPESCRIPT_LIB_DESTINATION , 'lib.ts' ) , strLibResult ) ;
202+ fs . writeFileSync ( path . join ( TYPESCRIPT_LIB_DESTINATION , 'lib.index.ts' ) , strIndexResult ) ;
231203 }
232204
233205 /**
234206 * Escape text such that it can be used in a javascript string enclosed by double quotes (")
235207 */
236208 function escapeText ( text ) {
237209 // See http://www.javascriptkit.com/jsref/escapesequence.shtml
238- var _backspace = '\b' . charCodeAt ( 0 ) ;
239- var _formFeed = '\f' . charCodeAt ( 0 ) ;
240- var _newLine = '\n' . charCodeAt ( 0 ) ;
241- var _nullChar = 0 ;
242- var _carriageReturn = '\r' . charCodeAt ( 0 ) ;
243- var _tab = '\t' . charCodeAt ( 0 ) ;
244- var _verticalTab = '\v' . charCodeAt ( 0 ) ;
245- var _backslash = '\\' . charCodeAt ( 0 ) ;
246- var _doubleQuote = '"' . charCodeAt ( 0 ) ;
247-
248- var startPos = 0 , chrCode , replaceWith = null , resultPieces = [ ] ;
249-
250- for ( var i = 0 , len = text . length ; i < len ; i ++ ) {
210+ const _backspace = '\b' . charCodeAt ( 0 ) ;
211+ const _formFeed = '\f' . charCodeAt ( 0 ) ;
212+ const _newLine = '\n' . charCodeAt ( 0 ) ;
213+ const _nullChar = 0 ;
214+ const _carriageReturn = '\r' . charCodeAt ( 0 ) ;
215+ const _tab = '\t' . charCodeAt ( 0 ) ;
216+ const _verticalTab = '\v' . charCodeAt ( 0 ) ;
217+ const _backslash = '\\' . charCodeAt ( 0 ) ;
218+ const _doubleQuote = '"' . charCodeAt ( 0 ) ;
219+
220+ const len = text . length ;
221+ let startPos = 0 ;
222+ let chrCode ;
223+ let replaceWith = null ;
224+ let resultPieces = [ ] ;
225+
226+ for ( let i = 0 ; i < len ; i ++ ) {
251227 chrCode = text . charCodeAt ( i ) ;
252228 switch ( chrCode ) {
253229 case _backspace :
@@ -288,7 +264,10 @@ function updateTSDist() {
288264 resultPieces . push ( text . substring ( startPos , len ) ) ;
289265 return resultPieces . join ( '' ) ;
290266 }
291-
267+
268+ function stripSourceMaps ( str ) {
269+ return str . replace ( / \/ \/ # s o u r c e M a p p i n g U R L [ ^ \n ] + / gm, '' ) ;
270+ }
292271 /// End of import
293272}
294273
@@ -298,7 +277,18 @@ fs.watchFile(services, () =>{
298277 updateTSDist ( )
299278} )
300279
280+ // We need to re-direct non TSC JS requests back to a real copy of
281+ // monaco, so grab the list of official releases from the TS CDN
282+ // and use the latest stable release, as that is most likely the
283+ // closest version to your dev build
284+ let latestStable = "4.3.2"
285+ nodeFetch ( 'https://typescript.azureedge.net/indexes/releases.json' ) . then ( req => req . json ( ) ) . then ( releases => {
286+ latestStable = releases . versions . pop ( )
287+ } ) ;
288+
301289http . createServer ( function ( req , res ) {
290+ res . setHeader ( "Access-Control-Allow-Origin" , "*" )
291+
302292 const incoming = url . parse ( req . url )
303293 if ( incoming . path . endsWith ( "typescriptServices.js" ) ) {
304294 // Use the built version
@@ -308,7 +298,7 @@ http.createServer(function (req, res) {
308298 } else {
309299 // Redirect to the TS CDN
310300 res . writeHead ( 302 , {
311- 'Location' : `https://typescript.azureedge.net/cdn/3.9.2 /monaco/ ${ incoming . path } `
301+ 'Location' : `https://typescript.azureedge.net/cdn/${ latestStable } /monaco${ incoming . path } `
312302 } ) ;
313303 }
314304
@@ -317,6 +307,7 @@ http.createServer(function (req, res) {
317307
318308console . log ( "Starting servers\n" )
319309console . log ( " - [✓] file watcher: " + services )
310+ updateTSDist ( )
320311console . log ( " - [✓] http server: http://localhost:5615" )
321312
322313console . log ( "\n\nGet started: http://www.staging-typescript.org/play?ts=dev" )
0 commit comments