Skip to content
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

Cannot split a chunk that has already been edited #208

Open
danielroe opened this issue Mar 4, 2022 · 2 comments
Open

Cannot split a chunk that has already been edited #208

danielroe opened this issue Mar 4, 2022 · 2 comments

Comments

@danielroe
Copy link

danielroe commented Mar 4, 2022

calling s.replace() after previously replacing a section in a magic string throws the following error:

 ERROR  Cannot split a chunk that has already been edited (0:63 – "const base = '/__NUXT_BASE__/'")    09:37:01

  at MagicString._splitChunk (node_modules/magic-string/dist/magic-string.cjs.js:893:10)
  at MagicString._split (node_modules/magic-string/dist/magic-string.cjs.js:883:43)
  at MagicString.overwrite (node_modules/magic-string/dist/magic-string.cjs.js:670:8)
  at node_modules/magic-string/dist/magic-string.cjs.js:1054:11
  at Array.forEach (<anonymous>)
  at MagicString.replace (node_modules/magic-string/dist/magic-string.cjs.js:1052:12)
  at Object.transform (src/plugins/dynamic-base.ts:89:11)
  at node_modules/rollup/dist/shared/rollup.js:22795:37
  at processTicksAndRejections (internal/process/task_queues.js:95:5)

cc: @antfu
related: nuxt/nuxt#13439

@danielroe
Copy link
Author

danielroe commented Mar 31, 2022

Here's an reproduction:

import MagicString from 'magic-string'

const s = new MagicString('from foo')

s.replace(/from foo/g, 'bar')
s.replace(/foo/g, 'baz')

// Cannot split a chunk that has already been edited (0:5 – "from foo")

I wouldn't expect the second .replace call to match as the replacement has already been made.

@antfu
Copy link
Collaborator

antfu commented Apr 1, 2022

https://github.com/Rich-Harris/magic-string#sreplace-regexp-substitution-

It will always match against the original string

This is by design (or limitation), since you can't calculate the offset of the replaced string. The solution I can think about is to avoid cascading replace on MagicString but inside the replacer function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants