-
-
Save haf/f671f1113d2c5dead5a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var webpack = require('webpack'); | |
var path = require('path'); | |
var ExtractTextPlugin = require("extract-text-webpack-plugin"); | |
var loadersByExtension = require("./config/loadersByExtension"); | |
var joinEntry = require("./config/joinEntry"); | |
function reactEntry(options, name) { | |
return (options.prerender ? "./config/prerender?" : "./config/app?") + name; | |
} | |
module.exports = function(options) { | |
var entry = { | |
index: './js/index.js', | |
}; | |
var loaders = { | |
"js": options.hotComponents ? ["react-hot-loader", "jsx-loader?harmony"] : "jsx-loader?harmony", | |
"json": "json-loader", | |
"txt": "raw-loader", | |
"png|jpg|jpeg|gif|svg": "url-loader?limit=10000", | |
"woff|woff2": "url-loader?limit=100000", | |
"ttf|eot": "file-loader", | |
"wav|mp3": "file-loader", | |
"html": "html-loader" | |
}; | |
var stylesheetLoaders = { | |
"css": "css-loader", | |
"less": "css-loader!less-loader", | |
"styl": "css-loader!stylus-loader", | |
"scss|sass": "css-loader!sass-loader" | |
}; | |
var root = path.join(__dirname, "js"); | |
var publicPath = options.devServer ? | |
"http://localhost:2992/_assets/" : | |
"/_assets/"; | |
var output = { | |
path: path.join(__dirname, "build", options.prerender ? "prerender" : "public"), | |
publicPath: publicPath, | |
filename: "[name].js" + (options.longTermCaching && !options.prerender ? "?[chunkhash]" : ""), | |
chunkFilename: (options.devServer ? "[id].js" : "[name].js") + (options.longTermCaching && !options.prerender ? "?[chunkhash]" : ""), | |
sourceMapFilename: "debugging/[file].map", | |
libraryTarget: options.prerender ? "commonjs2" : undefined, | |
pathinfo: options.debug | |
}; | |
var excludeFromStats = [ | |
/node_modules[\\\/]react(-router)?[\\\/]/, | |
/node_modules[\\\/]items-store[\\\/]/ | |
]; | |
var plugins = [ | |
function() { | |
if(!options.prerender) { | |
this.plugin("done", function(stats) { | |
var jsonStats = stats.toJson({ | |
chunkModules: true, | |
exclude: excludeFromStats | |
}); | |
jsonStats.publicPath = publicPath; | |
require("fs").writeFileSync(path.join(__dirname, "build", "stats.json"), JSON.stringify(jsonStats)); | |
}); | |
} | |
}, | |
new webpack.PrefetchPlugin("react"), | |
new webpack.PrefetchPlugin("react/lib/ReactComponentBrowserEnvironment") | |
]; | |
var aliasLoader = {}, | |
externals = [], | |
alias = {}; | |
if(options.prerender) { | |
aliasLoader["react-proxy$"] = "react-proxy/unavailable"; | |
externals.push( | |
/^react(\/.*)?$/, | |
/^reflux(\/.*)?$/, | |
"async" | |
); | |
plugins.push(new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 })); | |
} | |
if(options.commonsChunk) { | |
plugins.push(new webpack.optimize.CommonsChunkPlugin("commons", "commons.js" + (options.longTermCaching && !options.prerender ? "?[chunkhash]" : ""))); | |
} | |
Object.keys(stylesheetLoaders).forEach(function(ext) { | |
var loaders = stylesheetLoaders[ext]; | |
if(Array.isArray(loaders)) loaders = loaders.join("!"); | |
if(options.prerender) { | |
stylesheetLoaders[ext] = "null-loader"; | |
} else if(options.separateStylesheet) { | |
stylesheetLoaders[ext] = ExtractTextPlugin.extract("style-loader", loaders); | |
} else { | |
stylesheetLoaders[ext] = "style-loader!" + loaders; | |
} | |
}); | |
if(options.separateStylesheet && !options.prerender) { | |
plugins.push(new ExtractTextPlugin("[name].css" + (options.longTermCaching ? "?[contenthash]" : ""))); | |
} | |
if(options.minimize) { | |
plugins.push( | |
new webpack.optimize.UglifyJsPlugin(), | |
new webpack.optimize.DedupePlugin(), | |
new webpack.DefinePlugin({ | |
"process.env": { | |
NODE_ENV: JSON.stringify("production") | |
} | |
}), | |
new webpack.NoErrorsPlugin() | |
); | |
} | |
return { | |
entry: entry, | |
output: output, | |
target: options.prerender ? "node" : "web", | |
module: { | |
loaders: loadersByExtension(loaders).concat(loadersByExtension(stylesheetLoaders)) | |
}, | |
devtool: options.devtool, | |
debug: options.debug, | |
resolveLoader: { | |
root: path.join(__dirname, "node_modules"), | |
alias: aliasLoader | |
}, | |
externals: externals, | |
resolve: { | |
root: root, | |
modulesDirectories: ["node_modules"], | |
extensions: ["", ".js", ".jsx"], | |
alias: alias | |
}, | |
plugins: plugins, | |
devServer: { | |
stats: { | |
cached: false, | |
exclude: excludeFromStats | |
} | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment