2016-12-29 64 views
2

我試圖創建一個serviceworker塊(sw.js)與webpack2使用CommonsChunkPlugin,但當試圖註冊/sw.js作爲服務人員時,我得到錯誤Uncaught ReferenceError: webpackJsonp is not definedServiceManager與CommonsChunkPlugin塊不起作用,webpackJsonp沒有定義

顯然webpackJsonp是異步加載的塊,並搞亂我的serviceworker文件。無論如何刪除serviceworker塊的異步加載?

我的WebPack配置:

{ 
    entry: { 
    main: [ 
     'react-hot-loader/patch', 
     `webpack-dev-server/client?http://${host}:${port}`, 
     'webpack/hot/only-dev-server', 
     './index.jsx', 
    ], 
    sw: './sw.js', 
    vendor: [...], 
    }, 
    output: { 
    filename: '[name].js', 
    path: resolve(__dirname, 'dist'), 
    publicPath: '/', 
    }, 
    resolve: { extensions: ['.js', '.jsx'] }, 
    performance: { hints: false }, 
    context: resolve(__dirname, 'src'), 
    devtool: 'inline-source-map', 

    devServer: { 
    hot: true, 
    host, 
    port, 
    contentBase: resolve(__dirname, 'dist'), 
    publicPath: '/', 
    }, 

    module: { 
    rules: [ 
     { 
     test: /\.jsx?$/, 
     use: 'babel-loader', 
     exclude: /node_modules/, 
     }, 
     { 
     test: /\.css$/, 
     loader: ExtractTextPlugin.extract('css-loader'), 
     }, 
    ], 
    }, 

    plugins: [ 
    new ExtractTextPlugin('main.css'), 
    new webpack.HotModuleReplacementPlugin(), 
    new webpack.NamedModulesPlugin(), 
    new webpack.optimize.CommonsChunkPlugin({ 
     names: ['vendor', 'manifest'], 
    }), 
    ], 
}; 

回答

0

如果只提取您的WebPack運行時代碼(其中有webpackJsonp定義)到您的清單束和所有其他腳本之前加載它,它應該工作。

所以,你想改寫爲

plugins: [ 
    ... 
    new webpack.optimize.CommonsChunkPlugin({ 
     name: 'vendor' 
    }), 
    new webpack.optimize.CommonsChunkPlugin({ 
     name: 'manifest', 
     chunks: 'vendor', 
     minChunks: Infinity 
    }), 
    ],