@ -1,21 +1,23 @@
var gulp = require ( 'gulp' ) ;
var metadata = require ( './metadata' ) ;
var es = require ( 'event-stream' ) ;
var path = require ( 'path' ) ;
var fs = require ( 'fs' ) ;
var rimraf = require ( 'rimraf' ) ;
var cp = require ( 'child_process' ) ;
var httpServer = require ( 'http-server' ) ;
var typedoc = require ( "gulp-typedoc" ) ;
var CleanCSS = require ( 'clean-css' ) ;
var uncss = require ( 'uncss' ) ;
var WEBSITE _GENERATED _PATH = path . join ( _ _dirname , 'website/playground/new-samples' ) ;
var MONACO _EDITOR _VERSION = ( function ( ) {
var packageJsonPath = path . join ( _ _dirname , 'package.json' ) ;
var packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath ) . toString ( ) ) ;
var version = packageJson . version ;
const gulp = require ( 'gulp' ) ;
const metadata = require ( './metadata' ) ;
const es = require ( 'event-stream' ) ;
const path = require ( 'path' ) ;
const fs = require ( 'fs' ) ;
const rimraf = require ( 'rimraf' ) ;
const cp = require ( 'child_process' ) ;
const httpServer = require ( 'http-server' ) ;
const typedoc = require ( "gulp-typedoc" ) ;
const CleanCSS = require ( 'clean-css' ) ;
const uncss = require ( 'uncss' ) ;
const File = require ( 'vinyl' ) ;
const ts = require ( 'typescript' ) ;
const WEBSITE _GENERATED _PATH = path . join ( _ _dirname , 'website/playground/new-samples' ) ;
const MONACO _EDITOR _VERSION = ( function ( ) {
const packageJsonPath = path . join ( _ _dirname , 'package.json' ) ;
const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath ) . toString ( ) ) ;
const version = packageJson . version ;
if ( ! /\d+\.\d+\.\d+/ . test ( version ) ) {
console . log ( 'unrecognized package.json version: ' + version ) ;
process . exit ( 0 ) ;
@ -33,6 +35,9 @@ gulp.task('release', ['clean-release'], function() {
// min folder
releaseOne ( 'min' ) ,
// esm folder
ESM _release ( ) ,
// package.json
gulp . src ( 'package.json' )
. pipe ( es . through ( function ( data ) {
@ -62,31 +67,51 @@ gulp.task('release', ['clean-release'], function() {
)
} ) ;
/ * *
* Release to ` dev ` or ` min ` .
* /
function releaseOne ( type ) {
return es . merge (
gulp . src ( 'node_modules/monaco-editor-core/' + type + '/**/*' )
. pipe ( addPluginContribs ( ) )
. pipe ( addPluginContribs ( type ) )
. pipe ( gulp . dest ( 'release/' + type ) ) ,
pluginStreams ( 'release/' + type + '/' )
pluginStreams ( type , 'release/' + type + '/' )
)
}
function pluginStreams ( destinationPath ) {
/ * *
* Release plugins to ` dev ` or ` min ` .
* /
function pluginStreams ( type , destinationPath ) {
return es . merge (
metadata . METADATA . PLUGINS . map ( function ( plugin ) {
return pluginStream ( plugin , destinationPath ) ;
return pluginStream ( plugin , type, destinationPath) ;
} )
) ;
}
function pluginStream ( plugin , destinationPath ) {
var contribPath = path . join ( plugin . paths . npm , plugin . contrib . substr ( plugin . modulePrefix . length ) ) + '.js' ;
/ * *
* Release a plugin to ` dev ` or ` min ` .
* /
function pluginStream ( plugin , type , destinationPath ) {
var pluginPath = plugin . paths [ ` npm/ ${ type } ` ] ; // npm/dev or npm/min
var contribPath = path . join ( pluginPath , plugin . contrib . substr ( plugin . modulePrefix . length ) ) + '.js' ;
return (
gulp . src ( [
plugin . paths . npm + '/**/*' ,
'!' + contribPath ,
'!' + plugin . paths . npm + '/**/monaco.d.ts'
pluginPath + '/**/*' ,
'!' + contribPath
] )
. pipe ( es . through ( function ( data ) {
if ( ! /_\.contribution/ . test ( data . path ) ) {
this . emit ( 'data' , data ) ;
return ;
}
let contents = data . contents . toString ( ) ;
contents = contents . replace ( 'define(["require", "exports"],' , 'define(["require", "exports", "vs/editor/editor.api"],' ) ;
data . contents = new Buffer ( contents ) ;
this . emit ( 'data' , data ) ;
} ) )
. pipe ( gulp . dest ( destinationPath + plugin . modulePrefix ) )
) ;
}
@ -94,10 +119,10 @@ function pluginStream(plugin, destinationPath) {
/ * *
* Edit editor . main . js :
* - rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
* - append contribs from plugins
* - append monaco. contribution module s from plugins
* - append new AMD module 'vs/editor/editor.main' that stiches things together
* /
function addPluginContribs ( ) {
function addPluginContribs ( type ) {
return es . through ( function ( data ) {
if ( ! /editor\.main\.js$/ . test ( data . path ) ) {
this . emit ( 'data' , data ) ;
@ -113,22 +138,53 @@ function addPluginContribs() {
metadata . METADATA . PLUGINS . forEach ( function ( plugin ) {
allPluginsModuleIds . push ( plugin . contrib ) ;
var contribPath = path . join ( _ _dirname , plugin . paths . npm , plugin . contrib . substr ( plugin . modulePrefix . length ) ) + '.js' ;
var pluginPath = plugin . paths [ ` npm/ ${ type } ` ] ; // npm/dev or npm/min
var contribPath = path . join ( _ _dirname , pluginPath , plugin . contrib . substr ( plugin . modulePrefix . length ) ) + '.js' ;
var contribContents = fs . readFileSync ( contribPath ) . toString ( ) ;
// Check for the anonymous define call case 1
// transform define(function() {...}) to define("moduleId",["require"],function() {...})
var anonymousContribDefineCase1 = contribContents . indexOf ( 'define(function' ) ;
if ( anonymousContribDefineCase1 >= 0 ) {
contribContents = (
contribContents . substring ( 0 , anonymousContribDefineCase1 )
+ ` define(" ${ plugin . contrib } ",["require"],function `
+ contribContents . substring ( anonymousContribDefineCase1 + 'define(function' . length )
) ;
}
// Check for the anonymous define call case 2
// transform define([ to define("moduleId",[
var anonymousContribDefineCase2 = contribContents . indexOf ( 'define([' ) ;
if ( anonymousContribDefineCase2 >= 0 ) {
contribContents = (
contribContents . substring ( 0 , anonymousContribDefineCase2 )
+ ` define(" ${ plugin . contrib } ",[ `
+ contribContents . substring ( anonymousContribDefineCase2 + 'define([' . length )
) ;
}
var contribDefineIndex = contribContents . indexOf ( 'define("' + plugin . contrib ) ;
if ( contribDefineIndex === - 1 ) {
console . error ( '(1) CANNOT DETERMINE AMD define location for contribution' , plugin ) ;
process . exit ( - 1 ) ;
contribDefineIndex = contribContents . indexOf ( 'define(\'' + plugin . contrib ) ;
if ( contribDefineIndex === - 1 ) {
console . error ( '(1) CANNOT DETERMINE AMD define location for contribution' , pluginPath ) ;
process . exit ( - 1 ) ;
}
}
var depsEndIndex = contribContents . indexOf ( ']' , contribDefineIndex ) ;
if ( contribDefineIndex === - 1 ) {
console . error ( '(2) CANNOT DETERMINE AMD define location for contribution' , plugin ) ;
console . error ( '(2) CANNOT DETERMINE AMD define location for contribution' , plugin Path ) ;
process . exit ( - 1 ) ;
}
contribContents = contribContents . substring ( 0 , depsEndIndex ) + ',"vs/editor/edcore.main"' + contribContents . substring ( depsEndIndex ) ;
contribContents = contribContents . substring ( 0 , depsEndIndex ) + ',"vs/editor/editor.api"' + contribContents . substring ( depsEndIndex ) ;
contribContents = contribContents . replace (
'define("vs/basic-languages/_.contribution",["require","exports"],' ,
'define("vs/basic-languages/_.contribution",["require","exports","vs/editor/editor.api"],' ,
) ;
extraContent . push ( contribContents ) ;
} ) ;
@ -145,6 +201,181 @@ function addPluginContribs() {
} ) ;
}
function ESM _release ( ) {
return es . merge (
gulp . src ( 'node_modules/monaco-editor-core/esm/**/*' )
. pipe ( ESM _addImportSuffix ( ) )
. pipe ( ESM _addPluginContribs ( 'release/esm' ) )
. pipe ( gulp . dest ( 'release/esm' ) ) ,
ESM _pluginStreams ( 'release/esm/' )
)
}
/ * *
* Release plugins to ` esm ` .
* /
function ESM _pluginStreams ( destinationPath ) {
return es . merge (
metadata . METADATA . PLUGINS . map ( function ( plugin ) {
return ESM _pluginStream ( plugin , destinationPath ) ;
} )
) ;
}
/ * *
* Release a plugin to ` esm ` .
* Adds a dependency to 'vs/editor/editor.api' in contrib files in order for ` monaco ` to be defined .
* Rewrites imports for 'monaco-editor-core/**'
* /
function ESM _pluginStream ( plugin , destinationPath ) {
const DESTINATION = path . join ( _ _dirname , destinationPath ) ;
let pluginPath = plugin . paths [ ` esm ` ] ;
return (
gulp . src ( [
pluginPath + '/**/*'
] )
. pipe ( es . through ( function ( data ) {
if ( ! /\.js$/ . test ( data . path ) ) {
this . emit ( 'data' , data ) ;
return ;
}
let contents = data . contents . toString ( ) ;
const info = ts . preProcessFile ( contents ) ;
for ( let i = info . importedFiles . length - 1 ; i >= 0 ; i -- ) {
const importText = info . importedFiles [ i ] . fileName ;
const pos = info . importedFiles [ i ] . pos ;
const end = info . importedFiles [ i ] . end ;
if ( ! /(^\.\/)|(^\.\.\/)/ . test ( importText ) ) {
// non-relative import
if ( ! /^monaco-editor-core/ . test ( importText ) ) {
console . error ( ` Non-relative import for unknown module: ${ importText } in ${ data . path } ` ) ;
process . exit ( 0 ) ;
}
const myFileDestPath = path . join ( DESTINATION , plugin . modulePrefix , data . relative ) ;
const importFilePath = path . join ( DESTINATION , importText . substr ( 'monaco-editor-core/esm/' . length ) ) ;
let relativePath = path . relative ( path . dirname ( myFileDestPath ) , importFilePath ) ;
if ( ! /(^\.\/)|(^\.\.\/)/ . test ( relativePath ) ) {
relativePath = './' + relativePath ;
}
contents = (
contents . substring ( 0 , pos + 1 )
+ relativePath
+ contents . substring ( end + 1 )
) ;
}
}
data . contents = new Buffer ( contents ) ;
this . emit ( 'data' , data ) ;
} ) )
. pipe ( es . through ( function ( data ) {
if ( ! /monaco\.contribution\.js$/ . test ( data . path ) ) {
this . emit ( 'data' , data ) ;
return ;
}
const myFileDestPath = path . join ( DESTINATION , plugin . modulePrefix , data . relative ) ;
const apiFilePath = path . join ( DESTINATION , 'vs/editor/editor.api' ) ;
let relativePath = path . relative ( path . dirname ( myFileDestPath ) , apiFilePath ) ;
if ( ! /(^\.\/)|(^\.\.\/)/ . test ( relativePath ) ) {
relativePath = './' + relativePath ;
}
let contents = data . contents . toString ( ) ;
contents = (
` import ' ${ relativePath } '; \n ` +
contents
) ;
data . contents = new Buffer ( contents ) ;
this . emit ( 'data' , data ) ;
} ) )
. pipe ( ESM _addImportSuffix ( ) )
. pipe ( gulp . dest ( destinationPath + plugin . modulePrefix ) )
) ;
}
function ESM _addImportSuffix ( ) {
return es . through ( function ( data ) {
if ( ! /\.js$/ . test ( data . path ) ) {
this . emit ( 'data' , data ) ;
return ;
}
let contents = data . contents . toString ( ) ;
const info = ts . preProcessFile ( contents ) ;
for ( let i = info . importedFiles . length - 1 ; i >= 0 ; i -- ) {
const importText = info . importedFiles [ i ] . fileName ;
const pos = info . importedFiles [ i ] . pos ;
const end = info . importedFiles [ i ] . end ;
if ( /\.css$/ . test ( importText ) ) {
continue ;
}
contents = (
contents . substring ( 0 , pos + 1 )
+ importText + '.js'
+ contents . substring ( end + 1 )
) ;
}
data . contents = new Buffer ( contents ) ;
this . emit ( 'data' , data ) ;
} ) ;
}
/ * *
* - Rename esm / vs / editor / editor . main . js to esm / vs / editor / edcore . main . js
* - Create esm / vs / editor / editor . main . js that that stiches things together
* /
function ESM _addPluginContribs ( dest ) {
const DESTINATION = path . join ( _ _dirname , dest ) ;
return es . through ( function ( data ) {
if ( ! /editor\.main\.js$/ . test ( data . path ) ) {
this . emit ( 'data' , data ) ;
return ;
}
this . emit ( 'data' , new File ( {
path : data . path . replace ( /editor\.main/ , 'edcore.main' ) ,
base : data . base ,
contents : data . contents
} ) ) ;
const mainFileDestPath = path . join ( DESTINATION , 'vs/editor/editor.main.js' ) ;
let mainFileImports = [ ] ;
metadata . METADATA . PLUGINS . forEach ( function ( plugin ) {
const contribDestPath = path . join ( DESTINATION , plugin . contrib ) ;
let relativePath = path . relative ( path . dirname ( mainFileDestPath ) , contribDestPath ) ;
if ( ! /(^\.\/)|(^\.\.\/)/ . test ( relativePath ) ) {
relativePath = './' + relativePath ;
}
mainFileImports . push ( relativePath ) ;
} ) ;
let mainFileContents = (
mainFileImports . map ( ( name ) => ` import ' ${ name } '; ` ) . join ( '\n' )
+ ` \n \n export * from './edcore.main'; `
) ;
this . emit ( 'data' , new File ( {
path : data . path ,
base : data . base ,
contents : new Buffer ( mainFileContents )
} ) ) ;
} ) ;
}
/ * *
* Edit monaco . d . ts :
* - append monaco . d . ts from plugins
@ -159,7 +390,8 @@ function addPluginDTS() {
var extraContent = [ ] ;
metadata . METADATA . PLUGINS . forEach ( function ( plugin ) {
var dtsPath = path . join ( plugin . paths . npm , 'monaco.d.ts' ) ;
var pluginPath = plugin . paths [ ` npm/min ` ] ; // npm/dev or npm/min
var dtsPath = path . join ( pluginPath , '../monaco.d.ts' ) ;
try {
extraContent . push ( fs . readFileSync ( dtsPath ) . toString ( ) ) ;
} catch ( err ) {