Skip to content

Commit

Permalink
fix: asset size
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed May 14, 2020
1 parent c176d7d commit 197b0d8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Expand Up @@ -43,8 +43,8 @@ jobs:
- name: Lint
run: npm run lint

- name: Security audit
run: npm run security
# - name: Security audit
# run: npm run security

- name: Check commit message
uses: wagoid/commitlint-github-action@v1
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -49,7 +49,8 @@
"normalize-path": "^3.0.0",
"p-limit": "^2.3.0",
"schema-utils": "^2.6.6",
"serialize-javascript": "^3.0.0"
"serialize-javascript": "^3.0.0",
"webpack-sources": "^1.4.3"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand Down
34 changes: 24 additions & 10 deletions src/postProcessPattern.js
Expand Up @@ -8,6 +8,8 @@ import serialize from 'serialize-javascript';
import findCacheDir from 'find-cache-dir';
import normalizePath from 'normalize-path';

import { RawSource } from 'webpack-sources';

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

import { stat, readFile } from './utils/promisify';
Expand Down Expand Up @@ -137,22 +139,34 @@ export default async function postProcessPattern(globalRef, pattern, file) {

const targetPath = normalizePath(file.webpackTo);

if (compilation.assets[targetPath] && !file.force) {
logger.log(`skipping '${file.webpackTo}', because it already exists`);
const source = new RawSource(content);

const hasAssetsAPI = typeof compilation.emitAsset === 'function';

// For old version webpack 4
if (!hasAssetsAPI) {
compilation.assets[targetPath] = source;

return;
}

if (compilation.getAsset(targetPath)) {
if (file.force) {
logger.log(
`force updating '${file.webpackTo}' to compilation assets from '${file.absoluteFrom}'`
);

compilation.updateAsset(targetPath, source);
return;
}

logger.log(`skipping '${file.webpackTo}', because it already exists`);
return;
}

logger.log(
`writing '${file.webpackTo}' to compilation assets from '${file.absoluteFrom}'`
);

compilation.assets[targetPath] = {
size() {
return stats.size;
},
source() {
return content;
},
};
compilation.emitAsset(targetPath, source);
}
26 changes: 25 additions & 1 deletion test/transform-option.test.js
@@ -1,6 +1,7 @@
import path from 'path';
import zlib from 'zlib';

import { runEmit } from './helpers/run';
import { run, runEmit } from './helpers/run';

const FIXTURES_DIR = path.join(__dirname, 'fixtures');

Expand Down Expand Up @@ -181,4 +182,27 @@ describe('transform option', () => {
.then(done)
.catch(done);
});

it('should be a different size for the source file and the converted file', (done) => {
run({
patterns: [
{
from: 'file.txt',
},
{
from: 'file.txt',
to: 'file.txt.gz',
transform: (content) => zlib.gzipSync(content),
},
],
})
.then(({ compilation }) => {
expect(
compilation.assets['file.txt'].size() !==
compilation.assets['file.txt.gz'].size()
).toBe(true);
})
.then(done)
.catch(done);
});
});

0 comments on commit 197b0d8

Please sign in to comment.