اذهب إلى المحتوى
  • 0

فشل التجميع الفرعي في webpack

عبد النور محمد

السؤال

لدي مشكلة في تكوين webpack الخاص بي. بعد تنفيذ html-webpack-plugin ، حصلت على خطأ  في  index.html الذي تم إنشاؤه.

  Error: Child compilation failed:
  Conflict: Multiple assets emit to the same filename index.html:
  Error: Conflict: Multiple assets emit to the same filename index.html

هذه هي الاعدادات الخاصة بي 

var webpack = require('webpack'),
    path = require('path');


var CopyWebpackPlugin = require('copy-webpack-plugin'),
    ExtractTextWebpackPlugin = require('extract-text-webpack-plugin'),
    HtmlWebpackPlugin = require('html-webpack-plugin'),

const sourcePath = path.resolve(__dirname, './src');
const staticPath = path.resolve(__dirname, './static');

module.exports = function (env) {

    const nodeEnv = env && env.prod ? 'production' : 'development';
    const isProd = nodeEnv === 'production';

    const postcssLoader = {
        loader: 'postcss-loader',
        options: {
            plugins: function () {
                return [
                    require('autoprefixer')
                ];
            }
        }
    }

    const plugins = [
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks: Infinity,
            filename: 'vendor.bundle.js'
        }),
        new webpack.EnvironmentPlugin({
            NODE_ENV: nodeEnv,
        }),
        new HtmlWebpackPlugin({
            template: 'index.html',
            minify: {
                removeComments: true,
                collapseWhitespace: true,
                removeAttributeQuotes: true
            },
            chunksSortMode: 'dependency'
        })
    ];

    if(isProd) {
        plugins.push(
            new webpack.LoaderOptionsPlugin({
                minimize: true,
                debug: false
            }),
            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,
                },
            })
        );
    } else {
        plugins.push(
            new webpack.HotModuleReplacementPlugin()
        );
    }

    return {
        devtool: isProd? 'source-map' : 'eval',
        context: sourcePath,

        entry: {
            app: './app/entry.ts',
            vendor: './app/vendor.ts'
        },

        output: {
            path: staticPath,
            filename: '[name].bundle.js',
        },

        module: {
            rules: [
                {
                    test: /\.html$/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'file-loader',
                        query: {
                            name: '[name].[ext]'
                        },
                    },
                },
                {
                    test: /\.css$/,
                    exclude: /node_modules/,
                    use: [
                        'style-loader',
                        'css-loader',
                        'postcss-loader'
                    ]
                },
                {
                    test: /\.scss$/,
                    exclude: /node_modules/,
                    use: [
                        'style-loader',
                        'css-loader',
                        'postcss-loader',
                        'sass-loader'
                    ]
                },
                {
                    test: /\.ts$/,
                    exclude: /node_modules/,
                    use: [
                        'ts-loader'
                    ],
                },
            ],
        },

        resolve: {
            alias: {
                Public: path.resolve(__dirname,'src/public'),
                Style: path.resolve(__dirname,'src/styles')
            },
            extensions: ['.ts','.js', '.html'],
            modules: [
                path.resolve(__dirname, 'node_modules'),
                sourcePath
            ]
        },

        plugins,

        performance: isProd && {
            maxAssetSize: 100,
            maxEntrypointSize: 300,
            hints: 'warning'
        },

        stats: {
            colors: {
                green: '\u001b[32m'
            }
        },

        devServer: {
            contentBase: './src',
            historyApiFallback: true,
            port: 3000,
            compress: isProd,
            inline: !isProd,
            hot: !isProd,
            stats: {
                assets: true,
                children: false,
                chunks: false,
                hash: false,
                modules: false,
                publicPath: false,
                timings: true,
                version: false,
                warnings: true,
                color: {
                    green: '\u001b[32m'
                }
            },
        }
    };
};

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

المشكلة في الواقع هي المحمل file-loader ، لأنها ببساطة تنسخ الملف. مع مرور الوقت تحاول الإضافة html-webpack-plugin كتابة الملف index.html، والذي تمت كتابته بالفعل بواسطة المحمل file-loader، مما يؤدي إلى حدوث تعارض. هناك عدة طرق لحل هذه المشكلة. 

يمكنك استخدام مُحمل html-loader لكود HTML الخاص بك، على الرغم من أنك إذا كنت تتوقع نسخ HTML الذي تم استيراده ببساطة، فهذا ليس الخيار الصحيح. 

إذا كنت ترغب في الاستمرار في استخدام المحمل file-loader لملفات HTML الأخرى، فيمكنك استبعاد index.html حتى تعود الإضافة html-webpack-plugin إلى المُحمل الافتراضية. يتطلب require.resolve أن يعمل مثل جملة require ولكنه يمنحك المسار الكامل full path للوحدة بدلاً من محتواها.

{
    test: /\.html$/,
    exclude: [/node_modules/, require.resolve('./index.html')],	// لن يتم تحميل هذه الملفات من قبل file-loader
    use: {
        loader: 'file-loader',
        query: {
            name: '[name].[ext]'
        },
    },
},

الكود السابق سوف يحل المشكة، لكن عندما لا يوجد أي محمل مع القالب، فإن الإضافة html-webpack-plugin تستخدم أداة تحميل ejs كبديل احتياطي. أما إذا لم تكن بحاجة إلى أي محمل لملفات .html، فيمكنك إزالة القاعدة بالكامل وستعمل بشكل جيد.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 1

هناك تعارض بين عمل الإضافة html-webpack-plugin و قواعد التحميل file-loader ل HTML

{
  test: /\.html$/,
  exclude: /node_modules/,
  use: {
    loader: 'file-loader',
    query: {
      name: '[name].[ext]'
    },
  },
},
  
  
  // يتعارض مع 
  
  new HtmlWebpackPlugin({
    template: 'index.html',
    minify: {
      removeComments: true,
      collapseWhitespace: true,
      removeAttributeQuotes: true
    },
    chunksSortMode: 'dependency'
  })

جرب إضافة تغيير بسيط لصيغة اسم الملفات في file-loader كالتالي

use: {
  loader: 'file-loader',
  query: {
    name: '[name]_.[ext]'
  },
},

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...