-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
118 additions
and
67 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
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,95 @@ | ||
import type { Compiler, Compilation as CompilationType, sources } from 'webpack'; | ||
|
||
/** | ||
* Gets the normal module hook for webpack 4 & webpack 5. | ||
* | ||
* @returns | ||
*/ | ||
export const getNormalModuleHook = ( | ||
compiler: Compiler, | ||
compilation: CompilationType | ||
): CompilationType['hooks']['normalModuleLoader'] => { | ||
const { NormalModule } = | ||
// Webpack 5 flow | ||
compiler.webpack || | ||
// Webpack 4 flow | ||
require('webpack'); | ||
|
||
const normalModuleHook = | ||
NormalModule && typeof NormalModule.getCompilationHooks !== 'undefined' | ||
? // Webpack 5 flow | ||
NormalModule.getCompilationHooks(compilation).loader | ||
: // Webpack 4 flow | ||
compilation.hooks.normalModuleLoader; | ||
|
||
return normalModuleHook; | ||
}; | ||
|
||
/** | ||
* Returns the string representation of an assets source. | ||
* | ||
* @param source | ||
* @returns | ||
*/ | ||
export const getAssetSourceContents = (assetSource: sources.Source): string => { | ||
const source = assetSource.source(); | ||
if (typeof source === 'string') { | ||
return source; | ||
} | ||
|
||
return source.toString(); | ||
}; | ||
|
||
/** | ||
* Returns a webpack 4 & 5 compatible hook for optimizing assets. | ||
* | ||
* @param compilation | ||
* @returns | ||
*/ | ||
export const getOptimizeAssetsHook = ( | ||
compiler: Compiler, | ||
compilation: CompilationType | ||
): { tap: CompilationType['hooks']['processAssets']['tap'] } => { | ||
const { Compilation, version } = | ||
// Webpack 5 flow | ||
compiler.webpack || | ||
// Webpack 4 flow | ||
require('webpack'); | ||
const isWebpack4 = version.startsWith('4.'); | ||
const optimizeAssets = | ||
// Webpack 5 flow | ||
compilation.hooks.processAssets || | ||
// Webpack 4 flow | ||
compilation.hooks.optimizeAssets; | ||
|
||
return { | ||
tap: (pluginName: string, callback: (assets: CompilationType['assets']) => void) => { | ||
optimizeAssets.tap( | ||
isWebpack4 | ||
? pluginName | ||
: { | ||
name: pluginName, | ||
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, | ||
}, | ||
callback | ||
); | ||
}, | ||
}; | ||
}; | ||
|
||
/** | ||
* Returns webpack 4 & 5 compatible sources. | ||
* @returns | ||
*/ | ||
export const getSources = (compiler: Compiler): typeof sources => { | ||
const { sources } = | ||
// Webpack 5 flow | ||
compiler.webpack; | ||
|
||
return ( | ||
// Webpack 5 flow | ||
sources || | ||
// Webpack 4 flow | ||
require('webpack-sources') | ||
); | ||
}; |