@open-editor/webpack
Version:
🚀 A webpack plugin for fast find source code.
2 lines (1 loc) • 1.92 kB
JavaScript
;var shared=require("@open-editor/shared"),node=require("@open-editor/shared/node"),server=require("@open-editor/server"),_documentCurrentScript=typeof document<"u"?document.currentScript:null,__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=>node.resolvePath(path,typeof document>"u"?require("url").pathToFileURL(__filename).href:_documentCurrentScript&&_documentCurrentScript.tagName.toUpperCase()==="SCRIPT"&&_documentCurrentScript.src||new URL("index.js",document.baseURI).href),PLUGIN_NAME="OpenEditorPlugin",LOADER_PATH=resolvePath("./transform"),portPromiseCache={};class OpenEditorPlugin{constructor(options={}){__publicField(this,"options"),this.options={rootDir:shared.normalizePath(options.rootDir??process.cwd()),...options}}apply(compiler){node.isDev()&&(compiler.hooks.afterEnvironment.tap(PLUGIN_NAME,()=>{var _a;compiler.options.module.rules.push({test:/[\\/]node_modules[\\/]/,include:shared.ENTRY_MATCH_RE,use:data=>this.handleLoaderConfiguration(data)}),(_a=compiler.options.resolve).alias||(_a.alias={}),compiler.options.resolve.alias[shared.CLIENT_MODULE_ID]=resolvePath(shared.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:!shared.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]=server.setupServer({...this.options,...this.options.server??{}})))}}module.exports=OpenEditorPlugin;