2016-01-18 125 views
5

我嘗試建立包含socket.io的WebPack問題試圖捆綁socket.io

的package.json

{ 
    "name": "socketio-webpack", 
    "version": "1.0.0", 
    "description": "", 
    "license": "ISC", 
    "dependencies": { 
    "socket.io": "1.4.4" 
    }, 
    "devDependencies": { 
    "babel-core": "6.4.0", 
    "babel-loader": "6.2.1", 
    "babel-preset-es2015": "6.3.13", 
    "socket.io": "1.4.4", 
    "webpack": "1.12.11" 
    } 
} 

webpack.config.js

一個的WebPack包
var path = require('path'); 

module.exports = { 
    entry: "./src/entry.js", 

    output: { 
     path: __dirname, 
     filename: "bundle.js" 
    }, 

    module: { 
     loaders: [ 
      { 
       test: /\.js?/, 
       include: path.resolve(__dirname, 'src'), 
       loaders: [ 
        'babel?presets[]=es2015', 
       ] 
      } 
     ] 
    } 
}; 

src/entry.js

import Server from 'socket.io'; 

運行的WebPack給出了以下警告和錯誤:

Hash: 0bfb3971f31b27c6c1fb 
Version: webpack 1.12.11 
Time: 2106ms 
    Asset  Size Chunks    Chunk Names 
bundle.js 1.21 MB  0 [emitted] main 
    + 170 hidden modules 

WARNING in ./~/socket.io-client/socket.io.js 
Critical dependencies: 
1:475-482 This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results. 
@ ./~/socket.io-client/socket.io.js 1:475-482 

WARNING in ./~/ws/lib/Validation.js 
Module not found: Error: Cannot resolve module 'utf-8-validate' in D:\temp\socketio-webpack\node_modules\ws\lib 
@ ./~/ws/lib/Validation.js 10:19-44 

WARNING in ./~/ws/lib/BufferUtil.js 
Module not found: Error: Cannot resolve module 'bufferutil' in D:\temp\socketio-webpack\node_modules\ws\lib 
@ ./~/ws/lib/BufferUtil.js 10:19-40 

ERROR in ./~/socket.io/lib/index.js 
Module not found: Error: Cannot resolve module 'fs' in D:\temp\socketio-webpack\node_modules\socket.io\lib 
@ ./~/socket.io/lib/index.js 7:11-24 

ERROR in ./~/socket.io-client/package.json 
Module parse failed: D:\temp\socketio-webpack\node_modules\socket.io-client\package.json Line 2: Unexpected token : 
You may need an appropriate loader to handle this file type. 
| { 
| "_args": [ 
|  [ 
|  "[email protected]", 
@ ./~/socket.io/lib/index.js 11:20-55 

ERROR in ./~/engine.io/lib/server.js 
Module not found: Error: Cannot resolve module 'fs' in D:\temp\socketio-webpack\node_modules\engine.io\lib 
@ ./~/engine.io/lib/server.js 8:19-32 

ERROR in ./~/ws/lib/WebSocketServer.js 
Module not found: Error: Cannot resolve module 'tls' in D:\temp\socketio-webpack\node_modules\ws\lib 
@ ./~/ws/lib/WebSocketServer.js 15:10-24 

ERROR in ./~/options/lib/options.js 
Module not found: Error: Cannot resolve module 'fs' in D:\temp\socketio-webpack\node_modules\options\lib 
@ ./~/options/lib/options.js 6:9-22 

ERROR in ./~/mime-db/db.json 
Module parse failed: D:\temp\socketio-webpack\node_modules\mime-db\db.json Line 2: Unexpected token : 
You may need an appropriate loader to handle this file type. 
| { 
| "application/1d-interleaved-parityfec": { 
|  "source": "iana" 
| }, 
@ ./~/mime-db/index.js 11:17-37 

的WebPack似乎是從即使我已經明確地只包括src目錄node_modules目錄解析模塊。

回答

5

Webpack只是試圖解決所有的需求。當你捆綁一個服務器應用程序時,你必須實現一個小破解here

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

var nodeModules = {}; 
fs.readdirSync('node_modules') 
    .filter(function(x) { 
    return ['.bin'].indexOf(x) === -1; 
    }) 
    .forEach(function(mod) { 
    nodeModules[mod] = 'commonjs ' + mod; 
    }); 

module.exports = { 
    entry: './src/main.js', 
    target: 'node', 
    output: { 
    path: path.join(__dirname, 'build'), 
    filename: 'backend.js' 
    }, 
    externals: nodeModules 
}