Skip to content
This repository has been archived by the owner on Dec 24, 2021. It is now read-only.

Commit

Permalink
Resolves #10
Browse files Browse the repository at this point in the history
Add new option `compressOutput=true|false` that allows to change the
behavior of the plugin. By default this option is disabled and the
plugin compresses the files based on the Parcel bundle object and that
match the `test` regular expression.

If `true` it compresses all files in the output directory and
subdirectories that match the `test` regular expression.
  • Loading branch information
ralscha committed Apr 23, 2020
1 parent 19a1819 commit b520b9e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ To configure, add a file called `.compressrc` in your project's root folder, or
"lgblock": undefined, // 16 - 24
"nPostfix": undefined, // 0 - 3
"nDirect": undefined // 0 to (15 << nPostfix) in steps of (1 << nPostfix)
}
},
// a flag that changes the behavior of the plugin, be default this option is disabled
// and the plugin compresses all the files it receives via the Parcel bundle object.
// if true the plugin compresses all files in the outputdirectory and subdirectories
// that match the test regular expression
compressOutput: false
}
```

Expand Down
43 changes: 35 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const path = require('path');
const { default: pQueue } = require('p-queue');
const { cosmiconfig } = require('cosmiconfig');
const chalk = require('chalk');
Expand All @@ -12,6 +13,7 @@ const zopfli = zopfliAdapter();

const defaultOptions = {
test: '.',
compressOutput: false,
threshold: undefined,
concurrency: 2,
gzip: {
Expand All @@ -37,27 +39,52 @@ let output = [];

module.exports = bundler => {
bundler.on('bundled', async (bundle) => {
console.log(bundle);
if (process.env.NODE_ENV === 'production') {
const start = new Date().getTime();
console.log(chalk.bold('\n🗜️ Compressing bundled files...\n'));

try {
const explorer = cosmiconfig('compress');
const { config: { gzip, brotli, test, threshold } } = (await explorer.search()) || { config: defaultOptions };
const { config: { gzip, brotli, test, threshold, compressOutput } } = (await explorer.search()) || { config: defaultOptions };

const fileTest = new RegExp(test);
function* filesToCompress(bundle) {
if (bundle.name && fileTest.test(bundle.name)) {
yield bundle.name
let filesToCompress;
let input;

if (compressOutput === true) {
input = bundle.entryAsset.options.outDir;
filesToCompress = function* (dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const f = path.join(dir, file);
if (fs.statSync(f).isDirectory()) {
try {
yield* filesToCompress(f);
} catch (error) {
continue;
}
} else {
if (fileTest.test(f)) {
yield f;
}
}
}
}
for (var child of bundle.childBundles) {
yield* filesToCompress(child)
} else {
input = bundle;
filesToCompress = function* (bundle) {
if (bundle.name && fileTest.test(bundle.name)) {
yield bundle.name
}
for (var child of bundle.childBundles) {
yield* filesToCompress(child)
}
}
}

const queue = new pQueue({ concurrency: defaultOptions.concurrency });

[...filesToCompress(bundle)].forEach(file => {
[...filesToCompress(input)].forEach(file => {
queue.add(() => gzipCompress(file, { ...defaultOptions.gzip, threshold, ...gzip }));
queue.add(() => brotliCompress(file, { ...defaultOptions.brotli, threshold, ...brotli }));
});
Expand Down

0 comments on commit b520b9e

Please sign in to comment.