rails / webpacker

Use Webpack to manage app-like JavaScript modules in Rails
MIT License
5.31k stars 1.47k forks source link

SyntaxError: Invalid regular expression: /^(https?://)?((([a-zd]([a-zd-]*[a-zd])*).)+[a-z]{2,}|((d{1,3}.){3}d{1,3}))(:d+)?(/[-a-zd%_.~+]*)*(?[&a-zd%_.~+=-]*)?(#[-a-zd_]*)?$/: Invalid group #3236

Closed erosinlove closed 2 years ago

erosinlove commented 2 years ago

I installed gem 'webpacker', '>= 6.0.0.rc.6' and after compiled assets with this command rbenv exec bundle exec rake assets:clean assets:precompile I got the error as below

Compiled all packs in /Users/xx/xxx/public/packs SyntaxError: Invalid regular expression: /^(https?://)?(((a-zd).)+[a-z]{2,}|((d{1,3}.){3}d{1,3}))(:d+)?(/[-a-zd%_.~+])(?[&a-zd%_.~+=-])?(#[-a-zd_]*)?$/: Invalid group at new RegExp () at /Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:410:24 at Hook.eval [as call] (eval at create (/Users/xx/xxx/node_modules/tapable/lib/HookCodeFactory.js:19:10), :7:16) at JavascriptParser.evaluateExpression (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:3168:25) at JavascriptParser.getRenameIdentifier (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1384:23) at JavascriptParser.walkVariableDeclaration (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:2076:31) at JavascriptParser.walkStatement (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1584:10) at JavascriptParser.walkStatements (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1445:9) at /Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1619:9 at JavascriptParser.inBlockScope (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:3066:3)

justin808 commented 2 years ago

@erosinlove can you provide reproduction steps on a public repo?

erosinlove commented 2 years ago

@justin808 I just updated the version from 5.4.3 to 6.0.0-rc.6 then run bundle after that compile the assets rbenv exec bundle exec rake assets:clean assets:precompile and got the error

justin808 commented 2 years ago

I'm pretty sure this is related to some inconsistent package versions or another upgrade issue.

Are you sure that you followed all the steps in https://github.com/rails/webpacker/blob/master/docs/v6_upgrade.md ?

erosinlove commented 2 years ago

@justin808 Yes, I followed all steps below are all errors from my application

Compiled all packs in /Users/xx/xxx/public/packs SyntaxError: Invalid regular expression: /^(https?://)?(((a-zd).)+[a-z]{2,}|((d{1,3}.){3}d{1,3}))(:d+)?(/[-a-zd%_.~+])(?[&a-zd%_.~+=-])?(#[-a-zd_])?$/: Invalid group at new RegExp () at /Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:410:24 at Hook.eval [as call] (eval at create (/Users/xx/xxx/node_modules/tapable/lib/HookCodeFactory.js:19:10), :7:16) at JavascriptParser.evaluateExpression (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:3169:25) at JavascriptParser.getRenameIdentifier (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1385:23) at JavascriptParser.walkVariableDeclaration (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:2077:31) at JavascriptParser.walkStatement (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1585:10) at JavascriptParser.walkStatements (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1446:9) at /Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1620:9 at JavascriptParser.inBlockScope (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:3067:3) SyntaxError: Invalid regular expression: /^(https?://)?((([a-zd]([a-zd-][a-zd])).)+[a-z]{2,}|((d{1,3}.){3}d{1,3}))(:d+)?(/[-a-zd%_.~+])(?[&a-zd%_.~+=-])?(#[-a-zd_]*)?$/: Invalid group at new RegExp () at /Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:410:24 at Hook.eval [as call] (eval at create (/Users/xx/xxx/node_modules/tapable/lib/HookCodeFactory.js:19:10), :7:16) at JavascriptParser.evaluateExpression (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:3169:25) at JavascriptParser.getRenameIdentifier (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1385:23) at JavascriptParser.walkVariableDeclaration (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:2077:31) at JavascriptParser.walkStatement (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1585:10) at JavascriptParser.walkStatements (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1446:9) at /Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:1620:9 at JavascriptParser.inBlockScope (/Users/xx/xxx/node_modules/webpack/lib/javascript/JavascriptParser.js:3067:3)

assets by status 3.89 MiB [cached] 9 assets asset manifest.json 5.7 KiB [compared for emit] 2 related assets Entrypoint application [big] 3.88 MiB (14.5 MiB) = 6 assets 6 auxiliary assets Entrypoint server_rendering [big] 1.94 MiB (7.9 MiB) = js/runtime-3c479da69bf4e2b43c21.js 3.19 KiB js/187-d6d8afdbac84758cae0f.js 1.83 MiB js/982-ad9596c6d1ca43665797.js 105 KiB css/server_rendering-34ef9502.css 2.13 KiB js/server_rendering-2e74825ba5dd32d8fff6.js 316 bytes 5 auxiliary assets orphan modules 1.69 MiB (javascript) 6.82 KiB (runtime) [orphan] 636 modules runtime modules 7.78 KiB 16 modules modules by path ./node_modules/ 8.55 MiB (javascript) 42.8 KiB (css/mini-extract) javascript modules 8.55 MiB 1396 modules css modules 42.8 KiB modules by path ./node_modules/@fullcalendar/ 40 KiB 5 modules modules by path ./node_modules/prismjs/ 2.79 KiB 2 modules modules by path ./app/javascript/ 665 KiB modules by path ./app/javascript/components/.js 167 KiB 32 modules modules by path ./app/javascript/channels/ 1.82 KiB 5 modules modules by path ./app/javascript/src/.js 449 KiB 2 modules ./app/javascript/application.js + 14 modules 45.8 KiB [built] [code generated] ./app/javascript/server_rendering.js 669 bytes [built] [code generated] ./app/javascript/components/ sync ^.\/.*$ 1.64 KiB [built] [code generated]

WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB). This can impact web performance. Assets: js/application-f18fe8a4e08eebe98517.js (296 KiB) js/187-d6d8afdbac84758cae0f.js (1.83 MiB) js/6-213b34e12494f4ad1af5.js (1.62 MiB) js/application-f18fe8a4e08eebe98517.js.map.gz (255 KiB) js/6-213b34e12494f4ad1af5.js.gz (434 KiB) js/187-d6d8afdbac84758cae0f.js.gz (525 KiB) js/6-213b34e12494f4ad1af5.js.map.gz (1.37 MiB) js/187-d6d8afdbac84758cae0f.js.map.gz (1.87 MiB) js/187-d6d8afdbac84758cae0f.js.br (272 KiB) js/6-213b34e12494f4ad1af5.js.br (348 KiB) js/187-d6d8afdbac84758cae0f.js.map.br (937 KiB) js/6-213b34e12494f4ad1af5.js.map.br (1.08 MiB)

WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance. Entrypoints: application (3.88 MiB) js/runtime-3c479da69bf4e2b43c21.js js/187-d6d8afdbac84758cae0f.js css/6-7bb3dccc.css js/6-213b34e12494f4ad1af5.js js/982-ad9596c6d1ca43665797.js js/application-f18fe8a4e08eebe98517.js server_rendering (1.94 MiB) js/runtime-3c479da69bf4e2b43c21.js js/187-d6d8afdbac84758cae0f.js js/982-ad9596c6d1ca43665797.js css/server_rendering-34ef9502.css js/server_rendering-2e74825ba5dd32d8fff6.js

webpack 5.64.4 compiled with 2 warnings in 78460 ms

justin808 commented 2 years ago

If you can create a reproduction case on Github, I'll investigate this!

erosinlove commented 2 years ago

Hi @justin808 I found that my error is const componentRequireContext = require.context("components", true);

from

 const componentRequireContext = require.context("components", true);
 window.componentRequireContext = componentRequireContext;

 const ReactRailsUJS = require("react_ujs");
 ReactRailsUJS.useContext(componentRequireContext);

Do you have any advice on how to load all components under app/javascript/components/*.js

erosinlove commented 2 years ago

Hi @justin808 now, I can fix it with the following steps

  1. change from webpacker to shakapacker

  2. update the syntax from const componentRequireContext = require.context("components", true); to const componentRequireContext = require.context( "components", true, /^(https?:\/\/)?((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(:\d+)?(\/[-a-z\d%_.~+]*)*(?:[&a-z\d%_.~+=-]*)?(#[-a-z\d_]*)?$/ );