Skip to content

Commit 56260a6

Browse files
Mathichakermanx
andauthored
fix: normalize path on windows (#61)
* fix: SingleFileFs on windows * fix: normalize filename so it work on windows * fix: stabilize snapshots across platforms --------- Co-authored-by: _Kerman <kermanx@qq.com>
1 parent 67336be commit 56260a6

7 files changed

Lines changed: 125 additions & 216 deletions

File tree

‎.gitattributes‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
crates/jsshaker/tests/fixtures/** linguist-vendored
2-
crates/jsshaker/tests/snapshots/** linguist-generated
1+
crates/jsshaker/tests/fixtures/** linguist-vendored text eol=lf
2+
crates/jsshaker/tests/snapshots/** linguist-generated text eol=lf

‎.github/workflows/test.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ env:
1111

1212
jobs:
1313
snapshot:
14-
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
os: [ubuntu-latest, windows-latest]
17+
runs-on: ${{ matrix.os }}
1518
steps:
1619
- uses: actions/checkout@v4
1720
- name: Setup Rust

‎crates/jsshaker/src/vfs.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ impl Vfs for StdFs {
5959
pub struct SingleFileFs(pub String);
6060

6161
impl SingleFileFs {
62+
#[cfg(windows)]
63+
pub const ENTRY_PATH: &'static str = "\\entry.js";
64+
#[cfg(not(windows))]
6265
pub const ENTRY_PATH: &'static str = "/entry.js";
6366
}
6467

‎packages/rollup-plugin/index.ts‎

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { OutputChunk, Plugin } from "rollup";
22
import { Options as JsShakerOptions, shakeMultiModule } from "jsshaker";
33
import { createFilter, FilterPattern } from "unplugin-utils";
4+
import { normalize } from "node:path";
45

56
export 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 = (

‎packages/rollup-plugin/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"jsshaker": "workspace:*"
2828
},
2929
"devDependencies": {
30+
"@types/node": "^25.0.0",
3031
"rollup": "^4.53.5",
3132
"tsdown": "^0.18.1",
3233
"unplugin-utils": "^0.3.1"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"types": ["node"]
4+
}
5+
}

0 commit comments

Comments
 (0)