From 6f1805ceb01016a4848221d9f690360d28c1e5df Mon Sep 17 00:00:00 2001 From: Karson Date: Wed, 15 Mar 2023 16:15:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Enpm=E5=8C=85=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增grunt进行JS和CSS打包 移除bower包管理 优化压缩打包 --- .bowerrc | 11 ----- Gruntfile.js | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++ bower.json | 34 -------------- package.json | 95 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 220 insertions(+), 45 deletions(-) delete mode 100644 .bowerrc create mode 100644 Gruntfile.js delete mode 100755 bower.json create mode 100644 package.json diff --git a/.bowerrc b/.bowerrc deleted file mode 100644 index 3f53a734..00000000 --- a/.bowerrc +++ /dev/null @@ -1,11 +0,0 @@ -{ - "directory": "public/assets/libs", - "ignoredDependencies": [ - "es6-promise", - "file-saver", - "html2canvas", - "jspdf", - "jspdf-autotable", - "pdfmake" - ] -} diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 00000000..5fc38383 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,125 @@ +module.exports = function (grunt) { + + grunt.initConfig({ + pkg: grunt.file.readJSON('package.json'), + copy: { + main: { + files: [] + } + } + }); + + var build = function (module, type, callback) { + var config = { + compile: { + options: type === 'js' ? { + optimizeCss: "standard", + optimize: "none", //可使用uglify|closure|none + preserveLicenseComments: true, + removeCombined: false, + baseUrl: "./public/assets/js/", //JS文件所在的基础目录 + name: "require-" + module, //来源文件,不包含后缀 + out: "./public/assets/js/require-" + module + ".min.js" //目标文件 + } : { + optimizeCss: "default", + optimize: "uglify", //可使用uglify|closure|none + cssIn: "./public/assets/css/" + module + ".css", //JS文件所在的基础目录 + out: "./public/assets/css/" + module + ".min.css" //目标文件 + } + } + }; + + + var content = grunt.file.read("./public/assets/js/require-" + module + ".js"), + pattern = /^require\.config\(\{[\r\n]?[\n]?(.*?)[\r\n]?[\n]?}\);/is; + + var matches = content.match(pattern); + if (matches) { + if (type === 'js') { + var data = matches[1].replaceAll(/(urlArgs|baseUrl):(.*)\n/gi, ''); + const parse = require('parse-config-file'), fs = require('fs'); + require('jsonminify'); + + data = JSON.minify("{\n" + data + "\n}"); + let options = parse(data); + options.paths.tableexport = "empty:"; + Object.assign(config.compile.options, options); + } + let requirejs = require("./application/admin/command/Min/r"); + + try { + requirejs.optimize(config.compile.options, function (buildResponse) { + // var contents = require('fs').readFileSync(config.compile.options.out, 'utf8'); + callback(); + }, function (err) { + console.error(err); + callback(); + }); + } catch (err) { + console.error(err); + callback(); + } + } + }; + + // 加载 "copy" 插件 + grunt.loadNpmTasks('grunt-contrib-copy'); + + grunt.registerTask('frontend:js', 'build frontend js', function () { + var done = this.async(); + build('frontend', 'js', done); + }); + + grunt.registerTask('backend:js', 'build backend js', function () { + var done = this.async(); + build('backend', 'js', done); + }); + + grunt.registerTask('frontend:css', 'build frontend css', function () { + var done = this.async(); + build('frontend', 'css', done); + }); + + grunt.registerTask('backend:css', 'build frontend css', function () { + var done = this.async(); + build('backend', 'css', done); + }); + + // 注册部署JS和CSS任务 + grunt.registerTask('deploy', 'deploy', function () { + const fs = require('fs'); + const path = require("path") + const nodeModulesDir = path.resolve(__dirname, "./node_modules"); + + const getAllFiles = function (dirPath, arrayOfFiles) { + files = fs.readdirSync(dirPath) + + arrayOfFiles = arrayOfFiles || [] + + files.forEach(function (file) { + if (fs.statSync(dirPath + "/" + file).isDirectory()) { + arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles) + } else { + arrayOfFiles.push(path.join(__dirname, dirPath, "/", file)) + } + }); + + return arrayOfFiles + }; + const mainPackage = grunt.config.get('pkg'); + let dists = mainPackage.dists || []; + let files = []; + + Object.keys(dists).forEach(key => { + let src = ["**/*LICENSE*", "**/*license*"]; + src = src.concat(Array.isArray(dists[key]) ? dists[key] : [dists[key]]); + files.push({expand: true, cwd: nodeModulesDir + "/" + key, src: src, dest: 'public/assets/libs/' + key + "/"}); + }); + grunt.config.set('copy.main.files', files); + grunt.task.run("copy:main"); + }); + + // 注册默认任务 + grunt.registerTask('default', ['deploy', 'frontend:js', 'backend:js', 'frontend:css', 'backend:css']); + +}; diff --git a/bower.json b/bower.json deleted file mode 100755 index 78509804..00000000 --- a/bower.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "name": "fastadmin", - "description": "the fastest admin framework", - "main": "", - "license": "Apache2.0", - "homepage": "https://www.fastadmin.net", - "private": true, - "dependencies": { - "jquery": "^2.1.4", - "bootstrap": "^3.3.7", - "font-awesome": "^4.6.1", - "bootstrap-table": "fastadmin-bootstraptable#~1.11.5", - "jstree": "~3.3.2", - "moment": "~2.29.0", - "toastr": "~2.1.3", - "eonasdan-bootstrap-datetimepicker": "~4.17.43", - "bootstrap-select": "~1.13.18", - "require-css": "~0.1.8", - "tableExport.jquery.plugin": "~1.10.3", - "jquery-slimscroll": "~1.3.8", - "jquery.cookie": "~1.4.1", - "Sortable": "~1.10.0", - "nice-validator": "~1.1.1", - "art-template": "~3.1.3", - "bootstrap-daterangepicker": "~2.1.25", - "fastadmin-citypicker": "~1.3.1", - "fastadmin-cxselect": "~1.4.0", - "fastadmin-dragsort": "~1.0.0", - "fastadmin-addtabs": "~1.0.5", - "fastadmin-selectpage": "~1.0.6", - "fastadmin-layer": "~3.5.1", - "bootstrap-slider": "*" - } -} diff --git a/package.json b/package.json new file mode 100644 index 00000000..30743bb4 --- /dev/null +++ b/package.json @@ -0,0 +1,95 @@ +{ + "name": "fastadmin", + "version": "1.4.0", + "description": "FastAdmin是一款基于ThinkPHP+Bootstrap的极速后台开发框架。", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git@gitee.com:karson/fastadmin.git" + }, + "dependencies": { + "art-template": "^3.1.3", + "bootstrap": "^3.4.1", + "bootstrap-daterangepicker": "~2.1.25", + "bootstrap-select": "^1.13.18", + "bootstrap-slider": "^11.0.2", + "coloris": "npm:@melloware/coloris@^0.16.1", + "eonasdan-bootstrap-datetimepicker": "^4.17.49", + "fastadmin-addtabs": "karsonzhang/jquery-addtabs#semver:~1.0.7", + "fastadmin-bootstraptable": "karsonzhang/fastadmin-bootstraptable", + "fastadmin-citypicker": "karsonzhang/fastadmin-citypicker#semver:~1.3.2", + "fastadmin-cxselect": "karsonzhang/fastadmin-cxselect#semver:~1.4.0", + "fastadmin-dragsort": "karsonzhang/fastadmin-dragsort#semver:~1.0.1", + "fastadmin-layer": "karsonzhang/fastadmin-layer#semver:~3.5.2", + "fastadmin-selectpage": "karsonzhang/fastadmin-selectpage#semver:~1.0.11", + "font-awesome": "^4.6.1", + "jquery": "^3.6.1", + "jquery-slimscroll": "~1.3.8", + "jquery.cookie": "~1.4.1", + "jstree": "~3.3.2", + "moment": "^2.10", + "nice-validator": "~1.1.1", + "require-css": "~0.1.8", + "sortablejs": "^1.12.0", + "tableexport.jquery.plugin": "^1.26.0", + "toastr": "~2.1.3" + }, + "author": "FastAdmin", + "license": "Apache-2.0", + "devDependencies": { + "grunt": "^1.5.3", + "grunt-contrib-clean": "^2.0.1", + "grunt-contrib-copy": "^1.0.0", + "jsonminify": "^0.4.2", + "parse-config-file": "^1.0.4" + }, + "overrides": { + "eonasdan-bootstrap-datetimepicker": { + "moment-timezone": "^0.5.0" + }, + "canvg": { + "xmldom": "^0.7.0" + } + }, + "dists": { + "art-template": "dist/**", + "bootstrap": "dist/**", + "bootstrap-daterangepicker": [ + "daterangepicker.js", + "daterangepicker.css" + ], + "bootstrap-select": "dist/**", + "bootstrap-slider": "dist/**", + "coloris": ["dist/umd/**", "dist/*.css"], + "eonasdan-bootstrap-datetimepicker": "build/**", + "fastadmin-addtabs": "*.js", + "fastadmin-bootstraptable": "dist/**", + "fastadmin-citypicker": "dist/**", + "fastadmin-cxselect": "js/**", + "fastadmin-dragsort": "*.js", + "fastadmin-layer": "dist/**", + "fastadmin-selectpage": "*", + "font-awesome": [ + "css/**", + "fonts/**" + ], + "jquery": "dist/**", + "jquery-slimscroll": "*.js", + "jquery.cookie": "*.js", + "jstree": "dist/**", + "moment": [ + "moment.js", + "locale/**" + ], + "nice-validator": "dist/**", + "require-css": "*.js", + "sortablejs": "*.js", + "tableexport.jquery.plugin": "tableExport.min.js", + "toastr": [ + "toastr.js", + "build/**" + ] + } +}