Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanPan0131 committed Aug 3, 2023
1 parent 00c90fc commit 919f9ba
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 47 deletions.
67 changes: 20 additions & 47 deletions minify.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
var fs = require('fs');
const fs = require('fs');

const querystring = require('querystring');
const https = require('https');

const minify = require('@node-minify/core');
const gcc = require('@node-minify/google-closure-compiler');

function readFiles(dirname, onFileContent, onError) {
fs.readdir(dirname, (err, filenames) => {
if (err) {
Expand All @@ -25,52 +28,22 @@ function readFiles(dirname, onFileContent, onError) {
});
}

function minify(content) {
return new Promise((resolve, reject) => {
var chunk = '';
const query = querystring.stringify({
input: content
});
const req = https.request({
method: 'POST',
hostname: 'www.toptal.com',
path: '/developers/javascript-minifier/api/raw',
}, function (res) {
res.setEncoding('utf8');
res.on('data', d => {
chunk += d;
});
res.on('end', () => {
if (res.statusCode !== 200) reject(JSON.parse(chunk));
else resolve(chunk);
});
});
req.on('error', function (err) {
reject(err);
});
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setHeader('Content-Length', query.length);
req.end(query, 'utf8');
})
}

readFiles('./out/_next/static/chunks/', (path, content) => {
minify(content)
.then(minified => {
console.log(path)
fs.writeFileSync(path, minified);
})
.catch(err => {
console.error(err);
})
}, function (err) {
console.error(err);
});
minify({
compressor: gcc,
input: path,
output: path,
callback: function (err, min) {
if (err) console.error(err);
else console.log(path);
}
});
}, err => console.error(err));

readFiles('./out/_next/static/css/', (path, content) => {
console.log(path)
content = content.replace(/\/\*([\s\S]*?)\*\//gi, '');
fs.writeFileSync(path, content);
}, function (err) {
console.error(err);
});
var minified = content.replace(/\/\*([\s\S]*?)\*\//gi, '');
fs.writeFile(path, minified, err => {
if (err) console.error(err);
else console.log(path);
});
}, err => console.error(err));
Loading

0 comments on commit 919f9ba

Please sign in to comment.