├── .crunshrc
├── .eslintrc
├── .gitignore
├── .npmrc
├── COMMANDLINE-ARGUMENTS.md
├── README.md
├── index.js
└── package.json
/.crunshrc:
--------------------------------------------------------------------------------
1 | {
2 | "js" : {
3 | "mangle": true,
4 | "beautify": false,
5 | "fromString": true
6 | },
7 | "css" : {
8 | "maxLineLen": 500,
9 | "expandVars": true
10 | },
11 | "html": {
12 | "customAttrSurround": [["/\\{\\{#[^}]+\\}\\}/", "/\\{\\{\\/[^}]+\\}\\}/"]],
13 | "removeComments" : true,
14 | "html5" : true,
15 | "preserveLineBreaks" : false,
16 | "processScripts" : ["text/x-handlebars-template"],
17 | "removeCommentsFromCDATA" : true,
18 | "collapseWhitespace" : true,
19 | "collapseBooleanAttributes" : false,
20 | "removeAttributeQuotes" : false,
21 | "removeRedundantAttributes" : false,
22 | "useShortDoctype" : true,
23 | "removeEmptyAttributes" : false,
24 | "removeOptionalTags" : false,
25 | "removeEmptyElements" : false
26 | },
27 | "inputfile" : "c:/git/film/dev/index.html",
28 | "outputfolder" : "./public/"
29 | }
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "env": {
3 | "mocha": true,
4 | "node": true
5 | },
6 | "rules":{
7 | "no-trailing-spaces" : "off",
8 | "no-multiple-empty-lines":"off",
9 | "space-before-function-paren" : "off",
10 | "keyword-spacing" : "off",
11 | "indent" : "off",
12 | "quotes": ["warn", "double", "avoid-escape"],
13 | "eol-last" : "off",
14 | "space-before-blocks" : "off",
15 | "no-mixed-spaces-and-tabs" : "warn",
16 | "key-spacing" : "off",
17 | "comma-spacing" : "off",
18 | "no-multi-spaces" : "off",
19 | "padded-blocks" : "off",
20 | "spaced-comment": "off",
21 | "no-extra-boolean-cast" : "off",
22 | "max-depth":["error", 4],
23 | "no-negated-condition":"off",
24 | "no-implicit-coercion":"off",
25 | "no-unused-vars":"warn",
26 | "no-useless-escape" : "off",
27 | "space-in-parens" : "off",
28 | "semi-spacing": "off",
29 | "no-useless-concat": "off",
30 | "max-params":["error", 3],
31 | "max-nested-callbacks":["error", { "max": 3 }],
32 | "max-statements" : ["warn", { "max": 60 }],
33 | "max-lines": ["warn", 300],
34 | "no-lonely-if" :"off",
35 | "space-infix-ops" : "warn",
36 | "camelcase" : "off",
37 | "comma-dangle" : "warn",
38 | "no-new-func":"off",
39 | "no-prototype-builtins":"off",
40 | "no-new":"warn"
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 |
3 | ## Ignore Visual Studio temporary files, build results, and
4 | ## files generated by popular Visual Studio add-ons.
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # DNX
46 | project.lock.json
47 | project.fragment.lock.json
48 | artifacts/
49 |
50 | *_i.c
51 | *_p.c
52 | *_i.h
53 | *.ilk
54 | *.meta
55 | *.obj
56 | *.pch
57 | *.pdb
58 | *.pgc
59 | *.pgd
60 | *.rsp
61 | *.sbr
62 | *.tlb
63 | *.tli
64 | *.tlh
65 | *.tmp
66 | *.tmp_proj
67 | *.log
68 | *.vspscc
69 | *.vssscc
70 | .builds
71 | *.pidb
72 | *.svclog
73 | *.scc
74 |
75 | # Chutzpah Test files
76 | _Chutzpah*
77 |
78 | # Visual C++ cache files
79 | ipch/
80 | *.aps
81 | *.ncb
82 | *.opendb
83 | *.opensdf
84 | *.sdf
85 | *.cachefile
86 | *.VC.db
87 | *.VC.VC.opendb
88 |
89 | # Visual Studio profiler
90 | *.psess
91 | *.vsp
92 | *.vspx
93 | *.sap
94 |
95 | # TFS 2012 Local Workspace
96 | $tf/
97 |
98 | # Guidance Automation Toolkit
99 | *.gpState
100 |
101 | # ReSharper is a .NET coding add-in
102 | _ReSharper*/
103 | *.[Rr]e[Ss]harper
104 | *.DotSettings.user
105 |
106 | # JustCode is a .NET coding add-in
107 | .JustCode
108 |
109 | # TeamCity is a build add-in
110 | _TeamCity*
111 |
112 | # DotCover is a Code Coverage Tool
113 | *.dotCover
114 |
115 | # NCrunch
116 | _NCrunch_*
117 | .*crunch*.local.xml
118 | nCrunchTemp_*
119 |
120 | # MightyMoose
121 | *.mm.*
122 | AutoTest.Net/
123 |
124 | # Web workbench (sass)
125 | .sass-cache/
126 |
127 | # Installshield output folder
128 | [Ee]xpress/
129 |
130 | # DocProject is a documentation generator add-in
131 | DocProject/buildhelp/
132 | DocProject/Help/*.HxT
133 | DocProject/Help/*.HxC
134 | DocProject/Help/*.hhc
135 | DocProject/Help/*.hhk
136 | DocProject/Help/*.hhp
137 | DocProject/Help/Html2
138 | DocProject/Help/html
139 |
140 | # Click-Once directory
141 | publish/
142 |
143 | # Publish Web Output
144 | *.[Pp]ublish.xml
145 | *.azurePubxml
146 | # TODO: Comment the next line if you want to checkin your web deploy settings
147 | # but database connection strings (with potential passwords) will be unencrypted
148 | *.pubxml
149 | *.publishproj
150 |
151 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
152 | # checkin your Azure Web App publish settings, but sensitive information contained
153 | # in these scripts will be unencrypted
154 | PublishScripts/
155 |
156 | # NuGet Packages
157 | *.nupkg
158 | # The packages folder can be ignored because of Package Restore
159 | **/packages/*
160 | # except build/, which is used as an MSBuild target.
161 | !**/packages/build/
162 | # Uncomment if necessary however generally it will be regenerated when needed
163 | #!**/packages/repositories.config
164 | # NuGet v3's project.json files produces more ignoreable files
165 | *.nuget.props
166 | *.nuget.targets
167 |
168 | # Microsoft Azure Build Output
169 | csx/
170 | *.build.csdef
171 |
172 | # Microsoft Azure Emulator
173 | ecf/
174 | rcf/
175 |
176 | # Windows Store app package directories and files
177 | AppPackages/
178 | BundleArtifacts/
179 | Package.StoreAssociation.xml
180 | _pkginfo.txt
181 |
182 | # Visual Studio cache files
183 | # files ending in .cache can be ignored
184 | *.[Cc]ache
185 | # but keep track of directories ending in .cache
186 | !*.[Cc]ache/
187 |
188 | # Others
189 | ClientBin/
190 | ~$*
191 | *~
192 | *.dbmdl
193 | *.dbproj.schemaview
194 | *.pfx
195 | *.publishsettings
196 | node_modules/
197 | orleans.codegen.cs
198 |
199 | # Since there are multiple workflows, uncomment next line to ignore bower_components
200 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
201 | #bower_components/
202 |
203 | # RIA/Silverlight projects
204 | Generated_Code/
205 |
206 | # Backup & report files from converting an old project file
207 | # to a newer Visual Studio version. Backup files are not needed,
208 | # because we have git ;-)
209 | _UpgradeReport_Files/
210 | Backup*/
211 | UpgradeLog*.XML
212 | UpgradeLog*.htm
213 |
214 | # SQL Server files
215 | *.mdf
216 | *.ldf
217 |
218 | # Business Intelligence projects
219 | *.rdl.data
220 | *.bim.layout
221 | *.bim_*.settings
222 |
223 | # Microsoft Fakes
224 | FakesAssemblies/
225 |
226 | # GhostDoc plugin setting file
227 | *.GhostDoc.xml
228 |
229 | # Node.js Tools for Visual Studio
230 | .ntvs_analysis.dat
231 |
232 | # Visual Studio 6 build log
233 | *.plg
234 |
235 | # Visual Studio 6 workspace options file
236 | *.opt
237 |
238 | # Visual Studio LightSwitch build output
239 | **/*.HTMLClient/GeneratedArtifacts
240 | **/*.DesktopClient/GeneratedArtifacts
241 | **/*.DesktopClient/ModelManifest.xml
242 | **/*.Server/GeneratedArtifacts
243 | **/*.Server/ModelManifest.xml
244 | _Pvt_Extensions
245 |
246 | # Paket dependency manager
247 | .paket/paket.exe
248 | paket-files/
249 |
250 | # FAKE - F# Make
251 | .fake/
252 |
253 | # JetBrains Rider
254 | .idea/
255 | *.sln.iml
256 |
257 | # CodeRush
258 | .cr/
259 |
260 | # Python Tools for Visual Studio (PTVS)
261 | __pycache__/
262 | *.pyc
263 | /npm-debug.*.*
264 | /.vscode
265 | /build
266 | /coverage
267 | /deployager__deployager_rsa.pem
268 | /deployager__deployager_rsa
269 | /public
270 | /yarn.lock
271 |
--------------------------------------------------------------------------------
/.npmrc:
--------------------------------------------------------------------------------
1 | message=":arrow_up: %s"
--------------------------------------------------------------------------------
/COMMANDLINE-ARGUMENTS.md:
--------------------------------------------------------------------------------
1 | # Help
2 | If no parameters are given the app searches a file called ```.crunshrc```.
3 | The content should look like the following example:
4 |
5 | ## Example of `.crunshrc` containing default values
6 | ```js
7 | {
8 | "js" : {
9 | "mangle": true,
10 | "beautify": false,
11 | "fromString": true
12 | },
13 | "css" : {
14 | "maxLineLen": 500,
15 | "expandVars": true
16 | },
17 | "html": {
18 | "customAttrSurround": [["/\\{\\{#[^}]+\\}\\}/", "/\\{\\{\\/[^}]+\\}\\}/"]],
19 | "removeComments" : true,
20 | "html5" : true,
21 | "preserveLineBreaks" : false,
22 | "processScripts" : ["text/x-handlebars-template"],
23 | "removeCommentsFromCDATA" : true,
24 | "collapseWhitespace" : true,
25 | "collapseBooleanAttributes" : false,
26 | "removeAttributeQuotes" : false,
27 | "removeRedundantAttributes" : false,
28 | "useShortDoctype" : true,
29 | "removeEmptyAttributes" : false,
30 | "removeOptionalTags" : false,
31 | "removeEmptyElements" : false
32 | },
33 | "inputfile" : "c:/git/film/dev/index.html",
34 | "outputfolder" : "./public/"
35 | }
36 | ```
37 |
38 | ## Usage
39 |
40 | ```sh
41 | crunsh [--targetfolder ./out];
42 | ```
43 |
44 | ## Parameters
45 | |Name|Description|
46 | |----|-----------|
47 | |--outputfolder|Targetfolder for minified files.|
48 | |help, --help, /? |Show this help.|
49 | |version, --version, -v|Show version.|
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Minify HTML, Javascript and CSS withoud headache.
2 |
3 | ## usage
4 |
5 | ```$ crunsh index.html --outputfolder ../public```
6 |
7 | => Creates a minified index.html, app.js and style.css in ```outputfolder```.
8 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | "use strict";
4 |
5 | var CLI = require("n-cli");
6 | var cli = new CLI({
7 | silent: false,
8 | handleUncaughtException : true,
9 | runcom : ".crunshrc"
10 | });
11 |
12 |
13 | var hbAttrWrapOpen = /\{\{#[^}]+\}\}/;
14 | var hbAttrWrapClose = /\{\{\/[^}]+\}\}/;
15 | var hbAttrWrapPair = [hbAttrWrapOpen, hbAttrWrapClose];
16 | var htmlMinifyOptions = {
17 | customAttrSurround: [hbAttrWrapPair],
18 | removeComments : true,
19 | html5 : true,
20 | preserveLineBreaks : false,
21 | processScripts : ["text/x-handlebars-template"],
22 | removeCommentsFromCDATA : true,
23 | collapseWhitespace : true,
24 | collapseBooleanAttributes : false,
25 | removeAttributeQuotes : false,
26 | removeRedundantAttributes : false,
27 | useShortDoctype : true,
28 | removeEmptyAttributes : false,
29 | removeOptionalTags : false,
30 | removeEmptyElements : false
31 | };
32 | var jsMinifyOptions = {
33 | mangle: true,
34 | beautify: false,
35 | fromString: true
36 | };
37 | var cssMinifyOptions = {
38 | maxLineLen: 500,
39 | expandVars: true
40 | };
41 | var fs = require("fs");
42 | var path = require("path");
43 | var cheerio = require("cheerio");
44 |
45 | function crunsh (argv){
46 | var js = "";
47 | var css = "";
48 | var inputFilename = argv._[0];
49 | var outputFolder = cli.resolvePath(argv.notNull("outputfolder"));
50 | var dir = path.dirname(inputFilename);
51 | var $ = cheerio.load(fs.readFileSync(inputFilename));
52 |
53 | $("script:not([src=''])").each(function(){
54 | var src = $(this).attr("src");
55 | if (src !== undefined){
56 | cli.stdout(cli.color.yellow("try fetch " + src + "...\n"));
57 | if (fs.existsSync(path.join(dir, src))){
58 | js += ";"+fs.readFileSync(path.join(dir, src)).toString() + "\n";
59 | cli.stdout(cli.color.white("remove " + src + " from HTML\n"));
60 | $(this).remove();
61 | } else {
62 | cli.stdout(cli.color.red("skip " + src + "\n"));
63 | }
64 | }
65 | });
66 | $("link").each(function(){
67 | var src = $(this).attr("href");
68 | if (src !== undefined && src.toLowerCase().slice(src.length - 4, src.length) === ".css"){
69 | cli.stdout(cli.color.cyan("try fetch " + src + "...\n"));
70 | if (fs.existsSync(path.join(dir, src))){
71 | css += fs.readFileSync(path.join(dir, src)).toString() + "\n";
72 | cli.stdout(cli.color.white("remove " + src + " from HTML\n"));
73 | $(this).remove();
74 | } else {
75 | cli.stdout(cli.color.red("skip " + src + "\n"));
76 | }
77 | }
78 | });
79 |
80 |
81 | cli.stdout(cli.color.green("minify assets.\n"));
82 |
83 | var cssTarget = (path.join(outputFolder, "style.css"));
84 | var jsTarget = (path.join(outputFolder, "app.js"));
85 | var htmlTarget = (path.join(outputFolder, "index.html"));
86 |
87 | cli.stdout(cli.color.yellow("minify js...\n"));
88 | var UglifyJS = require("uglify-js");
89 | if (jsMinifyOptions.mangle === true){
90 | js = UglifyJS.minify(js, jsMinifyOptions);
91 | }
92 | cli.stdout(cli.color.white("write js " + jsTarget + "\n"));
93 | $("body").append("");
94 | fs.writeFileSync(jsTarget, js.code || js);
95 |
96 |
97 |
98 | cli.stdout(cli.color.yellow("minify css...\n"));
99 | $("head").append("");
100 | var uglifycss = require("uglifycss");
101 | var minifiedCss = uglifycss.processString(css, cssMinifyOptions);
102 | cli.stdout(cli.color.white("write css " + cssTarget + "\n"));
103 | fs.writeFileSync(cssTarget, minifiedCss);
104 |
105 |
106 | var finalize = function(){
107 | cli.stdout(cli.color.yellow("minify html...\n"));
108 | var minify = require("html-minifier").minify;
109 | $("body").html(minify($("body").html(), htmlMinifyOptions));
110 | $("head").html(minify($("head").html(), htmlMinifyOptions));
111 | cli.stdout(cli.color.white("write html " + htmlTarget + "\n"));
112 | fs.writeFileSync(htmlTarget, $.html());
113 | cli.stdout(cli.color.green("done.\n"));
114 | };
115 |
116 | var packageJson = cli.resolvePath(path.join(dir, "package.json"));
117 | if (fs.existsSync(packageJson)){
118 | cli.stdout(cli.color.green("found " + packageJson + "...\n"));
119 | cli.stdout(cli.color.yellow("try to bump version of " + packageJson + " ...\n"));
120 | var npm = require("npm");
121 | npm.load({
122 | loaded: false,
123 | prefix: dir
124 | }, function (err) {
125 | if (err) throw new cli.Error(err);
126 | npm.commands.version(["patch"], function (er, data) {
127 | if (er){
128 | throw new cli.Error(er);
129 | } else {
130 | var packageJsonObject = require(packageJson);
131 | cli.stdout(cli.color.yellow("Add author " + packageJsonObject.author + " to HTML.\n"));
132 | $("head").append("");
133 |
134 | cli.stdout(cli.color.yellow("Add version " + packageJsonObject.version + " to HTML.\n"));
135 | $("head").append("");
136 |
137 | finalize();
138 | }
139 | });
140 | });
141 | } else {
142 | finalize();
143 | }
144 |
145 | }
146 |
147 | cli.on(function(){
148 | if (this.argv._.length !== 0){
149 | var f = this.argv._[0];
150 | if (!f){
151 | throw new cli.Error("missing-parameter", "");
152 | }
153 | if (!fs.existsSync(f)){
154 | throw new cli.Error("file-not-found", f);
155 | }
156 | crunsh(this.argv);
157 | }
158 | });
159 |
160 | cli.runcom(function(rc){
161 | console.log(this.argv._)
162 | if (this.argv._.length === 0){
163 | if (!rc.settings.inputfile){
164 | throw new cli.Error("missing-property-inputfile", rc.fullpath);
165 | }
166 | if (!rc.settings.outputfolder){
167 | throw new cli.Error("missing-property-outputfolder", rc.fullpath);
168 | }
169 |
170 | htmlMinifyOptions = rc.settings.html || htmlMinifyOptions;
171 | jsMinifyOptions = rc.settings.js || jsMinifyOptions;
172 | cssMinifyOptions = rc.settings.css || cssMinifyOptions;
173 |
174 | this.argv._[0] = rc.settings.inputfile;
175 | this.argv.outputfolder = rc.settings.outputfolder;
176 | crunsh(this.argv);
177 | }
178 | });
179 |
180 |
181 |
182 |
183 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "crunsh-single-page",
3 | "version": "1.0.6",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "dev": "node index.js c:/git/film/dev/index.html --outputfolder c:/git/film/public",
8 | "test": "echo \"Error: no test specified\" && exit 1",
9 | "bump": "npm version patch && git push && git push --tags && npm publish"
10 | },
11 | "bin": {
12 | "crunsh": "index.js"
13 | },
14 | "repository": {
15 | "type": "git",
16 | "url": "git+https://github.com/s-a/crunsh-single-page.git"
17 | },
18 | "author": "s-a",
19 | "license": "MIT",
20 | "bugs": {
21 | "url": "https://github.com/s-a/crunsh-single-page/issues"
22 | },
23 | "homepage": "https://github.com/s-a/crunsh-single-page#readme",
24 | "dependencies": {
25 | "chalk": "^1.1.3",
26 | "cheerio": "^0.22.0",
27 | "html-minifier": "^3.1.0",
28 | "n-cli": "^1.2.7",
29 | "npm": "^3.10.9",
30 | "pretty-error": "^2.0.1",
31 | "uglify-js": "^2.7.3",
32 | "uglifycss": "0.0.21"
33 | }
34 | }
35 |
--------------------------------------------------------------------------------