-
Notifications
You must be signed in to change notification settings - Fork 12
/
vue.config.js
269 lines (249 loc) · 11.3 KB
/
vue.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const CopyWebpackPlugin = require('copy-webpack-plugin');
// const SriPlugin = require('webpack-subresource-integrity');
const WriteFileWebpackPlugin = require('write-file-webpack-plugin');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
const path = require('path');
const fs = require('fs');
const createHash = require('crypto').createHash;
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const PoLoaderOptimizer = require('webpack-i18n-tools')();
let coreVersion = '2.0.5-history-fix';
// try {
// // Will fail until @nimiq/core export map is updated to also make the package.json file available
// coreVersion = require('@nimiq/core/package.json').version;
// } catch (e) {
// // Fallback to reading the package.json file directly
// pkgJson = require('fs').readFileSync(path.join(__dirname, 'node_modules/@nimiq/core/package.json'));
// coreVersion = JSON.parse(pkgJson).version;
// }
if (!coreVersion) throw new Error('Could not determine @nimiq/core version');
const crypto = require('crypto');
// Fix build for Node version with OpenSSL 3
const origCreateHash = crypto.createHash;
crypto.createHash = (alg, opts) => {
return origCreateHash(alg === 'md4' ? 'md5' : alg, opts);
};
const buildName = process.env.NODE_ENV === 'production'
? process.env.build
: 'local';
if (!buildName) throw new Error('Please specify the build config with the `build` environment variable');
const domain = buildName === 'mainnet'
? 'https://hub.nimiq.com'
: buildName === 'testnet'
? process.env.VUE_APP_HUB_URL
: 'http://localhost:8080';
const browserWarningTemplate = fs.readFileSync(
path.join(__dirname, 'node_modules/@nimiq/browser-warning/dist/browser-warning.html.template'));
const browserWarningIntegrityHash = `sha256-${createHash('sha256')
.update(fs.readFileSync(path.join(__dirname, 'node_modules/@nimiq/browser-warning/dist/browser-warning.js')))
.digest('base64')}`;
const bitcoinJsIntegrityHash = `sha256-${createHash('sha256')
.update(fs.readFileSync(path.join(__dirname, 'public/bitcoin/BitcoinJS.min.js')))
.digest('base64')}`;
// Accessible within client code via process.env.VUE_APP_BITCOIN_JS_INTEGRITY_HASH,
// see https://cli.vuejs.org/guide/mode-and-env.html#using-env-variables-in-client-side-code
process.env.VUE_APP_BITCOIN_JS_INTEGRITY_HASH = bitcoinJsIntegrityHash;
console.log('Building for:', buildName);
const configureWebpack = {
plugins: [
// ...(process.env.NODE_ENV === 'production' ? [new SriPlugin({
// hashFuncNames: ['sha256'],
// })] : []),
new CopyWebpackPlugin({ patterns: [
{
from: 'node_modules/@nimiq/browser-warning/dist/browser-warning.js*',
to: './',
flatten: true,
},
{
from: 'node_modules/@nimiq/vue-components/dist/iqons.min.*.svg',
to: './img/',
flatten: true,
},
{
from: 'node_modules/@nimiq/vue-components/dist/NimiqVueComponents.umd.min.lang-*.js',
to: './js/',
flatten: true,
transformPath(path) {
// The bundled NimiqVueComponents.umd.js tries to load the the non-minified files
return path.replace('.min', '');
},
},
// {
// from: 'node_modules/@nimiq/core',
// to: `./nimiq/v${coreVersion}/`,
// },
]}),
new WriteFileWebpackPlugin(),
new PoLoaderOptimizer(),
// new BundleAnalyzerPlugin(),
],
// Resolve config for yarn build
resolve: {
alias: {
config: path.join(__dirname, `src/config/config.${buildName}.ts`)
}
},
// Fix sourcemaps (https://www.mistergoodcat.com/post/the-joy-that-is-source-maps-with-vuejs-and-typescript)
devtool: 'source-map', // exact mapping; slow to build; small; enabled code minification and extracted maps
// TODO: 'eval-source-map' temporarily removed for webpack-i18n-tools, will be fixed in future versions
node: false,
output: {
crossOriginLoading: 'anonymous',
devtoolModuleFilenameTemplate: info => {
let $filename = 'sources://' + info.resourcePath;
if (info.resourcePath.match(/\.vue$/) && !info.query.match(/type=script/)) {
$filename = 'webpack-generated:///' + info.resourcePath + '?' + info.hash;
}
return $filename;
},
devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]',
},
externals: {
// We use a pre-built bundle for BitcoinJS (see public/bitcoin/BitcoinJS.min.js), also including a polyfill for
// node's 'buffer'. BitcoinJS is bundled separately via browserify for handling of polyfills of node natives.
// Everywhere we use BitcoinJS, we load it first on demand via BitcoinJSLoader which exposes BitcoinJS as global
// variable. To instruct webpack to not bundle BitcoinJS and Buffer but use the global BitcoinJS(.Buffer), we
// mark them as external here via the option 'root' which means it should use the value specified in the
// following expression (see documentation at https://v4.webpack.js.org/configuration/externals/). However,
// because we load BitcoinJS only asynchronously on demand, we have to apply a hack to avoid Webpack caching the
// dependency while it's still undefined. Instead, we ensure the export is always re-evaluated to the current
// global variable BitcoinJS by overwriting the exports of the cached module as getter. Note that we access the
// module as arguments[0] in the expression, because it's named differently in the minified production code and
// the served development version. To see how this plays together with the webpack runtime, have a look at how
// the dependency is emitted in the generated code and __webpack_require__'s implementation:
// https://devtools.tech/blog/understanding-webpacks-require---rid---7VvMusDzMPVh17YyHdyL
'bitcoinjs-lib': 'root Object.defineProperty(arguments[0], \'exports\', { get: () => BitcoinJS }).exports',
'buffer': 'root Object.defineProperty(arguments[0], \'exports\', { get: () => BitcoinJS }).exports',
},
};
const pages = {
index: {
// entry for the page
entry: 'src/main.ts',
// the source template
template: 'public/index.html',
// insert browser warning html template
browserWarningTemplate,
browserWarningIntegrityHash,
domain,
coreVersion,
// output as dist/index.html
filename: 'index.html',
// chunks to include on this page, by default includes
// extracted common chunks and vendor chunks.
chunks: ['chunk-vendors', 'chunk-common', 'index']
},
iframe: {
// entry for the page
entry: 'src/iframe.ts',
// the source template
template: 'public/iframe.html',
coreVersion,
// output as dist/iframe.html
filename: 'iframe.html',
// chunks to include on this page, by default includes
// extracted common chunks and vendor chunks.
chunks: ['chunk-vendors', 'chunk-common', 'iframe']
},
'cashlink-app': {
// entry for the page
entry: 'src/cashlink.ts',
// the source template
template: 'public/cashlink.html',
// insert browser warning html template
browserWarningTemplate,
browserWarningIntegrityHash,
domain,
coreVersion,
// output as dist/cashlink/index.html
filename: 'cashlink/index.html',
// chunks to include on this page, by default includes
// extracted common chunks and vendor chunks.
chunks: ['chunk-vendors', 'chunk-common', 'cashlink-app']
},
hubexport: {
// entry for the page
entry: 'src/export.ts',
// the source template
template: 'public/export.html',
coreVersion,
// output as dist/iframe.html
filename: 'export.html',
},
};
if (buildName === 'local' || buildName === 'testnet') {
pages.demos = {
// entry for the page
entry: 'demos/Demo.ts',
// the source template
template: 'demos/index.html',
coreVersion,
bitcoinJsIntegrityHash,
// output as dist/demos.html
filename: 'demos.html',
// chunks to include on this page, by default includes
// extracted common chunks and vendor chunks.
chunks: ['chunk-vendors', 'chunk-common', 'demos'],
};
pages.callback = {
entry: 'demos/callback.ts',
template: 'demos/callback.html',
filename: 'callback.html',
chunks: [],
};
}
module.exports = {
pages,
integrity: true,
configureWebpack,
chainWebpack: config => {
// Do not put prefetch/preload links into the landing pages
config.plugins.delete('prefetch-index');
config.plugins.delete('preload-index');
config.plugins.delete('prefetch-iframe');
config.plugins.delete('preload-iframe');
config.plugins.delete('prefetch-cashlink-app');
config.plugins.delete('preload-cashlink-app');
config.plugins.delete('prefetch-demos');
config.plugins.delete('preload-demos');
config.plugins.delete('prefetch-callback');
config.plugins.delete('preload-callback');
config.module
.rule('ts')
.use('ts-loader')
.loader('ts-loader')
.tap(options => {
options.configFile = `tsconfig.${buildName}.json`
return options
});
config.module
.rule('po')
.test(/\.pot?$/)
.use('po-loader')
.loader('webpack-i18n-tools')
.end()
.end();
config
.plugin('script-ext-html-webpack-plugin')
.use(ScriptExtHtmlWebpackPlugin, [{
defaultAttribute: 'defer',
}]);
},
// Add dependencies here which should be transpiled by babel-loader via @vue/cli-plugin-babel. This is needed as our
// Webpack version is too old to process some modern js syntax in the listed dependencies.
// When changing to Webpack 5, some or all can probably be removed.
transpileDependencies: ['@nimiq/utils', '@nimiq/ledger-api'],
// For iOS debugging in BrowserStack, BrowserStack's localhost tunnel bs-local.com needs to be used, see
// https://www.browserstack.com/docs/live/local-testing/ios-troubleshooting-guide. However, on bs-local.com features
// only available on https like crypto.subtle or service workers do not work unless served as https. The Wallet also
// needs to served with this option and the Keyguard has to be served over https as well. Safari has problems to
// open the https page with invalid certificate, but Chrome iOS works and also uses the Safari engine. To be able to
// use the dev tools with Chrome iOS on BrowserStack, launch BrowserStack with Safari first, then switch to Chrome.
...(process.env['browserstack-ios-testing'] ? {
devServer: {
https: true,
disableHostCheck: true,
},
} : null),
};