|
| 1 | +constpath=require('path') |
| 2 | + |
| 3 | +classJSEntryWebpackPlugin{ |
| 4 | +constructor(options={}){ |
| 5 | +this.options={ |
| 6 | +filename:'index.js', |
| 7 | +template:'auto', |
| 8 | +publicPath:options.publicPath===undefined ?'auto' :options.publicPath, |
| 9 | + ...options, |
| 10 | +} |
| 11 | +} |
| 12 | + |
| 13 | +apply(compiler){ |
| 14 | +compiler.hooks.emit.tap('InjectCssEntry',asynccompilation=>{ |
| 15 | +// console.log(Object.keys(compilation.assets)) |
| 16 | + |
| 17 | +/** output filenames for the given entry names */ |
| 18 | +constentryNames=Object.keys(compiler.options.entry) |
| 19 | +constoutputFileNames=newSet( |
| 20 | +(entryNames.length ?entryNames :['main']).map(entryName=> |
| 21 | +// Replace '[name]' with entry name |
| 22 | +this.options.filename.replace(/\[name\]/g,entryName), |
| 23 | +), |
| 24 | +) |
| 25 | + |
| 26 | +/** Option for every entry point */ |
| 27 | +constentryOption=Array.from(outputFileNames).map(filename=>({ |
| 28 | + ...this.options, |
| 29 | + filename, |
| 30 | +}))[0] |
| 31 | + |
| 32 | +/** The public path used inside the html file */ |
| 33 | +constpublicPath=this.getPublicPath( |
| 34 | +compilation, |
| 35 | +entryOption.filename, |
| 36 | +entryOption.publicPath, |
| 37 | +) |
| 38 | + |
| 39 | +/** build output path */ |
| 40 | +consttemplatePath=this.getTemplatePath(entryOption.template,compilation.options) |
| 41 | + |
| 42 | +/** Generated file paths from the entry point names */ |
| 43 | +constassets=this.htmlWebpackPluginAssets( |
| 44 | +compilation, |
| 45 | +// 只处理一个 |
| 46 | +Array.from(compilation.entrypoints.keys()).slice(0,1), |
| 47 | +publicPath, |
| 48 | +templatePath, |
| 49 | +) |
| 50 | + |
| 51 | +// js entry |
| 52 | +if(!compilation.assets[assets.entry])return |
| 53 | +// const { libsImportCode } = await import("../src/dev-utils/external.js"); |
| 54 | +// const libs = libsImportCode(["lowcoder-sdk"]); |
| 55 | +// ${libsImportCode(["lowcoder-sdk"])} |
| 56 | +letcontent=`(function() { |
| 57 | +
|
| 58 | + // adding entry points to single bundle.js file |
| 59 | + let scripts =${JSON.stringify(assets.js)}; |
| 60 | + for (let i = 0; i < scripts.length; i++) { |
| 61 | + const scriptEle = document.createElement('script'); |
| 62 | + scriptEle.src = scripts[i]; |
| 63 | + document.body.appendChild(scriptEle); |
| 64 | + } |
| 65 | + })()`; |
| 66 | + |
| 67 | +compilation.assets[entryOption.filename]={ |
| 68 | +source(){ |
| 69 | +returncontent |
| 70 | +}, |
| 71 | +size(){ |
| 72 | +returncontent.length |
| 73 | +}, |
| 74 | +} |
| 75 | +}) |
| 76 | +} |
| 77 | + |
| 78 | +htmlWebpackPluginAssets(compilation,entryNames,publicPath,templatePath){ |
| 79 | +// https://github.com/jantimon/html-webpack-plugin/blob/main/index.js#L640 |
| 80 | +constassets={ |
| 81 | + publicPath, |
| 82 | + templatePath, |
| 83 | +entry:'', |
| 84 | +js:[], |
| 85 | +css:[], |
| 86 | +} |
| 87 | + |
| 88 | +// Extract paths to .js, .mjs and .css files from the current compilation |
| 89 | +constentryPointPublicPathMap={} |
| 90 | +constextensionRegexp=/\.(css|js)(\?|$)/ |
| 91 | +for(leti=0;i<entryNames.length;i++){ |
| 92 | +constentryName=entryNames[i] |
| 93 | +/** entryPointUnfilteredFiles - also includes hot module update files */ |
| 94 | +constentryPointUnfilteredFiles=compilation.entrypoints.get(entryName).getFiles() |
| 95 | + |
| 96 | +constentryPointFiles=entryPointUnfilteredFiles.filter(chunkFile=>{ |
| 97 | +// compilation.getAsset was introduced in webpack 4.4.0 |
| 98 | +// once the support pre webpack 4.4.0 is dropped please |
| 99 | +// remove the following guard: |
| 100 | +constasset=compilation.getAsset&&compilation.getAsset(chunkFile) |
| 101 | +if(!asset){ |
| 102 | +returntrue |
| 103 | +} |
| 104 | +// Prevent hot-module files from being included: |
| 105 | +constassetMetaInformation=asset.info||{} |
| 106 | +return!(assetMetaInformation.hotModuleReplacement||assetMetaInformation.development) |
| 107 | +}) |
| 108 | + |
| 109 | +// Prepend the publicPath and append the hash depending on the |
| 110 | +// webpack.output.publicPath and hashOptions |
| 111 | +// E.g. bundle.js -> /bundle.js?hash |
| 112 | +constentryPointPublicPaths=entryPointFiles.map(chunkFile=>{ |
| 113 | +consturlPath=this.urlencodePath(chunkFile) |
| 114 | +constentryPointPublicPath=publicPath+urlPath |
| 115 | + |
| 116 | +if(chunkFile.endsWith('.js')){ |
| 117 | +assets.entry=urlPath |
| 118 | +} |
| 119 | +returnentryPointPublicPath |
| 120 | +}) |
| 121 | + |
| 122 | +entryPointPublicPaths.forEach(entryPointPublicPath=>{ |
| 123 | +constextMatch=extensionRegexp.exec(entryPointPublicPath) |
| 124 | +// Skip if the public path is not a .css, .mjs or .js file |
| 125 | +if(!extMatch){ |
| 126 | +return |
| 127 | +} |
| 128 | +// Skip if this file is already known |
| 129 | +// (e.g. because of common chunk optimizations) |
| 130 | +if(entryPointPublicPathMap[entryPointPublicPath]){ |
| 131 | +return |
| 132 | +} |
| 133 | +entryPointPublicPathMap[entryPointPublicPath]=true |
| 134 | +constext=extMatch[1] |
| 135 | +assets[ext].push(entryPointPublicPath) |
| 136 | +}) |
| 137 | +} |
| 138 | +returnassets |
| 139 | +} |
| 140 | + |
| 141 | +getPublicPath(compilation,outputName,customPublicPath){ |
| 142 | +constcompilationHash=compilation.hash |
| 143 | + |
| 144 | +/** |
| 145 | + *@type {string} the configured public path to the asset root |
| 146 | + * if a path publicPath is set in the current webpack config use it otherwise |
| 147 | + * fallback to a relative path |
| 148 | + */ |
| 149 | +constwebpackPublicPath=compilation.getAssetPath(compilation.outputOptions.publicPath,{ |
| 150 | +hash:compilationHash, |
| 151 | +}) |
| 152 | + |
| 153 | +// Webpack 5 introduced "auto" as default value |
| 154 | +constisPublicPathDefined=webpackPublicPath!=='auto' |
| 155 | + |
| 156 | +letpublicPath= |
| 157 | +// If the html-webpack-plugin options contain a custom public path uset it |
| 158 | +customPublicPath!=='auto' |
| 159 | + ?customPublicPath |
| 160 | + :isPublicPathDefined |
| 161 | + ?// If a hard coded public path exists use it |
| 162 | +webpackPublicPath |
| 163 | + :// If no public path was set get a relative url path |
| 164 | +path |
| 165 | +.relative( |
| 166 | +path.resolve(compilation.options.output.path,path.dirname(outputName)), |
| 167 | +compilation.options.output.path, |
| 168 | +) |
| 169 | +.split(path.sep) |
| 170 | +.join('/') |
| 171 | + |
| 172 | +if(publicPath.length&&publicPath.substr(-1,1)!=='/'){ |
| 173 | +publicPath+='/' |
| 174 | +} |
| 175 | + |
| 176 | +returnpublicPath |
| 177 | +} |
| 178 | + |
| 179 | +getTemplatePath(template,options){ |
| 180 | +const{ context, output}=options |
| 181 | + |
| 182 | +returntemplate==='auto' |
| 183 | + ?path.join(output.path,path.sep) |
| 184 | + :path.join(context||'',template) |
| 185 | +} |
| 186 | + |
| 187 | +urlencodePath(filePath){ |
| 188 | +// some+path/demo.html?value=abc?def |
| 189 | +constqueryStringStart=filePath.indexOf('?') |
| 190 | +consturlPath=queryStringStart===-1 ?filePath :filePath.substr(0,queryStringStart) |
| 191 | +constqueryString=filePath.substr(urlPath.length) |
| 192 | +// Encode all parts except '/' which are not part of the querystring: |
| 193 | +constencodedUrlPath=urlPath.split('/').map(encodeURIComponent).join('/') |
| 194 | +returnencodedUrlPath+queryString |
| 195 | +} |
| 196 | +} |
| 197 | + |
| 198 | +JSEntryWebpackPlugin.version=1 |
| 199 | + |
| 200 | +module.exports=JSEntryWebpackPlugin |