import { IPlugin , IUserConfig } from 'build-scripts' ; import { getWebpackConfig } from 'build-scripts-config' ; import * as WebpackPluginImport from 'webpack-plugin-import' ; import { Options } from './types' ; import setUMDConfig from './setUMDConfig' ; import genRuntime from './genRuntime' ; import setExternals from './setExternals' ; import appendLifecycle from './appendLifecycle' ; import setDevLog from './setDevLog' ; const plugin = ( { onGetWebpackConfig , context , registerTask , onHook , registerUserConfig , hasRegistration } , options = { } ) => { const { command , userConfig , webpack , commandArgs } = context ; const { minify : outerMinify , sourceMap : outerSourceMap , outputDir : outerOutputDir } = ( userConfig || { } ) as IUserConfig ; const hasOutputDirRegistered = hasRegistration ( 'outputDir' , 'userConfig' ) ; if ( ! hasOutputDirRegistered ) { registerUserConfig ( { name : 'outputDir' , validation : 'string' , } ) ; } options . outputDir = options . outputDir ?? ( outerOutputDir as string ) ; const { moduleExternals , minify , sourceMap , } = options as any as Options ?? { } ; const mode = command === 'start' ? 'development' : 'production' ; const baseConfig = getWebpackConfig ( mode ) ; baseConfig . name ( 'MicroModule' ) ; const localMinify = outerMinify ?? minify ; if ( localMinify !== undefined ) { baseConfig . optimization . minimize ( minify ) ; } const localSourceMap = outerSourceMap ?? sourceMap ; if ( localSourceMap !== undefined ) { baseConfig . devtool ( sourceMap ? 'source-map' : false ) ; sourceMap && ( command === 'build' ) && baseConfig . optimization . minimizer ( 'TerserPlugin' ) . tap ( ( [ opts ] ) => [ { ... opts , sourceMap : true } , ] ) ; } const defineVariables = { 'process.env.NODE_ENV' : JSON . stringify ( mode || 'development' ) , 'process.env.SERVER_PORT' : JSON . stringify ( commandArgs . port ) , } ; baseConfig . plugin ( 'DefinePlugin' ) . use ( ( webpack as any ) . DefinePlugin , [ defineVariables ] ) . end ( ) ; baseConfig . plugin ( 'WebpackPluginImport' ) . use ( WebpackPluginImport , [ [ { libraryName : /@ali\/ice-.*/ , stylePath : 'style.js' , } , ] ] ) . end ( ) ; setUMDConfig ( { context , onGetWebpackConfig } , options as any as Options ) ; if ( moduleExternals ) { setExternals ( { onGetWebpackConfig } , { moduleExternals } ) ; } appendLifecycle ( { onGetWebpackConfig } ) ; registerTask ( 'icestark-module' , baseConfig ) ; onHook ( `after.${ command }.compile` , ( args ) => { genRuntime ( { context } , options as any as Options ) ; if ( command === 'start' ) { setDevLog ( args , options ) ; } } ) ; } ; export default plugin ;