-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
213 lines (188 loc) · 6.77 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const { configureWebpack, graphQL } = require('@magento/pwa-buildpack');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const fs = require('fs');
const { promisify } = require('util');
const path = require('path');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const {
getMediaURL,
getStoreConfigData,
getAvailableStoresConfigData,
getPossibleTypes
} = graphQL;
const { DefinePlugin } = webpack;
// const { LimitChunkCountPlugin } = webpack.optimize;
const getCleanTemplate = templateFile => {
return new Promise(resolve => {
fs.readFile(templateFile, 'utf8', (err, data) => {
resolve(
data.replace(
/(?<inlineddata><!-- Inlined Data -->.*\s<!-- \/Inlined Data -->)/gs,
''
)
);
});
});
};
module.exports = async env => {
/**
* configureWebpack() returns a regular Webpack configuration object.
* You can customize the build by mutating the object here, as in
* this example. Since it's a regular Webpack configuration, the object
* supports the `module.noParse` option in Webpack, documented here:
* https://webpack.js.org/configuration/module/#modulenoparse
*/
const config = await configureWebpack({
context: __dirname,
vendor: [
'@apollo/client',
'apollo-cache-persist',
'informed',
'react',
'react-dom',
'react-feather',
'react-redux',
'react-router-dom',
'redux',
'redux-actions',
'redux-thunk'
],
special: {
'react-feather': {
esModules: true
}
},
env
});
const mediaUrl = await getMediaURL();
const storeConfigData = await getStoreConfigData();
const { availableStores } = await getAvailableStoresConfigData();
const writeFile = promisify(fs.writeFile);
/**
* Loop the available stores when there is provided STORE_VIEW_CODE
* in the .env file, because should set the store name from the
* given store code instead of the default one.
*/
const availableStore = availableStores.find(
({ store_code }) => store_code === process.env.STORE_VIEW_CODE
);
global.MAGENTO_MEDIA_BACKEND_URL = mediaUrl;
global.LOCALE = storeConfigData.locale.replace('_', '-');
global.AVAILABLE_STORE_VIEWS = availableStores;
const possibleTypes = await getPossibleTypes();
const htmlWebpackConfig = {
filename: 'index.html',
minify: {
collapseWhitespace: true,
removeComments: true
}
};
// Strip UPWARD mustache from template file during watch
if (
process.env.npm_lifecycle_event &&
process.env.npm_lifecycle_event.includes('watch')
) {
const devTemplate = await getCleanTemplate('./template.html');
// Generate new gitignored html file based on the cleaned template
await writeFile('template.generated.html', devTemplate);
htmlWebpackConfig.template = './template.generated.html';
} else {
htmlWebpackConfig.template = './template.html';
}
config.module.noParse = [
/@adobe\/adobe\-client\-data\-layer/,
/braintree\-web\-drop\-in/
];
config.plugins = [
...config.plugins,
new DefinePlugin({
/**
* Make sure to add the same constants to
* the globals object in jest.config.js.
*/
POSSIBLE_TYPES: JSON.stringify(possibleTypes),
STORE_NAME: availableStore
? JSON.stringify(availableStore.store_name)
: JSON.stringify(storeConfigData.store_name),
STORE_VIEW_CODE: process.env.STORE_VIEW_CODE
? JSON.stringify(process.env.STORE_VIEW_CODE)
: JSON.stringify(storeConfigData.code),
AVAILABLE_STORE_VIEWS: JSON.stringify(availableStores),
DEFAULT_LOCALE: JSON.stringify(global.LOCALE),
DEFAULT_COUNTRY_CODE: JSON.stringify(
process.env.DEFAULT_COUNTRY_CODE || 'US'
),
__DEV__: process.env.NODE_ENV !== 'production'
}),
new HTMLWebpackPlugin(htmlWebpackConfig)
];
/**
* Typescript Support
*/
config.plugins.push(new ForkTsCheckerWebpackPlugin());
config.resolve.extensions.push('.ts', '.tsx');
config.module.rules.push({
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/
});
// set the entry point
config.entry.client = path.resolve(__dirname, 'src', 'index.tsx');
// change the sw.js to use sw.ts
const serviceWorkerPlugin = config.plugins.find(
plugin => plugin.constructor.name === 'ServiceWorkerPlugin'
);
serviceWorkerPlugin.config.injectManifestConfig.swSrc = path.resolve(__dirname, 'src', 'ServiceWorker', 'sw.ts');
/*
Commenting out this section until SSR is fully implemented
*/
// const serverConfig = Object.assign({}, config, {
// target: 'node',
// devtool: false,
// module: { ...config.module },
// name: 'server-config',
// output: {
// ...config.output,
// filename: '[name].[hash].SERVER.js',
// strictModuleExceptionHandling: true
// },
// optimization: {
// minimize: false
// },
// plugins: [...config.plugins]
// });
// TODO: get LocalizationPlugin working in Node
// const browserPlugins = new Set()
// .add('HtmlWebpackPlugin')
// .add('LocalizationPlugin')
// .add('ServiceWorkerPlugin')
// .add('VirtualModulesPlugin')
// .add('WebpackAssetsManifest');
// remove browser-only plugins
// serverConfig.plugins = serverConfig.plugins.filter(
// plugin => !browserPlugins.has(plugin.constructor.name)
// );
// remove browser-only module rules
// serverConfig.module.rules = serverConfig.module.rules.map(rule => {
// if (`${rule.test}` === '/\\.css$/') {
// return {
// ...rule,
// oneOf: rule.oneOf.map(ruleConfig => ({
// ...ruleConfig,
// use: ruleConfig.use.filter(
// loaderConfig => loaderConfig.loader !== 'style-loader'
// )
// }))
// };
// }
// return rule;
// });
// add LimitChunkCountPlugin to avoid code splitting
// serverConfig.plugins.push(
// new LimitChunkCountPlugin({
// maxChunks: 1
// })
// );
return [config];
};