UNPKG

2.18 kBJavaScriptView Raw
1const path = require('path')
2const webpack = require('webpack')
3const ManifestPlugin = require('webpack-assets-manifest')
4
5const ENV = process.env.NODE_ENV
6const TARGET = process.env.GNOLL_TARGET
7const ASSETS_CACHING = process.env.GNOLL_ASSETS_CACHING
8// const DEV_SERVER = process.env.GNOLL_DEV_SERVER
9
10const paths = require('../utils/paths')
11const getBabelConfig = require('../utils/getBabelConfig')
12
13const babelConfig = getBabelConfig()
14
15const STATIC_FILES_REGEXP = /\.(png|svg|jpg|jpeg|gif|webp|eot|ttf|woff|woff2|otf|mp4|ogg|webm|mp3)$/
16
17const entry = [path.resolve(paths.root, 'src/index.js')]
18
19const output = {
20 path: path.join(paths.root, 'build'),
21 filename: '[name].js',
22 publicPath: '/'
23}
24
25const plugins = []
26
27if (TARGET === 'node' || TARGET === 'universal') {
28 output.libraryTarget = 'commonjs'
29}
30
31// Optimizations for long term caching
32if (TARGET === 'web' && ASSETS_CACHING) {
33 output.filename = '[name].[chunkhash].js'
34 plugins.push(
35 new ManifestPlugin({ output: 'manifest.json', writeToDisk: true })
36 )
37}
38
39if (TARGET === 'web' && ENV === 'production') {
40 plugins.push(new webpack.HashedModuleIdsPlugin())
41}
42
43const rules = [
44 {
45 test: /\.jsx?$/,
46 use: ['source-map-loader'],
47 enforce: 'pre'
48 },
49 {
50 test: /\.jsx?$/,
51 include: [path.join(paths.root, 'src')],
52 loader: 'babel-loader',
53 options: {
54 ...babelConfig,
55 cacheDirectory: path.join(paths.cache, 'babel-loader')
56 }
57 },
58 {
59 test: STATIC_FILES_REGEXP,
60 loader: 'file-loader',
61 options: {
62 emitFile: TARGET === 'web'
63 }
64 }
65]
66
67module.exports = {
68 entry,
69 output,
70 target: TARGET,
71 mode: ENV === 'production' ? 'production' : 'development',
72 plugins,
73 module: {
74 rules
75 },
76 resolve: {
77 extensions: ['.js', '.jsx', '.json'],
78 modules: [
79 'node_modules',
80 // when gnoll is installed with `npm link`,
81 // webpack-dev-server entry is inside local node_modules
82 path.resolve(__dirname, '..', 'node_modules')
83 ]
84 },
85 resolveLoader: {
86 modules: [
87 'node_modules',
88 // when gnoll is installed with `npm link`,
89 // loaders are inside local node_modules dir
90 path.resolve(__dirname, '..', 'node_modules')
91 ]
92 },
93 devtool: ENV !== 'production' ? 'cheap-module-source-map' : undefined
94}