1 | const path = require('path')
|
2 | const webpack = require('webpack')
|
3 | const AssetsPlugin = require('assets-webpack-plugin')
|
4 | const shelljs = require('shelljs')
|
5 |
|
6 | const HtmlWebpackPlugin = require('html-webpack-plugin')
|
7 |
|
8 | const commonPlugins = []
|
9 |
|
10 | const resolve = {
|
11 | alias: {
|
12 | '~': path.resolve('./src')
|
13 | }
|
14 | }
|
15 |
|
16 | const clientConfig = {
|
17 | entry: {
|
18 | client: ['./src/client/entry.js'],
|
19 | },
|
20 | output: {
|
21 | path: path.resolve('./build/public'),
|
22 | publicPath: '/',
|
23 |
|
24 | filename: '[name]_[chunkhash:7].js',
|
25 | },
|
26 | module: {
|
27 | rules: [...commonLoadersWithPresets()],
|
28 |
|
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 |
|
44 | }),
|
45 | ],
|
46 | }
|
47 |
|
48 | const 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',
|
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,
|
71 | /^.+assets\.json$/i,
|
72 | ]
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | const 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 |
|
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 |
|
104 | }),
|
105 | ]
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 | shelljs.mkdir('-p', clientConfig.output.path)
|
111 | shelljs.cp('-rf', './src/public/.', clientConfig.output.path)
|
112 |
|
113 | const argv = process.argv[2]
|
114 | if (argv === 'all' || argv === 'cordovaOnly') {
|
115 | shelljs.mkdir('-p', cordovaConfig.output.path)
|
116 | shelljs.cp('-rf', './src/public/', cordovaConfig.output.path)
|
117 | }
|
118 |
|
119 | module.exports = { clientConfig, serverConfig, cordovaConfig }
|
120 |
|
121 |
|
122 | function 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 }]
|
142 | ],
|
143 | cacheDirectory: true,
|
144 | }
|
145 | }],
|
146 | }, {
|
147 | test: /\.sql$/,
|
148 |
|
149 | use: ['raw-loader'],
|
150 | }, {
|
151 | test: /\.(?!(jsx?|json|s?css|less|html?|sql)$)([^.]+$)/,
|
152 |
|
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 | }
|