美文网首页webpack学习
webpack配置多个入口文件和多个输出

webpack配置多个入口文件和多个输出

作者: ismyshellyiqi | 来源:发表于2018-03-12 16:05 被阅读0次
  1. 多个入口,多个出口的添加
//config.js
const glob = require('glob')
const HtmlWebpackPlugin = require('html-webpack-plugin')

const getEntrys = function(entryPattern){
  let entrys = []
  let entry = {}
  glob.sync(entryPattern).forEach((path) => {
    let length = path.split('/').length - 1
    path = path.split('/')[length].split('.')[0]
    entry[path] = `./src/pages/${path}/${path}.js`
  })
  return entry
}

const getOutHtmls = function(entryPattern){
  let outHtmls = []
  glob.sync(entryPattern).forEach((path) => {
    let length = path.split('/').length - 1
    let filename = path.split('/')[length]
    let template = path
    pageName = path.split('/')[length].split('.')[0]
    outHtmls.push(new HtmlWebpackPlugin({
      filename,
      template,
      inject:true,
      chunks: [pageName, 'common'],
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
      },
      // chunksSortMode: 'dependency'
    }))
  })
  return outHtmls
}

module.exports = {
  getEntrys,
  getOutHtmls
}
  1. webpack基础的配置
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const ManifestWebpackPlugin = require('webpack-manifest-plugin')
const ExtractTextPlugin = require("extract-text-webpack-plugin")
const webpack = require('webpack')
const multipe = require('./config/config.js')
const getEntrys = multipe.getEntrys
const getOutHtmls = multipe.getOutHtmls
//根据具体目录结构来确定路径
const entryPattern = './src/pages/**.html';

module.exports = {
  entry:getEntrys(entryPattern),
  output: {
    filename: '[name]/js/[name].[hash].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  resolve: {
    extensions: ['.js', '.json'],
    alias: {
      '@': path.resolve(__dirname, './src'),
      'config':path.resolve(__dirname, './config'),
    }
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use:['babel-loader'],
        exclude: "/node_modules"
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: ["css-loader?importLoaders=1&&minimize","postcss-loader"]
        }),
        exclude:"/node_modules"
      },
      // LESS
      {
        test: /\.less$/,
        use: ExtractTextPlugin.extract({ 
          fallback: 'style-loader', 
          use: ['css-loader?importLoaders=1&&minimize', 'postcss-loader', 'less-loader'] })
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        loader: 'file-loader',
        options:{
          limit:10000,
          name:'[name]/img/[name].[hash].[ext]'
        },
        exclude: "/node_modules"
      }
    ]
  },
  plugins: [
    new ManifestWebpackPlugin(),
    new CleanWebpackPlugin(['dist']),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common' // 指定公共 bundle 的名称。
    }),
    new ExtractTextPlugin({
      filename:"[name]/css/[name].[contenthash].css",
      allChunks:true
    }),
    ...getOutHtmls(entryPattern)
  ]
}

多个入口(html文件),多个出口的配置demo

相关文章

  • webpack配置多个入口文件和多个输出

    多个入口,多个出口的添加 webpack基础的配置 多个入口(html文件),多个出口的配置demo

  • webpack 配置多文件

    1 基础 webpack 入口 配置 2 配置多个HtmlWebpackPlugin 生成多个html文件 Htm...

  • webpack 多个入口输出多个文件解决方案 | webpack

    1、webpack 多个单页面入口,需要重复写多个entry; 2、webpack entry不支持glob,不可...

  • webpack

    webpack基本概念 1、entry:入口 webpack可以同时打包多个文件,只要规定了各个文件的入口文...

  • 8 多页面应用打包

    多页面应用打包 在webpack.config.js中修改入口和出口配置 修改入口为对象,支持多个js入口,同时修...

  • Webpack4-Output

    配置 output 选项可以控制 webpack 如何向硬盘写入编译文件。注意,即使可以存在多个入口起点,但只指定...

  • webpack 多页面打包通用方案

    20200810 ? 今天发布一个webpack多页面打包的通用方案 原理: 配置多个入口、多个 (出口依赖的)模...

  • Webpack

    基本安装 初始化项目 安装webpack 新建文件webpack.config.js 可以有多个入口(entry)...

  • webpack配置项

    webpack配置项主要包括以下几个: entry:入口,定义要打包的文件 output:出口,定义打包输出的文件...

  • webpack教程(一)

    目录 (一) 其他 准备工作 安装webpack(二) webpack配置 单入口文件配置 多入口配置 准备工作 ...

网友评论

    本文标题:webpack配置多个入口文件和多个输出

    本文链接:https://www.haomeiwen.com/subject/ajbpfftx.html