From 3fcea91bf49923a18582e37ce73dd19cf3c87dbb Mon Sep 17 00:00:00 2001 From: shuxiaokai <2581105856@qq.com> Date: Mon, 19 Apr 2021 18:00:45 +0800 Subject: [PATCH] =?UTF-8?q?cli:=20=E6=9B=B4=E6=96=B0=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/default.build.js | 99 ++++++++++++++++++++++++ build/single.build.js | 61 +++++++++++++++ package.json | 9 ++- vue.config.js | 166 +++++------------------------------------ 4 files changed, 186 insertions(+), 149 deletions(-) create mode 100644 build/default.build.js create mode 100644 build/single.build.js diff --git a/build/default.build.js b/build/default.build.js new file mode 100644 index 00000000..fa3f8654 --- /dev/null +++ b/build/default.build.js @@ -0,0 +1,99 @@ +/** + * @description electron默认打包配置 + * @author shuxiaokai + */ +const path = require("path"); +const config = require("../src/config"); + +module.exports = { + pages: { + index: { + entry: "src/renderer/main.js", //添加了entry则不需要rendererProcessFile + template: "public/index.html", + }, + }, + //=====================================devserver====================================// + devServer: { + port: 9999, + }, + //=====================================css相关配置====================================// + css: { + loaderOptions: { + css: { + // 这里的选项会传递给 css-loader + }, + sass: { + prependData: `@import "@/scss/index.scss";`, + }, + }, + sourceMap: false, + }, + //=====================================扩展webpack配置====================================// + configureWebpack: { + resolve: { + alias: { + "@": path.resolve(__dirname, "../src/renderer"), + "~": path.resolve(__dirname, "../"), + }, + }, + }, + pluginOptions: { + electronBuilder: { + extends: null, + externals: [ + "vue", + "axios", + "vue-electron", + "vue-router", + "vuex", + "vuex-electron", + "element-ui", + "js-cookie", + "mockjs", + "nprogress", + "monaco-editor", + "vuedraggable", + "ali-oss", + "json5", + "echarts", + "brace", + "urllib", + "cookie-parser", + "got", + "form-data", + "proxy-agent", + ], + mainProcessFile: "src/main/index.js", + mainProcessWatch: ["src/main/index.js"], + builderOptions: { + productName: config.renderConfig.layout.title, + appId: "com.example.yourapp", + publish: [ + { + provider: "generic", + url: "", + }, + ], + nsis: { + oneClick: false, // 是否一键安装 + allowToChangeInstallationDirectory: true, // 允许修改安装目录 + }, + mac: { + icon: "build/icons/icon.icns", + }, + win: { + icon: "build/icons/icon.ico", + }, + linux: { + icon: "build/icons", + }, + }, + }, + }, + //=====================================eslint配置====================================// + lintOnSave: "error", //未通过eslint 禁止代码提交 + //=====================================打包上线配置====================================// + publicPath: config.build.publicPath || "/", + outputDir: "dist", //输出文件类型 + productionSourceMap: true, //打包时候js是否添加sourceMap +}; diff --git a/build/single.build.js b/build/single.build.js new file mode 100644 index 00000000..0645b0af --- /dev/null +++ b/build/single.build.js @@ -0,0 +1,61 @@ +/** + * @description 默认打包配置 + * @author shuxiaokai + */ +const path = require("path"); +const HtmlWebpackPlugin = require("html-webpack-plugin"); +const HtmlWebpackInlineSourcePlugin = require("html-webpack-inline-source-plugin"); +const config = require("../src/config"); + +module.exports = { + //=====================================css相关配置====================================// + css: { + loaderOptions: { + sass: { + prependData: `@import "@/scss/index.scss";`, + }, + }, + extract: false, + sourceMap: false, + }, + //=====================================扩展webpack配置====================================// + configureWebpack: { + resolve: { + alias: { + "@": path.resolve(__dirname, "../src/renderer"), + "~": path.resolve(__dirname, "../"), + }, + }, + optimization: { + splitChunks: false, // makes there only be 1 js file - leftover from earlier attempts but doesn't hurt + }, + plugins: [ + new HtmlWebpackPlugin({ + templateParameters: { + BASE_URL: config.build.publicPath || "/", + }, + filename: "index.html", // the output file name that will be created + template: "public/index.html", // this is important - a template file to use for insertion + inlineSource: ".(js|css|png|jpg|woff|ttf)$", // embed all javascript and css inline + }), + new HtmlWebpackInlineSourcePlugin(), + ], + }, + chainWebpack: (webpackConfig) => { + const fontsRule = webpackConfig.module.rule("fonts"); + // clear all existing loaders. + // if you don't do this, the loader below will be appended to + // existing loaders of the rule. + fontsRule.uses.clear(); + webpackConfig.module + .rule("fonts") + .test(/\.(ttf|otf|eot|woff|woff2)$/) + .use("base64-inline-loader") + .loader("base64-inline-loader") + .end(); + }, + //=====================================eslint配置====================================// + lintOnSave: "error", //未通过eslint 禁止代码提交 + //=====================================打包上线配置====================================// + publicPath: config.build.publicPath || "/", +} diff --git a/package.json b/package.json index 7b77bafa..7b139587 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,11 @@ "private": true, "scripts": { "dev:web": "vue-cli-service serve src/renderer/main.js", - "dev:doc-export": "vue-cli-service serve src/renderer/pages/modules/apidoc/doc-export/main.js --single", - "build:doc-offline": "vue-cli-service build src/renderer/pages/modules/apidoc/doc-export/main.js --single", - "build:doc-export": "vue-cli-service build src/renderer/pages/modules/apidoc/doc-export/main.js", + "dev:cli": "vue-cli-service serve src/renderer/main.js --cli", + "dev:doc-offline": "vue-cli-service serve src/renderer/pages/modules/apidoc/doc-export/main.js --web", + "dev:doc-share": "vue-cli-service serve src/renderer/pages/modules/apidoc/doc-export/main.js --web", + "build:doc-share": "vue-cli-service build src/renderer/pages/modules/apidoc/doc-export/main.js --web", + "build:doc-offline": "vue-cli-service build src/renderer/pages/modules/apidoc/doc-export/main.js --offline", "build:web": "vue-cli-service build src/renderer/main.js", "test:e2e": "vue-cli-service test:e2e", "lint": "vue-cli-service lint", @@ -71,6 +73,7 @@ "eslint-plugin-vue": "^6.2.2", "html-webpack-inline-source-plugin": "0.0.10", "husky": "^4.3.0", + "minimist": "^1.2.5", "sass": "^1.26.5", "sass-loader": "^8.0.2", "vue-cli-plugin-electron-builder": "~2.0.0-rc.5", diff --git a/vue.config.js b/vue.config.js index fde164d5..e919b741 100644 --- a/vue.config.js +++ b/vue.config.js @@ -1,159 +1,33 @@ -const path = require("path"); -const HtmlWebpackPlugin = require("html-webpack-plugin"); -const HtmlWebpackInlineSourcePlugin = require("html-webpack-inline-source-plugin"); +const minimist = require("minimist"); const config = require("./src/config"); +const defaultBuildConfig = require("./build/default.build"); +const singleBuildConfig = require("./build/single.build"); process.env.VUE_APP_TITLE = config.renderConfig.layout.title; -const isShareDoc = process.argv.find((val) => val === "--single"); +const rawArgv = process.argv.slice(2); +const argv = minimist(rawArgv); +const buildWebPage = argv.web; //在线连接和生成静态HTML页面 +const buildSingleWebPage = argv.offline; //仅生成单个文件 let vueConfig = {}; -if (isShareDoc) { - vueConfig = { - //=====================================css相关配置====================================// - css: { - loaderOptions: { - sass: { - prependData: `@import "@/scss/index.scss";`, - }, - }, - extract: false, - sourceMap: false, - }, - //=====================================扩展webpack配置====================================// - configureWebpack: { - resolve: { - alias: { - "@": path.resolve(__dirname, "src/renderer"), - "~": path.resolve(__dirname, ""), - }, - }, - optimization: { - splitChunks: false, // makes there only be 1 js file - leftover from earlier attempts but doesn't hurt - }, - plugins: [ - new HtmlWebpackPlugin({ - templateParameters: { - BASE_URL: config.build.publicPath || "/", - }, - filename: "index.html", // the output file name that will be created - template: "public/index.html", // this is important - a template file to use for insertion - inlineSource: ".(js|css|png|jpg|woff|ttf)$", // embed all javascript and css inline - }), - new HtmlWebpackInlineSourcePlugin(), - ], - }, - chainWebpack: (webpackConfig) => { - const fontsRule = webpackConfig.module.rule("fonts"); - // clear all existing loaders. - // if you don't do this, the loader below will be appended to - // existing loaders of the rule. - fontsRule.uses.clear(); - webpackConfig.module - .rule("fonts") - .test(/\.(ttf|otf|eot|woff|woff2)$/) - .use("base64-inline-loader") - .loader("base64-inline-loader") - .end(); - }, - //=====================================eslint配置====================================// - lintOnSave: "error", //未通过eslint 禁止代码提交 - //=====================================打包上线配置====================================// - publicPath: config.build.publicPath || "/", - // outputDir: "dist", //输出文件类型 - }; +if (buildSingleWebPage) { + vueConfig = singleBuildConfig; +} else if (buildWebPage) { + delete defaultBuildConfig.pages; + vueConfig = defaultBuildConfig; } else { - vueConfig = { - pages: { - index: { - entry: "src/renderer/main.js", //添加了entry则不需要rendererProcessFile - template: "public/index.html", + defaultBuildConfig.css = { + loaderOptions: { + css: { + // 这里的选项会传递给 css-loader }, - }, - //=====================================devserver====================================// - devServer: { - port: 9999, - }, - //=====================================css相关配置====================================// - css: { - loaderOptions: { - css: { - // 这里的选项会传递给 css-loader - }, - sass: { - prependData: `@import "@/scss/index.scss";`, - }, - }, - sourceMap: false, - }, - //=====================================扩展webpack配置====================================// - configureWebpack: { - resolve: { - alias: { - "@": path.resolve(__dirname, "src/renderer"), - "~": path.resolve(__dirname, ""), - }, - }, - }, - pluginOptions: { - electronBuilder: { - extends: null, - externals: [ - "vue", - "axios", - "vue-electron", - "vue-router", - "vuex", - "vuex-electron", - "element-ui", - "js-cookie", - "mockjs", - "nprogress", - "monaco-editor", - "vuedraggable", - "ali-oss", - "json5", - "echarts", - "brace", - "urllib", - "cookie-parser", - "got", - "form-data", - "proxy-agent", - ], - mainProcessFile: "src/main/index.js", - mainProcessWatch: ["src/main/index.js"], - builderOptions: { - productName: config.renderConfig.layout.title, - appId: "com.example.yourapp", - publish: [ - { - provider: "generic", - url: "", - }, - ], - nsis: { - oneClick: false, // 是否一键安装 - allowToChangeInstallationDirectory: true, // 允许修改安装目录 - }, - mac: { - icon: "build/icons/icon.icns", - }, - win: { - icon: "build/icons/icon.ico", - }, - linux: { - icon: "build/icons", - }, - }, + sass: { + prependData: `@import "@/scss/index.scss";`, }, }, - //=====================================eslint配置====================================// - lintOnSave: "error", //未通过eslint 禁止代码提交 - //=====================================打包上线配置====================================// - publicPath: config.build.publicPath || "/", - outputDir: "dist", //输出文件类型 - productionSourceMap: true, //打包时候js是否添加sourceMap + sourceMap: false, }; + vueConfig = defaultBuildConfig } module.exports = vueConfig;