Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use posix separator for emitting assets #392

Merged
merged 1 commit into from Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});