webpack 简单配置

Posted MvloveYouForever

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webpack 简单配置相关的知识,希望对你有一定的参考价值。

const ExtractTextPlugin = require(‘extract-text-webpack-plugin‘)     //打包的css拆分,将一部分抽离出来
const webpack = require(‘webpack‘); //引入的webpack,使用lodash
const htmlWebpackPlugin = require(‘html-webpack-plugin‘)  //将html打包
const path = require(‘path‘);  //引入node的path模块
const CopyWebpackPlugin = require(‘copy-webpack-plugin‘)

module.exports = function() {
    return {
        entry: ‘./src/index.js‘,
        output: {
            path: path.resolve(__dirname, ‘../dist‘), //定位,输出文件的目标路径
            filename: ‘[name].js‘,
            // publicPath: publicPath,
            // sourceMapFilename: ‘[name].map‘
        },
        optimization: {
            splitChunks: {
                chunks: ‘all‘
            }
        },
        resolve: {
            // 解决,react-router-dom 等依赖react的包报错缺少 react的问题
            alias: {
                ‘react‘: ‘nervjs‘,
                ‘react-dom‘: ‘nervjs‘,
                // 除非你想使用 createClass,否则这一条配置是没有必要的
                ‘create-react-class‘: "nerv-create-class"
            },
            modules: [ // 模块的查找目录
                path.resolve(__dirname, ‘../node_modules‘)
            ],
            extensions: [".js", ".json", ".jsx", ".css"], // 用到的文件的扩展
        },
        module: {
            rules: [
                {
                    test: /.js$/,
                    // include: [
                    //     path.resolve(__dirname, ‘src‘)
                    // ],
                    use: ‘babel-loader‘
                },
                // css 引入外来或者公共样式,与模块式css混淆会导致 外来样式不认识
                // 此时 公共样式统一放在 assets文件夹下
                {
                    test: /.css$/,
                    include: [
                        path.resolve(__dirname, ‘../src/assets‘)
                    ],
                    use: ExtractTextPlugin.extract({
                        fallback: ‘style-loader‘,
                        use: [
                            ‘css-loader‘
                        ]
                    })
                },

                // css 模块儿化配置
                {
                    test: /.css$/,
                    exclude: [
                        path.resolve(__dirname, ‘../src/assets‘),
                    ],
                    // use: ‘css-loader‘
                    use: ExtractTextPlugin.extract({
                        fallback: ‘style-loader‘,
                        use: [
                            { loader: ‘css-loader‘,
                                options: {
                                    modules: true,
                                    sourceMap: true,
                                    importLoaders: 1,
                                    localIdentName: ‘[name]__[local]___[hash:base64:5]‘
                                }
                            }
                        ]
                    })
                },
                {
                    test: /.(jpg|png|gif)$/,
                    use: [
                        {
                            loader: ‘file-loader‘ //根据文件地址加载文件
                        }
                    ]
                }, {
                    test: /.(woff|woff2|eot|ttf|svg)$/,
                    use: [
                        {
                            loader: ‘url-loader?limit=100000‘ //
                        }
                    ]
                }
            ],
        },
        plugins: [
            // 提取css
            new ExtractTextPlugin(‘style.css‘),

            // 直接复制过去的样式文件
            new CopyWebpackPlugin([  //src下其他的文件直接复制到dist目录下
                { from:‘src/assets‘,to: ‘../dist/assets‘ }
            ]),
            // 此处的入口地址一定要写正确哦
            new HtmlWebpackPlugin({
                filename: ‘index.html‘,
                template: ‘./index.html‘
            })
        ],
    };
}

package.json

{
  "name": "idhp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack-dev-server --env.NODE_ENV=dev --config webpack.config.js --mode development",
    "build": "webpack --env.NODE_ENV=prod --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.0.0-beta.53",
    "@babel/plugin-transform-react-jsx": "^7.0.0-beta.53",
    "@babel/preset-env": "^7.0.0-beta.53",
    "babel-loader": "^8.0.0-beta.0",
    "copy-webpack-plugin": "^4.5.2",
    "css-loader": "^1.0.0",
    "extract-text-webpack-plugin": "^4.0.0-beta.0",
    "file-loader": "^1.1.11",
    "html-webpack-plugin": "^3.2.0",
    "postcss-loader": "^2.1.6",
    "style-loader": "^0.21.0",
    "webpack": "^4.16.0",
    "webpack-cli": "^3.0.8",
    "webpack-dev-server": "^3.1.4",
    "webpack-merge": "^4.1.3"
  },
  "dependencies": {
    "nervjs": "^1.3.1",
    "react-router-dom": "^4.3.1",
    "swiper": "^4.3.3"
  }
}

 

以上是关于webpack 简单配置的主要内容,如果未能解决你的问题,请参考以下文章

webpack4常用片段

WebPack的配置(起步)

webpack的简单配置

webpack 简单配置

vue webpack 起步配置loader

vue webpack 起步配置loader