Skip to content

Commit

Permalink
Add support for rspack
Browse files Browse the repository at this point in the history
  • Loading branch information
filipsobol committed Oct 9, 2024
1 parent e825cfa commit 2212bd4
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/sonda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"open": "^10.1.0"
},
"devDependencies": {
"@rspack/core": "^1.0.8",
"esbuild": "^0.23.1",
"load-source-map": "workspace:^*",
"rollup": "^4.24.0",
Expand Down
92 changes: 92 additions & 0 deletions packages/sonda/src/bundlers/rspack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { join } from 'path';
import { normalizeOptions, normalizePath } from '../utils';
import { generateReportFromAssets } from '../report/generate';
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 SondaRspackPlugin {
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]';

const importsMap = new Map<string, string[]>();

compiler.hooks.compilation.tap( 'SondaWebpackPlugin', ( compilation, { normalModuleFactory } ) => {
normalModuleFactory.hooks.afterResolve.tap( 'SondaWebpackPlugin', resolveData => {
const importer = resolveData.contextInfo.issuer;
const imported = resolveData.createData.resource;

if ( !importer || !imported ) {
return;
}

if ( !importsMap.has( importer ) ) {
importsMap.set( importer, [] );
}

const imports = importsMap.get( importer )!;

imports.push( imported );
importsMap.set( importer, imports );
} );

compilation.hooks.optimizeModules.tap( 'SondaWebpackPlugin', ( modules ) => {
Array
.from( modules )
.forEach( module => {
if ( !isNormalModule( module ) ) {
return;
}

if ( !module.resource ) {
return;
}

this.inputs[ normalizePath( module.resource ) ] = {
bytes: module.size(),
format: getFormat( module ),
imports: importsMap.get( module.resource ) || [],
belongsTo: null,
};
} );
} );
} );

compiler.hooks.afterEmit.tapAsync( 'SondaWebpackPlugin', ( compilation, callback ) => {
const outputPath = compiler.options.output.path || compiler.outputPath || process.cwd();
const assets = compilation.getAssets().map( asset => join( outputPath, asset.name ) );

generateReportFromAssets(
assets,
this.inputs,
normalizeOptions( this.options )
)
.then( () => callback() );
} );
}
}

function getFormat( module: NormalModule ): ModuleFormat {
if ( !jsRegexp.test( module.resource ) ) {
return 'unknown';
}

if ( module.type === 'javascript/esm' || module.buildMeta?.exportsType === 'namespace' ) {
return 'esm';
}

return 'cjs';
}

function isNormalModule( module: Module | NormalModule | null ): module is NormalModule {
return module !== null && 'resource' in module;
}
1 change: 1 addition & 0 deletions packages/sonda/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { SondaEsbuildPlugin } from './bundlers/esbuild.js';
export { SondaRollupPlugin } from './bundlers/rollup.js';
export { SondaRspackPlugin } from './bundlers/rspack.js';
export { SondaWebpackPlugin } from './bundlers/webpack.js';

export type {
Expand Down
2 changes: 1 addition & 1 deletion playground/rspack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "1.0.0",
"scripts": {
"dev": "rsbuild dev --open",
"build-----skip": "rsbuild build",
"build": "rsbuild build",
"preview": "rsbuild preview"
},
"dependencies": {
Expand Down
5 changes: 3 additions & 2 deletions playground/rspack/rsbuild.config.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { defineConfig } from '@rsbuild/core';
import { SondaWebpackPlugin } from 'sonda';
import { SondaRspackPlugin } from 'sonda';

export default defineConfig( {
tools: {
rspack: {
devtool: 'source-map',
plugins: [
new SondaWebpackPlugin( {
new SondaRspackPlugin( {
gzip: true,
brotli: true,
detailed: true
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2212bd4

Please sign in to comment.