1
0
mirror of https://github.com/S2-/minifyfromhtml.git synced 2025-08-02 20:00:05 +02:00

4 Commits

Author SHA1 Message Date
s2
5a7294bcf9 v1.1.1 2019-04-15 15:43:19 +02:00
s2
91ce9fe859 ignore dist 2019-04-15 15:42:49 +02:00
s2
96dbb27eea refactor 2019-04-15 15:42:39 +02:00
s2
01c4e4d60d quickstart 2019-04-15 12:09:09 +02:00
7 changed files with 56 additions and 81 deletions

2
.eslintignore Normal file
View File

@@ -0,0 +1,2 @@
node_modules
dist

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/dist

View File

@@ -2,12 +2,15 @@
This little helper minifies scripts and css starting from an html file. I made it after asking this question on stackoverflow: https://stackoverflow.com/questions/50188269/minify-js-and-css-in-one-go-starting-from-html/
It takes an input html file, parses it, and outputs all included javascript and css files minified to a file you specify:
```
minifyfromhtml --js=<output js file> --css=<output css file> < <input file>
```
## Quickstart
example:
```
minifyfromhtml --js=dist/mywidget.min.js --css=dist/mywidget.min.css < example/index.html
npm i git+https://github.com/S2-/minifyfromhtml.git#semver:1.x
node node_modules\minifyfromhtml\minifyfromhtml.js --js=dist.js --css=dist.css < index.html
```
=>
```
css/mywidget.css -> dist.css
js/jquery.js -> dist.js
js/spectacularwidget.js -> dist.js
```

View File

@@ -1 +0,0 @@
#mywidget{border:2px solid red}

View File

@@ -1 +0,0 @@
var spectacularwidget=function(a){a.innerHTML="this is so spectacular!"};window.spectacularwidget=spectacularwidget;

View File

@@ -17,15 +17,15 @@ let usage = `usage:
if (argv.h) {
console.log(usage);
return;
process.exit(0);
}
if (!argv.js || !argv.css) {
console.log(usage);
return;
process.exit(0);
}
var excludeFiles = argv.exclude || [];
let excludeFiles = argv.exclude || [];
if (typeof(excludeFiles) === 'string') {
excludeFiles = [excludeFiles];
}
@@ -40,7 +40,8 @@ let readStdin = function(cb) {
process.stdin.on('end', function() {
cb(stdin);
});
}
};
readStdin(function(html) {
let dom = new JSDOM(html);
@@ -57,79 +58,49 @@ readStdin(function(html) {
}
}
return scripts;
}
};
//process scripts
let scripts = getTagAttrs(dom, 'script', 'src');
//remove exluded
excludeFiles.forEach(i => {
var index = scripts.indexOf(i);
if (index !== -1) {
scripts.splice(index, 1);
}
});
let processedScripts = {};
for (let i = 0; i < scripts.length; i++) {
let script = scripts[i];
minify(script, 'stream')
.then(function(data) {
processedScripts[script] = data;
if (Object.keys(processedScripts).length === scripts.length) {
//write scripts
//clear out dist file
fs.writeFileSync(argv.js, '');
//write files
for (let i = 0; i < scripts.length; i++) {
const script = scripts[i];
console.log(script + ' -> ' + argv.js);
fs.appendFileSync(argv.js, processedScripts[script] + '\n');
}
let processThings = function(things, outFile) {
//remove exluded
excludeFiles.forEach(i => {
let index = things.indexOf(i);
if (index !== -1) {
things.splice(index, 1);
}
});
let processedThings = {};
for (let i = 0; i < things.length; i++) {
let thing = things[i];
minify(thing)
.then(function(data) {
processedThings[thing] = data;
if (Object.keys(processedThings).length === things.length) {
//write things
//clear out dist file
fs.writeFileSync(outFile, '');
//write files
for (let i = 0; i < things.length; i++) {
const thing = things[i];
console.log(thing + ' -> ' + outFile);
fs.appendFileSync(outFile, processedThings[thing] + '\n');
}
}
});
}
};
if (argv.js) {
processThings(getTagAttrs(dom, 'script', 'src'), argv.js);
}
//process css
let styles = getTagAttrs(dom, 'link', 'href');
//remove exluded
excludeFiles.forEach(i => {
var index = styles.indexOf(i);
if (index !== -1) {
styles.splice(index, 1);
}
});
let processedStyles = {};
fs.writeFileSync(argv.css, '');
for (let i = 0; i < styles.length; i++) {
let style = styles[i];
minify(style, 'stream')
.then(function(data) {
processedStyles[style] = data;
if (Object.keys(processedStyles).length === styles.length) {
//write styles
//clear out dist file
fs.writeFileSync(argv.css, '');
//write files
for (let i = 0; i < styles.length; i++) {
const style = styles[i];
console.log(style + ' -> ' + argv.css);
fs.appendFileSync(argv.css, processedStyles[style] + '\n');
}
}
});
if (argv.css) {
processThings(getTagAttrs(dom, 'link', 'href'), argv.css);
}
});

View File

@@ -1,6 +1,6 @@
{
"name": "minifyfromhtml",
"version": "1.1.0",
"version": "1.1.1",
"description": "minify scripts and css starting from an html file",
"main": "minifyfromhtml.js",
"scripts": {