-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify
SondaWebpackPlugin
to work in both webpack and rspack
- Loading branch information
1 parent
4a3ad06
commit 5335e31
Showing
4 changed files
with
35 additions
and
114 deletions.
There are no files selected for viewing
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,69 @@ | ||
import { join } from 'path'; | ||
import { normalizeOptions, normalizePath } from '../utils'; | ||
import { generateReportFromAssets } from '../report/generate'; | ||
import type { Compiler, StatsModule } from 'webpack'; | ||
import type { Options, ModuleFormat, JsonReport } from '../types'; | ||
import { NormalModule, type Compiler, type Module } from 'webpack'; | ||
|
||
const jsRegexp = /\.[c|m]?[t|j]s[x]?$/; | ||
|
||
export class SondaWebpackPlugin { | ||
options: Partial<Options>; | ||
inputs: JsonReport[ 'inputs' ]; | ||
|
||
constructor ( options?: Partial<Options> ) { | ||
this.options = options || {}; | ||
this.inputs = {}; | ||
} | ||
|
||
apply( compiler: Compiler ): void { | ||
compiler.options.output.devtoolModuleFilenameTemplate = '[absolute-resource-path]'; | ||
|
||
compiler.hooks.compilation.tap( 'SondaWebpackPlugin', ( compilation ) => { | ||
compilation.hooks.optimizeModules.tap( 'SondaWebpackPlugin', ( modules ) => { | ||
Array | ||
.from( modules ) | ||
.forEach( module => { | ||
if ( !isNormalModule( module ) ) { | ||
return; | ||
} | ||
compiler.hooks.afterEmit.tapPromise( 'SondaWebpackPlugin', compilation => { | ||
const inputs: JsonReport[ 'inputs' ] = {}; | ||
const stats = compilation.getStats().toJson( { | ||
modules: true, | ||
providedExports: true | ||
} ); | ||
|
||
const imports = module.dependencies.reduce( ( acc, dependency ) => { | ||
const module = compilation.moduleGraph.getModule( dependency ); | ||
const outputPath = stats.outputPath || compiler.outputPath; | ||
const modules = stats.modules?.filter( mod => mod.nameForCondition && mod.moduleType !== 'asset/inline' ) || []; | ||
|
||
if ( isNormalModule( module ) ) { | ||
acc.push( normalizePath( module.resource ) ); | ||
} | ||
modules.forEach( module => { | ||
const imports = modules.reduce( ( acc, { nameForCondition, issuerName, reasons } ) => { | ||
if ( issuerName === module.name || reasons?.some( reason => reason.resolvedModule === module.name ) ) { | ||
acc.push( normalizePath( nameForCondition! ) ); | ||
} | ||
|
||
return acc; | ||
}, [] as Array<string> ); | ||
return acc; | ||
}, [] as Array<string> ); | ||
|
||
this.inputs[ normalizePath( module.resource ) ] = { | ||
bytes: module.size(), | ||
format: getFormat( module ), | ||
imports, | ||
belongsTo: null, | ||
}; | ||
} ); | ||
inputs[ normalizePath( module.nameForCondition! ) ] = { | ||
bytes: module.size || 0, | ||
format: getFormat( module ), | ||
imports, | ||
belongsTo: null | ||
}; | ||
} ); | ||
} ); | ||
|
||
compiler.hooks.emit.tapAsync( 'SondaWebpackPlugin', ( compilation, callback ) => { | ||
const outputPath = compiler.options.output.path || compiler.outputPath || process.cwd(); | ||
const assets = Object.keys( compilation.assets ).map( name => join( outputPath, name ) ); | ||
|
||
generateReportFromAssets( | ||
assets, | ||
this.inputs, | ||
return generateReportFromAssets( | ||
stats.assets?.map( asset => join( outputPath, asset.name ) ) || [], | ||
inputs, | ||
normalizeOptions( this.options ) | ||
) | ||
.then(() => callback()); | ||
); | ||
} ); | ||
} | ||
} | ||
|
||
function getFormat( module: NormalModule ): ModuleFormat { | ||
if ( !jsRegexp.test( module.resource ) ) { | ||
function getFormat( module: StatsModule ): ModuleFormat { | ||
if ( !jsRegexp.test( module.nameForCondition! ) ) { | ||
return 'unknown'; | ||
} | ||
|
||
if ( module.type === 'javascript/esm' || module.buildMeta?.exportsType === 'namespace' ) { | ||
if ( module.moduleType === 'javascript/esm' ) { | ||
return 'esm'; | ||
} | ||
|
||
return 'cjs'; | ||
} | ||
if ( Array.isArray( module.providedExports ) && module.providedExports.length > 0 ) { | ||
return 'esm'; | ||
} | ||
|
||
function isNormalModule( module: Module | NormalModule | null ): module is NormalModule { | ||
return module !== null && 'resource' in module; | ||
return 'cjs'; | ||
} |
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