-
Notifications
You must be signed in to change notification settings - Fork 2
fix(print-ready-pdfs): make plugin compatible with Webpack 5 #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Add test script to verify plugin works with Webpack 5 bundler (Angular). Currently fails due to Node.js core module imports (module, path). Relates to: imgly/ubq#11471
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
24fdd67 to
156c099
Compare
Webpack 5 removed automatic Node.js core module polyfills. This caused build failures when bundling the plugin because it statically analyzed imports of 'module', 'path', 'fs', and 'url' even though they were only used in Node.js runtime code paths. Changes: - Use indirect dynamic import via `new Function()` to prevent Webpack from statically analyzing Node.js module imports in production builds - Use direct imports in test environments (vitest) where new Function doesn't have access to the module system's dynamic import callback - Post-process gs.js during build to add `webpackIgnore` magic comments to Emscripten-generated Node.js imports Fixes: imgly/ubq#11471
156c099 to
c41bf8e
Compare
Elia-Darwish
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Review Summary
Critical Issues (2 found)
1. Silent failure in CSP-restricted environments
Files: pdfx.ts:154-161, ghostscript-module.ts:38-42
The indirect import pattern using new Function will throw an EvalError in environments with strict Content Security Policy (CSP) that disallows unsafe-eval. This affects Electron apps, sandboxed Node.js, and corporate environments.
Recommendation: Wrap the function creation in try-catch with meaningful error message and fallback to direct import.
2. Redundant regex pattern in addWebpackIgnoreComments
File: esbuild/config.mjs:24-30
The second regex pattern is a subset of the first and is redundant. The first pattern already matches both single and double quotes.
Fix: Remove the redundant second pattern from the array.
Important Issues (4 found)
3. Missing error context on dynamic import failures
Files: pdfx.ts:162-164, ghostscript-module.ts:44-48
If dynamic imports fail, users get cryptic errors like Cannot read properties of undefined instead of actionable messages about ICC profile loading or Ghostscript module initialization.
4. Inconsistent isTestEnv check pattern
Files: pdfx.ts:151-152 vs ghostscript-module.ts:33-35
pdfx.ts lacks the typeof process !== "undefined" guard that ghostscript-module.ts has, which could cause ReferenceError in some browser environments.
5. Comment inaccuracy about vitest
Files: pdfx.ts:149, ghostscript-module.ts:31
The comment "where new Function does not work" is technically inaccurate. The function constructor works in vitest Node.js environment. The real reason is likely test tooling compatibility (mocking, coverage, debugging).
6. No unit test for addWebpackIgnoreComments function
File: esbuild/config.mjs:15-35
The core transform function has no unit tests. If the regex breaks or gs.js format changes, there is no safety net.
Suggestions (4 found)
- Test name misleading:
webpack5-angular-test.shdoes not use Angular - consider renaming towebpack5-compatibility-test.sh - Test uses cached project directory: Only creates project if it does not exist, which can lead to stale configurations
- No verification that dist/gs.js is transformed: Test does not verify
webpackIgnorecomments were added - Quote style not preserved: Single-quote imports are converted to double-quotes
Strengths
- Good documentation with links to GitHub issue #11471
- Defense in depth approach (gs.js transformation + indirect imports)
- Appropriate integration test that actually runs Webpack 5
- Clean .gitignore for test artifacts
Recommended Action
- Remove redundant regex pattern in addWebpackIgnoreComments (the first pattern already matches both single and double quotes) - Rename webpack5-angular-test.sh to webpack5-compatibility-test.sh (the test doesn't use Angular) - Fix misleading comment about vitest - the issue is that indirect imports bypass vitest's mocking system, not that new Function doesn't work
Review ResponseThank you for the detailed review! I've analyzed each issue and addressed the ones with merit. Changes Made (d0ff834)✅ Critical Issue #2: Redundant regex pattern ✅ Suggestion: Test name misleading ✅ Important Issue #5: Comment inaccuracy about vitest Issues That Don't Require Changes❌ Critical Issue #1: CSP-restricted environments ❌ Important Issue #3: Missing error context ❌ Important Issue #4: Inconsistent isTestEnv check
|
…to Node.js Address PR review feedback about potential CSP failures with new Function(). CSP is a browser security mechanism and doesn't apply to Node.js environments where this code path executes.
- Add changelog entry for Webpack 5 compatibility fix - Fix test:webpack5 script to reference renamed test file
Summary
Fixes Webpack 5 / Angular compatibility issue where the plugin failed to build due to Node.js core module imports being statically analyzed.
gs.jsduring build to addwebpackIgnoremagic commentsProblem
Webpack 5 removed automatic Node.js core module polyfills. The plugin had imports of
module,path,fs, andurlthat were only used in Node.js runtime code paths, but Webpack 5 statically analyzed them and failed with:Solution
For
gs.js(Emscripten-generated): Post-process during build to add/* webpackIgnore: true */comments.For TypeScript code: Use indirect dynamic import via
new Function()that Webpack can't statically analyze, with fallback to direct imports in test environments (vitest):Test plan
pnpm run test:webpack5- Webpack 5 bundling test passespnpm run test:silent- Vitest integration tests pass (5/5)pnpm run test:node- Node.js runtime test passesFixes: https://github.com/imgly/ubq/issues/11471