2016-11-13 24 views
0

我使用的是建立一個應用程序陣營+反應路由器+終極版+打字稿+的WebPack,這裏是我的tsconfig.json:陣營+反應路由器+打字稿的WebPack產品包問題

{ 
    "compilerOptions": { 
     "outDir": "./dist/", 
     "sourceMap": true, 
     "noImplicitAny": true, 
     "module": "commonjs", 
     "target": "es6", 
     "jsx": "react" 
    }, 
    "files": [ 
     "./src/index.tsx" 
    ] 
} 

這是我的webpack.config.js樣子:

var PROD = JSON.parse(process.env.PROD_ENV || '0'); 
... 
module.exports = { 
    ... 
    plugins: PROD ? [ 
     new webpack.optimize.UglifyJsPlugin({ 
      compress: { warnings: false } 
     }) 
    ] : [], 
    ... 
    module: { 
     loaders: [ 
      // Sass loader for stylesheet. 
      { test: /\.scss$/, loaders: ["style", "css", "sass"], exclude: /node_modules/ }, 
      // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'. 
      { test: /\.tsx?$/, loaders: ["babel-loader", "ts-loader"], exclude: /node_modules/ }, 
     ], 

     preLoaders: [ 
      // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. 
      { test: /\.js$/, loader: "source-map-loader" } 
     ] 
    }, 
    externals: { 
     "react": "React", 
     "react-dom": "ReactDOM", 
     "react-router": "ReactRouter", 
     "redux": "Redux", 
     "react-redux": "ReactRedux" 
    }, 
} 

現在,如果我用的WebPack捆綁,一切都看起來不錯,但如果我跑

PROD_ENV=1 webpack 

嘗試生成變醜JS束文件,它給了我下面的錯誤編譯消息:

ERROR in ./dist/bundle.js from UglifyJs 
SyntaxError: Unexpected token: name (AppRoutes) [./src/routes.tsx:8,0] 

任何幫助,將不勝感激!謝謝。

+0

什麼是'。/ src/routes.tsx'看起來像? –

回答

1

UglifyJS使用它自己的ES5解析器。您很可能會使用Babel進行解析和轉換,使用Webpack進行解析和轉換,並仍然生成UglifyJS解析器不支持的源代碼。

通常這意味着要回到任何裝載器生成Webpack的輸入模塊源代碼,並確保一切都符合ES5。爲UglifyJS插件配置{debug: true},並在包中找到非法ES5語法。

如果你的裝載機生產開發和生產相同的輸出,那麼你就可以創建一個開發包,並通過命令行運行UglifyJS或粘貼到AST Explorer和使用提供uglify-js解析器。

另外,你也可以嘗試babili,看看它是如何工作的。