2017-09-29 30 views
2

我有一個webpack安裝程序幾乎可以正常工作,但是當我導入一些在ES6中編寫的第三方庫時,我看到UglifyJS發出的錯誤。下面是node-postgres一個例子:如何將ES6轉儲到第三方庫的ES5?

module.exports = { 
    prepareValue: function prepareValueWrapper (value) { 
    // this ensures that extra arguments do not get passed into prepareValue 
    // by accident, eg: from calling values.map(utils.prepareValue) 
    return prepareValue(value) 
    }, 
    normalizeQueryConfig, 
    postgresMd5PasswordHash, 
    md5 
} 

這裏是它應該如何看在ES5:

module.exports = { 
    prepareValue: function prepareValueWrapper (value) { 
    // this ensures that extra arguments do not get passed into prepareValue 
    // by accident, eg: from calling values.map(utils.prepareValue) 
    return prepareValue(value) 
    }, 
    normalizeQueryConfig: normalizeQueryConfig, 
    postgresMd5PasswordHash: postgresMd5PasswordHash, 
    md5: md5 
} 

當原始代碼被UglifyJS處理,我看到這個錯誤:

ERROR in proxy.js from UglifyJs

Unexpected token: punc (,) [proxy.js:382,22]

指向上面的代碼。

當我正在編譯一個TypeScript項目時,我懷疑我需要通過Webpack流中的一些轉譯程序處理第三方庫代碼,以便在捆綁之前將它們轉換爲ES5。

這裏是我的webpack.config.js

const path = require('path'); 

const root = (dir) => { 
    return path.resolve(__dirname, dir); 
}; 

module.exports = { 
    entry: './src/main.ts', 
    module: { 
     rules: [ 
      { 
       test: /\.ts$/, 
       use: 'ts-loader', 
       exclude: /node_modules/ 
      } 
     ] 
    }, 
    resolve: { 
     extensions: [".ts", ".js"], 
     modules: [root('node_modules'), root('src')] 
    }, 
    output: { 
     filename: 'proxy.js', 
     path: path.resolve(__dirname, 'build') 
    }, 
    target: 'node' 
}; 

我該怎麼辦呢?

回答