Skip to content

Commit

Permalink
fix: normalize path segment separation (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Feb 19, 2019
1 parent a1c5372 commit 8f5e638
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 47 deletions.
34 changes: 19 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"is-glob": "^4.0.0",
"loader-utils": "^1.1.0",
"minimatch": "^3.0.4",
"p-limit": "^2.1.0"
"p-limit": "^2.1.0",
"normalize-path": "^3.0.0"
},
"devDependencies": {
"@babel/cli": "^7.1.5",
Expand Down
19 changes: 13 additions & 6 deletions src/preProcessPattern.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'path';

import isGlob from 'is-glob';
import normalizePath from 'normalize-path';

import escape from './utils/escape';
import normalize from './utils/normalize';
import isObject from './utils/isObject';
import { stat } from './utils/promisify';

Expand Down Expand Up @@ -41,6 +42,7 @@ export default function preProcessPattern(globalRef, pattern) {
pattern.context = path.join(context, pattern.context);
}

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

info(`processing from: '${pattern.from}' to: '${pattern.to}'`);
Expand Down Expand Up @@ -68,9 +70,11 @@ export default function preProcessPattern(globalRef, pattern) {
const globOptions = Object.assign({}, pattern.from);
delete globOptions.glob;

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

return Promise.resolve(pattern);
}
Expand All @@ -81,6 +85,9 @@ 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);

debug(
`determined '${pattern.from}' to be read from '${pattern.absoluteFrom}'`
);
Expand All @@ -89,7 +96,7 @@ export default function preProcessPattern(globalRef, pattern) {
// If from doesn't appear to be a glob, then log a warning
if (isGlob(pattern.from) || pattern.from.indexOf('*') !== -1) {
pattern.fromType = 'glob';
pattern.glob = escape(pattern.context, pattern.from);
pattern.glob = normalize(pattern.context, pattern.from);

// We need to add context directory as dependencies to avoid problems when new files added in directories
// when we already in watch mode and this directories are not in context dependencies
Expand Down Expand Up @@ -130,7 +137,7 @@ export default function preProcessPattern(globalRef, pattern) {

pattern.fromType = 'dir';
pattern.context = pattern.absoluteFrom;
pattern.glob = escape(pattern.absoluteFrom, '**/*');
pattern.glob = normalize(pattern.absoluteFrom, '**/*');
pattern.absoluteFrom = path.join(pattern.absoluteFrom, '**/*');
pattern.globOptions = {
dot: true,
Expand All @@ -140,7 +147,7 @@ export default function preProcessPattern(globalRef, pattern) {

pattern.fromType = 'file';
pattern.context = path.dirname(pattern.absoluteFrom);
pattern.glob = escape(pattern.absoluteFrom);
pattern.glob = normalize(pattern.absoluteFrom);
pattern.globOptions = {
dot: true,
};
Expand Down
8 changes: 6 additions & 2 deletions src/processPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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 @@ -41,6 +42,9 @@ export default function processPattern(globalRef, pattern) {
file.relativeFrom = path.basename(file.relativeFrom);
}

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

debug(`found ${from}`);

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

// ensure forward slashes
file.webpackTo = file.webpackTo.replace(/\\/g, '/');
// Ensure forward slashes
file.webpackTo = normalizePath(file.webpackTo);

info(`determined that '${from}' should write to '${file.webpackTo}'`);

Expand Down
8 changes: 7 additions & 1 deletion src/utils/escape.js → src/utils/normalize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path';

export default function escape(context, from) {
import normalizePath from 'normalize-path';

function escape(context, from) {
if (from && path.isAbsolute(from)) {
return from;
}
Expand All @@ -24,3 +26,7 @@ export default function escape(context, from) {

return `${absoluteContext}/${from}`;
}

export default function normalize(context, from) {
return normalizePath(escape(context, from));
}
Loading

0 comments on commit 8f5e638

Please sign in to comment.