|
| 1 | +import { createRequire } from 'node:module'; |
| 2 | +import { |
| 3 | + type RsbuildConfig, |
| 4 | + type RsbuildPlugin, |
| 5 | + type Rspack, |
| 6 | + rspack, |
| 7 | +} from '@rsbuild/core'; |
| 8 | +import { |
| 9 | + JS_EXTENSIONS_PATTERN, |
| 10 | + REACT_DIRECTIVE_REGEX, |
| 11 | + SHEBANG_PREFIX, |
| 12 | + SHEBANG_REGEX, |
| 13 | +} from '../constant'; |
| 14 | +import { importMetaUrlShim } from './shims'; |
| 15 | +const require = createRequire(import.meta.url); |
| 16 | + |
| 17 | +const PLUGIN_NAME = 'rsbuild:entry'; |
| 18 | + |
| 19 | +const matchFirstLine = (source: string, regex: RegExp) => { |
| 20 | + const [firstLine] = source.split('\n'); |
| 21 | + if (!firstLine) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + const matched = regex.exec(firstLine); |
| 25 | + if (!matched) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + return matched[0]; |
| 30 | +}; |
| 31 | + |
| 32 | +class PostEntryPlugin { |
| 33 | + private enabledImportMetaUrlShim: boolean; |
| 34 | + private shebangEntries: Record<string, string> = {}; |
| 35 | + private reactDirectives: Record<string, string> = {}; |
| 36 | + private importMetaUrlShims: Record<string, { startsWithUseStrict: boolean }> = |
| 37 | + {}; |
| 38 | + |
| 39 | + constructor({ |
| 40 | + importMetaUrlShim = true, |
| 41 | + }: { |
| 42 | + importMetaUrlShim: boolean; |
| 43 | + }) { |
| 44 | + this.enabledImportMetaUrlShim = importMetaUrlShim; |
| 45 | + } |
| 46 | + |
| 47 | + apply(compiler: Rspack.Compiler) { |
| 48 | + compiler.hooks.entryOption.tap(PLUGIN_NAME, (_context, entries) => { |
| 49 | + for (const name in entries) { |
| 50 | + const entry = (entries as Rspack.EntryStaticNormalized)[name]; |
| 51 | + if (!entry) continue; |
| 52 | + |
| 53 | + let first: string | undefined; |
| 54 | + if (Array.isArray(entry)) { |
| 55 | + first = entry[0]; |
| 56 | + } else if (Array.isArray(entry.import)) { |
| 57 | + first = entry.import[0]; |
| 58 | + } else if (typeof entry === 'string') { |
| 59 | + first = entry; |
| 60 | + } |
| 61 | + |
| 62 | + if (typeof first !== 'string') continue; |
| 63 | + |
| 64 | + const filename = first.split('?')[0]!; |
| 65 | + const isJs = JS_EXTENSIONS_PATTERN.test(filename); |
| 66 | + if (!isJs) continue; |
| 67 | + |
| 68 | + const content = compiler.inputFileSystem!.readFileSync!(filename, { |
| 69 | + encoding: 'utf-8', |
| 70 | + }); |
| 71 | + |
| 72 | + // Shebang |
| 73 | + if (content.startsWith(SHEBANG_PREFIX)) { |
| 74 | + const shebangMatch = matchFirstLine(content, SHEBANG_REGEX); |
| 75 | + if (shebangMatch) { |
| 76 | + this.shebangEntries[name] = shebangMatch; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // React directive |
| 81 | + const reactDirective = matchFirstLine(content, REACT_DIRECTIVE_REGEX); |
| 82 | + if (reactDirective) { |
| 83 | + this.reactDirectives[name] = reactDirective; |
| 84 | + } |
| 85 | + |
| 86 | + // import.meta.url shim |
| 87 | + if (this.enabledImportMetaUrlShim) { |
| 88 | + this.importMetaUrlShims[name] = { |
| 89 | + startsWithUseStrict: |
| 90 | + // This is a hypothesis that no comments will occur before "use strict;". |
| 91 | + // But it should cover most cases. |
| 92 | + content.startsWith('use strict;') || |
| 93 | + content.startsWith('"use strict";'), |
| 94 | + }; |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { |
| 100 | + compilation.hooks.chunkAsset.tap(PLUGIN_NAME, (chunk, filename) => { |
| 101 | + const isJs = JS_EXTENSIONS_PATTERN.test(filename); |
| 102 | + if (!isJs) return; |
| 103 | + |
| 104 | + const name = chunk.name; |
| 105 | + if (!name) return; |
| 106 | + |
| 107 | + const shebangEntry = this.shebangEntries[name]; |
| 108 | + if (shebangEntry) { |
| 109 | + this.shebangEntries[filename] = shebangEntry; |
| 110 | + } |
| 111 | + |
| 112 | + const reactDirective = this.reactDirectives[name]; |
| 113 | + if (reactDirective) { |
| 114 | + this.reactDirectives[filename] = reactDirective; |
| 115 | + } |
| 116 | + |
| 117 | + const importMetaUrlShimInfo = this.importMetaUrlShims[name]; |
| 118 | + if (importMetaUrlShimInfo) { |
| 119 | + this.importMetaUrlShims[filename] = importMetaUrlShimInfo; |
| 120 | + } |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => { |
| 125 | + compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets) => { |
| 126 | + const chunkAsset = Object.keys(assets); |
| 127 | + for (const name of chunkAsset) { |
| 128 | + if (this.enabledImportMetaUrlShim) { |
| 129 | + compilation.updateAsset(name, (old) => { |
| 130 | + const importMetaUrlShimInfo = this.importMetaUrlShims[name]; |
| 131 | + if (importMetaUrlShimInfo) { |
| 132 | + const replaceSource = new rspack.sources.ReplaceSource(old); |
| 133 | + |
| 134 | + if (importMetaUrlShimInfo.startsWithUseStrict) { |
| 135 | + replaceSource.replace( |
| 136 | + 0, |
| 137 | + 11, // 'use strict;'.length, |
| 138 | + `"use strict";\n${importMetaUrlShim}`, |
| 139 | + ); |
| 140 | + } else { |
| 141 | + replaceSource.insert(0, importMetaUrlShim); |
| 142 | + } |
| 143 | + |
| 144 | + return replaceSource; |
| 145 | + } |
| 146 | + |
| 147 | + return old; |
| 148 | + }); |
| 149 | + } |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + compilation.hooks.processAssets.tap( |
| 154 | + { |
| 155 | + name: PLUGIN_NAME, |
| 156 | + // Just after minify stage, to avoid from being minified. |
| 157 | + stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1, |
| 158 | + }, |
| 159 | + (assets) => { |
| 160 | + const chunkAsset = Object.keys(assets); |
| 161 | + for (const name of chunkAsset) { |
| 162 | + const shebangValue = this.shebangEntries[name]; |
| 163 | + const reactDirectiveValue = this.reactDirectives[name]; |
| 164 | + |
| 165 | + if (shebangValue || reactDirectiveValue) { |
| 166 | + compilation.updateAsset(name, (old) => { |
| 167 | + const replaceSource = new rspack.sources.ReplaceSource(old); |
| 168 | + // Shebang |
| 169 | + if (shebangValue) { |
| 170 | + replaceSource.insert(0, `${shebangValue}\n`); |
| 171 | + } |
| 172 | + |
| 173 | + // React directives |
| 174 | + if (reactDirectiveValue) { |
| 175 | + replaceSource.insert(0, `${reactDirectiveValue}\n`); |
| 176 | + } |
| 177 | + |
| 178 | + return replaceSource; |
| 179 | + }); |
| 180 | + } |
| 181 | + } |
| 182 | + }, |
| 183 | + ); |
| 184 | + }); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +const entryModuleLoaderPlugin = (): RsbuildPlugin => ({ |
| 189 | + name: PLUGIN_NAME, |
| 190 | + setup(api) { |
| 191 | + api.modifyBundlerChain((config, { CHAIN_ID }) => { |
| 192 | + const rule = config.module.rule(CHAIN_ID.RULE.JS); |
| 193 | + rule |
| 194 | + .use('shebang') |
| 195 | + .loader(require.resolve('./entryModuleLoader.js')) |
| 196 | + .options({}); |
| 197 | + }); |
| 198 | + }, |
| 199 | +}); |
| 200 | + |
| 201 | +export const composePostEntryChunkConfig = ({ |
| 202 | + importMetaUrlShim, |
| 203 | +}: { |
| 204 | + importMetaUrlShim: boolean; |
| 205 | +}): RsbuildConfig => { |
| 206 | + return { |
| 207 | + plugins: [entryModuleLoaderPlugin()], |
| 208 | + tools: { |
| 209 | + rspack: { |
| 210 | + plugins: [ |
| 211 | + new PostEntryPlugin({ |
| 212 | + importMetaUrlShim: importMetaUrlShim, |
| 213 | + }), |
| 214 | + ], |
| 215 | + }, |
| 216 | + }, |
| 217 | + }; |
| 218 | +}; |
0 commit comments