Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

@vue/cli 4.4.5怎么把项目打包成单一html文件,不要css,js等文件?

需求是将vue项目打包成可以直接单独打开的html文件,类似直接保存的网页。image.png
把css、js文件嵌入到html中,可以直接通过分享html文件打开。求解

`// vue.config.js
const path = require('path');
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
const resolve = (dir) => path.join(__dirname, dir);
var HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
module.exports = {
publicPath: process.env.NODE_ENV === 'production' ? '/site/vue-demo/' : '/', // 公共路径
indexPath: 'index.html', // 相对于打包路径index.html的路径
outputDir: process.env.outputDir || 'dist', // 'dist', 生产环境构建文件的目录
assetsDir: 'static', // 相对于outputDir的静态资源(js、css、img、fonts)目录
lintOnSave: false, // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码
runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本
productionSourceMap: !IS_PROD, // 生产环境的 source map
parallel: require("os").cpus().length > 1, // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用,仅作用于生产构建。
pwa: {}, // 向 PWA 插件传递选项。
chainWebpack: config => {

config.resolve.symlinks(true); // 修复热更新失效
// 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中
config.plugin("html").tap(args => {
  // 修复 Lazy loading routes Error
  args[0].chunksSortMode = "none";
  return args;
});
config.resolve.alias // 添加别名
  .set('@', resolve('src'))
  .set('@assets', resolve('src/assets'))
  .set('@components', resolve('src/components'))
  .set('@views', resolve('src/views'))
  .set('@store', resolve('src/store'));

},
css: {

extract: IS_PROD,
requireModuleExtension: false,// 去掉文件名中的 .module
loaderOptions: {
  // 给 less-loader 传递 Less.js 相关选项
  less: {
    // `globalVars` 定义全局对象,可加入全局变量
    globalVars: {
      primary: '#333'
    }
  }
}

},
devServer: {

overlay: { // 让浏览器 overlay 同时显示警告和错误
  warnings: true,
  errors: true
},
host: "localhost",
port: 8080, // 端口号
https: false, // https:{type:Boolean}
open: false, //配置自动启动浏览器
hotOnly: true, // 热更新
proxy: {
}

},
plugins: [

new HtmlWebpackPlugin({
  inlineSource: '.(js|css)$' // embed all javascript and css inline
}),
new HtmlWebpackInlineSourcePlugin()

]
}
`


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

已解决,路由懒加载改为import引入就可以了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...