Skip to content

Commit

Permalink
fix: dist regexp in windows os (umijs#5462)
Browse files Browse the repository at this point in the history
* fix: windows bug in ignored watch files

* fix: windows bug in ignored watch files«

* chore: windows ci

* chore: windows ci

* chore: sep

* chore: test

* chore: console.log

* chore: windows

* chore: use anymatch in webpack

* chore: format

* chore: test

* chore: anymatch

Co-authored-by: pr <pr>
  • Loading branch information
ycjcl868 authored Sep 22, 2020
1 parent 20bbce4 commit a5908e3
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
report.*.json
output.txt
/examples/*/dist
packages/bundler-webpack/src/fixtures/outputPath/bar
/examples/*/node_modules
/examples/*/yarn.lock
/examples/*/app/public
Expand Down
3 changes: 3 additions & 0 deletions packages/bundler-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"publishConfig": {
"access": "public"
},
"devDependencies": {
"anymatch": "3.1.1"
},
"dependencies": {
"@babel/core": "7.11.6",
"@types/sockjs-client": "1.1.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/bundler-webpack/src/fixtures/outputPath/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export default {
outputPath: 'bar',
}
16 changes: 16 additions & 0 deletions packages/bundler-webpack/src/fixtures/outputPath/expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as path from 'path';
import anymatch from 'anymatch';
import { IExpectOpts } from '../types';

export default ({ ignored }: IExpectOpts) => {
expect(
anymatch(ignored as any)(path.join(__dirname, 'bar', 'index.js'))
).toBeTruthy();
expect(
anymatch(ignored as any)(path.join(__dirname, 'bar'))
).toBeFalsy();
// issue: https://github.com/umijs/umi/issues/5416
expect(anymatch(ignored as any)(path.join(__dirname, 'distributor', 'index.tsx'))).toBeFalsy();
expect(anymatch(ignored as any)(path.join(__dirname, 'distributor'))).toBeFalsy();
expect(anymatch(ignored as any)(path.join(__dirname, 'node_modules'))).toBeTruthy();
}
1 change: 1 addition & 0 deletions packages/bundler-webpack/src/fixtures/outputPath/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('outputPath');
3 changes: 3 additions & 0 deletions packages/bundler-webpack/src/fixtures/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

import * as webpack from 'webpack';

export interface IExpectOpts {
files: string[];
indexJS: string;
indexCSS: string;
cwd: string;
ignored: webpack.Options.WatchOptions['ignored']
}
22 changes: 17 additions & 5 deletions packages/bundler-webpack/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ readdirSync(fixtures).forEach((fixture) => {
: test;
fn(fixture, async () => {
// get user config
let config = {};
let config = {
outputPath: 'dist',
};
try {
config = require(join(cwd, 'config.ts')).default;
config = Object.assign(
{},
config,
require(join(cwd, 'config.ts')).default,
);
} catch (e) {}

// init bundler
Expand Down Expand Up @@ -55,13 +61,19 @@ readdirSync(fixtures).forEach((fixture) => {
// expect
let indexCSS = '';
try {
indexCSS = readFileSync(join(cwd, 'dist/index.css'), 'utf-8');
indexCSS = readFileSync(
join(cwd, config.outputPath, 'index.css'),
'utf-8',
);
} catch (e) {}
require(join(cwd, 'expect.ts')).default({
indexJS: readFileSync(join(cwd, 'dist/index.js'), 'utf-8'),
indexJS: readFileSync(join(cwd, config.outputPath, 'index.js'), 'utf-8'),
indexCSS,
files: readdirSync(join(cwd, 'dist')).filter((f) => f.charAt(0) !== '.'),
files: readdirSync(join(cwd, config.outputPath)).filter(
(f) => f.charAt(0) !== '.',
),
cwd,
ignored: bundler.getIgnoredWatchRegExp(),
});
});
});
Expand Down
28 changes: 18 additions & 10 deletions packages/bundler-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ 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 getConfig, { IOpts as IGetConfigOpts } from './getConfig/getConfig';
import { join, sep } from 'path';
import { join } from 'path';

interface IOpts {
cwd: string;
Expand Down Expand Up @@ -54,6 +55,20 @@ class Bundler {
});
}

/**
* get ignored watch dirs regexp, for test case
*/
getIgnoredWatchRegExp = (): defaultWebpack.Options.WatchOptions['ignored'] => {
const { outputPath } = this.config;
const absOutputPath = winPath(join(this.cwd, outputPath as string, '/'));
// need ${sep} after outputPath
return process.env.WATCH_IGNORED === 'none'
? undefined
: new RegExp(
process.env.WATCH_IGNORED || `(node_modules|${absOutputPath})`,
);
};

setupDevServerOpts({
bundleConfigs,
bundleImplementor = defaultWebpack,
Expand All @@ -62,9 +77,8 @@ class Bundler {
bundleImplementor?: typeof defaultWebpack;
}): IServerOpts {
const compiler = bundleImplementor(bundleConfigs);
const { devServer, outputPath } = this.config;
const { devServer } = this.config;
// 这里不做 winPath 处理,是为了和下方的 path.sep 匹配上
const absOutputPath = join(this.cwd, outputPath as string);
// @ts-ignore
const compilerMiddleware = webpackDevMiddleware(compiler, {
// must be /, otherwise it will exec next()
Expand All @@ -73,13 +87,7 @@ class Bundler {
writeToDisk: devServer && devServer?.writeToDisk,
watchOptions: {
// not watch outputPath dir and node_modules
ignored:
process.env.WATCH_IGNORED === 'none'
? undefined
: new RegExp(
process.env.WATCH_IGNORED ||
`(node_modules|${absOutputPath}${sep})`,
),
ignored: this.getIgnoredWatchRegExp(),
},
});

Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4263,20 +4263,20 @@ any-promise@^1.0.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"

[email protected], anymatch@^3.0.3, anymatch@~3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.1.tgz#c55ecf02185e2469259399310c173ce31233b142"
dependencies:
normalize-path "^3.0.0"
picomatch "^2.0.4"

anymatch@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-2.0.0.tgz#bcb24b4f37934d9aa7ac17b4adaf89e7c76ef2eb"
dependencies:
micromatch "^3.1.4"
normalize-path "^2.1.1"

anymatch@^3.0.3, anymatch@~3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.1.tgz#c55ecf02185e2469259399310c173ce31233b142"
dependencies:
normalize-path "^3.0.0"
picomatch "^2.0.4"

append-buffer@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/append-buffer/-/append-buffer-1.0.2.tgz#d8220cf466081525efea50614f3de6514dfa58f1"
Expand Down

0 comments on commit a5908e3

Please sign in to comment.