浏览代码

add build.js

zxlie 7 年之前
父节点
当前提交
935b7306e1

+ 23 - 22
v2.0/apps/background/index.js

@@ -15,31 +15,34 @@ var BgPageInstance = (function () {
     let ajaxDbgCache = {};
 
 
+    //各种元素的就绪情况
+    let _readyStateMgr = {};
+    let _fcp_detect_interval = [];
+
     //侦测就绪情况
-    let _detectReadyState = function (callback) {
-        if (_readyState.css && _readyState.js && _readyState.html) {
-            _readyState.allDone = true;
-        }
-        if (_readyState.allDone && typeof callback === 'function') {
-            callback();
-        }
-    };
+    let _detectReadyState = function (getType, callback) {
+
+        chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
+            let tabId = tabs[0].id;
+            _readyStateMgr[tabId][getType] = true;
+
+            if (_readyStateMgr[tabId].css && _readyStateMgr[tabId].js) {
+                _readyStateMgr[tabId].allDone = true;
+            }
+            if (_readyStateMgr[tabId].allDone && typeof callback === 'function') {
+                callback();
+            }
+        });
 
-    //各种元素的就绪情况
-    let _readyState = {
-        css: false,
-        js: false,
-        html: true,
-        allDone: false
     };
-    let _fcp_detect_interval = [];
 
     /**
      * 执行前端FCPHelper检测
      */
     let _doFcpDetect = function (tab) {
+        _readyStateMgr[tab.id] = _readyStateMgr[tab.id] || {};
         //所有元素都准备就绪
-        if (_readyState.allDone) {
+        if (_readyStateMgr[tab.id].allDone) {
             clearInterval(_fcp_detect_interval[tab.id]);
             chrome.tabs.sendMessage(tab.id, {
                 type: MSG_TYPE.CODE_STANDARDS,
@@ -513,6 +516,7 @@ var BgPageInstance = (function () {
      */
     let _addExtensionListener = function () {
         chrome.runtime.onMessage.addListener(function (request, sender, callback) {
+
             //提取配置项
             if (request.type === MSG_TYPE.GET_OPTIONS) {
                 Settings.getOptsFromBgPage(callback);
@@ -574,18 +578,15 @@ var BgPageInstance = (function () {
             }
             //CSS准备就绪
             else if (request.type === MSG_TYPE.CSS_READY) {
-                _readyState.css = true;
-                _detectReadyState(callback);
+                _detectReadyState('css', callback);
             }
             //JS准备就绪
             else if (request.type === MSG_TYPE.JS_READY) {
-                _readyState.js = true;
-                _detectReadyState(callback);
+                _detectReadyState('js', callback);
             }
             //HTML准备就绪
             else if (request.type === MSG_TYPE.HTML_READY) {
-                _readyState.html = true;
-                _detectReadyState(callback);
+                _detectReadyState('html', callback);
             }
             // ===========================以上为编码规范检测====end==================================
 

+ 1 - 1
v2.0/apps/json-format/index.css

@@ -2,7 +2,7 @@
 @import url("./without-ui.css");
 
 body {
-    background: #fff;
+    background-color: #fff;
 }
 .wp-json {
     width:auto;

+ 1 - 90
v2.0/apps/json-format/index.html

@@ -40,96 +40,7 @@
                 </div>
             </div>
 
-        <textarea name="demo" id="demo" cols="30" rows="10" class="hide" ref="demoBox">
-{
-    "date": "20180322",
-    "message": "Success !",
-    "status": 200,
-    "city": "北京",
-    "count": 632,
-    "data": {
-        "shidu": "34%",
-        "pm25": 73,
-        "pm10": 91,
-        "quality": "良",
-        "wendu": "5",
-        "ganmao": "极少数敏感人群应减少户外活动",
-        "yesterday": {
-            "date": "21日星期三",
-            "sunrise": "06:19",
-            "high": "高温 11.0℃",
-            "low": "低温 1.0℃",
-            "sunset": "18:26",
-            "aqi": 85,
-            "fx": "南风",
-            "fl": "<3级",
-            "type": "多云",
-            "notice": "阴晴之间,谨防紫外线侵扰"
-        },
-        "forecast": [
-            {
-                "date": "22日星期四",
-                "sunrise": "06:17",
-                "high": "高温 17.0℃",
-                "low": "低温 1.0℃",
-                "sunset": "18:27",
-                "aqi": 98,
-                "fx": "西南风",
-                "fl": "<3级",
-                "type": "晴",
-                "notice": "愿你拥有比阳光明媚的心情"
-            },
-            {
-                "date": "23日星期五",
-                "sunrise": "06:16",
-                "high": "高温 18.0℃",
-                "low": "低温 5.0℃",
-                "sunset": "18:28",
-                "aqi": 118,
-                "fx": "无持续风向",
-                "fl": "<3级",
-                "type": "多云",
-                "notice": "阴晴之间,谨防紫外线侵扰"
-            },
-            {
-                "date": "24日星期六",
-                "sunrise": "06:14",
-                "high": "高温 21.0℃",
-                "low": "低温 7.0℃",
-                "sunset": "18:29",
-                "aqi": 52,
-                "fx": "西南风",
-                "fl": "<3级",
-                "type": "晴",
-                "notice": "愿你拥有比阳光明媚的心情"
-            },
-            {
-                "date": "25日星期日",
-                "sunrise": "06:13",
-                "high": "高温 22.0℃",
-                "low": "低温 7.0℃",
-                "sunset": "18:30",
-                "aqi": 71,
-                "fx": "西南风",
-                "fl": "<3级",
-                "type": "晴",
-                "notice": "愿你拥有比阳光明媚的心情"
-            },
-            {
-                "date": "26日星期一",
-                "sunrise": "06:11",
-                "high": "高温 21.0℃",
-                "low": "低温 8.0℃",
-                "sunset": "18:31",
-                "aqi": 97,
-                "fx": "西南风",
-                "fl": "<3级",
-                "type": "多云",
-                "notice": "阴晴之间,谨防紫外线侵扰"
-            }
-        ]
-    }
-}</textarea>
+        <textarea name="demo" id="demo" cols="30" rows="10" class="hide" ref="demoBox">{date:"20180322",message:"Success !",status:200,city:"北京",count:632,data:{shidu:"34%",pm25:73,pm10:91,quality:"良",wendu:"5",ganmao:"极少数敏感人群应减少户外活动",yesterday:{date:"21日星期三",sunrise:"06:19",high:"高温 11.0℃",low:"低温 1.0℃",sunset:"18:26",aqi:85,fx:"南风",fl:"<3级",type:"多云",notice:"阴晴之间,谨防紫外线侵扰"},forecast:[{date:"22日星期四",sunrise:"06:17",high:"高温 17.0℃",low:"低温 1.0℃",sunset:"18:27",aqi:98,fx:"西南风",fl:"<3级",type:"晴",notice:"愿你拥有比阳光明媚的心情"},{date:"23日星期五",sunrise:"06:16",high:"高温 18.0℃",low:"低温 5.0℃",sunset:"18:28",aqi:118,fx:"无持续风向",fl:"<3级",type:"多云",notice:"阴晴之间,谨防紫外线侵扰"},{date:"24日星期六",sunrise:"06:14",high:"高温 21.0℃",low:"低温 7.0℃",sunset:"18:29",aqi:52,fx:"西南风",fl:"<3级",type:"晴",notice:"愿你拥有比阳光明媚的心情"},{date:"25日星期日",sunrise:"06:13",high:"高温 22.0℃",low:"低温 7.0℃",sunset:"18:30",aqi:71,fx:"西南风",fl:"<3级",type:"晴",notice:"愿你拥有比阳光明媚的心情"},{date:"26日星期一",sunrise:"06:11",high:"高温 21.0℃",low:"低温 8.0℃",sunset:"18:31",aqi:97,fx:"西南风",fl:"<3级",type:"多云",notice:"阴晴之间,谨防紫外线侵扰"}]}};</textarea>
         </div>
         <script src="index.js"></script>
     </body>

+ 0 - 5
v2.0/apps/static/vendor/codemirror/active-line.js

@@ -2,11 +2,6 @@
 // Distributed under an MIT license: http://codemirror.net/LICENSE
 
 (function(mod) {
-    if (typeof exports == "object" && typeof module == "object") // CommonJS
-        mod(require("../../lib/codemirror"));
-    else if (typeof define == "function" && define.amd) // AMD
-        define(["../../lib/codemirror"], mod);
-    else // Plain browser env
         mod(CodeMirror);
 })(function(CodeMirror) {
     "use strict";

+ 0 - 5
v2.0/apps/static/vendor/codemirror/javascript.js

@@ -2,11 +2,6 @@
 // Distributed under an MIT license: http://codemirror.net/LICENSE
 
 (function(mod) {
-  if (typeof exports == "object" && typeof module == "object") // CommonJS
-    mod(require("../../lib/codemirror"));
-  else if (typeof define == "function" && define.amd) // AMD
-    define(["../../lib/codemirror"], mod);
-  else // Plain browser env
     mod(CodeMirror);
 })(function(CodeMirror) {
 "use strict";

+ 0 - 5
v2.0/apps/static/vendor/codemirror/matchbrackets.js

@@ -2,11 +2,6 @@
 // Distributed under an MIT license: http://codemirror.net/LICENSE
 
 (function(mod) {
-    if (typeof exports == "object" && typeof module == "object") // CommonJS
-        mod(require("../../lib/codemirror"));
-    else if (typeof define == "function" && define.amd) // AMD
-        define(["../../lib/codemirror"], mod);
-    else // Plain browser env
         mod(CodeMirror);
 })(function(CodeMirror) {
     var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&

+ 0 - 5
v2.0/apps/static/vendor/codemirror/placeholder.js

@@ -2,11 +2,6 @@
 // Distributed under an MIT license: http://codemirror.net/LICENSE
 
 (function(mod) {
-    if (typeof exports == "object" && typeof module == "object") // CommonJS
-        mod(require("../../lib/codemirror"));
-    else if (typeof define == "function" && define.amd) // AMD
-        define(["../../lib/codemirror"], mod);
-    else // Plain browser env
         mod(CodeMirror);
 })(function(CodeMirror) {
     CodeMirror.defineOption("placeholder", "", function(cm, val, old) {

+ 0 - 3
v2.0/apps/static/vendor/syntaxhighlighter/shBrushCss.js

@@ -16,9 +16,6 @@
  */
 ;(function()
 {
-	// CommonJS
-	typeof(require) != 'undefined' ? SyntaxHighlighter = require('shCore').SyntaxHighlighter : null;
-
 	function Brush()
 	{
 		function getKeywordsCSS(str)

+ 0 - 2
v2.0/apps/static/vendor/syntaxhighlighter/shBrushJScript.js

@@ -16,8 +16,6 @@
  */
 ;(function()
 {
-	// CommonJS
-	typeof(require) != 'undefined' ? SyntaxHighlighter = require('shCore').SyntaxHighlighter : null;
 
 	function Brush()
 	{

+ 0 - 2
v2.0/apps/static/vendor/syntaxhighlighter/shBrushSql.js

@@ -16,8 +16,6 @@
  */
 ;(function()
 {
-	// CommonJS
-	typeof(require) != 'undefined' ? SyntaxHighlighter = require('shCore').SyntaxHighlighter : null;
 
 	function Brush()
 	{

+ 0 - 2
v2.0/apps/static/vendor/syntaxhighlighter/shBrushXml.js

@@ -16,8 +16,6 @@
  */
 ;(function()
 {
-	// CommonJS
-	typeof(require) != 'undefined' ? SyntaxHighlighter = require('shCore').SyntaxHighlighter : null;
 
 	function Brush()
 	{

文件差异内容过多而无法显示
+ 0 - 10942
v2.0/apps/static/vendor/vue/vue.js


+ 92 - 0
v2.0/build.js

@@ -0,0 +1,92 @@
+/**
+ * FeHelper Build Script
+ * @author zhaoxianlie
+ */
+
+let path = require('path');
+let shell = require('shelljs');
+let glob = require('glob');
+let jsMinifier = require('babel-minify');
+let cssMinifier = require('uglifycss');
+let htmlMinifier = require('html-minifier');
+let fs = require('fs');
+let rootPath = path.resolve('.');
+
+// copy files
+let copyFiles = function () {
+    console.time('> 文件拷贝');
+    shell.cd(rootPath);
+    shell.mkdir('-p', 'output');
+    shell.rm('-rf', 'output/*');
+    shell.cp('-r', 'apps', 'output');
+    console.timeEnd('> 文件拷贝');
+};
+
+// js 文件压缩
+let jsCompress = function () {
+    console.time('> Javascript文件压缩');
+    glob.sync('output/apps/**/*.js').map(cf => {
+        let rawJs = fs.readFileSync(cf, 'utf8').toString();
+        let compressedJs = jsMinifier(rawJs);
+        if (compressedJs.code) {
+            fs.writeFileSync(cf, compressedJs.code);
+        } else {
+            console.log('---------------------', cf, '----------------------');
+            console.log(compressedJs.error.message, '\n')
+        }
+    });
+    console.timeEnd('> Javascript文件压缩');
+};
+
+// css 文件压缩
+let cssCompress = function () {
+    console.time('> CSS文件压缩');
+    let reg = /\@import\s+url\(\s*('|")([^\)]+)\1\s*\)\s*;?/gm;
+
+    glob.sync('output/apps/**/*.css').map(cf => {
+        let rawCss = fs.readFileSync(cf, 'utf8');
+
+        rawCss = rawCss.toString().replace(reg, ($0, $1, importedFile) => {
+            let iPath = path.resolve(cf, '../' + importedFile);
+            return fs.readFileSync(iPath, 'utf8').toString();
+        });
+        let compressedCss = cssMinifier.processString(rawCss, {maxLineLen: 500, expandVars: true});
+        fs.writeFileSync(cf, compressedCss);
+    });
+    console.timeEnd('> CSS文件压缩');
+};
+
+// html 文件压缩
+let htmlCompress = function () {
+    console.time('> HTML文件压缩');
+    glob.sync('output/apps/**/*.html').map(cf => {
+        try {
+            let rawHtml = fs.readFileSync(cf, 'utf8').toString();
+            let compressedHtml = htmlMinifier.minify(rawHtml, {
+                collapseWhitespace: true
+            });
+            fs.writeFileSync(cf, compressedHtml);
+        } catch (e) {
+        }
+    });
+    console.timeEnd('> HTML文件压缩');
+};
+
+// zip the fehelper
+let zipForChromeWebStore = function () {
+    console.time('> FeHelper打包');
+    shell.cd(`${rootPath}/output`);
+    shell.exec('zip -r fehelper.zip apps/ > /dev/null');
+    console.timeEnd('> FeHelper打包');
+    console.log('\n\n================================================================================');
+    console.log('    去Chrome商店发布吧:https://chrome.google.com/webstore/developer/dashboard');
+    console.log('================================================================================\n\n');
+};
+
+/************************build start*****************************/
+copyFiles();
+cssCompress();
+htmlCompress();
+jsCompress();
+zipForChromeWebStore();
+

+ 5 - 6
v2.0/package.json

@@ -2,18 +2,17 @@
   "name": "fehelper",
   "version": "1.0.0",
   "description": "chrome extension",
-  "main": "index.js",
+  "main": "build.js",
   "dependencies": {},
   "devDependencies": {
-    "copy-webpack-plugin": "^4.5.1",
+    "babel-minify": "^0.4.0",
     "glob": "^7.1.2",
+    "html-minifier": "^3.5.14",
     "shelljs": "^0.8.1",
-    "uglifyjs-webpack-plugin": "^1.2.4",
-    "webpack": "^4.4.1",
-    "webpack-cli": "^2.0.13"
+    "uglifycss": "0.0.29"
   },
   "scripts": {
-    "dev": "webpack --mode production"
+    "build": "node build.js"
   },
   "repository": {
     "type": "git",

+ 67 - 32
v2.0/webpack.config.js

@@ -1,8 +1,54 @@
+/**
+ * FeHelper Webpack Config
+ * @author zhaoxianlie
+ */
+
 let path = require('path');
 let shell = require('shelljs');
 let glob = require('glob');
+let uglifyCss = require('uglifycss');
+let htmlMinifier = require('html-minifier');
+let fs = require('fs');
 let CopyWebpackPlugin = require('copy-webpack-plugin');
-let UglifyJsPlugin = require('uglifyjs-webpack-plugin');
+let rootPath = path.resolve('.');
+
+// html 文件压缩
+let htmlCompress = function () {
+    glob.sync('output/apps/**/*.html').map(cf => {
+        try {
+            let rawHtml = fs.readFileSync(cf).toString();
+            let compressedHtml = htmlMinifier.minify(rawHtml, {
+                collapseWhitespace: true
+            });
+            fs.writeFileSync(cf, compressedHtml);
+        } catch (e) {
+            console.log(cf, '压缩失败')
+        }
+    });
+};
+
+// css 文件压缩
+let cssCompress = function () {
+    let reg = /\@import\s+url\(\s*('|")([^\)]+)\1\s*\)/gm;
+
+    glob.sync('output/apps/**/*.css').map(cf => {
+        let rawCss = fs.readFileSync(cf);
+
+        rawCss = rawCss.toString().replace(reg, ($0, $1, importedFile) => {
+            let iPath = path.resolve(cf, '../' + importedFile);
+            return fs.readFileSync(iPath).toString() + ';';
+        });
+        let uglifiedCss = uglifyCss.processString(rawCss, {maxLineLen: 500, expandVars: true});
+        fs.writeFileSync(cf, uglifiedCss);
+    });
+};
+
+
+// zip the fehelper
+let zipForChromeWebStore = function () {
+    shell.cd(`${rootPath}/output`);
+    shell.exec('zip -r fehelper.zip apps/ > /dev/null');
+};
 
 // 所有的功能模块
 let mods = (() => {
@@ -13,58 +59,47 @@ let mods = (() => {
 })();
 
 // 动态生成entry
-let entries = (globPath) => {
-    let files = glob.sync(globPath);
-    let entries = {}, entry, dirname, basename;
-
-    for (let i = 0; i < files.length; i++) {
-        entry = files[i];
-        dirname = path.dirname(entry);
-        basename = path.basename(entry, '.js');
-        entries[path.join(dirname, basename)] = './' + entry;
-    }
+let buildEntry = (globPath) => {
+
+    let entries = {}, dirname, basename;
+
+    glob.sync(globPath).map(jf => {
+        dirname = path.dirname(jf);
+        basename = path.basename(jf, '.js');
+        entries[path.join(dirname, basename)] = './' + jf;
+    });
+
     return entries;
 };
 
 // 文件拷贝的配置
 let copyConfig = (() => {
     return mods.map(mod => {
-        let from = {glob: 'apps/' + (mod === 'manifest.json') ? mod : (mod + '/**/*')};
-        let to = {to: 'output/apps'};
-        return {from: from, to: to}
+        let from = {glob: './apps/' + ((mod === 'manifest.json') ? mod : (mod + '/**/*'))};
+        return {from: from, to: './'}
     });
 })();
 
 module.exports = {
-    entry: entries('apps/**/*.js'),
+    entry: buildEntry('apps/**/*.js'),
     output: {
         path: path.resolve(__dirname, 'output'),
         filename: "[name].js",
         chunkFilename: '[id].js'
     },
 
-    module: {
-        // rules: [{
-        //     test: /\.js$/,
-        //     loader: 'babel-loader',
-        // }, {
-        //     test: /\.css$/,
-        //     loader: 'css-loader'
-        // }, {
-        //     test: /\.(png|jpg|jpeg|gif|ico)$/,
-        //     loader: 'file-loader'
-        // }, {
-        //     test: /\.html$/,
-        //     loader: 'file-loader'
-        // }]
+    optimization: {
+        minimize: false
     },
+
     plugins: [
         new CopyWebpackPlugin(copyConfig, {ignore: ['*.js']}),
-        new UglifyJsPlugin(),
         function () {
             this.plugin('done', stats => {
-                console.log(stats)
-            });
+                cssCompress();
+                htmlCompress();
+                zipForChromeWebStore();
+            })
         }
     ]
 };

部分文件因为文件数量过多而无法显示