Skip to content

Commit

Permalink
fix: use posix separator for emitting assets (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jul 19, 2019
1 parent 89c73c8 commit 7f08be6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
34 changes: 19 additions & 15 deletions src/postProcessPattern.js
Expand Up @@ -6,6 +6,7 @@ import loaderUtils from 'loader-utils';
import cacache from 'cacache';
import serialize from 'serialize-javascript';
import findCacheDir from 'find-cache-dir';
import normalizePath from 'normalize-path';

import { name, version } from '../package.json';

Expand Down Expand Up @@ -138,26 +139,29 @@ export default function postProcessPattern(globalRef, pattern, file) {
`transforming path '${file.webpackTo}' for '${file.absoluteFrom}'`
);

return Promise.resolve(
pattern.transformPath(file.webpackTo, file.absoluteFrom)
)
return Promise.resolve()
.then(() =>
pattern.transformPath(file.webpackTo, file.absoluteFrom)
)
.then((newPath) => {
// Developers can use invalid slashes we should fix it
file.webpackTo = path.normalize(newPath);
})
.then(() => content);
file.webpackTo = newPath;

return content;
});
}

return content;
})
.then((content) => {
const hash = loaderUtils.getHashDigest(content);
const targetPath = normalizePath(file.webpackTo);
const targetAbsolutePath = normalizePath(file.absoluteFrom);

if (
!copyUnmodified &&
written[file.webpackTo] &&
written[file.webpackTo][file.absoluteFrom] &&
written[file.webpackTo][file.absoluteFrom] === hash
written[targetPath] &&
written[targetPath][targetAbsolutePath] &&
written[targetPath][targetAbsolutePath] === hash
) {
logger.info(
`skipping '${file.webpackTo}', because content hasn't changed`
Expand All @@ -168,13 +172,13 @@ export default function postProcessPattern(globalRef, pattern, file) {

logger.debug(`adding '${file.webpackTo}' for tracking content changes`);

if (!written[file.webpackTo]) {
written[file.webpackTo] = {};
if (!written[targetPath]) {
written[targetPath] = {};
}

written[file.webpackTo][file.absoluteFrom] = hash;
written[targetPath][targetAbsolutePath] = hash;

if (compilation.assets[file.webpackTo] && !file.force) {
if (compilation.assets[targetPath] && !file.force) {
logger.info(
`skipping '${file.webpackTo}', because it already exists`
);
Expand All @@ -186,7 +190,7 @@ export default function postProcessPattern(globalRef, pattern, file) {
`writing '${file.webpackTo}' to compilation assets from '${file.absoluteFrom}'`
);

compilation.assets[file.webpackTo] = {
compilation.assets[targetPath] = {
size() {
return stats.size;
},
Expand Down
31 changes: 19 additions & 12 deletions test/CopyPlugin.test.js
Expand Up @@ -162,24 +162,19 @@ describe('apply function', () => {

if (opts.expectedAssetKeys && opts.expectedAssetKeys.length > 0) {
expect(Object.keys(compilation.assets).sort()).toEqual(
opts.expectedAssetKeys
.sort()
.map(removeIllegalCharacterForWindows)
.map((item) => item.replace(/\//g, path.sep))
opts.expectedAssetKeys.sort().map(removeIllegalCharacterForWindows)
);
} else {
expect(compilation.assets).toEqual({});
}

if (opts.expectedAssetContent) {
// eslint-disable-next-line guard-for-in
for (const key in opts.expectedAssetContent) {
const assetName = key.replace(/(\/|\\)/g, path.sep);

for (const assetName in opts.expectedAssetContent) {
expect(compilation.assets[assetName]).toBeDefined();

if (compilation.assets[assetName]) {
let expectedContent = opts.expectedAssetContent[key];
let expectedContent = opts.expectedAssetContent[assetName];

if (!Buffer.isBuffer(expectedContent)) {
expectedContent = Buffer.from(expectedContent);
Expand Down Expand Up @@ -244,10 +239,7 @@ describe('apply function', () => {
.then(() => {
if (opts.expectedAssetKeys && opts.expectedAssetKeys.length > 0) {
expect(Object.keys(compilation.assets).sort()).toEqual(
opts.expectedAssetKeys
.sort()
.map(removeIllegalCharacterForWindows)
.map((item) => item.replace(/\//g, path.sep))
opts.expectedAssetKeys.sort().map(removeIllegalCharacterForWindows)
);
} else {
expect(compilation.assets).toEqual({});
Expand Down Expand Up @@ -2566,4 +2558,19 @@ describe('apply function', () => {
});
});
});

it('should move a file and use posix separator for emitting assets', (done) => {
runEmit({
expectedAssetKeys: ['dir/nestedfile.txt'],
patterns: [
{
context: HELPER_DIR,
from: 'directory/nested/nestedfile.txt',
to: 'dir',
},
],
})
.then(done)
.catch(done);
});
});

0 comments on commit 7f08be6

Please sign in to comment.