UNPKG

@open-editor/webpack

Version:

🚀 A webpack plugin for fast find source code.

2 lines (1 loc) • 1.69 kB
import{normalizePath,ENTRY_MATCH_RE,CLIENT_MODULE_ID,ENTRY_ESM_MATCH_RE}from"@open-editor/shared";import{isDev,resolvePath as resolvePath$1}from"@open-editor/shared/node";import{setupServer}from"@open-editor/server";var __defProp=Object.defineProperty,__defNormalProp=(obj,key,value)=>key in obj?__defProp(obj,key,{enumerable:!0,configurable:!0,writable:!0,value}):obj[key]=value,__publicField=(obj,key,value)=>__defNormalProp(obj,typeof key!="symbol"?key+"":key,value);const resolvePath=path=>resolvePath$1(path,import.meta.url),PLUGIN_NAME="OpenEditorPlugin",LOADER_PATH=resolvePath("./transform"),portPromiseCache={};class OpenEditorPlugin{constructor(options={}){__publicField(this,"options"),this.options={rootDir:normalizePath(options.rootDir??process.cwd()),...options}}apply(compiler){isDev()&&(compiler.hooks.afterEnvironment.tap(PLUGIN_NAME,()=>{var _a;compiler.options.module.rules.push({test:/[\\/]node_modules[\\/]/,include:ENTRY_MATCH_RE,use:data=>this.handleLoaderConfiguration(data)}),(_a=compiler.options.resolve).alias||(_a.alias={}),compiler.options.resolve.alias[CLIENT_MODULE_ID]=resolvePath(CLIENT_MODULE_ID)}),compiler.hooks.make.tapPromise(PLUGIN_NAME,async()=>{await this.startDebugServer()}))}handleLoaderConfiguration(data){const{resource,compiler}=data;return!compiler||compiler==="client"?{options:{...this.options,isCommonjs:!ENTRY_ESM_MATCH_RE.test(resource)},loader:LOADER_PATH}:[]}async startDebugServer(){const cacheKey=`${this.options.rootDir}${this.options.onOpenEditor}`;this.options.port=await(portPromiseCache[cacheKey]||(portPromiseCache[cacheKey]=setupServer({...this.options,...this.options.server??{}})))}}export{OpenEditorPlugin as default};