webpack4 构建vue多页工程

多页应用开发环境配置

构建开发环境的配置,需要使用到webpack-dev-server,以及webpack4中的mode配置和devServer。

而多页应用的开发则需要使用到html-webpack-plugin这个插件,来构建html

项目的目录结构

-- src
  |-- views
    |-- home
      |-- index.js
      |-- index.html
      |-- index.vue

多页面的entry和页面的构建打包

const setMPA = () => {
  const entrys = {};
    const htmlWebpackPlugins = [];
    
    const entryFiles = glob.sync(path.join(__dirname, "src"));
    
    Object.keys(entryFiles)
      .map((index) => {
        const entry = entryFiles[index];
        
          const match = entry.match(/src\/views\/(.*)\/index\.js/);        
          const pageName = match && match[1];
        
          entrys[pageName] = entry;
          htmlWebpackPlugins(
              new HtmlWebpackPlugin({
                  fileName: `${pageName}.html`,
                    template: path.join(__dirname, `src/views/${pageName}/index.html`),
                    title: `${pageName} title`
                })
            )
      })
    
    return {
      entrys,
        htmlWebpackPlugins
    }
}

webpack.dev.js 开发环境的配置

// webpack.dev.js
const path = require('path');
const glob = require('glob');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const webpack = require('webpack');

const setMPA = () => {
	const entrys = {};
    const htmlWebpackPlugins = [];
    
    const entryFiles = glob.sync(path.join(__dirname, "src"));
    
    Object.keys(entryFiles)
    	.map((index) => {
    		const entry = entryFiles[index];
        
        	const match = entry.match(/src\/views\/(.*)\/index\.js/);        
        	const pageName = match && match[1];
        
        	entrys[pageName] = entry;
        	htmlWebpackPlugins(
            	new HtmlWebpackPlugin({
                	fileName: `${pageName}.html`,
                    template: path.join(__dirname, `src/views/${pageName}/index.html`),
                    title: `${pageName} title`
                })
            )
    	})
    
    return {
    	entrys,
        htmlWebpackPlugins
    }
}

const { entrys, htmlWebpackPlugins } = setMPA();

module.exports = {
	mode: 'development',
    entry: entrys,
    output: {
    	filename: 'static/js/[name].js',
        path: path.posix.join(__dirname, 'dist')
    },
    resolve: {
    	alias: {
        	'@': path.join(__dirname, 'src')
        }
    },
    module: {
    	rules: [
            {
            	test: /\.js$/,
                use: 'babel-loader'
            },
            {
            	test: /\.vue$/,
                use: 'vue-loader'
            },
            {
            	test: /\.(png|jpe?g|gif|svg)$/,
                use: [
                    {
                    	loader: 'url-loader',
                        options: {
                        	name: 'images/[name].[ext]',
                            limit: 10240
                        }
                    }
                ]
            },
            {
            	test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: [
                    {
                    	loader: 'file-loader',
                        options: {
                        	name: 'fonts/[name].[ext]'
                        }
                    }
                ]
            },
            {
            	test: /\.css$/,
                use: [
                	'vue-style-loader',
                    'css-loader'
                ]
            },
            {
            	test: /\.less$/,
                use: [
                	'vue-style-loader',
                    'css-loader',
                    'less-loader'
                ]
            },
            {
            	test: /\.styl(us)?$/,
                use: [
                	'vue-style-loader',
                    'css-loader',
                    'stylue-loader'
                ]
            }
        ]
    },
    plugins: [
    	new VueLoaderPlugin(),
        // 热加载
        new webpack.HotModuleReplacementPlugin()
    ].concat(htmlWebpackPlugins),
    devServer: {
    	contentBase: path.join(__dirname, 'dist'),
        hot: true
    }
}

babelrc的配置,应用与编译ES的语法

/* .babelrc  */
{
  "presets": [
    "@babel/perset-env"
  ]
}

浏览器的兼容版本,>1%==市场的使用份额大于1%

/* .browserslistrc  */
last 2 version
>1%
IOS 7

正式环境配置

vue的正式环境需要配置mode: production,默认开启UglifyJsPlugin(js代码压缩)。

正式环境的资源hash迭代(hash、chunkhash、contenthash)

使用loader配置资源的提取,针对样式的提取使用mini-css-extract-plugin插件去提取,配合html-webpack-plguin把提取出来的css[chunk]配置进去html中

告警信息人性化,使用插件@soda/friendly-errors-webpack-plugin,

/**
 * font and image 资源  --> hash
 * 从js提取出font和image资源
 */
module.exports = {
  module: {
    rules: [
      {
        test: /\.(png|jpe?g|gif|svg)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              name: '[name]_[hash:8].[ext]',
              limit: 10240 // 10k https://www.npmjs.com/package/url-loader#limit
            }
          }
        ]
      },
      {
        test: /\.(woff|woff2|eof|ttf|tof)$/,
        use: [
          {
            loader: 'file-loader', // https://www.npmjs.com/package/file-loader#name
            options: {
              name: '[name]_[hash:8].[ext]' // https://www.npmjs.com/package/file-loader#placeholders
            }
          }
        ]
      }
    ]
  }
}

sass 预处理器,配置时需要注意, css 样式处理,使用到contenthash

 /**
 * css --> contenthash 当css的内容没有任何改变,不会随着js[chunkhash]的改变而改变
 * 遵从js的chunkhash
 * 从js中提取出css到文件中
 */
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader'
        ]
      },
      {
        test: /\.(scss|sass)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: 'sass-loader',
            options: {
              fiber: require('fibers'),
              implemation: require('sass'),
              // indentedSyntax: true
            }
          }
        ]
      },
      {
        test: /\.less$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'less-loader'
        ]
      },
      {
        test: /\.styl(us)?$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'stylus-loader'
        ]
      }
    ]
  },
  plugins: [
    // https://www.npmjs.com/package/mini-css-extract-plugin
    // 提取出css
    new MiniCssExtractPlugin({
      filename: '[name]_[contenthash:8].css'
    })
  ]
}
const path = requrie('path');
const HtmlWebpackPlguin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const FriendlyErrorsWebpackPlugin = require('@soda/friendly-errors-webpack-plugin');
const webpack = require('webpack');

const setMPA = () => {
  const entrys = {};
    const htmlWebpackPlugins = [];
    
    const entryFiles = glob.sync(path.join(__dirname, "src"));
    
    Object.keys(entryFiles)
      .map((index) => {
        const entry = entryFiles[index];
        
          const match = entry.match(/src\/views\/(.*)\/index\.js/);        
          const pageName = match && match[1];
        
          entrys[pageName] = entry;
          htmlWebpackPlugins(
              new HtmlWebpackPlugin({
                  fileName: `${pageName}.html`,
                    template: path.join(__dirname, `src/views/${pageName}/index.html`),
                    title: `${pageName} title`
                })
            )
      })
    
    return {
      entrys,
        htmlWebpackPlugins
    }
}

const { entrys, htmlWebpackPlguins } = setMPA(); 

module.exports = {
  mode: 'production',
    entry: entrys,
    output: {
      filename: '[name]_[chunkhash:8].js',
        path: path.join(__dirname, 'dist')
    },
    resolve: {
        // 配置文件夹的别名
      alias: {
          '@': path.join(__dirname, 'src')
        }
    },
    module: {
      rules: [
            {
              test: /\.js$/,
                use: 'babel-loader'
            },
            {
              test: /\.vue$/,
                use: [
                    'cache-loader',
                    'vue-loader'
                ]
            },
            {
                test: /\.(png|jpe?g|gif|svg)$/,
                use: [
                  {
                    loader: 'url-loader',
                    options: {
                      name: '[name]_[hash:8].[ext]',
                      limit: 10240 // 10k https://www.npmjs.com/package/url-loader#limit
                    }
                  }
                ]
            },
            {
                test: /\.(woff|woff2|eof|ttf|tof)$/,
                use: [
                    {
                        loader: 'file-loader', // https://www.npmjs.com/package/file-loader#name
                        options: {
                            name: '[name]_[hash:8].[ext]' // https://www.npmjs.com/package/file-loader#placeholders
                        }
                    }
                ]
            },
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                  'css-loader'
                ]
            },
            {
                test: /\.(scss|sass)$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    {
                        loader: 'sass-loader',
                        options: {
                            fiber: require('fibers'),
                            implemation: require('sass'),
                            // indentedSyntax: true
                        }
                    }
                ]
            },
            {
                test: /\.less$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    'less-loader'
                ]
            },
            {
                test: /\.styl(us)?$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    'stylus-loader'
                ]
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin(), // vue-loader15版本之后,需要添加添加此插件
        // 重新打包是清除打包的内容
        new CleanWebpackPlugin(),
        // 提取css
        new MiniCssExtractPlugin({
          filename: '[name]_[contenthash:8].css'
        }),
        // https://www.npmjs.com/package/@soda/friendly-errors-webpack-plugin
        new FriendlyErrorsWebpackPlugin()
    ].concat(htmlWebpackPlugins)
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue + Webpack多页/单页脚手架是一种用于构建Vue.js项目的工具。Vue.js是一个用于构建用户界面的JavaScript框架,而Webpack是一个模块打包工具。 多页/单页脚手架可以根据项目的需求选择构建方式。多页应用指的是每个页面都有自己的HTML文件,而单页应用则是只有一个HTML文件。在多页脚手架中,每个页面都有自己的入口文件和模板文件,Webpack会根据入口文件将各个页面的代码打包成独立的文件。这种方式适用于需要多个独立页面项目。 而在单页脚手架中,只有一个HTML文件和一个入口文件。所有的页面内容都是通过Vue的路由机制来进行渲染和切换。这种方式适用于更复杂的单页应用,其中页面之间的切换是通过异步加载和前端路由来实现。 使用Vue + Webpack多页/单页脚手架的优点包括: 1. 灵活性:可以根据项目需求选择多页或单页应用方式。 2. 开发效率:脚手架提供了一套默认的配置,可以使开发者更快速地开始项目开发。 3. 模块化:使用Webpack打包模块,使得代码更易于管理和维护。 4. 热更新:脚手架支持热更新,可以在开发过程中实时看到修改的效果。 5. 生态系统:Vue.jsWebpack都有庞大的社区支持,可以方便地找到资料和解决问题。 总结起来,Vue + Webpack多页/单页脚手架是一种方便的工具,可以根据项目需求选择适合的构建方式,并提供了一系列的开发工具和优化配置,使开发更高效、更方便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值