question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

config.define incorrectly escapes -

See original GitHub issue

Describe the bug

  define: {
    " from 'styled-components'": " from 'styled-components/macro'",
  }
SyntaxError: Invalid regular expression: /(?<![\p{L}\p{N}_$]|(?<!\.\.)\.)(process\.env\.NODE_ENV|global\.process\.env\.NODE_ENV|globalThis\.process\.env\.NODE_ENV|__vite_process_env_NODE_ENV| from 'styled\-components'|process\.env\.|global\.process\.env\.|globalThis\.process\.env\.)(?![\p{L}\p{N}_$]|\s*?=[^=])/: Invalid escape

Removing - in dist/node/chunks/dep-0fc8e132.js fixes the problem:

                    return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');

I’m on vite@3.0.9

Reproduction

https://stackblitz.com/edit/vitejs-vite-vvdzuy?file=vite.config.js

System Info

System:
    OS: macOS 11.6.4
    CPU: (8) arm64 Apple M1
    Memory: 114.94 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.17.0 - /usr/local/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 8.12.1 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 104.0.5112.101
    Firefox: 104.0.1
    Safari: 14.1.2
  npmPackages:
    @vitejs/plugin-react: ^2.0.1 => 2.0.1
    vite: ^3.0.9 => 3.0.9

Used Package Manager

yarn

Logs

Click to expand!
error when starting dev server:
SyntaxError: Invalid regular expression: /(?<![\p{L}\p{N}_$]|(?<!\.\.)\.)(process\.env\.NODE_ENV|global\.process\.env\.NODE_ENV|globalThis\.process\.env\.NODE_ENV|__vite_process_env_NODE_ENV| from 'styled\-components'|process\.env\.|global\.process\.env\.|globalThis\.process\.env\.)(?![\p{L}\p{N}_$]|\s*?=[^=])/: Invalid escape
    at new RegExp (<anonymous>)
    at generatePattern (file:///Users/<project-url>/node_modules/vite/dist/node/chunks/dep-0fc8e132.js:60374:15)
    at definePlugin (file:///Users/<project-url>/node_modules/vite/dist/node/chunks/dep-0fc8e132.js:60389:28)
    at resolvePlugins (file:///Users/<project-url>/node_modules/vite/dist/node/chunks/dep-0fc8e132.js:62253:9)
    at resolveConfig (file:///Users/<project-url>/node_modules/vite/dist/node/chunks/dep-0fc8e132.js:62817:30)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async createServer (file:///Users/<project-url>/node_modules/vite/dist/node/chunks/dep-0fc8e132.js:59183:20)
    at async CAC.<anonymous> (file:///Users/<project-url>/node_modules/vite/dist/node/cli.js:699:24)
error Command failed with exit code 1.

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tony19commented, Sep 5, 2022

Your original config looks like it’s just trying to create an alias. You can do that with resolve.alias:

// vite.config.js
import { defineConfig } from 'vite'

export default defineConfig({
  resolve: {
    alias: {
      'styled-components': 'styled-components/macro',
    }
  }
})
0reactions
adrianhelvikcommented, Sep 5, 2022

Yeah, I tried that, but babel did not recognize the import as a macro when I did that. I ended up using the macro explicitly on an as needed basis. The code works fine without the macro. Class names are just less readable.

Hardly the biggest issue with migrating a large 6 year old ejected CRA application to Vite without messing up pull requests. 😄 I managed to do it though!

Read more comments on GitHub >

github_iconTop Results From Across the Web

config.define incorrectly escapes - · Issue #9964 · vitejs/vite
Describe the bug define: { " from 'styled-components'": " from 'styled-components/macro'", } SyntaxError: Invalid regular expression: /(?
Read more >
cmake incorrectly escapes bison target option - Stack Overflow
As a workaround, try the following: string(REPLACE " " [[\\040]] compile_flags "${compile_flags}") where compile_flags is set(compile_flags "-- ...
Read more >
[#CONFIGURATION-418] incorrect backslash parsing - Apache
I am using Commons Configuration (PropertiesConfiguration) and some of my data are windows shares: share1 or share2.
Read more >
no-useless-escape - ESLint - Pluggable JavaScript Linter
This rule flags escapes that can be safely removed without changing behavior. Examples of incorrect code for this rule: /*eslint no-useless-escape: ...
Read more >
Invalid escape sequence in a regular expression - IBM
Placing a '\' (backslash) in front of the character in the regular expression generates an 'Invalid escape sequence' compilation error. This only occurs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found