UNPKG

5.16 kBJavaScriptView Raw
1const fs = require('fs')
2const webpack = require('webpack')
3const nodeExternals = require('webpack-node-externals')
4const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
5const config = require('./paths')
6const path = require('path')
7
8// This is the Webpack configuration.
9// It is focused on developer experience and fast rebuilds.
10module.exports = (options) => {
11 const babelRcPath = path.resolve('.babelrc')
12 const hasBabelRc = fs.existsSync(babelRcPath)
13 const mainBabelOptions = {
14 babelrc: true,
15 cacheDirectory: true,
16 presets: []
17 }
18
19 if (hasBabelRc) {
20 console.log('> Using .babelrc defined in your app root')
21 } else {
22 mainBabelOptions.presets.push(require.resolve('@lectro/babel-preset-lectro'))
23 }
24
25 return {
26 // Webpack can target multiple environments such as `node`,
27 // `browser`, and even `electron`. Since Lectro is focused on Node,
28 // we set the default target accordingly.
29 target: 'node',
30 // The benefit of Webpack over just using babel-cli or babel-node
31 // command is sourcemap support. Although it slows down compilation,
32 // it makes debugging dramatically easier.
33 devtool: 'source-map',
34 // Webpack allows you to define externals - modules that should not be
35 // bundled. When bundling with Webpack for the backend - you usually
36 // don't want to bundle its node_modules dependencies. This creates an externals
37 // function that ignores node_modules when bundling in Webpack.
38 // @see https://github.com/liady/webpack-node-externals
39 externals: nodeExternals({
40 whitelist: [
41 /\.(eot|woff|woff2|ttf|otf)$/,
42 /\.(svg|png|jpg|jpeg|gif|ico|webm)$/,
43 /\.(mp4|mp3|ogg|swf|webp)$/,
44 /\.(css|scss|sass|less|styl)$/
45 ]
46 }),
47 // As of Webpack 2 beta, Webpack provides performance hints.
48 // Since we are not targeting a browser, bundle size is not relevant.
49 // Additionally, the performance hints clutter up our nice error messages.
50 performance: {
51 hints: false
52 },
53 // Since we are wrapping our own webpack config, we need to properly resolve
54 // Lectro's and the given user's node_modules without conflict.
55 resolve: {
56 extensions: ['.js', '.json'],
57 modules: [config.userNodeModulesPath, path.resolve(__dirname, '../node_modules')]
58 },
59 resolveLoader: {
60 modules: [config.userNodeModulesPath, path.resolve(__dirname, '../node_modules')]
61 },
62 node: {
63 __filename: true,
64 __dirname: true
65 },
66 entry: {
67 main:`${config.serverSrcPath}/index.js`
68 },
69 // This sets the default output file path, name, and compile target
70 // module type. Since we are focused on Node.js, the libraryTarget
71 // is set to CommonJS2
72 output: {
73 path: config.serverBuildPath,
74 filename: '[name].js',
75 sourceMapFilename: '[name].map',
76 publicPath: config.publicPath,
77 libraryTarget: 'commonjs2'
78 },
79 // Define a few default Webpack loaders. Notice the use of the new
80 // Webpack 2 configuration: module.rules instead of module.loaders
81 module: {
82 rules: [
83 // This is the development configuration.
84 // It is focused on developer experience and fast rebuilds.
85 {
86 test: /\.json$/,
87 loader: 'json-loader'
88 },
89 // Process JS with Babel (transpiles ES6 code into ES5 code).
90 {
91 test: /\.(js|jsx)$/,
92 loader: 'babel-loader',
93 exclude: [
94 /node_modules/,
95 config.buildPath
96 ],
97 options: mainBabelOptions
98 }
99 ]
100 },
101 plugins: [
102 // We define some sensible Webpack flags. One for the Node environment,
103 // and one for dev / production. These become global variables. Note if
104 // you use something like eslint or standard in your editor, you will
105 // want to configure __DEV__ as a global variable accordingly.
106 new webpack.DefinePlugin({
107 'process.env.NODE_ENV': JSON.stringify(options.env),
108 '__DEV__': options.env === 'development'
109 }),
110 // In order to provide sourcemaps, we automagically insert this at the
111 // top of each file using the BannerPlugin.
112 new webpack.BannerPlugin({
113 raw: true,
114 entryOnly: false,
115 banner: `require('${
116 // Is source-map-support installed as project dependency, or linked?
117 (require.resolve('source-map-support').indexOf(process.cwd()) === 0)
118 // If it's resolvable from the project root, it's a project dependency.
119 ? 'source-map-support/register'
120 // It's not under the project, it's linked via lerna.
121 : require.resolve('source-map-support/register')
122 }')`
123 }),
124 // The FriendlyErrorsWebpackPlugin (when combined with source-maps)
125 // gives Lectro its human-readable error messages.
126 new FriendlyErrorsWebpackPlugin({
127 clearConsole: options.env === 'development'
128 }),
129 // The NoEmitOnErrorsPlugin plugin prevents Webpack
130 // from printing out compile time stats to the console.
131 new webpack.NoEmitOnErrorsPlugin()
132 ]
133 }
134}