Title: `ng serve` serves broken JS (HTTP 500 / "Failed to fetch dynamically imported module") when a chunk contains a `//# sourceMappingURL=` line inside a template literal · Issue #33544 · angular/angular-cli · GitHub
Open Graph Title: `ng serve` serves broken JS (HTTP 500 / "Failed to fetch dynamically imported module") when a chunk contains a `//# sourceMappingURL=` line inside a template literal · Issue #33544 · angular/angular-cli
X Title: `ng serve` serves broken JS (HTTP 500 / "Failed to fetch dynamically imported module") when a chunk contains a `//# sourceMappingURL=` line inside a template literal · Issue #33544 · angular/angular-cli
Description: Command serve Is this a regression? Yes, this behavior used to work in the previous version The previous version in which this bug was not present was No response Description When serving with sourcemaps enabled, createAngularMemoryPlugi...
Open Graph Description: Command serve Is this a regression? Yes, this behavior used to work in the previous version The previous version in which this bug was not present was No response Description When serving with sour...
X Description: Command serve Is this a regression? Yes, this behavior used to work in the previous version The previous version in which this bug was not present was No response Description When serving with sour...
Opengraph URL: https://github.com/angular/angular-cli/issues/33544
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`ng serve` serves broken JS (HTTP 500 / \"Failed to fetch dynamically imported module\") when a chunk contains a `//# sourceMappingURL=` line inside a template literal","articleBody":"### Command\n\nserve\n\n### Is this a regression?\n\n- [x] Yes, this behavior used to work in the previous version\n\n### The previous version in which this bug was not present was\n\n_No response_\n\n### Description\n\nWhen serving with sourcemaps enabled, `createAngularMemoryPlugin`'s `load` hook removes sourcemap URL comments from the served chunk before handing it to Vite:\n\nhttps://github.com/angular/angular-cli/blob/main/packages/angular/build/src/tools/vite/plugins/angular-memory-plugin.ts#L107\n\n```ts\ncode: mapContents ? code.replace(/^\\/\\/# sourceMappingURL=[^\\r\\n]*/gm, '') : code,\n```\n\nI believe this is done to strip the chunk's own trailing comment, but the g and m flags make the regex match EVERY line in the file that starts with `//# sourceMappingURL=` , including lines that are content inside a template literal, not real comments.\n\nThe consequence is: any library that embeds bundled code as a string (the classic `worker-loader` inline-worker pattern, where a webpack-built worker bundle is embedded as a string and instantiated via `new Blob(...)`). The embedded bundle text ends with its own `//# sourceMappingURL=xxx.worker.js.map` line. The emitted chunk then looks like:\n\n```js\ncreateInlineWorker(`/******/ (() => { // webpackBootstrap\n...thousands of lines of worker code...\n//# sourceMappingURL=abc123.worker.js.map`, null);\n```\n\nNote the template literal's **closing backtick is on the same line** as the embedded comment. The `replace` deletes that entire line — closing backtick and `, null);` included — producing an unterminated template literal. Vite's import-analysis then fails to parse the chunk and the dev server responds 500 with:\n\n```\nFailed to parse source for import analysis because the content contains invalid JS syntax.\n```\n\nIn the browser, if the chunk is lazy, this surfaces as:\n\n```\nTypeError: Failed to fetch dynamically imported module: https://.../my-component.entry-XXXXXXXX.js\n```\n\nImportant to note is that `ng build` is unaffected because the plugin only runs in the dev server.\n\n\n### Minimal Reproduction\n\n1. `ng new repro --defaults && cd repro`\n2. Add to `src/main.ts` (this simulates what a bundled inline-worker dependency looks like after esbuild re-emits it — a string with mixed quotes and `\\n` escapes is printed as a multiline template literal):\n\n```ts\nexport const workerSource =\n 'console.log(\"double\") + console.log(\\'single\\');\\n' +\n 'console.log(\"double\") + console.log(\\'single\\');\\n' +\n '//# sourceMappingURL=inline-worker.js.map';\nconsole.log(workerSource.length);\n```\n\n3. `ng serve` (development configuration, `sourceMap` enabled — the default)\n4. Open the app. The served `main.js` contains an unterminated template literal and fails to parse; Vite returns 500 with `Failed to parse source for import analysis`.\n\nYou can observe the mechanism standalone: `esbuild` emits the string above as\n\n```js\nvar workerSource = `console.log(\"double\") + console.log('single');\nconsole.log(\"double\") + console.log('single');\n//# sourceMappingURL=inline-worker.js.map`;\n```\n\nand applying `code.replace(/^\\/\\/# sourceMappingURL=[^\\r\\n]*/gm, '')` to it removes the last line including the closing backtick — invalid JS.\n\n### Exception or Error\n\n```text\n[vite] Internal server error: Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.\n Plugin: vite:import-analysis\n File: .angular/vite-root/\u003cproject>/chunk-XXXXXXXX.js\n\n\nBrowser (when the broken chunk is a dependency of a lazy chunk):\n\n\nTypeError: Failed to fetch dynamically imported module: https://localhost:4200/.../component.entry-XXXXXXXX.js\n```\n\n### Your Environment\n\n```text\n- Angular CLI / `@angular/build`: 22.0.0 (regex verified still present in 22.0.5 and on `main`)\n- Vite: 7.3.2\n- Node: 24.x\n- OS: Linux\n```\n\n### Anything else relevant?\n\nSuggested fix — only strip the chunk's own trailing comment by anchoring the match to the end of the file instead of using `g`/`m`:\n\n```ts\ncode: mapContents ? code.replace(/(^|\\n)\\/\\/# sourceMappingURL=[^\\r\\n]*\\s*$/, '$1') : code,\n```\n\nAs a workaround we've been running this as a `patch-package` patch and it fixes the issue. Mid-file occurrences (which are always string content, since esbuild already consumes real sourcemap comments during bundling) are preserved, and Vite still appends its own inline map when serving.\n\nThe same greedy pattern also appears in `javascript-transformer-worker.ts` (`data.replace(/^\\/\\/# sourceMappingURL=[^\\r\\n]*/gm, '')`) and may deserve the same anchoring, although we have not observed a failure through that path.","author":{"url":"https://github.com/roman-garcia","@type":"Person","name":"roman-garcia"},"datePublished":"2026-07-10T12:54:22Z","url":"https://github.com/angular/angular-cli/issues/33544"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:04f8dfb4-4982-d132-fe90-2ed75e7bcb22 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A2CE:21AC3F:16B6A4F:1CD1942:6B011EB6 |
| html-safe-nonce | b64c0834cb251785a48ebf298fbce4d46a349420079fe7dd3bf8c68d3590fcea |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMkNFOjIxQUMzRjoxNkI2QTRGOjFDRDE5NDI6NkIwMTFFQjYiLCJ2aXNpdG9yX2lkIjoiMjM5MTQ3ODIxNjQ0MDIyNTQ2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 73b625d3bbb058b80e49ddbac718ffd22f5a064c8fc6cf75998a798d4fcc4835 |
| hovercard-subject-tag | issue:4855173195 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular-cli/33544/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5e8458ae54598aa9011414e0982e73db3fa2fcad74c5744624f5f1dec001c8ff/angular/angular-cli/issues/33544 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5e8458ae54598aa9011414e0982e73db3fa2fcad74c5744624f5f1dec001c8ff/angular/angular-cli/issues/33544 |
| og:image:alt | Command serve Is this a regression? Yes, this behavior used to work in the previous version The previous version in which this bug was not present was No response Description When serving with sour... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | roman-garcia |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular-cli git https://github.com/angular/angular-cli.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 36891867 |
| octolytics-dimension-repository_nwo | angular/angular-cli |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 36891867 |
| octolytics-dimension-repository_network_root_nwo | angular/angular-cli |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width