3

I want to replace all instances of

() => import('@/components/something/Something')

with

() => import(/* webpackChunkName: "components--something--Something" */ '@/components/something/Something')

So far I have this find/replace regex

Find: \(\) => import\('(.+?)'\)

Replace: () => import(/* webpackChunkName: "$1" */ '$1')

Which replaces:

() => import('@/components/something/Something')

with

() => import(/* webpackChunkName: "@/components/something/Something" */ '@/components/something/Something')

My problem is that I don't know how to replace the "@" and "/" characters

Do I need to execute two find/replace queries or can it be done with a single one? I want to remove the "@" entirely and replace the "/" by "--"

Any help much appreciated!

Thanks

3
  • 2
    It can't be done in only one find/replace. Commented Aug 26, 2019 at 20:21
  • good to know.. but I would still like to know how to do it as two find/replace operations Commented Aug 26, 2019 at 20:31
  • Sorry but tonight I'm watching Twin Peaks. Commented Aug 26, 2019 at 21:39

2 Answers 2

3

With two find/replace:

find: (\(\) => import\()'@/([^']*)'\)
replace: $1/* webpackChunkName: "$2" */  '@/$2')

demo

and

find: ((?:\G(?!^)|\(\) => import\(/\*)[^/*]*)/
replace: $1--

demo

Sign up to request clarification or add additional context in comments.

Comments

0

If the path always matches the @/part1/part2/part3 pattern, then you could group those parts individually and use your backreferences like so:

Find:

\(\) => import\('(@/(.+?)/(.+?)/(.+?))'\)

Replace:

() => import(/* webpackChunkName: "$2--$3--$4" */ '$1')

However, if the number of path parts varies (which is more likely) then you're best bet is probably a second and third set of find and replaces along these lines:

Get rid of the @/

Find:

(?<=\(\) => import\(/\* webpackChunkName: ")@/

Replace:


(Replace with nothing)

Convert / to --

Find:

(?<=\(\) => import\(/\* webpackChunkName: ")(.*?)/(.*?)(?=" \*/)

Replace:

$1--$2

(You'll have to run this several times per line)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.