2014-12-01 55 views

回答

5

什麼樣的應用,這是?如果您使用的是package.json,則可以使用npm腳本在postinstall步驟中運行webpack。

+0

我編譯webpack編程(從我的index.js),它只適用於我的本地機器。有道理,是不是dynos的文件系統應該是隻讀的? – 2015-12-22 22:52:06

1

我已經放置在普通的依賴devDependencies解決了這個問題,我改變了後腳本到:

node_modules/.bin/webpack 
2

你可以設置你的package.json postinstall以下NODE_ENV=production webpack -p

然後設置startnode

但是您需要確保將您的webpack設置爲生產,或者將其設置在您的webpack.config.js中,否則我們bpack.config.js(生產)作爲生產配置。

設置我webpack.config.js內一切如下..

const path = require('path'); 
const webpack = require('webpack'); 
const debug = process.env.NODE_ENV !== "production"; 


module.exports = { 
    entry: [ 
    './src/index.js' 
    ], 
    output: { 
    path: path.resolve(__dirname, 'src'), 
    filename: 'bundle.js' 
    }, 
    devtool: debug ? "inline-sourcemap" : null, 
    module: { 
    loader: [{ 
     exclude: /node_modules/, 
     loader: 'babel', 
     query: { 
     presets: ['angular'] 
     } 
    }, 
    { 
     test: /\.css$/, 
     loader: "style-loader!css-loader" 
    }] 
    }, 
    devServer: { 
    historyApiFallback: true, 
    contentBase: 'src' 
    }, 
    plugins: debug ? [] : [ 
    new webpack.optimize.DedupePlugin(), 
    new webpack.optimize.OccurenceOrderPlugin(), 
    new webpack.optimize.UglifyJsPlugin({ 
    mangle: {except: ['$', 'exports', 'require', 'app']}, 
    compress: {warnings: false}, 
    sourceMap: false 
    }) 
    ] 
} 

所以基本上,一旦命令運行npm run postinstall束將在目錄中每webpack.config.js生成(輸出) 。但請記住,在運行'npm start'之前,使用命令NODE_ENV=production webpack -p包含在package.json中。看下面的例子..

{ 
    "name": "", 
    "version": "1.0.0", 
    "description": "", 
    "main": "./src/bundle.js", 
    "engines": { 
    "node": "6.4.0" 
    }, 
    "scripts": { 
    "start": "node ./src/server.js", 
    "postinstall": "NODE_ENV=production webpack -p" 
    }, 
    "author": "", 
    "license": "ISC", 
    "dependencies": ...