Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
crates/jsshaker/tests/fixtures/** linguist-vendored
crates/jsshaker/tests/snapshots/** linguist-generated
crates/jsshaker/tests/fixtures/** linguist-vendored text eol=lf
crates/jsshaker/tests/snapshots/** linguist-generated text eol=lf
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ env:

jobs:
snapshot:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Setup Rust
Expand Down
3 changes: 3 additions & 0 deletions crates/jsshaker/src/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ impl Vfs for StdFs {
pub struct SingleFileFs(pub String);

impl SingleFileFs {
#[cfg(windows)]
pub const ENTRY_PATH: &'static str = "\\entry.js";
#[cfg(not(windows))]
pub const ENTRY_PATH: &'static str = "/entry.js";
}

Expand Down
42 changes: 19 additions & 23 deletions packages/rollup-plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OutputChunk, Plugin } from "rollup";
import { Options as JsShakerOptions, shakeMultiModule } from "jsshaker";
import { createFilter, FilterPattern } from "unplugin-utils";
import { normalize } from "node:path";

export interface Options {
preset?: "safest" | "recommended" | "smallest" | "disabled";
Expand Down Expand Up @@ -63,7 +64,7 @@ export default function rollupPluginJsShaker(
[entryFileName]: entrySource,
};
for (const [fileName, module] of Object.entries(bundle)) {
sources[fileName] = module.code;
sources[normalize(fileName)] = module.code;
}

const startTime = Date.now();
Expand All @@ -79,31 +80,26 @@ export default function rollupPluginJsShaker(

delete shaken.output[entryFileName];
const maxFileNameLength = Math.max(
...Object.keys(shaken.output).map((n) => n.length),
...Object.keys(bundle).map((n) => n.length),
);
let totalOriginalSize = 0;
let totalShakenSize = 0;
for (const [fileName, chunk] of Object.entries(shaken.output)) {
const module = bundle[fileName];
if (module && module.type === "chunk") {
const percentage = (
(chunk.code.length / module.code.length) *
100
).toFixed(2);
this.info(
`- ${fileName.padEnd(maxFileNameLength)} ${percentage}% (${module.code.length} -> ${chunk.code.length} bytes)`,
);
totalOriginalSize += module.code.length;
totalShakenSize += chunk.code.length;
module.code = chunk.code;
// if (chunk.sourceMapJson) {
// module.map = JSON.parse(chunk.sourceMapJson);
// }
} else {
throw new Error(
`JsShaker Vite plugin expected to find module ${fileName} in the bundle.`,
);
}
for (const [fileName, module] of Object.entries(bundle)) {
const chunk = shaken.output[normalize(fileName)];
if (!chunk) continue;
const percentage = (
(chunk.code.length / module.code.length) *
100
).toFixed(2);
this.info(
`- ${fileName.padEnd(maxFileNameLength)} ${percentage}% (${module.code.length} -> ${chunk.code.length} bytes)`,
);
totalOriginalSize += module.code.length;
totalShakenSize += chunk.code.length;
module.code = chunk.code;
// if (chunk.sourceMapJson) {
// module.map = JSON.parse(chunk.sourceMapJson);
// }
}

const totalPercentage = (
Expand Down
1 change: 1 addition & 0 deletions packages/rollup-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"jsshaker": "workspace:*"
},
"devDependencies": {
"@types/node": "^25.0.0",
"rollup": "^4.53.5",
"tsdown": "^0.18.1",
"unplugin-utils": "^0.3.1"
Expand Down
5 changes: 5 additions & 0 deletions packages/rollup-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"types": ["node"]
}
}
Loading
Loading