/**
 * Minified by jsDelivr using Terser v5.37.0.
 * Original file: /npm/poi@12.10.3/lib/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
const os=require("os"),path=require("path"),fs=require("fs-extra"),resolveFrom=require("resolve-from"),cac=require("cac"),chalk=require("chalk"),merge=require("lodash.merge"),logger=require("@poi/logger"),Hooks=require("./Hooks"),WebpackUtils=require("./WebpackUtils"),createConfigLoader=require("./utils/createConfigLoader"),loadEnvs=require("./utils/loadEnvs"),parseArgs=require("./utils/parseArgs"),PoiError=require("./utils/PoiError"),spinner=require("./utils/spinner"),validateConfig=require("./utils/validateConfig"),{normalizePlugins:normalizePlugins,mergePlugins:mergePlugins}=require("./utils/plugins");module.exports=class{constructor(e=process.argv,{defaultConfigFiles:i=["poi.config.js","poi.config.ts","package.json",".poirc",".poirc.json",".poirc.js"],extendConfigLoader:s,config:o}={}){this.rawArgs=e,this.logger=logger,this.spinner=spinner,this.PoiError=PoiError,this.args=parseArgs(e),this.hooks=new Hooks,this.testRunners=new Map,this.args.has("debug")&&logger.setOptions({debug:!0}),this.mode=this.args.get("mode"),this.mode||(this.mode="development"),(this.args.has("prod")||this.args.has("production"))&&(this.mode="production"),this.args.has("test")&&(this.mode="test"),this.args.args[0]&&/^test(:|$)/.test(this.args.args[0])&&(this.mode="test"),logger.debug(`Running in ${this.mode} mode`),this.cwd=this.args.get("cwd"),this.cwd||(this.cwd=process.cwd()),this.loadRequiredModules(),this.configLoader=createConfigLoader(this.cwd),s&&s(this.configLoader),loadEnvs(this.mode,this.resolveCwd(".env")),process.env.NODE_ENV||(process.env.NODE_ENV=this.mode),this.webpackUtils=new WebpackUtils(this);const r=void 0===this.args.get("config")?this.args.get("c"):this.args.get("config");if(o||!1===r)logger.debug("Poi config file was disabled"),this.config=o||{};else{const e="string"==typeof r?[r]:i,{path:s,data:o}=this.configLoader.load({files:e,packageKey:"poi"});s?logger.debug("Using Poi config file:",s):logger.debug("Not using any Poi config file"),this.configPath=s,this.config="function"==typeof o?o(this.args.options):o,this.config=this.config||{}}this.pkg=this.configLoader.load({files:["package.json"]}),this.pkg.data=this.pkg.data||{},this.initPlugins(),this.initCLI(),this.mergeConfig(),this.applyPlugins(),this.hooks.invoke("createConfig",this.config)}get isProd(){return"production"===this.mode}initCLI(){const e=this.cli=cac();this.command=e.command("[...entries]","Entry files to start bundling",{ignoreOptionDefaultValue:!0}).usage("[...entries] [options]").action((async()=>{logger.debug("Using default handler");const e=this.createWebpackChain(),i=this.createWebpackCompiler(e.toConfig());await this.runCompiler(i)})),this.extendCLI(),e.option("--mode <mode>","Set mode","development").option("--prod, --production","Alias for --mode production").option("--test","Alias for --mode test").option("--no-config","Disable config file").option("-c, --config <path>","Set the path to config file").option("-r, --require <module>","Require a module before bootstrapping").option("--plugin, --plugins <plugin>","Add a plugin (can be used for multiple times)").option("--debug","Show debug logs").option("--inspect-webpack","Inspect webpack config in your editor").version(require("../package").version).help((i=>{for(const s of i)if(s.title&&s.title.includes("For more info")){const i=s.body.split("\n");i.shift(),i.unshift(`  $ ${e.name} --help`,`  $ ${e.name} --serve --help`,`  $ ${e.name} --prod --help`),s.body=i.join("\n")}})),this.cli.parse(this.rawArgs,{run:!1}),logger.debug("Command args",this.cli.args),logger.debug("Command options",this.cli.options)}loadRequiredModules(){process.env.TS_NODE_COMPILER_OPTIONS=JSON.stringify({module:"commonjs"});const e=this.args.get("require")||this.args.get("r");e&&[].concat(e).forEach((e=>{if(!this.localRequire(e))throw new PoiError({message:`Cannot find module "${e}" in current directory!`})}))}hasDependency(e){return[...Object.keys(this.pkg.data.dependencies||{}),...Object.keys(this.pkg.data.devDependencies||{})].includes(e)}initPlugins(){const e=this.configPath?path.dirname(this.configPath):this.resolveCwd(),i=normalizePlugins(this.args.get("plugin")||this.args.get("plugins"),e),s=normalizePlugins(this.config.plugins,e);this.plugins=[{resolve:require.resolve("./plugins/command-options")},{resolve:require.resolve("./plugins/config-babel")},{resolve:require.resolve("./plugins/config-vue")},{resolve:require.resolve("./plugins/config-css")},{resolve:require.resolve("./plugins/config-font")},{resolve:require.resolve("./plugins/config-image")},{resolve:require.resolve("./plugins/config-eval")},{resolve:require.resolve("./plugins/config-html")},{resolve:require.resolve("./plugins/config-electron")},{resolve:require.resolve("./plugins/config-misc-loaders")},{resolve:require.resolve("./plugins/config-reason")},{resolve:require.resolve("./plugins/config-yarn-pnp")},{resolve:require.resolve("./plugins/config-jsx-import")},{resolve:require.resolve("./plugins/config-react-refresh")},{resolve:require.resolve("./plugins/watch")},{resolve:require.resolve("./plugins/serve")},{resolve:require.resolve("./plugins/eject-html")},{resolve:require.resolve("@poi/plugin-html-entry")}].concat(mergePlugins(s,i)).map((e=>("string"==typeof e.resolve&&(e._resolve=e.resolve,e.resolve=require(e.resolve)),e)))}extendCLI(){for(const e of this.plugins)e.resolve.cli&&e.resolve.cli(this,e.options)}applyPlugins(){let e=this.plugins.filter((e=>!e.resolve.when||e.resolve.when(this)));for(const i of e)i.resolve.filterPlugins&&(e=i.resolve.filterPlugins(this.plugins,i.options));for(const i of e)i.resolve.apply&&(logger.debug(`Apply plugin: \`${chalk.bold(i.resolve.name)}\``),i._resolve&&logger.debug(`location: ${i._resolve}`),i.resolve.apply(this,i.options))}hasPlugin(e){return this.plugins&&this.plugins.find((i=>i.resolve.name===e))}mergeConfig(){const e=this.createConfigFromCLIOptions();logger.debug("Config from command options",e),this.config=validateConfig(this,merge({},this.config,e))}hook(e,i){return this.hooks.add(e,i),this}registerTestRunner(e,i){if(this.testRunners.has(e))throw new PoiError({message:`Test runner "${e}" has already been registered!`});return this.testRunners.set(e,i),this}resolveCwd(...e){return path.resolve(this.cwd,...e)}resolveOutDir(...e){return this.resolveCwd(this.config.output.dir,...e)}async run(){await this.hooks.invokePromise("beforeRun"),await this.cli.runMatchedCommand(),await this.hooks.invokePromise("afterRun")}createConfigFromCLIOptions(){const{minimize:e,sourceMap:i,format:s,moduleName:o,outDir:r,publicUrl:t,target:n,clean:l,parallel:a,cache:g,jsx:c,extractCss:u,hot:p,host:h,port:d,open:f,proxy:m,fileNames:v,html:b,publicFolder:k,babelrc:q,babelConfigFile:w,reactRefresh:C}=this.cli.options;return{entry:this.cli.args.length>0?this.cli.args:void 0,output:{minimize:e,sourceMap:i,format:s,moduleName:o,dir:r,publicUrl:t,target:n,clean:l,fileNames:v,html:b},parallel:a,cache:g,publicFolder:k,babel:{jsx:c,babelrc:q,configFile:w},css:{extract:u},devServer:{hot:p,host:h,port:d,open:f,proxy:m},reactRefresh:C}}createWebpackChain(e){const i=require("./utils/WebpackChain");e=Object.assign({type:"client",mode:this.mode},e);const s=new i({configureWebpack:this.config.configureWebpack,opts:e});if(require("./webpack/webpack.config")(s,this),this.hooks.invoke("createWebpackChain",s,e),this.config.chainWebpack&&this.config.chainWebpack(s,e),this.cli.options.inspectWebpack){const i=()=>{const i=Math.random().toString(36).substring(7),o=path.join(os.tmpdir(),`poi-inspect-webpack-config-${i}.js`),r=`// ${JSON.stringify(e)}\nvar config = ${s.toString()}\n\n`;fs.writeFileSync(o,r,"utf8"),require("@poi/dev-utils/open")(o,{wait:!1})};s.plugin("inspect-webpack").use(class{apply(e){e.hooks.afterEnvironment.tap("inspect-webpack",i)}})}return s}runCompiler(e,i){return new Promise(((s,o)=>{i?e.watch({},(e=>{if(e)return o(e);s()})):e.run(((e,i)=>{if(e)return o(e);s(i)}))}))}createWebpackCompiler(e){const i=require("webpack")(e),s=i.watch.bind(i);return i.watch=(e,i)=>s(e,((e,s)=>{if(e)throw e;i(null,s)})),i}getCacheConfig(e,i,s){let o="";if(s){const e=this.configLoader.resolve({files:[].concat(s)});e&&(o=fs.readFileSync(e,"utf8"))}return{cacheDirectory:this.resolveCwd("node_modules/.cache",e),cacheIdentifier:`${JSON.stringify(i)}::${o}`}}localResolve(e,i=this.cwd){return resolveFrom.silent(i,e)}localRequire(e,i){const s=this.localResolve(e,i);return s?require(s):null}};
//# sourceMappingURL=/sm/eb9d95f25ee0e52fa38e8672a1744f298be04c9e7683e35beda579bd3b10d62f.map