Skip to content

Commit

Permalink
fix(utils): routeToChunkName with invalid regular expression (#6156)
Browse files Browse the repository at this point in the history
* fix(utils): routeToChunkName with invalid regular expression

* fix: build watch opts regepx
  • Loading branch information
ycjcl868 authored Feb 23, 2021
1 parent 4052ef6 commit e80ed23
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/bundler-webpack/src/fixtures/t++/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
externals: {
react: 'React',
}
}
5 changes: 5 additions & 0 deletions packages/bundler-webpack/src/fixtures/t++/expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IExpectOpts } from '../types';

export default ({ indexJS }: IExpectOpts) => {
expect(indexJS).toContain(`default.a.createElement("div"`);
}
2 changes: 2 additions & 0 deletions packages/bundler-webpack/src/fixtures/t++/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export default () => <div>111</div>;
6 changes: 4 additions & 2 deletions packages/bundler-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions packages/utils/src/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
6 changes: 5 additions & 1 deletion packages/utils/src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { escapeRegExp } from 'lodash';
import winPath from './winPath/winPath';

function lastSlash(str: string) {
Expand All @@ -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?$/, '')
Expand Down

0 comments on commit e80ed23

Please sign in to comment.