Skip to content

Commit

Permalink
fix: compatibility with webpack@5 (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 29, 2020
1 parent 13cebc1 commit 714af2f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ npm-debug.log*
/local
/reports
/node_modules
/test/fixtures/\[special\?directory\]
/test/fixtures/\[special\$directory\]

.DS_Store
Thumbs.db
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import webpack from 'webpack';
import validateOptions from 'schema-utils';
import pLimit from 'p-limit';

Expand All @@ -6,6 +7,11 @@ import preProcessPattern from './preProcessPattern';
import processPattern from './processPattern';
import postProcessPattern from './postProcessPattern';

// webpack 5 exposes the sources property to ensure the right version of webpack-sources is used
const { RawSource } =
// eslint-disable-next-line global-require
webpack.sources || require('webpack-sources');

class CopyPlugin {
constructor(options = {}) {
validateOptions(schema, options, {
Expand Down Expand Up @@ -89,10 +95,12 @@ class CopyPlugin {
absoluteFrom,
targetPath,
webpackTo,
source,
data,
force,
} = asset;

const source = new RawSource(data);

// For old version webpack 4
/* istanbul ignore if */
if (typeof compilation.emitAsset !== 'function') {
Expand Down
22 changes: 9 additions & 13 deletions src/postProcessPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import cacache from 'cacache';
import serialize from 'serialize-javascript';
import findCacheDir from 'find-cache-dir';
import normalizePath from 'normalize-path';
import { RawSource } from 'webpack-sources';

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

import { readFile } from './utils/promisify';

/* eslint-disable no-param-reassign */

export default async function postProcessPattern(globalRef, pattern, file) {
const { logger, compilation, inputFileSystem } = globalRef;

Expand All @@ -27,10 +25,10 @@ export default async function postProcessPattern(globalRef, pattern, file) {

logger.debug(`reading '${file.absoluteFrom}' to write to assets`);

let content;
let data;

try {
content = await readFile(inputFileSystem, file.absoluteFrom);
data = await readFile(inputFileSystem, file.absoluteFrom);
} catch (error) {
compilation.errors.push(error);

Expand All @@ -49,7 +47,7 @@ export default async function postProcessPattern(globalRef, pattern, file) {
let defaultCacheKeys = {
version,
transform: pattern.transform,
contentHash: crypto.createHash('md4').update(content).digest('hex'),
contentHash: crypto.createHash('md4').update(data).digest('hex'),
};

if (typeof pattern.cacheTransform.keys === 'function') {
Expand All @@ -73,18 +71,16 @@ export default async function postProcessPattern(globalRef, pattern, file) {
`getting cached transformation for '${file.absoluteFrom}'`
);

content = result.data;
({ data } = result);
} catch (_ignoreError) {
content = await pattern.transform(content, file.absoluteFrom);
data = await pattern.transform(data, file.absoluteFrom);

logger.debug(`caching transformation for '${file.absoluteFrom}'`);

content = await cacache
.put(cacheDirectory, cacheKeys, content)
.then(() => content);
await cacache.put(cacheDirectory, cacheKeys, data);
}
} else {
content = await pattern.transform(content, file.absoluteFrom);
data = await pattern.transform(data, file.absoluteFrom);
}
}

Expand All @@ -103,7 +99,7 @@ export default async function postProcessPattern(globalRef, pattern, file) {
{ resourcePath: file.absoluteFrom },
file.webpackTo,
{
content,
content: data,
context: pattern.context,
}
);
Expand All @@ -123,7 +119,7 @@ export default async function postProcessPattern(globalRef, pattern, file) {
);
}

file.source = new RawSource(content);
file.data = data;
file.targetPath = normalizePath(file.webpackTo);
file.force = pattern.force;

Expand Down

0 comments on commit 714af2f

Please sign in to comment.