|
| 1 | +const path = require('path') |
| 2 | + |
| 3 | +class JSEntryWebpackPlugin { |
| 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', async compilation => { |
| 15 | + // console.log(Object.keys(compilation.assets)) |
| 16 | + |
| 17 | + /** output filenames for the given entry names */ |
| 18 | + const entryNames = Object.keys(compiler.options.entry) |
| 19 | + const outputFileNames = new Set( |
| 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 | + const entryOption = Array.from(outputFileNames).map(filename => ({ |
| 28 | + ...this.options, |
| 29 | + filename, |
| 30 | + }))[0] |
| 31 | + |
| 32 | + /** The public path used inside the html file */ |
| 33 | + const publicPath = this.getPublicPath( |
| 34 | + compilation, |
| 35 | + entryOption.filename, |
| 36 | + entryOption.publicPath, |
| 37 | + ) |
| 38 | + |
| 39 | + /** build output path */ |
| 40 | + const templatePath = this.getTemplatePath(entryOption.template, compilation.options) |
| 41 | + |
| 42 | + /** Generated file paths from the entry point names */ |
| 43 | + const assets = 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 | + let content = `(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 | + return content |
| 70 | + }, |
| 71 | + size() { |
| 72 | + return content.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 | + const assets = { |
| 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 | + const entryPointPublicPathMap = {} |
| 90 | + const extensionRegexp = /\.(css|js)(\?|$)/ |
| 91 | + for (let i = 0; i < entryNames.length; i++) { |
| 92 | + const entryName = entryNames[i] |
| 93 | + /** entryPointUnfilteredFiles - also includes hot module update files */ |
| 94 | + const entryPointUnfilteredFiles = compilation.entrypoints.get(entryName).getFiles() |
| 95 | + |
| 96 | + const entryPointFiles = 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 | + const asset = compilation.getAsset && compilation.getAsset(chunkFile) |
| 101 | + if (!asset) { |
| 102 | + return true |
| 103 | + } |
| 104 | + // Prevent hot-module files from being included: |
| 105 | + const assetMetaInformation = 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 | + const entryPointPublicPaths = entryPointFiles.map(chunkFile => { |
| 113 | + const urlPath = this.urlencodePath(chunkFile) |
| 114 | + const entryPointPublicPath = publicPath + urlPath |
| 115 | + |
| 116 | + if (chunkFile.endsWith('.js')) { |
| 117 | + assets.entry = urlPath |
| 118 | + } |
| 119 | + return entryPointPublicPath |
| 120 | + }) |
| 121 | + |
| 122 | + entryPointPublicPaths.forEach(entryPointPublicPath => { |
| 123 | + const extMatch = 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 | + const ext = extMatch[1] |
| 135 | + assets[ext].push(entryPointPublicPath) |
| 136 | + }) |
| 137 | + } |
| 138 | + return assets |
| 139 | + } |
| 140 | + |
| 141 | + getPublicPath(compilation, outputName, customPublicPath) { |
| 142 | + const compilationHash = 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 | + const webpackPublicPath = compilation.getAssetPath(compilation.outputOptions.publicPath, { |
| 150 | + hash: compilationHash, |
| 151 | + }) |
| 152 | + |
| 153 | + // Webpack 5 introduced "auto" as default value |
| 154 | + const isPublicPathDefined = webpackPublicPath !== 'auto' |
| 155 | + |
| 156 | + let publicPath = |
| 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 | + return publicPath |
| 177 | + } |
| 178 | + |
| 179 | + getTemplatePath(template, options) { |
| 180 | + const { context, output } = options |
| 181 | + |
| 182 | + return template === '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 | + const queryStringStart = filePath.indexOf('?') |
| 190 | + const urlPath = queryStringStart === -1 ? filePath : filePath.substr(0, queryStringStart) |
| 191 | + const queryString = filePath.substr(urlPath.length) |
| 192 | + // Encode all parts except '/' which are not part of the querystring: |
| 193 | + const encodedUrlPath = urlPath.split('/').map(encodeURIComponent).join('/') |
| 194 | + return encodedUrlPath + queryString |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +JSEntryWebpackPlugin.version = 1 |
| 199 | + |
| 200 | +module.exports = JSEntryWebpackPlugin |
0 commit comments