11import { OutputChunk , Plugin } from "rollup" ;
22import { Options as JsShakerOptions , shakeMultiModule } from "jsshaker" ;
33import { createFilter , FilterPattern } from "unplugin-utils" ;
4+ import { normalize } from "node:path" ;
45
56export interface Options {
67 preset ?: "safest" | "recommended" | "smallest" | "disabled" ;
@@ -63,7 +64,7 @@ export default function rollupPluginJsShaker(
6364 [ entryFileName ] : entrySource ,
6465 } ;
6566 for ( const [ fileName , module ] of Object . entries ( bundle ) ) {
66- sources [ fileName ] = module . code ;
67+ sources [ normalize ( fileName ) ] = module . code ;
6768 }
6869
6970 const startTime = Date . now ( ) ;
@@ -79,31 +80,26 @@ export default function rollupPluginJsShaker(
7980
8081 delete shaken . output [ entryFileName ] ;
8182 const maxFileNameLength = Math . max (
82- ...Object . keys ( shaken . output ) . map ( ( n ) => n . length ) ,
83+ ...Object . keys ( bundle ) . map ( ( n ) => n . length ) ,
8384 ) ;
8485 let totalOriginalSize = 0 ;
8586 let totalShakenSize = 0 ;
86- for ( const [ fileName , chunk ] of Object . entries ( shaken . output ) ) {
87- const module = bundle [ fileName ] ;
88- if ( module && module . type === "chunk" ) {
89- const percentage = (
90- ( chunk . code . length / module . code . length ) *
91- 100
92- ) . toFixed ( 2 ) ;
93- this . info (
94- `- ${ fileName . padEnd ( maxFileNameLength ) } ${ percentage } % (${ module . code . length } -> ${ chunk . code . length } bytes)` ,
95- ) ;
96- totalOriginalSize += module . code . length ;
97- totalShakenSize += chunk . code . length ;
98- module . code = chunk . code ;
99- // if (chunk.sourceMapJson) {
100- // module.map = JSON.parse(chunk.sourceMapJson);
101- // }
102- } else {
103- throw new Error (
104- `JsShaker Vite plugin expected to find module ${ fileName } in the bundle.` ,
105- ) ;
106- }
87+ for ( const [ fileName , module ] of Object . entries ( bundle ) ) {
88+ const chunk = shaken . output [ normalize ( fileName ) ] ;
89+ if ( ! chunk ) continue ;
90+ const percentage = (
91+ ( chunk . code . length / module . code . length ) *
92+ 100
93+ ) . toFixed ( 2 ) ;
94+ this . info (
95+ `- ${ fileName . padEnd ( maxFileNameLength ) } ${ percentage } % (${ module . code . length } -> ${ chunk . code . length } bytes)` ,
96+ ) ;
97+ totalOriginalSize += module . code . length ;
98+ totalShakenSize += chunk . code . length ;
99+ module . code = chunk . code ;
100+ // if (chunk.sourceMapJson) {
101+ // module.map = JSON.parse(chunk.sourceMapJson);
102+ // }
107103 }
108104
109105 const totalPercentage = (
0 commit comments