Selaa lähdekoodia

Add minify command

Alexandre Dieulot 5 vuotta sitten
vanhempi
commit
77c478f171
6 muutettua tiedostoa jossa 17 lisäystä ja 3 poistoa
  1. 1 0
      .gitignore
  2. 4 0
      README.md
  3. 1 0
      package.json
  4. 6 3
      test/app.js
  5. 1 0
      test/footer.html
  6. 4 0
      test/header.html

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+instantpage.min.js

+ 4 - 0
README.md

@@ -15,3 +15,7 @@ With [Node](https://nodejs.org/), run:
 `node test/app.js`
 
 And access http://127.0.0.1:8000/. Or specify another port with an argument after the filename.
+
+## Minifying
+
+To minify instantpage.js into instantpage.min.js install uglify-es globally (`npm i uglify-es -g`) then run `npm run minify`.

+ 1 - 0
package.json

@@ -7,6 +7,7 @@
     "test": "test"
   },
   "scripts": {
+    "minify": "uglifyjs ./instantpage.js --compress --mangle --toplevel --comments \"/^!/\" --output ./instantpage.min.js",
     "test": "echo \"Error: no test specified\" && exit 1"
   },
   "repository": {

+ 6 - 3
test/app.js

@@ -16,6 +16,7 @@ let SLEEP_TIME = 200
 let CACHE_MAX_AGE = 0
 let USE_WHITELIST = 0
 let INTENSITY = 65
+let USE_MINIFIED = 0
 
 function handleCookies(req) {
   const cookies = req.headers.cookie
@@ -40,6 +41,7 @@ function handleCookies(req) {
     if (isNaN(INTENSITY)) {
       INTENSITY = value.split(',')[4]
     }
+    USE_MINIFIED = cookieValueSplit[5]
   })
 }
 
@@ -50,6 +52,8 @@ function sha384(data) {
 }
 
 async function requestListener(req, res) {
+  handleCookies(req)
+
   let headers = {
     'Content-Type': 'text/html',
   }
@@ -62,7 +66,7 @@ async function requestListener(req, res) {
 
   let content = ''
 
-  const jsContent = await fsPromises.readFile(path.resolve(__dirname, '../instantpage.js'))
+  const jsContent = await fsPromises.readFile(path.resolve(__dirname, `../${USE_MINIFIED ? 'instantpage.min.js' : 'instantpage.js'}`))
   const jsHash = sha384(jsContent)
 
   if (pathString == 'instantpage.js') {
@@ -70,8 +74,6 @@ async function requestListener(req, res) {
     content += jsContent
   }
   else if (!isNaN(page)) {
-    handleCookies(req)
-
     await sleep(SLEEP_TIME)
 
     if (CACHE_MAX_AGE) {
@@ -96,6 +98,7 @@ async function requestListener(req, res) {
     content = content.replace(':value_sleep', `value="${SLEEP_TIME}"`)
     content = content.replace(':value_cacheAge', `value="${CACHE_MAX_AGE}"`)
     content = content.replace(':value_intensity', `value="${INTENSITY}"`)
+    content = content.replace(':checked_minified', USE_MINIFIED ? 'checked' : '')
 
     content += `<h1>Page ${page}</h1>`
     for (let i = 1; i <= 3; i++) {

+ 1 - 0
test/footer.html

@@ -30,6 +30,7 @@ form.addEventListener('submit', (e) => {
     form.cacheAge.value,
     form.whitelist.checked ? 1 : 0,
     form.intensity.value,
+    form.minified.checked ? 1 : 0,
   ].join(',')
   document.cookie = `instantpage_test=${cookieValue}`
   location.reload()

+ 4 - 0
test/header.html

@@ -77,6 +77,10 @@ form input[type="text"] {
     Cache max age (in seconds):
     <input name="cacheAge" type="number" :value_cacheAge>
   </label>
+  <label>
+    Use minified:
+    <input name="minified" type="checkbox" :checked_minified>
+  </label>
   <label>
     <input type="submit" value="OK">
   </label>