javascript - webpack配置,页面没有自动刷新,求帮忙看看
伊谢尔伦
伊谢尔伦 2017-04-11 12:09:16
[JavaScript讨论组]

修改html看到浏览器提示,如下图了,但页面没有刷新。。。修改CSS,JS之类的练这个命令都没打出来。

目录结构:

webpack.config.js

var webpack = require('webpack');
var glob = require('glob');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var autoprefixer = require('autoprefixer');
var args = require('yargs').argv;

// parameters
var isProd = args.prod;
var isMock = args.mock;

var plugins = [
    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery'
    }),
    new webpack.DefinePlugin({
        __PROD__: isProd,
        __MOCK__: isMock
    }),
    new webpack.optimize.CommonsChunkPlugin('common', isProd ? 'common/common.[hash].js' : 'common/common.js'),
    new ExtractTextPlugin(isProd ? '[name]/[name].[contenthash].css' : '[name]/[name].css'),
    // new webpack.HotModuleReplacementPlugin()
];


var base = './';

var entryJs={}

entryJs['common'] = [
    // 3rd dependencies
    'normalize.css/normalize.css',
    'bootstrap/dist/js/bootstrap.min',
    'bootstrap/dist/css/bootstrap.min.css',
    'font-awesome/css/font-awesome.min.css'
];

if (isProd) {
    plugins.push(
        new webpack.NoErrorsPlugin(),
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            },
            mangle: false
        }),
        new webpack.optimize.OccurenceOrderPlugin()
    );
}

module.exports = {
    entry: entryJs,
    output: {
        path: base + 'build',
        filename: isProd ? '[name]/[name].[hash].js' : '[name]/[name].js',
        chunkFilename: isProd ? '[name]/[name].[hash].chunk.js' : '[name]/[name].chunk.js'
    },
    resolve: {
        root: [],
        //设置require或import的时候可以不需要带后缀
        extensions: ['', '.js', '.scss', '.css']
    },
    module: {
        preLoaders: [{
            test: /\.js$/,
            loader: 'eslint',
            exclude: /node_modules/
        }],
        loaders: [{
            test: /\.html$/,
            loader: 'html'
        }, {
            test: /\.scss$/,
            loader: ExtractTextPlugin.extract('style', 'css?sourceMap!postcss!sass?sourceMap', {
                publicPath: '../'
            })
        }, {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract('style', 'css?sourceMap', {
                publicPath: '../'
            })
        }, {
            test: /\.(woff|woff2|ttf|eot|svg)(\?]?.*)?$/,
            loader: 'file?name=assets/fonts/[name].[ext]?[hash]'
        }, {
            test: /\.(png|jpg)$/,
            loader: 'url?limit=8192&name=assets/images/[name].[hash].[ext]'
        }]
    },
    plugins: plugins,
    debug: !isProd,
    devtool: isProd ? false : 'inline-cheap-module-source-map',
    devServer: {
        contentBase: base + 'build',
        historyApiFallback: true,
        inline: true,
        stats: {
            modules: false,
            cached: false,
            colors: true,
            chunk: false
        },
        host: 'localhost',
        port: 8080
    },
    postcss: function () {
        return [autoprefixer];
    }
};



//多页面处理
var getFiles = function(filepath) {
    var files = glob.sync(filepath);
    var entries = {};
    files.forEach(function(item){
        var pathname = path.basename(item, path.extname(item))
        entries[pathname] = item;
    });
    console.log(entries)
    return entries;
}
var pages = getFiles('./src/pages/*/*.html');
// generate html and inject module
Object.keys(pages).forEach(function(pageName){
    plugins.push(
        new HtmlWebpackPlugin({
            template: './src/pages/'+ pageName+ '/' + pageName + '.html',
            filename: pageName +'.html',
            chunks: [ 'common', pageName],
            hash : true
        })
    );
});


var entryJs = getFiles('./src/pages/*/*.js');
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(1)
ringa_lee

搞错了,将入口文件的收集放置前面即可。

var webpack = require('webpack');
var glob = require('glob');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var autoprefixer = require('autoprefixer');
var args = require('yargs').argv;

// parameters
var isProd = args.prod;
var isMock = args.mock;

var plugins = [
    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery'
    }),
    new webpack.DefinePlugin({
        __PROD__: isProd,
        __MOCK__: isMock
    }),
    new webpack.optimize.CommonsChunkPlugin('common', isProd ? 'common/common.[hash].js' : 'common/common.js'),
    new ExtractTextPlugin(isProd ? '[name]/[name].[contenthash].css' : '[name]/[name].css'),
    // new webpack.HotModuleReplacementPlugin()
];


var base = './';


//多页面处理
var getFiles = function(filepath) {
    var files = glob.sync(filepath);
    var entries = {};
    files.forEach(function(item){
        var pathname = path.basename(item, path.extname(item))
        entries[pathname] = item;
    });
    console.log(entries)
    return entries;
}
var pages = getFiles('./src/pages/*/*.html');
// generate html and inject module
Object.keys(pages).forEach(function(pageName){
    plugins.push(
        new HtmlWebpackPlugin({
            template: './src/pages/'+ pageName+ '/' + pageName + '.html',
            filename: pageName +'.html',
            chunks: [ 'common', pageName],
            hash : true
        })
    );
});


var entryJs = getFiles('./src/pages/*/*.js');

entryJs['common'] = [
    // 3rd dependencies
    'normalize.css/normalize.css',
    'bootstrap/dist/js/bootstrap.min',
    'bootstrap/dist/css/bootstrap.min.css',
    'font-awesome/css/font-awesome.min.css'
];

if (isProd) {
    plugins.push(
        new webpack.NoErrorsPlugin(),
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            },
            mangle: false
        }),
        new webpack.optimize.OccurenceOrderPlugin()
    );
}


module.exports = {
    entry: entryJs,
    output: {
        path: base + 'build',
        filename: isProd ? '[name]/[name].[hash].js' : '[name]/[name].js',
        chunkFilename: isProd ? '[name]/[name].[hash].chunk.js' : '[name]/[name].chunk.js'
    },
    resolve: {
        root: [],
        //设置require或import的时候可以不需要带后缀
        extensions: ['', '.js', '.scss', '.css']
    },
    module: {
        preLoaders: [{
            test: /\.js$/,
            loader: 'eslint',
            exclude: /node_modules/
        }],
        loaders: [{
            test: /\.html$/,
            loader: 'html'
        }, {
            test: /\.scss$/,
            loader: ExtractTextPlugin.extract('style', 'css?sourceMap!postcss!sass?sourceMap', {
                publicPath: '../'
            })
        }, {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract('style', 'css?sourceMap', {
                publicPath: '../'
            })
        }, {
            test: /\.(woff|woff2|ttf|eot|svg)(\?]?.*)?$/,
            loader: 'file?name=assets/fonts/[name].[ext]?[hash]'
        }, {
            test: /\.(png|jpg)$/,
            loader: 'url?limit=8192&name=assets/images/[name].[hash].[ext]'
        }]
    },
    plugins: plugins,
    debug: !isProd,
    devtool: isProd ? false : 'inline-cheap-module-source-map',
    devServer: {
        contentBase: base + 'build',
        historyApiFallback: true,
        inline: true,
        stats: {
            modules: false,
            cached: false,
            colors: true,
            chunk: false
        },
        host: 'localhost',
        port: 8080
    },
    postcss: function () {
        return [autoprefixer];
    }
};


热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号