webpack--webpack模块化配置(十五)

coulder 2019-06-28

目录结构

webpack--webpack模块化配置(十五)

文件内容

/src/jie.js

const jie = function () {
    alert("jie");
}
module.exports = jie;

webpack_confgi/entry_webpack.js

import css from './css/index.css';
import less from './css/index.less';
import sass from './css/index.scss';
import jie from './jie.js';
{
    let name = "jie";
    document.getElementById('title').innerHTML = name;
}
jie()

webpack.config.js

const entry = require('./webpack_config/entry_webpack');
...
entry:entry.path,

打包,运行

npm run dev
npm run server

webpack--webpack模块化配置(十五)

全部代码webpack.confgi.js

const path = require('path');
const glob = require('glob');
const uglify = require('uglifyjs-webpack-plugin');
const htmlPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const PurifyCSSPlugin = require('purifycss-webpack');
const entry = require('./webpack_config/entry_webpack');
console.log(encodeURIComponent(process.env.type));
if (process.env.type == 'build') {
    var website = {
        publicPath: "http://pengrongjie.top:1717/"
    }    
} else {
    var website = {
        publicPath: "http://192.168.1.9:1717/"
    } 
}

module.exports = {
    // devtool: 'source-map',
    // 入口 
    // entry: {
    //     entry: './src/entry.js',
    // },
    entry:entry.path,
    // 出口
    output: {
        //绝对路径
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js',
        publicPath: website.publicPath
    },
    // 模块
    module: {
        //规则
        rules: [
            // {
            //     test: /\.css$/,
            //     use: [
            //         {
            //             loader:'style-loader'
            //         }
            //     ]
            // },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    // use: "css-loader"
                    use: [
                        { loader: 'css-loader', options: { importLoaders: 1 } },
                        'postcss-loader'
                    ]
                })
            },
            {
                test: /\.(png|jpg|gif)/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 5000,
                        outputPath: 'images/',
                    }
                }]
            }, {
                test: /\.(htm|html)$/i,
                use: ['html-withimg-loader']
            },
            // {
            //     test: /\.less$/,
            //     use: [{
            //         loader: 'style-loader'
            //     }, {
            //         loader: 'css-loader'
            //     }, {
            //         loader: 'less-loader'
            //     }]
            // }
            {
                test: /\.less$/,
                use: ExtractTextPlugin.extract({
                    use: [{
                        loader: 'css-loader',
                        options: { importLoaders: 1 }
                    }, {
                        loader: 'less-loader'
                    },'postcss-loader'],
                    fallback: 'style-loader'
                })
            },
            // {
            //     test: /\.scss$/,
            //     use: [{
            //         loader:'style-loader'
            //     },{
            //         loader:'css-loader'
            //     },{
            //         loader:'sass-loader'
            //     }]
            // },
            {
                test: /\.scss$/,
                use: ExtractTextPlugin.extract({
                    use: [{
                        loader: 'css-loader',
                        options: { importLoaders: 1 }
                    }, {
                        loader: 'sass-loader'
                        },
                        'postcss-loader'],
                    fallback: 'style-loader'
                })
            },
            // {
            //     test:/\.(js|jsx)$/,
            //     use:{
            //         loader:'babel-loader',
            //         options:{
            //             presets:[
            //                 'es2015',
            //                 'react'
            //             ]
            //         }
            //     },
            //     //过滤掉,不编译node_modules中的文件,
            //     exclude:/node_modules/
            // },
            {
                test:/\.(js|jsx)$/,
                use:{
                    loader:'babel-loader',
                },
                //过滤掉,不编译node_modules中的文件,
                exclude:/node_modules/
            }
            
        ]
    },
    //插件
    plugins: [
        // new uglify()
        new htmlPlugin({
            minify: {
                removeAttributeQuotes: true
            },
            hash: true,
            template: './src/index.html'
        }),
        new ExtractTextPlugin("css/index.css"),
        new PurifyCSSPlugin({
            paths:glob.sync(path.join(__dirname,'src/*.html')),
        })
    ],
    //开发服务
    devServer: {
        contentBase: path.resolve(__dirname, 'dist'),
        host: '192.168.1.9',
        compress: true, //服务端是否启用压缩
        port: 1717
    }
}

相关推荐