{"id":27306099,"url":"https://github.com/lengxien/vue-typescript-multi-pages","last_synced_at":"2025-04-12T03:40:56.976Z","repository":{"id":44039113,"uuid":"223917927","full_name":"LenGxien/vue-typescript-multi-pages","owner":"LenGxien","description":"typescript + vue多页面应用配置和构建优化","archived":false,"fork":false,"pushed_at":"2022-02-12T19:04:23.000Z","size":915,"stargazers_count":6,"open_issues_count":6,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T14:17:41.889Z","etag":null,"topics":["axios","element-ui","multiple","typescript","vue","vue-router","vuex","webpack"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LenGxien.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-25T10:07:41.000Z","updated_at":"2022-11-18T07:12:57.000Z","dependencies_parsed_at":"2022-08-27T19:11:06.137Z","dependency_job_id":null,"html_url":"https://github.com/LenGxien/vue-typescript-multi-pages","commit_stats":null,"previous_names":["lengxien/vue-typescript-multi-pages"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LenGxien%2Fvue-typescript-multi-pages","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LenGxien%2Fvue-typescript-multi-pages/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LenGxien%2Fvue-typescript-multi-pages/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LenGxien%2Fvue-typescript-multi-pages/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LenGxien","download_url":"https://codeload.github.com/LenGxien/vue-typescript-multi-pages/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514221,"owners_count":21116899,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["axios","element-ui","multiple","typescript","vue","vue-router","vuex","webpack"],"created_at":"2025-04-12T03:40:56.206Z","updated_at":"2025-04-12T03:40:56.970Z","avatar_url":"https://github.com/LenGxien.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vue-typescript-multi-pages\ntypescript + vue 多页面应用配置和构建优化\n\n## 项目目录结构：\n```shell\n├── public                     // 静态打包文件\n├── scripts                    // 自动创建脚本\n├── src                        // 源代码\n│   ├── assets                 // 主题 字体等静态资源\n│   ├── components             // 全局公用组件\n│   ├── config                 // 配置相关\n│   ├── utils                  // 全局公用方法\n│   ├── icons                  // 全局svg组件\n│   │── pages                  // 业务模块根目录modules\n│   │\t  ├── demo               // 页面\n│           ├── app.vue        // 入口页面\n│           └── main.ts        // 入口初始化\n│   │\t\t└── index              // 页面\n├── .gitignore                 // git 忽略项\n├── package.json               // 安装依赖包\n├── vue.config.js              // 项目配置\n├── README.md                // 文档说明\n```\n\n## 功能点：\n- [x] 各环境配置\n- [ ] 自动脚本新建页面和组件\n- [x] 打包构建优化（dllPlugin）\n\n## 构建配置和优化\n### 1.各环境配置\n根目录下新建`.env.name`，例如：\n`.env.test`文件\n```js\n# just a flag\nNODE_ENV = \"test\"\n\nVUE_APP_TITLE = 'test'\n\n# base url\nBASE_URL = \"http://xxx.com\"\n```\n根据不同环境配置不同接口地址和NODE_ENV，方便后续根据打包环境配置处理\n\n### 2.构建优化\n### 1.webpack dllplugin提取公共库，提升打包速度\n1.1 全局安装`webpack`、 `webpack-cli`\n```shell\nwebpack -v\n```\n\n1.2 生成 dll，在`package.json`文件新增\n```js\n\"scripts\": {\n  ...\n  \"dll\": \"webpack -p --progress --config ./webpack.dll.conf.js\"\n}\n```\n执行生成 dll\n```shell\n yarn run dll\n```\n\n1.3 根目录新建`webpack.dll.confl.js`文件，安装`CleanWebpackPlugin`插件\n```js\nconst path = require('path')\nconst webpack = require('webpack')\nconst { CleanWebpackPlugin } = require('clean-webpack-plugin')\n\n// dll文件存放的目录\nconst dllPath = 'public/vendor'\n\nmodule.exports = {\n  entry: {\n    // 需要提取的库文件\n    vendor: [\n      'vue',\n      'vue-router',\n      'vuex',\n      'axios',\n      'element-ui'\n    ]\n  },\n  output: {\n    path: path.join(__dirname, dllPath),\n    filename: '[name].dll.js',\n    // vendor.dll.js中暴露出的全局变量名，保持与 webpack.DllPlugin 中名称一致\n    library: '[name]_[hash]'\n  },\n  plugins: [\n    // 清除之前的dll文件\n    new CleanWebpackPlugin(),\n    // 设置环境变量\n    new webpack.DefinePlugin({\n      'process.env': {\n        NODE_ENV: 'production'\n      }\n    }),\n    new webpack.DllPlugin({\n      path: path.join(__dirname, dllPath, '[name]-manifest.json'),\n      // 保持与 output.library 中名称一致\n      name: '[name]_[hash]',\n      context: process.cwd()\n    })\n  ]\n}\n```\n1.4 忽略已编译文件\n\n为减少 webpack 对公共库的编译时间，在`vue.config.js`添加\n\n```js\nconst webpack = require('webpack')\n\nmodule.exports = {\n  ...\n  configureWebpack: {\n    plugins: [\n      new webpack.DllReferencePlugin({\n        context: process.cwd(),\n        manifest: require('./public/vendor/vendor-manifest.json')\n      })\n    ]\n  }\n}\n```\n1.5 自动引入生成的 dll 文件\n\n安装 `add-asset-html-webpack-plugin` ，自动引入生成文件\n```js\nmodule.exports = {\n  ...\n  configureWebpack: {\n    plugins: [\n      ...\n      // 将 dll 注入到 生成的 html 模板中\n      new AddAssetHtmlPlugin({\n        filepath: path.resolve(__dirname, './public/vendor/*.js'),\n        // dll 引用路径\n        publicPath: './vendor',\n        // dll最终输出的目录\n        outputPath: './vendor'\n      })\n    ]\n  }\n}\n```\n\n还有 `GZip压缩` 、 `speed-measure-webpack-plugin` 具体详细看 `vue.config.js`","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flengxien%2Fvue-typescript-multi-pages","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flengxien%2Fvue-typescript-multi-pages","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flengxien%2Fvue-typescript-multi-pages/lists"}