shellscape / webpack-manifest-plugin

webpack plugin for generating asset manifests
MIT License
1.44k stars 184 forks source link

Mangled output #21

Closed jknight12882 closed 7 years ago

jknight12882 commented 8 years ago

In multi compiler mode with a shared cache, I am getting the following mangled output.

{
  "common_en-US.js": "common_en-US-cbe78523083d0159240f.js",
  "common_en-US.js.map": "common_en-US-cbe78523083d0159240f.js.map"
}  "common_fr-CA.js": "common_fr-CA-2cf11bd51dc92d8b0ee2.js",
  "common_fr-CA.js.map": "common_fr-CA-2cf11bd51dc92d8b0ee2.js.map"
}

This is inconsistent, sometimes the file is correct, sometimes it is as above

jknight12882 commented 8 years ago

Config file for reference

import webpack from 'webpack';
import path from 'path';
import ManifestPlugin from 'webpack-manifest-plugin';

import locales from './locales';

const common = (locale, cache) => ({
    // verbose: true,
    devtool: 'source-map',
    entry: {
        [`common_${locale}`]: [
            ...
        ]
    },
    output: {
        filename: '[name]-[chunkhash].js',
        library: 'common',
        path: path.resolve(__dirname, '../dist/public/js')
    },
    resolve: {
        root: path.resolve(__dirname, '../src'),
        modulesDirectories: ['node_modules'],
        extensions: ['', '.json', '.js']
    },
    plugins: [
        new ManifestPlugin({
            cache,
            fileName: 'common-manifest.json'
        }),
        new webpack.DllReferencePlugin({
            context: path.resolve(__dirname, '..'),
            manifest: require(path.resolve(__dirname, '../.build/dll/react-manifest.json'))
        }),
        new webpack.DllPlugin({
            path: path.resolve(__dirname, '../.build/dll/[name]-manifest.json'),
            name: 'common',
            context: path.resolve(__dirname, '..')
        })
    ]
});

const dev = () => {
    const cache = { };

    return locales.map(locale => common(locale, cache));
};

const prod = () => {
    const cache = { };

    return locales.map(locale => {
        const config = Object.assign(common(locale, cache), {
            devtool: 'hidden-source-map'
        });

        config.plugins.push(
            new webpack.DefinePlugin({
                'process.env.NODE_ENV': JSON.stringify('production')
            }),
            new webpack.optimize.OccurrenceOrderPlugin(),
            new webpack.optimize.DedupePlugin(),
            new webpack.optimize.UglifyJsPlugin({
                sourceMap: true,
                compress: {
                    warnings: false
                }
            })
        );

        return config;
    });
};

export default (process.env.NODE_ENV === 'production') ? prod : dev;
karlshea commented 7 years ago

I'm also occasionally having the same issue. One build will be fine, the next will have a bad closing brace in the middle of the list.

mastilver commented 7 years ago

I know it won't be easy (and I'm not sure if feasible... :/), but it would be a great help if you could send me a failing PR reproducing the issue

mastilver commented 7 years ago

I'm reopening this one as I'm reverting the earlier fix, as it was causing another issue...

mastilver commented 7 years ago

So i now understand better how this issue is happening:

It's caused by this: https://github.com/webpack/webpack/blob/ef6ab6820a74db730cfec6fd419d2c211d62c0bd/lib/Compiler.js#L343

What's happening, is that webpack is writing the same file multiple times

To prevent that, we need to use https://github.com/mafintosh/mutexify to lock: https://github.com/danethurber/webpack-manifest-plugin/blob/1d691af97b955cc8c1c8cd5b684e54a81c7d0a3d/lib/plugin.js#L161 and release it on after-emit event

I will now focus on 2.x release, but if anyone feels like it, they can send us a PR :)