import { Connect , ResolvedConfig } from 'vite' ; import { getWorks , IMonacoEditorOpts , isCDN , resolveMonacoPath } from './index' ; import { IWorkerDefinition , languageWorksByLabel } from './lnaguageWork' ; const esbuild = require ( 'esbuild' ) ; import * as fs from 'fs' ; import path = require ( 'path' ) ; export function getFilenameByEntry ( entry ) { entry = path . basename ( entry , "js" ) ; return entry + '.bundle.js' ; } export const cacheDir = 'node_modules/.monaco/' ; export function getWorkPath ( works , options ) { const workerPaths = { } ; for ( const work of works ) { if ( isCDN ( options . publicPath ) ) { workerPaths [ work . label ] = options . publicPath + '/' + getFilenameByEntry ( work . entry ) ; } else { workerPaths [ work . label ] = '/' + options . publicPath + '/' + getFilenameByEntry ( work . entry ) ; } } if ( workerPaths [ 'typescript' ] ) { workerPaths [ 'javascript' ] = workerPaths [ 'typescript' ] ; } if ( workerPaths [ 'css' ] ) { workerPaths [ 'less' ] = workerPaths [ 'css' ] ; workerPaths [ 'scss' ] = workerPaths [ 'css' ] ; } if ( workerPaths [ 'html' ] ) { workerPaths [ 'handlebars' ] = workerPaths [ 'html' ] ; workerPaths [ 'razor' ] = workerPaths [ 'html' ] ; } return workerPaths ; } export function workerMiddleware ( middlewares , config , options ) { const works = getWorks ( options ) ; if ( fs . existsSync ( cacheDir ) ) { fs . rmdirSync ( cacheDir , { recursive : true , force : true } as fs . RmDirOptions ) ; } for ( const work of works ) { console . log ( config . base + options . publicPath + '/' + getFilenameByEntry ( work . entry ) ) ; middlewares . use ( config . base + options . publicPath + '/' + getFilenameByEntry ( work . entry ) , function ( req , res , next ) { if ( ! fs . existsSync ( cacheDir + getFilenameByEntry ( work . entry ) ) ) { esbuild . buildSync ( { entryPoints : [ resolveMonacoPath ( work . entry ) ] , bundle : true , outfile : cacheDir + getFilenameByEntry ( work . entry ) , } ) ; } const contentBuffer = fs . readFileSync ( cacheDir + getFilenameByEntry ( work . entry ) ) ; res . setHeader ( 'Content-Type' , 'text/javascript' ) ; res . end ( contentBuffer ) ; } ) ; } }