Skip to content

Commit

Permalink
fix: watch on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Mar 21, 2019
1 parent a8fc34e commit 04eb54d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 46 deletions.
19 changes: 8 additions & 11 deletions src/postProcessPattern.js
Expand Up @@ -114,17 +114,14 @@ export default function postProcessPattern(globalRef, pattern, file) {
file.webpackTo = file.webpackTo.replace(/\.?\[ext\]/g, '');
}

// Developers can use invalid slashes in regex we should fix it
file.webpackTo = normalizePath(
loaderUtils.interpolateName(
{ resourcePath: file.absoluteFrom },
file.webpackTo,
{
content,
regExp: file.webpackToRegExp,
context: pattern.context,
}
)
file.webpackTo = loaderUtils.interpolateName(
{ resourcePath: file.absoluteFrom },
file.webpackTo,
{
content,
regExp: file.webpackToRegExp,
context: pattern.context,
}
);
}

Expand Down
26 changes: 14 additions & 12 deletions src/preProcessPattern.js
Expand Up @@ -45,7 +45,16 @@ export default function preProcessPattern(globalRef, pattern) {
pattern.context = path.join(context, pattern.context);
}

const isFromGlobPatten = isObject(pattern.from) && pattern.from.glob;
// Todo remove this in next major
const isToDirectory =
path.extname(pattern.to) === '' || pattern.to.slice(-1) === path.sep;

// Normalize path
pattern.from = isFromGlobPatten ? pattern.from : normalizePath(pattern.from);
pattern.context = normalizePath(pattern.context);
pattern.to = normalizePath(pattern.to);

pattern.ignore = globalRef.ignore.concat(pattern.ignore || []);

logger.debug(`processing from: '${pattern.from}' to: '${pattern.to}'`);
Expand All @@ -57,15 +66,15 @@ export default function preProcessPattern(globalRef, pattern) {
case isTemplateLike.test(pattern.to):
pattern.toType = 'template';
break;
case path.extname(pattern.to) === '' || pattern.to.slice(-1) === '/':
case isToDirectory:
pattern.toType = 'dir';
break;
default:
pattern.toType = 'file';
}

// If we know it's a glob, then bail early
if (isObject(pattern.from) && pattern.from.glob) {
if (isFromGlobPatten) {
logger.debug(`determined '${pattern.absoluteFrom}' is a glob`);

pattern.fromType = 'glob';
Expand All @@ -75,9 +84,7 @@ export default function preProcessPattern(globalRef, pattern) {

pattern.glob = normalize(pattern.context, pattern.from.glob);
pattern.globOptions = globOptions;
pattern.absoluteFrom = normalizePath(
path.resolve(pattern.context, pattern.from.glob)
);
pattern.absoluteFrom = path.resolve(pattern.context, pattern.from.glob);

return Promise.resolve(pattern);
}
Expand All @@ -88,9 +95,6 @@ export default function preProcessPattern(globalRef, pattern) {
pattern.absoluteFrom = path.resolve(pattern.context, pattern.from);
}

// Normalize path when path separators are mixed (like `C:\\directory/nested-directory/`)
pattern.absoluteFrom = normalizePath(pattern.absoluteFrom);

logger.debug(
`determined '${pattern.from}' to be read from '${pattern.absoluteFrom}'`
);
Expand Down Expand Up @@ -147,9 +151,7 @@ export default function preProcessPattern(globalRef, pattern) {
pattern.fromType = 'dir';
pattern.context = pattern.absoluteFrom;
pattern.glob = normalize(pattern.absoluteFrom, '**/*');
pattern.absoluteFrom = normalizePath(
path.join(pattern.absoluteFrom, '**/*')
);
pattern.absoluteFrom = path.join(pattern.absoluteFrom, '**/*');
pattern.globOptions = {
dot: true,
};
Expand All @@ -159,7 +161,7 @@ export default function preProcessPattern(globalRef, pattern) {
fileDependencies.add(pattern.absoluteFrom);

pattern.fromType = 'file';
pattern.context = normalizePath(path.dirname(pattern.absoluteFrom));
pattern.context = path.dirname(pattern.absoluteFrom);
pattern.glob = normalize(pattern.absoluteFrom);
pattern.globOptions = {
dot: true,
Expand Down
7 changes: 0 additions & 7 deletions src/processPattern.js
Expand Up @@ -3,7 +3,6 @@ import path from 'path';
import globby from 'globby';
import pLimit from 'p-limit';
import minimatch from 'minimatch';
import normalizePath from 'normalize-path';

import isObject from './utils/isObject';

Expand Down Expand Up @@ -42,9 +41,6 @@ export default function processPattern(globalRef, pattern) {
file.relativeFrom = path.basename(file.relativeFrom);
}

// Ensure forward slashes
file.relativeFrom = normalizePath(file.relativeFrom);

logger.debug(`found ${from}`);

// Check the ignore list
Expand Down Expand Up @@ -113,9 +109,6 @@ export default function processPattern(globalRef, pattern) {
file.webpackTo = path.relative(output, file.webpackTo);
}

// Ensure forward slashes
file.webpackTo = normalizePath(file.webpackTo);

logger.info(
`determined that '${from}' should write to '${file.webpackTo}'`
);
Expand Down
25 changes: 9 additions & 16 deletions test/CopyPlugin.test.js
Expand Up @@ -10,7 +10,6 @@ import findCacheDir from 'find-cache-dir';
import cacache from 'cacache';
import isGzip from 'is-gzip';
import mkdirp from 'mkdirp';
import normalizePath from 'normalize-path';

import CopyPlugin from '../src/index';

Expand Down Expand Up @@ -687,9 +686,9 @@ describe('apply function', () => {
.then((compilation) => {
expect(
Array.from(compilation.contextDependencies)
.map((contextDependency) => normalizePath(contextDependency))
.map((contextDependency) => contextDependency)
.sort()
).toEqual([normalizePath(path.join(HELPER_DIR, 'directory'))].sort());
).toEqual([path.join(HELPER_DIR, 'directory')].sort());
})
.then(done)
.catch(done);
Expand Down Expand Up @@ -797,9 +796,7 @@ describe('apply function', () => {
expectedAssetKeys: [],
expectedWarnings: [
new Error(
`unable to locate 'nonexistent.txt' at '${normalizePath(
HELPER_DIR
)}/nonexistent.txt'`
`unable to locate 'nonexistent.txt' at '${HELPER_DIR}/nonexistent.txt'`
),
],
patterns: [
Expand All @@ -818,9 +815,7 @@ describe('apply function', () => {
expectedAssetKeys: [],
expectedWarnings: [
new Error(
`unable to locate 'nonexistent.txt' at '${normalizePath(
HELPER_DIR
)}/nonexistent.txt'`
`unable to locate 'nonexistent.txt' at '${HELPER_DIR}/nonexistent.txt'`
),
],
patterns: [
Expand Down Expand Up @@ -1057,13 +1052,13 @@ describe('apply function', () => {
.catch(done);
});

it('can move a file to a new directory with an extension and forward slash', (done) => {
it('can move a file to a new directory with an extension and path separator at end', (done) => {
runEmit({
expectedAssetKeys: ['newdirectory.ext/file.txt'],
patterns: [
{
from: 'file.txt',
to: 'newdirectory.ext/',
to: `newdirectory.ext${path.sep}`,
},
],
})
Expand Down Expand Up @@ -1236,7 +1231,7 @@ describe('apply function', () => {
],
})
.then((compilation) => {
const absFrom = normalizePath(path.join(HELPER_DIR, 'file.txt'));
const absFrom = path.join(HELPER_DIR, 'file.txt');

expect(Array.from(compilation.fileDependencies).sort()).toEqual(
[absFrom].sort()
Expand Down Expand Up @@ -1478,9 +1473,7 @@ describe('apply function', () => {
expectedAssetKeys: [],
expectedWarnings: [
new Error(
`unable to locate 'nonexistent' at '${normalizePath(
HELPER_DIR
)}/nonexistent'`
`unable to locate 'nonexistent' at '${HELPER_DIR}/nonexistent'`
),
],
patterns: [
Expand Down Expand Up @@ -1680,7 +1673,7 @@ describe('apply function', () => {
],
})
.then((compilation) => {
const absFrom = normalizePath(path.resolve(HELPER_DIR, 'directory'));
const absFrom = path.resolve(HELPER_DIR, 'directory');
expect(Array.from(compilation.contextDependencies).sort()).toEqual(
[absFrom].sort()
);
Expand Down

0 comments on commit 04eb54d

Please sign in to comment.