From e80ed23d8575ca951f8e969071769e16732b198a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BF=A1=E9=91=AB-King?= Date: Tue, 23 Feb 2021 15:20:44 +0800 Subject: [PATCH] fix(utils): routeToChunkName with invalid regular expression (#6156) * fix(utils): routeToChunkName with invalid regular expression * fix: build watch opts regepx --- packages/bundler-webpack/src/fixtures/t++/config.ts | 5 +++++ packages/bundler-webpack/src/fixtures/t++/expect.ts | 5 +++++ packages/bundler-webpack/src/fixtures/t++/index.tsx | 2 ++ packages/bundler-webpack/src/index.ts | 6 ++++-- packages/utils/src/routes.test.ts | 12 ++++++++++++ packages/utils/src/routes.ts | 6 +++++- 6 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 packages/bundler-webpack/src/fixtures/t++/config.ts create mode 100644 packages/bundler-webpack/src/fixtures/t++/expect.ts create mode 100644 packages/bundler-webpack/src/fixtures/t++/index.tsx diff --git a/packages/bundler-webpack/src/fixtures/t++/config.ts b/packages/bundler-webpack/src/fixtures/t++/config.ts new file mode 100644 index 000000000000..49b649a4127a --- /dev/null +++ b/packages/bundler-webpack/src/fixtures/t++/config.ts @@ -0,0 +1,5 @@ +export default { + externals: { + react: 'React', + } +} diff --git a/packages/bundler-webpack/src/fixtures/t++/expect.ts b/packages/bundler-webpack/src/fixtures/t++/expect.ts new file mode 100644 index 000000000000..94c0902a04f6 --- /dev/null +++ b/packages/bundler-webpack/src/fixtures/t++/expect.ts @@ -0,0 +1,5 @@ +import { IExpectOpts } from '../types'; + +export default ({ indexJS }: IExpectOpts) => { + expect(indexJS).toContain(`default.a.createElement("div"`); +} diff --git a/packages/bundler-webpack/src/fixtures/t++/index.tsx b/packages/bundler-webpack/src/fixtures/t++/index.tsx new file mode 100644 index 000000000000..ae92b12c1b3e --- /dev/null +++ b/packages/bundler-webpack/src/fixtures/t++/index.tsx @@ -0,0 +1,2 @@ + +export default () =>
111
; diff --git a/packages/bundler-webpack/src/index.ts b/packages/bundler-webpack/src/index.ts index 2f1c94037711..a0d884ae010a 100644 --- a/packages/bundler-webpack/src/index.ts +++ b/packages/bundler-webpack/src/index.ts @@ -2,7 +2,7 @@ import { IConfig, BundlerConfigType } from '@umijs/types'; import defaultWebpack from 'webpack'; import webpackDevMiddleware from 'webpack-dev-middleware'; import { IServerOpts, Server } from '@umijs/server'; -import { winPath } from '@umijs/utils'; +import { winPath, lodash as _ } from '@umijs/utils'; import { join } from 'path'; import getConfig, { IOpts as IGetConfigOpts } from './getConfig/getConfig'; @@ -60,7 +60,9 @@ class Bundler { */ getIgnoredWatchRegExp = (): defaultWebpack.Options.WatchOptions['ignored'] => { const { outputPath } = this.config; - const absOutputPath = winPath(join(this.cwd, outputPath as string, '/')); + const absOutputPath = _.escapeRegExp( + winPath(join(this.cwd, outputPath as string, '/')), + ); // need ${sep} after outputPath return process.env.WATCH_IGNORED === 'none' ? undefined diff --git a/packages/utils/src/routes.test.ts b/packages/utils/src/routes.test.ts index 5d7197d6fed7..9155537186b1 100644 --- a/packages/utils/src/routes.test.ts +++ b/packages/utils/src/routes.test.ts @@ -65,3 +65,15 @@ test('routeToChunkName cwd', () => { }), ).toEqual('p__users__id'); }); + +test('routeToChunkName cwd escape char', () => { + expect( + routeToChunkName({ + route: { + path: '/users/:id', + component: '/users/c++/pages/users/[id].jsx', + }, + cwd: '/users/c++', + }), + ).toEqual('p__users__id'); +}); diff --git a/packages/utils/src/routes.ts b/packages/utils/src/routes.ts index 462d8228b152..ff90f4b943d8 100644 --- a/packages/utils/src/routes.ts +++ b/packages/utils/src/routes.ts @@ -1,3 +1,4 @@ +import { escapeRegExp } from 'lodash'; import winPath from './winPath/winPath'; function lastSlash(str: string) { @@ -19,7 +20,10 @@ export const routeToChunkName: IRouteToChunkName = ( ) => { return typeof route.component === 'string' ? route.component - .replace(new RegExp(`^${lastSlash(winPath(cwd || '/'))}`), '') + .replace( + new RegExp(`^${escapeRegExp(lastSlash(winPath(cwd || '/')))}`), + '', + ) .replace(/^.(\/|\\)/, '') .replace(/(\/|\\)/g, '__') .replace(/\.jsx?$/, '')