fix: unescape webpack/rspack paths in load and transform#592
fix: unescape webpack/rspack paths in load and transform#592
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded a utility to unescape resource paths and applied it in webpack and rspack loaders so resource IDs are unescaped before normalization; added cross-bundler test fixtures to validate handling of paths containing Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can generate a title for your PR based on the changes with custom instructions.Set the |
commit: |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
test/fixtures/hash-path/unplugin.js (1)
40-43: Optionally add explicit token-not-found guards for clearer test failures.Right now
overwrite(...)relies on implicit failure behavior when tokens are missing. A direct guard would make fixture failures more diagnosable.♻️ Suggested small robustness tweak
const s = new MagicString(code) const index = code.indexOf('msg#hash') + if (index === -1) + throw new Error(`load expected token "msg#hash" in ${JSON.stringify(id)}`) s.overwrite(index, index + 'msg#hash'.length, 'msg -> through the load hook -> __unplugin__#hash') return s.toString() @@ const s = new MagicString(code) const index = code.indexOf('__unplugin__') + if (index === -1) + throw new Error(`transform expected token "__unplugin__" in ${JSON.stringify(id)}`) s.overwrite(index, index + '__unplugin__'.length, 'transform') return {Also applies to: 57-60
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/fixtures/hash-path/unplugin.js` around lines 40 - 43, The test fixture silently assumes the token exists before calling s.overwrite; add explicit guards that check the result of code.indexOf('msg#hash') (and similarly the other occurrence around lines 57-60) and throw a clear error if indexOf returns -1 so failures show a descriptive message; locate the token search using code.indexOf('msg#hash') and the mutation via s.overwrite(...) (and the paired s.toString() return) and validate the index before calling s.overwrite to produce a meaningful test-failure message when the token is missing.test/fixtures/hash-path/__test__/build.test.ts (1)
10-38: Optional: collapse repeated bundler assertions into a table-driven loop.Current tests are solid; this is just a maintainability cleanup to reduce duplication and ease future fixture additions.
♻️ Proposed refactor
describe('hash-path hooks', () => { - it('vite', async () => { - const content = await fs.readFile(r('vite/main.js.mjs'), 'utf-8') - expect(content).toContain(expected) - }) - - it('rollup', async () => { - const content = await fs.readFile(r('rollup/main.js'), 'utf-8') - expect(content).toContain(expected) - }) - - it('webpack', async () => { - const content = await fs.readFile(r('webpack/main.js'), 'utf-8') - expect(content).toContain(expected) - }) - - it('esbuild', async () => { - const content = await fs.readFile(r('esbuild/main.js'), 'utf-8') - expect(content).toContain(expected) - }) - - it('rspack', async () => { - const content = await fs.readFile(r('rspack/main.js'), 'utf-8') - expect(content).toContain(expected) - }) - - it('farm', async () => { - const content = await fs.readFile(r('farm/main.js'), 'utf-8') - expect(content).toContain(expected) - }) + const cases: Array<[string, string]> = [ + ['vite', 'vite/main.js.mjs'], + ['rollup', 'rollup/main.js'], + ['webpack', 'webpack/main.js'], + ['esbuild', 'esbuild/main.js'], + ['rspack', 'rspack/main.js'], + ['farm', 'farm/main.js'], + ] + + for (const [name, file] of cases) { + it(name, async () => { + const content = await fs.readFile(r(file), 'utf-8') + expect(content).toContain(expected) + }) + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/fixtures/hash-path/__test__/build.test.ts` around lines 10 - 38, Tests repeat the same assertion for multiple bundlers; refactor by replacing the repeated it(...) blocks with a table-driven loop: create an array of bundler names (e.g., ['vite','rollup','webpack','esbuild','rspack','farm']), iterate over it and for each name call it(bundleName, async () => { const content = await fs.readFile(r(`${bundleName}/main.js${bundleName === 'vite' ? '.mjs' : ''}`), 'utf-8'); expect(content).toContain(expected); }); use the existing r helper, fs.readFile, and expected variable so behavior is unchanged but duplication is removed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@test/fixtures/hash-path/__test__/build.test.ts`:
- Around line 10-38: Tests repeat the same assertion for multiple bundlers;
refactor by replacing the repeated it(...) blocks with a table-driven loop:
create an array of bundler names (e.g.,
['vite','rollup','webpack','esbuild','rspack','farm']), iterate over it and for
each name call it(bundleName, async () => { const content = await
fs.readFile(r(`${bundleName}/main.js${bundleName === 'vite' ? '.mjs' : ''}`),
'utf-8'); expect(content).toContain(expected); }); use the existing r helper,
fs.readFile, and expected variable so behavior is unchanged but duplication is
removed.
In `@test/fixtures/hash-path/unplugin.js`:
- Around line 40-43: The test fixture silently assumes the token exists before
calling s.overwrite; add explicit guards that check the result of
code.indexOf('msg#hash') (and similarly the other occurrence around lines 57-60)
and throw a clear error if indexOf returns -1 so failures show a descriptive
message; locate the token search using code.indexOf('msg#hash') and the mutation
via s.overwrite(...) (and the paired s.toString() return) and validate the index
before calling s.overwrite to produce a meaningful test-failure message when the
token is missing.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 4df8609d-b47d-49b1-b362-eb9c98c3ae02
📒 Files selected for processing (17)
src/rspack/loaders/load.tssrc/rspack/loaders/transform.tssrc/utils/webpack-like.tssrc/webpack/loaders/load.tssrc/webpack/loaders/transform.tstest/fixtures/hash-path/__test__/build.test.tstest/fixtures/hash-path/bun.config.jstest/fixtures/hash-path/esbuild.config.jstest/fixtures/hash-path/farm.config.jstest/fixtures/hash-path/rollup.config.jstest/fixtures/hash-path/rspack.config.jstest/fixtures/hash-path/src/main.jstest/fixtures/hash-path/src/msg#hash.jstest/fixtures/hash-path/unplugin.jstest/fixtures/hash-path/vite.config.jstest/fixtures/hash-path/webpack.config.jstest/fixtures/load/unplugin.js
|
Tests are failing, would you also check them? Thanks |
|
I fixed one error caused by this PR, caused by added normalization. The other error is currently on main. Codex says "Bun does not currently invoke hooks for modules that are only reached through re-exports." and suggests updating some counts on Bun. I don't yet fully understand the issue, but if you'd like a commit to review for this (either in this PR or another), let me know. |
Resolves #591 by unescaping paths before passing into load and transform.
I'm open to better / less redundant ways to test. I wanted to test that all hooks unescape (or never escape), on all platforms.
Summary by CodeRabbit
Bug Fixes
Tests