【问题标题】:WebPack final build output folder is emptyWebPack 最终构建输出文件夹为空
【发布时间】:2017-10-05 09:01:29
【问题描述】:

无法解决如何强制 WebPack 3.6 构建最终 dist
只是输出文件夹是空的。使用给定的配置,应用程序在浏览器内存中构建并运行,但是 dist 文件夹是空的,现在有任何物理文件。

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const PreloadWebpackPlugin = require('preload-webpack-plugin');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
const StyleExtHtmlWebpackPlugin = require('style-ext-html-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const autoprefixer = require('autoprefixer');

const staticSourcePath = path.join(__dirname, 'static');
const sourcePath = path.join(__dirname);
const buildPath = path.join(__dirname, 'dist');

module.exports = {
    devtool: 'cheap-module-source-map',
    entry: {
        /*base: path.resolve(staticSourcePath, 'src/sass/base.scss'),*/
        app: path.resolve(sourcePath, 'index.js')
    },
    output: {
        path: path.join(__dirname, 'dist'),
        filename: '[name].[chunkhash].js',
        publicPath: '/'
    },
    resolve: {
        extensions: ['.webpack-loader.js', '.web-loader.js', '.loader.js', '.js', '.jsx'],
        modules: [
            sourcePath,
            path.resolve(__dirname, 'node_modules')
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify('production')
        }),
        new webpack.optimize.ModuleConcatenationPlugin(),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            filename: 'vendor.[chunkhash].js',
            minChunks (module) {
                return module.context && module.context.indexOf('node_modules') >= 0;
            }
        }),
      /*  new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
                screw_ie8: true,
                conditionals: true,
                unused: true,
                comparisons: true,
                sequences: true,
                dead_code: true,
                evaluate: true,
                if_return: true,
                join_vars: true
            },
            output: {
                comments: false
            }
        }),*/
        new webpack.LoaderOptionsPlugin({
            options: {
                postcss: [
                    autoprefixer({
                        browsers: [
                            'last 3 version',
                            'ie >= 10'
                        ]
                    })
                ],
                context: staticSourcePath
            }
        }),
        new webpack.HashedModuleIdsPlugin(),
        new HtmlWebpackPlugin({
            template: path.join(__dirname, 'index.html'),
            path: buildPath,
            excludeChunks: ['base'],
            filename: 'index.html',
            minify: {
                collapseWhitespace: true,
                collapseInlineTagWhitespace: true,
                removeComments: true,
                removeRedundantAttributes: true
            }
        }),
        new PreloadWebpackPlugin({
            rel: 'preload',
            as: 'script',
            include: 'all',
            fileBlacklist: [/\.(css|map)$/, /base?.+/]
        }),
        new ScriptExtHtmlWebpackPlugin({
            defaultAttribute: 'defer'
        }),
        new ExtractTextPlugin({
            filename: '[name].[contenthash].css',
            allChunks: true
        }),
       /* new StyleExtHtmlWebpackPlugin({
            minify: true
        }),*/
        new CompressionPlugin({
            asset: '[path].gz[query]',
            algorithm: 'gzip',
            test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
            threshold: 10240,
            minRatio: 0.8
        })
    ],
    module: {



        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                  loader: 'babel-loader',
                  options: {
                    presets: ['env', 'react']
                  }
                },
                include: sourcePath
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: [
                        { loader: 'css-loader', options: { minimize: true } },
                        'postcss-loader',
                        'sass-loader'
                    ]
                })
            },
            {
                test: /\.(eot?.+|svg?.+|ttf?.+|otf?.+|woff?.+|woff2?.+)$/,
                use: 'file-loader?name=assets/[name]-[hash].[ext]'
            },
            {
                test: /\.(png|gif|jpg|svg)$/,
                use: [
                    'url-loader?limit=20480&name=assets/[name]-[hash].[ext]'
                ],
                include: staticSourcePath
            }
        ]
    }
}; 

项目结构如下:

D:\project_name
├───components
│   └───villages
│       └───neighborhoods
│           └───blocks
│               └───houses
├───css
│   └───404
│       └───font-awesome
│           ├───css
│           └───fonts
├───dist
├───flags
│   ├───1x1
│   └───4x3
├───fonts
│   ├───Raleway
│   └───roboto
├───icons
├───images
│   └───slides
├───img
│   ├───404
│   │   ├───demo
│   │   └───slides
│   └───works
│       └───thumbs
├───node_modules
│   └───...
├───js
│   └───404
└──index.html
└──index.js
└──package.json
└──webpack.config.js

我通过 Windows CMD 运行它

【问题讨论】:

  • 你能告诉我你的命令行脚本是什么来执行这个配置吗?

标签: webpack webpack-dev-server html-webpack-plugin


【解决方案1】:

你标记了,webpack-dev-server,据我所知,它不会将文件放在你的 dist 目录中,它只是用于开发预览,所以它在内部处理构建文件只构建改变的部分,

为生产使用构建:webpack -p

【讨论】:

  • 啊,是的,是我的知识不足:) dist 文件夹中使用webpack -pcommand 构建的文件准备好直接上传到生产服务器了吗?不需要任何额外的调整?
  • 是的,它们“通常”已经准备好生产了——但这当然取决于您的项目。
  • 你能告诉我把这个webpack -p放在哪里吗?我有同样的问题
  • @VipulTyagi 取决于您的设置,很可能您在 package.json 中设置了一个 npm 脚本,您可以运行类似 npm run build -- -p 的内容,其中 build 是脚本的名称,或者您是 set the mode in the config file
【解决方案2】:

您可以在配置文件中添加附加参数:

devServer: {
    writeToDisk: true,
}

使用该属性,您将在 ./dist 文件夹中的所有编译文件。

【讨论】:

    猜你喜欢
    • 2020-08-03
    • 2019-02-20
    • 1970-01-01
    • 2017-05-24
    • 2020-09-27
    • 2021-10-19
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    相关资源
    最近更新 更多