Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Mar 22, 2021
1 parent 6537f57 commit 0beceeb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ module.exports = {
const result = assets.reduce((accumulator, asset) => {
// The asset content can be obtained from `asset.source` using `source` method.
// The asset content is a [`Buffer`](https://nodejs.org/api/buffer.html) object, it could be converted to a `String` to be processed using `content.toString()`
const content = asset.source.source();
const content = asset.data;

accumulator = `${accumulator}${content}\n`;
return accumulator;
Expand Down
55 changes: 30 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ class CopyPlugin {
if (transform.transformer) {
logger.log(`transforming content for '${absoluteFilename}'...`);

const buffer = result.source.source();
const buffer = result.source.buffer();

if (transform.cache) {
const defaultCacheKeys = {
Expand Down Expand Up @@ -550,7 +550,7 @@ class CopyPlugin {
const contentHash = CopyPlugin.getContentHash(
compiler,
compilation,
result.source.source()
result.source.buffer()
);
const ext = path.extname(result.sourceFilename);
const base = path.basename(result.sourceFilename);
Expand Down Expand Up @@ -664,16 +664,16 @@ class CopyPlugin {

const mergedEtag =
assets.length === 1
? cache.getLazyHashedEtag(assets[0].source.source())
: assets.reduce((accumulator, asset, ind) => {
? cache.getLazyHashedEtag(assets[0].source.buffer())
: assets.reduce((accumulator, asset, i) => {
// eslint-disable-next-line no-param-reassign
accumulator = cache.mergeEtags(
ind === 1
i === 1
? cache.getLazyHashedEtag(
accumulator.source.source()
accumulator.source.buffer()
)
: accumulator,
cache.getLazyHashedEtag(asset.source.source())
cache.getLazyHashedEtag(asset.source.buffer())
);

return accumulator;
Expand All @@ -683,22 +683,24 @@ class CopyPlugin {
version,
from: item.from,
to: item.to,
transform: item.transformAll,
transformAll: item.transformAll,
})}`;
const eTag = cache.getLazyHashedEtag(mergedEtag);
const cacheItem = cache.getItemCache(cacheKeys, eTag);
let transformedAsset = await cacheItem.getPromise();

if (!transformedAsset) {
const { RawSource } = compiler.webpack.sources;

transformedAsset = {
filename: item.to,
};
transformedAsset = { filename: item.to };

try {
transformedAsset.source = new RawSource(
await item.transformAll(assets)
transformedAsset.data = await item.transformAll(
assets.map((asset) => {
return {
data: asset.source.buffer(),
sourceFilename: asset.sourceFilename,
absoluteFilename: asset.absoluteFilename,
};
})
);
} catch (error) {
compilation.errors.push(error);
Expand All @@ -710,29 +712,32 @@ class CopyPlugin {
const contentHash = CopyPlugin.getContentHash(
compiler,
compilation,
transformedAsset.source.source()
transformedAsset.data
);

const data = {
contentHash,
chunk: {
hash: contentHash,
contentHash,
},
};

const {
path: interpolatedFilename,
info: assetInfo,
} = compilation.getPathWithInfo(
normalizePath(item.to),
data
{
contentHash,
chunk: {
hash: contentHash,
contentHash,
},
}
);

transformedAsset.filename = interpolatedFilename;
transformedAsset.info = assetInfo;
}

const { RawSource } = compiler.webpack.sources;

transformedAsset.source = new RawSource(
transformedAsset.data
);
transformedAsset.force = item.force;

await cacheItem.storePromise(transformedAsset);
Expand Down
8 changes: 3 additions & 5 deletions test/transformAll-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ describe("transformAll option", () => {
to: "file.txt",
transformAll(assets) {
const result = assets.reduce((accumulator, asset) => {
const content =
asset.source.source().toString() || asset.sourceFilename;
const content = asset.data.toString() || asset.sourceFilename;
// eslint-disable-next-line no-param-reassign
accumulator = `${accumulator}${content}::`;
return accumulator;
Expand Down Expand Up @@ -164,7 +163,7 @@ describe("transformAll option", () => {
transformAll(assets) {
const result = assets.reduce((accumulator, asset) => {
// eslint-disable-next-line no-param-reassign
accumulator = `${accumulator}${asset.source.source()}::`;
accumulator = `${accumulator}${asset.data}::`;
return accumulator;
}, "");

Expand All @@ -189,8 +188,7 @@ describe("cache", () => {
to: "file.txt",
transformAll(assets) {
const result = assets.reduce((accumulator, asset) => {
const content =
asset.source.source().toString() || asset.sourceFilename;
const content = asset.data.toString() || asset.sourceFilename;
// eslint-disable-next-line no-param-reassign
accumulator = `${accumulator}${content}::`;
return accumulator;
Expand Down

0 comments on commit 0beceeb

Please sign in to comment.