|
| 1 | +import path from 'path'; |
| 2 | +import { Compiler, Entry, WebpackPluginInstance } from 'webpack'; |
| 3 | + |
| 4 | +type EntryType = string | string[] | Entry | (() => Promise<EntryType>) | any; |
| 5 | +type EntryFilterFunction = (entryName: string) => boolean; |
| 6 | +type EntryFilterType = string | EntryFilterFunction; |
| 7 | + |
| 8 | +export type Loader = () => string; |
| 9 | + |
| 10 | +export const registry: { |
| 11 | + [key: string]: Loader; |
| 12 | +} = {}; |
| 13 | + |
| 14 | +let uniqueIDCounter = 0; |
| 15 | +function getUniqueID() { |
| 16 | + const id = (++uniqueIDCounter).toString(16); |
| 17 | + |
| 18 | + return `webpack-inject-module-${id}`; |
| 19 | +} |
| 20 | + |
| 21 | +export enum ENTRY_ORDER { |
| 22 | + First = 1, |
| 23 | + Last, |
| 24 | + NotLast, |
| 25 | +} |
| 26 | + |
| 27 | +export interface IInjectOptions { |
| 28 | + entryName?: EntryFilterType; |
| 29 | + entryOrder?: ENTRY_ORDER; |
| 30 | + loaderID?: string; |
| 31 | +} |
| 32 | + |
| 33 | +function injectToArray(originalEntry: string[], newEntry: string, entryOrder = ENTRY_ORDER.NotLast): string[] { |
| 34 | + if (entryOrder === ENTRY_ORDER.First) { |
| 35 | + return [newEntry, ...originalEntry]; |
| 36 | + } |
| 37 | + |
| 38 | + if (entryOrder === ENTRY_ORDER.Last) { |
| 39 | + return [...originalEntry, newEntry]; |
| 40 | + } |
| 41 | + |
| 42 | + return [ |
| 43 | + ...originalEntry.splice(0, originalEntry.length - 1), |
| 44 | + newEntry, |
| 45 | + ...originalEntry.splice(originalEntry.length - 1), |
| 46 | + ]; |
| 47 | +} |
| 48 | + |
| 49 | +function createEntryFilter(filterOption?: EntryFilterType): EntryFilterFunction { |
| 50 | + if (filterOption === null || filterOption === undefined) { |
| 51 | + return () => true; |
| 52 | + } |
| 53 | + |
| 54 | + if (typeof filterOption === 'string') { |
| 55 | + return (entryName: string) => filterOption === entryName; |
| 56 | + } |
| 57 | + |
| 58 | + if (typeof filterOption === 'function') { |
| 59 | + return filterOption; |
| 60 | + } |
| 61 | + |
| 62 | + throw new Error(`Unknown entry filter: ${typeof filterOption}`); |
| 63 | +} |
| 64 | + |
| 65 | +export function injectEntry( |
| 66 | + originalEntry: EntryType | undefined, |
| 67 | + newEntry: string, |
| 68 | + options: IInjectOptions, |
| 69 | +): EntryType { |
| 70 | + if (originalEntry === undefined) { |
| 71 | + return newEntry; |
| 72 | + } |
| 73 | + |
| 74 | + const filterFunc = createEntryFilter(options.entryName); |
| 75 | + |
| 76 | + // Last module in an array gets exported, so the injected one must not be |
| 77 | + // last. https://webpack.github.io/docs/configuration.html#entry |
| 78 | + |
| 79 | + if (typeof originalEntry === 'string') { |
| 80 | + return injectToArray([originalEntry], newEntry, options.entryOrder); |
| 81 | + } |
| 82 | + |
| 83 | + if (Array.isArray(originalEntry)) { |
| 84 | + return injectToArray(originalEntry, newEntry, options.entryOrder); |
| 85 | + } |
| 86 | + |
| 87 | + if (typeof originalEntry === 'function') { |
| 88 | + // The entry function is meant to be called on each compilation (when using --watch, webpack-dev-server) |
| 89 | + // We wrap the original function in our own function to reflect this behavior. |
| 90 | + return async () => { |
| 91 | + const callbackOriginEntry = await originalEntry(); |
| 92 | + |
| 93 | + // Safe type-cast here because callbackOriginEntry cannot be an EntryFunc, |
| 94 | + // so the injectEntry call won't return one either. |
| 95 | + return injectEntry(callbackOriginEntry, newEntry, options); |
| 96 | + }; |
| 97 | + } |
| 98 | + if (Object.prototype.toString.call(originalEntry).slice(8, -1) === 'Object') { |
| 99 | + return Object.entries(originalEntry).reduce((a: Record<string, EntryType>, [key, entry]) => { |
| 100 | + if (filterFunc(key) || key === 'import') { |
| 101 | + a[key] = injectEntry(entry, newEntry, options); |
| 102 | + } else { |
| 103 | + a[key] = entry; |
| 104 | + } |
| 105 | + |
| 106 | + return a; |
| 107 | + }, {}); |
| 108 | + } |
| 109 | + |
| 110 | + return originalEntry; |
| 111 | +} |
| 112 | + |
| 113 | +export class WebpackInjectPlugin implements WebpackPluginInstance { |
| 114 | + private readonly options: IInjectOptions; |
| 115 | + |
| 116 | + private readonly loader: Loader; |
| 117 | + |
| 118 | + constructor(loader: Loader, options?: IInjectOptions) { |
| 119 | + this.loader = loader; |
| 120 | + this.options = { |
| 121 | + entryName: (options && options.entryName) || undefined, |
| 122 | + entryOrder: (options && options.entryOrder) || ENTRY_ORDER.NotLast, |
| 123 | + loaderID: (options && options.loaderID) || getUniqueID(), |
| 124 | + }; |
| 125 | + } |
| 126 | + |
| 127 | + apply(compiler: Compiler) { |
| 128 | + const id = this.options.loaderID!; |
| 129 | + const newEntry = path.resolve(__dirname, `loader?id=${id}!`); |
| 130 | + registry[id] = this.loader; |
| 131 | + compiler.options.entry = injectEntry(compiler.options.entry, newEntry, this.options); |
| 132 | + compiler.options.resolveLoader.extensions = ['.ts', '.js']; |
| 133 | + } |
| 134 | +} |
0 commit comments