2024-09-11 02:37:36 +00:00
"use strict" ;
/ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Copyright ( c ) Microsoft Corporation . All rights reserved .
* Licensed under the MIT License . See License . txt in the project root for license information .
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * /
2025-03-01 02:01:53 +00:00
var _ _createBinding = ( this && this . _ _createBinding ) || ( Object . create ? ( function ( o , m , k , k2 ) {
if ( k2 === undefined ) k2 = k ;
var desc = Object . getOwnPropertyDescriptor ( m , k ) ;
if ( ! desc || ( "get" in desc ? ! m . _ _esModule : desc . writable || desc . configurable ) ) {
desc = { enumerable : true , get : function ( ) { return m [ k ] ; } } ;
}
Object . defineProperty ( o , k2 , desc ) ;
} ) : ( function ( o , m , k , k2 ) {
if ( k2 === undefined ) k2 = k ;
o [ k2 ] = m [ k ] ;
} ) ) ;
var _ _setModuleDefault = ( this && this . _ _setModuleDefault ) || ( Object . create ? ( function ( o , v ) {
Object . defineProperty ( o , "default" , { enumerable : true , value : v } ) ;
} ) : function ( o , v ) {
o [ "default" ] = v ;
} ) ;
var _ _importStar = ( this && this . _ _importStar ) || ( function ( ) {
var ownKeys = function ( o ) {
ownKeys = Object . getOwnPropertyNames || function ( o ) {
var ar = [ ] ;
for ( var k in o ) if ( Object . prototype . hasOwnProperty . call ( o , k ) ) ar [ ar . length ] = k ;
return ar ;
} ;
return ownKeys ( o ) ;
} ;
return function ( mod ) {
if ( mod && mod . _ _esModule ) return mod ;
var result = { } ;
if ( mod != null ) for ( var k = ownKeys ( mod ) , i = 0 ; i < k . length ; i ++ ) if ( k [ i ] !== "default" ) _ _createBinding ( result , mod , k [ i ] ) ;
_ _setModuleDefault ( result , mod ) ;
return result ;
} ;
} ) ( ) ;
var _ _importDefault = ( this && this . _ _importDefault ) || function ( mod ) {
return ( mod && mod . _ _esModule ) ? mod : { "default" : mod } ;
} ;
2024-09-11 02:37:36 +00:00
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2025-03-01 02:01:53 +00:00
exports . bundleTask = bundleTask ;
2024-09-11 02:37:36 +00:00
exports . minifyTask = minifyTask ;
2025-03-01 02:01:53 +00:00
const event _stream _1 = _ _importDefault ( require ( "event-stream" ) ) ;
const gulp _1 = _ _importDefault ( require ( "gulp" ) ) ;
const gulp _filter _1 = _ _importDefault ( require ( "gulp-filter" ) ) ;
const path _1 = _ _importDefault ( require ( "path" ) ) ;
const fs _1 = _ _importDefault ( require ( "fs" ) ) ;
const pump _1 = _ _importDefault ( require ( "pump" ) ) ;
const vinyl _1 = _ _importDefault ( require ( "vinyl" ) ) ;
const bundle = _ _importStar ( require ( "./bundle" ) ) ;
2024-09-11 02:37:36 +00:00
const postcss _1 = require ( "./postcss" ) ;
2025-03-01 02:01:53 +00:00
const esbuild _1 = _ _importDefault ( require ( "esbuild" ) ) ;
const gulp _sourcemaps _1 = _ _importDefault ( require ( "gulp-sourcemaps" ) ) ;
const fancy _log _1 = _ _importDefault ( require ( "fancy-log" ) ) ;
const ansi _colors _1 = _ _importDefault ( require ( "ansi-colors" ) ) ;
const REPO _ROOT _PATH = path _1 . default . join ( _ _dirname , '../..' ) ;
2024-09-11 02:37:36 +00:00
const DEFAULT _FILE _HEADER = [
'/*!--------------------------------------------------------' ,
' * Copyright (C) Microsoft Corporation. All rights reserved.' ,
' *--------------------------------------------------------*/'
] . join ( '\n' ) ;
2025-03-01 02:01:53 +00:00
function bundleESMTask ( opts ) {
const resourcesStream = event _stream _1 . default . through ( ) ; // this stream will contain the resources
const bundlesStream = event _stream _1 . default . through ( ) ; // this stream will contain the bundled files
const entryPoints = opts . entryPoints . map ( entryPoint => {
if ( typeof entryPoint === 'string' ) {
return { name : path _1 . default . parse ( entryPoint ) . name } ;
2024-09-11 02:37:36 +00:00
}
2025-03-01 02:01:53 +00:00
return entryPoint ;
2024-09-11 02:37:36 +00:00
} ) ;
const bundleAsync = async ( ) => {
const files = [ ] ;
const tasks = [ ] ;
for ( const entryPoint of entryPoints ) {
2025-03-01 02:01:53 +00:00
( 0 , fancy _log _1 . default ) ( ` Bundled entry point: ${ ansi _colors _1 . default . yellow ( entryPoint . name ) } ... ` ) ;
2024-09-11 02:37:36 +00:00
// support for 'dest' via esbuild#in/out
const dest = entryPoint . dest ? . replace ( /\.[^/.]+$/ , '' ) ? ? entryPoint . name ;
2025-03-01 02:01:53 +00:00
// banner contents
const banner = {
js : DEFAULT _FILE _HEADER ,
css : DEFAULT _FILE _HEADER
} ;
// TS Boilerplate
if ( ! opts . skipTSBoilerplateRemoval ? . ( entryPoint . name ) ) {
const tslibPath = path _1 . default . join ( require . resolve ( 'tslib' ) , '../tslib.es6.js' ) ;
banner . js += await fs _1 . default . promises . readFile ( tslibPath , 'utf-8' ) ;
}
const contentsMapper = {
name : 'contents-mapper' ,
2024-09-11 02:37:36 +00:00
setup ( build ) {
2025-03-01 02:01:53 +00:00
build . onLoad ( { filter : /\.js$/ } , async ( { path } ) => {
const contents = await fs _1 . default . promises . readFile ( path , 'utf-8' ) ;
// TS Boilerplate
let newContents ;
if ( ! opts . skipTSBoilerplateRemoval ? . ( entryPoint . name ) ) {
newContents = bundle . removeAllTSBoilerplate ( contents ) ;
}
else {
newContents = contents ;
}
// File Content Mapper
const mapper = opts . fileContentMapper ? . ( path . replace ( /\\/g , '/' ) ) ;
if ( mapper ) {
newContents = await mapper ( newContents ) ;
}
2024-09-11 02:37:36 +00:00
return { contents : newContents } ;
} ) ;
}
} ;
2025-03-01 02:01:53 +00:00
const externalOverride = {
name : 'external-override' ,
setup ( build ) {
// We inline selected modules that are we depend on on startup without
// a conditional `await import(...)` by hooking into the resolution.
build . onResolve ( { filter : /^minimist$/ } , ( ) => {
return { path : path _1 . default . join ( REPO _ROOT _PATH , 'node_modules' , 'minimist' , 'index.js' ) , external : false } ;
} ) ;
} ,
} ;
const task = esbuild _1 . default . build ( {
2024-09-11 02:37:36 +00:00
bundle : true ,
packages : 'external' , // "external all the things", see https://esbuild.github.io/api/#packages
platform : 'neutral' , // makes esm
format : 'esm' ,
2024-09-24 04:46:08 +00:00
sourcemap : 'external' ,
2025-03-01 02:01:53 +00:00
plugins : [ contentsMapper , externalOverride ] ,
2024-09-11 02:37:36 +00:00
target : [ 'es2022' ] ,
loader : {
'.ttf' : 'file' ,
'.svg' : 'file' ,
'.png' : 'file' ,
'.sh' : 'file' ,
} ,
assetNames : 'media/[name]' , // moves media assets into a sub-folder "media"
2024-09-24 04:46:08 +00:00
banner : entryPoint . name === 'vs/workbench/workbench.web.main' ? undefined : banner , // TODO@esm remove line when we stop supporting web-amd-esm-bridge
2024-09-11 02:37:36 +00:00
entryPoints : [
{
2025-03-01 02:01:53 +00:00
in : path _1 . default . join ( REPO _ROOT _PATH , opts . src , ` ${ entryPoint . name } .js ` ) ,
2024-09-11 02:37:36 +00:00
out : dest ,
}
] ,
2025-03-01 02:01:53 +00:00
outdir : path _1 . default . join ( REPO _ROOT _PATH , opts . src ) ,
2024-09-11 02:37:36 +00:00
write : false , // enables res.outputFiles
metafile : true , // enables res.metafile
2025-03-01 02:01:53 +00:00
// minify: NOT enabled because we have a separate minify task that takes care of the TSLib banner as well
2024-09-11 02:37:36 +00:00
} ) . then ( res => {
for ( const file of res . outputFiles ) {
2024-09-24 04:46:08 +00:00
let sourceMapFile = undefined ;
2024-09-11 02:37:36 +00:00
if ( file . path . endsWith ( '.js' ) ) {
2024-09-24 04:46:08 +00:00
sourceMapFile = res . outputFiles . find ( f => f . path === ` ${ file . path } .map ` ) ;
2024-09-11 02:37:36 +00:00
}
2024-09-24 04:46:08 +00:00
const fileProps = {
2025-03-01 02:01:53 +00:00
contents : Buffer . from ( file . contents ) ,
2024-09-24 04:46:08 +00:00
sourceMap : sourceMapFile ? JSON . parse ( sourceMapFile . text ) : undefined , // support gulp-sourcemaps
2024-09-11 02:37:36 +00:00
path : file . path ,
2025-03-01 02:01:53 +00:00
base : path _1 . default . join ( REPO _ROOT _PATH , opts . src )
2024-09-24 04:46:08 +00:00
} ;
2025-03-01 02:01:53 +00:00
files . push ( new vinyl _1 . default ( fileProps ) ) ;
2024-09-11 02:37:36 +00:00
}
} ) ;
tasks . push ( task ) ;
}
await Promise . all ( tasks ) ;
return { files } ;
} ;
bundleAsync ( ) . then ( ( output ) => {
// bundle output (JS, CSS, SVG...)
2025-03-01 02:01:53 +00:00
event _stream _1 . default . readArray ( output . files ) . pipe ( bundlesStream ) ;
2024-09-11 02:37:36 +00:00
// forward all resources
2025-03-01 02:01:53 +00:00
gulp _1 . default . src ( opts . resources ? ? [ ] , { base : ` ${ opts . src } ` , allowEmpty : true } ) . pipe ( resourcesStream ) ;
2024-09-11 02:37:36 +00:00
} ) ;
2025-03-01 02:01:53 +00:00
const result = event _stream _1 . default . merge ( bundlesStream , resourcesStream ) ;
2024-09-11 02:37:36 +00:00
return result
2025-03-01 02:01:53 +00:00
. pipe ( gulp _sourcemaps _1 . default . write ( './' , {
2024-09-11 02:37:36 +00:00
sourceRoot : undefined ,
addComment : true ,
includeContent : true
} ) ) ;
}
2025-03-01 02:01:53 +00:00
function bundleTask ( opts ) {
2024-09-11 02:37:36 +00:00
return function ( ) {
2025-03-01 02:01:53 +00:00
return bundleESMTask ( opts . esm ) . pipe ( gulp _1 . default . dest ( opts . out ) ) ;
2024-09-11 02:37:36 +00:00
} ;
}
function minifyTask ( src , sourceMapBaseUrl ) {
const sourceMappingURL = sourceMapBaseUrl ? ( ( f ) => ` ${ sourceMapBaseUrl } / ${ f . relative } .map ` ) : undefined ;
return cb => {
const cssnano = require ( 'cssnano' ) ;
const svgmin = require ( 'gulp-svgmin' ) ;
2025-03-01 02:01:53 +00:00
const jsFilter = ( 0 , gulp _filter _1 . default ) ( '**/*.js' , { restore : true } ) ;
const cssFilter = ( 0 , gulp _filter _1 . default ) ( '**/*.css' , { restore : true } ) ;
const svgFilter = ( 0 , gulp _filter _1 . default ) ( '**/*.svg' , { restore : true } ) ;
( 0 , pump _1 . default ) ( gulp _1 . default . src ( [ src + '/**' , '!' + src + '/**/*.map' ] ) , jsFilter , gulp _sourcemaps _1 . default . init ( { loadMaps : true } ) , event _stream _1 . default . map ( ( f , cb ) => {
esbuild _1 . default . build ( {
2024-09-11 02:37:36 +00:00
entryPoints : [ f . path ] ,
minify : true ,
sourcemap : 'external' ,
outdir : '.' ,
2025-03-01 02:01:53 +00:00
packages : 'external' , // "external all the things", see https://esbuild.github.io/api/#packages
platform : 'neutral' , // makes esm
2024-09-11 02:37:36 +00:00
target : [ 'es2022' ] ,
write : false
} ) . then ( res => {
const jsFile = res . outputFiles . find ( f => / \ . js$ / . test ( f . path ) ) ;
const sourceMapFile = res . outputFiles . find ( f => / \ . js \ . map$ / . test ( f . path ) ) ;
const contents = Buffer . from ( jsFile . contents ) ;
const unicodeMatch = contents . toString ( ) . match ( /[^\x00-\xFF]+/g ) ;
if ( unicodeMatch ) {
cb ( new Error ( ` Found non-ascii character ${ unicodeMatch [ 0 ] } in the minified output of ${ f . path } . Non-ASCII characters in the output can cause performance problems when loading. Please review if you have introduced a regular expression that esbuild is not automatically converting and convert it to using unicode escape sequences. ` ) ) ;
}
else {
f . contents = contents ;
f . sourceMap = JSON . parse ( sourceMapFile . text ) ;
cb ( undefined , f ) ;
}
} , cb ) ;
2025-03-01 02:01:53 +00:00
} ) , jsFilter . restore , cssFilter , ( 0 , postcss _1 . gulpPostcss ) ( [ cssnano ( { preset : 'default' } ) ] ) , cssFilter . restore , svgFilter , svgmin ( ) , svgFilter . restore , gulp _sourcemaps _1 . default . write ( './' , {
2024-09-11 02:37:36 +00:00
sourceMappingURL ,
sourceRoot : undefined ,
includeContent : true ,
addComment : true
2025-03-01 02:01:53 +00:00
} ) , gulp _1 . default . dest ( src + '-min' ) , ( err ) => cb ( err ) ) ;
2024-09-11 02:37:36 +00:00
} ;
}
//# sourceMappingURL=optimize.js.map