-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove replace inject plugin with built-in copy; remove uglify
BREAKING CHANGE: requires webpack >=5 fixes #58
- Loading branch information
Showing
10 changed files
with
497 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
"functions": 90, | ||
"statements": 90, | ||
"exclude": [ | ||
"test/**" | ||
"test/**", | ||
"src/app/utils/resolveDirectory.ts" | ||
] | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { type LoaderContext } from 'webpack'; | ||
import { registry } from './plugin'; | ||
|
||
type LoaderOptions = { | ||
id: string; | ||
}; | ||
export function injectLoader(this: LoaderContext<LoaderOptions>, source: string | Buffer) { | ||
const options = this.getOptions(); | ||
let func = (arg: any) => ''; | ||
if (registry[options.id]) { | ||
func = registry[options.id]; | ||
} | ||
|
||
const rtn: any = func.call(this, source); | ||
|
||
if (rtn instanceof Promise) { | ||
const callback = this.async(); | ||
rtn.then((result) => { | ||
callback && callback(null, result); | ||
}).catch((err) => { | ||
callback && callback(err, undefined); | ||
}); | ||
return undefined; | ||
} | ||
|
||
return rtn; | ||
} | ||
|
||
export default injectLoader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import path from 'path'; | ||
import { Compiler, Entry, WebpackPluginInstance } from 'webpack'; | ||
|
||
type EntryType = string | string[] | Entry | (() => Promise<EntryType>) | any; | ||
type EntryFilterFunction = (entryName: string) => boolean; | ||
type EntryFilterType = string | EntryFilterFunction; | ||
|
||
export type Loader = () => string; | ||
|
||
export const registry: { | ||
[key: string]: Loader; | ||
} = {}; | ||
|
||
let uniqueIDCounter = 0; | ||
function getUniqueID() { | ||
const id = (++uniqueIDCounter).toString(16); | ||
|
||
return `webpack-inject-module-${id}`; | ||
} | ||
|
||
export enum ENTRY_ORDER { | ||
First = 1, | ||
Last, | ||
NotLast, | ||
} | ||
|
||
export interface IInjectOptions { | ||
entryName?: EntryFilterType; | ||
entryOrder?: ENTRY_ORDER; | ||
loaderID?: string; | ||
} | ||
|
||
function injectToArray(originalEntry: string[], newEntry: string, entryOrder = ENTRY_ORDER.NotLast): string[] { | ||
if (entryOrder === ENTRY_ORDER.First) { | ||
return [newEntry, ...originalEntry]; | ||
} | ||
|
||
if (entryOrder === ENTRY_ORDER.Last) { | ||
return [...originalEntry, newEntry]; | ||
} | ||
|
||
return [ | ||
...originalEntry.splice(0, originalEntry.length - 1), | ||
newEntry, | ||
...originalEntry.splice(originalEntry.length - 1), | ||
]; | ||
} | ||
|
||
function createEntryFilter(filterOption?: EntryFilterType): EntryFilterFunction { | ||
if (filterOption === null || filterOption === undefined) { | ||
return () => true; | ||
} | ||
|
||
if (typeof filterOption === 'string') { | ||
return (entryName: string) => filterOption === entryName; | ||
} | ||
|
||
if (typeof filterOption === 'function') { | ||
return filterOption; | ||
} | ||
|
||
throw new Error(`Unknown entry filter: ${typeof filterOption}`); | ||
} | ||
|
||
export function injectEntry( | ||
originalEntry: EntryType | undefined, | ||
newEntry: string, | ||
options: IInjectOptions, | ||
): EntryType { | ||
if (originalEntry === undefined) { | ||
return newEntry; | ||
} | ||
|
||
const filterFunc = createEntryFilter(options.entryName); | ||
|
||
// Last module in an array gets exported, so the injected one must not be | ||
// last. https://webpack.github.io/docs/configuration.html#entry | ||
|
||
if (typeof originalEntry === 'string') { | ||
return injectToArray([originalEntry], newEntry, options.entryOrder); | ||
} | ||
|
||
if (Array.isArray(originalEntry)) { | ||
return injectToArray(originalEntry, newEntry, options.entryOrder); | ||
} | ||
|
||
if (typeof originalEntry === 'function') { | ||
// The entry function is meant to be called on each compilation (when using --watch, webpack-dev-server) | ||
// We wrap the original function in our own function to reflect this behavior. | ||
return async () => { | ||
const callbackOriginEntry = await originalEntry(); | ||
|
||
// Safe type-cast here because callbackOriginEntry cannot be an EntryFunc, | ||
// so the injectEntry call won't return one either. | ||
return injectEntry(callbackOriginEntry, newEntry, options); | ||
}; | ||
} | ||
if (Object.prototype.toString.call(originalEntry).slice(8, -1) === 'Object') { | ||
return Object.entries(originalEntry).reduce((a: Record<string, EntryType>, [key, entry]) => { | ||
if (filterFunc(key) || key === 'import') { | ||
a[key] = injectEntry(entry, newEntry, options); | ||
} else { | ||
a[key] = entry; | ||
} | ||
|
||
return a; | ||
}, {}); | ||
} | ||
|
||
return originalEntry; | ||
} | ||
|
||
export class WebpackInjectPlugin implements WebpackPluginInstance { | ||
private readonly options: IInjectOptions; | ||
|
||
private readonly loader: Loader; | ||
|
||
constructor(loader: Loader, options?: IInjectOptions) { | ||
this.loader = loader; | ||
this.options = { | ||
entryName: (options && options.entryName) || undefined, | ||
entryOrder: (options && options.entryOrder) || ENTRY_ORDER.NotLast, | ||
loaderID: (options && options.loaderID) || getUniqueID(), | ||
}; | ||
} | ||
|
||
apply(compiler: Compiler) { | ||
const id = this.options.loaderID!; | ||
const newEntry = path.resolve(__dirname, `loader?id=${id}!`); | ||
registry[id] = this.loader; | ||
compiler.options.entry = injectEntry(compiler.options.entry, newEntry, this.options); | ||
compiler.options.resolveLoader.extensions = ['.ts', '.js']; | ||
} | ||
} |
Oops, something went wrong.