UNPKG

4.77 kBJavaScriptView Raw
1const path = require('path')
2const webpack = require('webpack')
3const AssetsPlugin = require('assets-webpack-plugin')
4const shelljs = require('shelljs')
5
6const HtmlWebpackPlugin = require('html-webpack-plugin')
7
8const commonPlugins = []
9
10const resolve = {
11 alias: {
12 '~': path.resolve('./src') // use ~ to refer to the src folder. e.g. if you are in myApp/src/client/utils/fruit.js, you want to import banana which is in myApp/src/share/banana.js, you can write import banana from '~/share/banana' (this is better than using a relative path: import banana from '../../share/banana')
13 }
14}
15
16const clientConfig = {
17 entry: {
18 client: ['./src/client/entry.js'],
19 },
20 output: {
21 path: path.resolve('./build/public'),
22 publicPath: '/',
23 // filename: 'client.js', //during development
24 filename: '[name]_[chunkhash:7].js',
25 },
26 module: {
27 rules: [...commonLoadersWithPresets()],
28 // noParse: //,
29 },
30 resolve,
31 plugins: [
32 ...commonPlugins,
33 new AssetsPlugin({
34 path: './build',
35 }),
36 new webpack.DefinePlugin({
37 'process.env.APP_ENV': '"web"',
38 'process.env.CORDOVA': false,
39 }),
40 new HtmlWebpackPlugin({
41 title: 'My Awesome App',
42 template: './src/share/index.html',
43 // filename: './src/share/index.html'
44 }),
45 ],
46}
47
48const serverConfig = {
49 entry: {
50 server: ['./src/server/entry.js'],
51 },
52 target: 'node',
53 output: {
54 path: path.resolve('./build/server'),
55 filename: 'server.js',
56 libraryTarget: 'commonjs2', // src: const express = require('express') -> webpack builds: const express = require('express'); otherwise webpack builds: const express = express, which is wrong
57 },
58 module: {
59 rules: [...commonLoadersWithPresets({ target: 'server' })],
60 },
61 resolve,
62 plugins: [
63 ...commonPlugins,
64 new webpack.DefinePlugin({
65 'process.env.APP_ENV': '"node"',
66 'process.env.CORDOVA': false,
67 }),
68 ],
69 externals: [
70 /^[@a-z][a-z/\.\-0-9]*$/i, // native modules will be excluded, e.g require('react/server')
71 /^.+assets\.json$/i, // these assets produced by assets-webpack-plugin
72 ]
73}
74
75
76/**
77 * Cordova
78 */
79
80const cordovaConfig = {
81 entry: {
82 cordovaClient: ['./src/client/entry.js'],
83 },
84 output: {
85 path: path.resolve('./cordova/www/build'),
86 publicPath: '/',
87 filename: '[name].js',
88 },
89 module: {
90 rules: [...commonLoadersWithPresets()],
91 // noParse: [],
92 },
93 resolve,
94 plugins: [
95 ...commonPlugins,
96 new webpack.DefinePlugin({
97 'process.env.APP_ENV': '"web"',
98 'process.env.CORDOVA': true,
99 }),
100 new HtmlWebpackPlugin({
101 title: 'My Awesome App',
102 template: './src/share/index.html',
103 // filename: './src/share/index.html'
104 }),
105 ]
106}
107
108// copy static assets
109
110shelljs.mkdir('-p', clientConfig.output.path)
111shelljs.cp('-rf', './src/public/.', clientConfig.output.path) // copy contents in the public folder into build, notice the dot ".""
112
113const argv = process.argv[2]
114if (argv === 'all' || argv === 'cordovaOnly') {
115 shelljs.mkdir('-p', cordovaConfig.output.path)
116 shelljs.cp('-rf', './src/public/', cordovaConfig.output.path)
117}
118
119module.exports = { clientConfig, serverConfig, cordovaConfig }
120
121
122function commonLoadersWithPresets({ target = 'client' } = {}) {
123 const loader = [{
124 test: /\.jsx?$/,
125 exclude: /(node_modules)/,
126 use: [{
127 loader: 'babel-loader',
128 query: {
129 presets: [
130 ['env', {
131 target: target === 'client'
132 ? { browsers: ['last 2 versions', '> 5%'] }
133 : { node: true },
134 modules: false,
135 useBuiltIns: true,
136 }],
137 'stage-0',
138 'react'
139 ],
140 plugins: [
141 ['transform-runtime', { polyfill: false }] // helpers: true (default) so babel use modulised helpers when doding spread { ... object }, polyfill: false, so babel don't polyfill Set, Map, etc in server.
142 ],
143 cacheDirectory: true, // cache into OS temp folder by default
144 }
145 }],
146 }, {
147 test: /\.sql$/,
148 // exclude: /(node_modules)/,
149 use: ['raw-loader'],
150 }, {
151 test: /\.(?!(jsx?|json|s?css|less|html?|sql)$)([^.]+$)/, // match everything except js, jsx, json, css, scss, less. You can add more
152 // exclude: /(node_modules)/,
153 use: [{
154 loader: 'url-loader',
155 query: {
156 limit: 10000,
157 name: '[name]_[hash:7].[ext]',
158 emitFile: target === 'client',
159 }
160 }],
161 }]
162 if (target !== 'client') {
163 loader.push({
164 test: /^((?!\.module).)*css$/i,
165 use: [{ loader: 'null-loader' }]
166 })
167 }
168
169 return loader
170}