-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
82 lines (74 loc) · 2.15 KB
/
webpack.config.js
File metadata and controls
82 lines (74 loc) · 2.15 KB
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
// Not using cozy-konnector-build/webpack.config because of the dual entry point on L39
const fs = require('fs')
var path = require('path')
const CopyPlugin = require('copy-webpack-plugin')
const SvgoInstance = require('svgo')
const webpack = require('webpack')
const currentDirectory = process.cwd()
const readManifest = () =>
JSON.parse(
fs.readFileSync(path.join(currentDirectory, './manifest.konnector'))
)
const manifest = readManifest()
const svgo = new SvgoInstance({
plugins: [
{
inlineStyles: { onlyMatchedOnce: false }
}
]
})
let iconName
try {
iconName = manifest.icon
// we run optimize only on SVG
if (!iconName.match(/\.svg$/)) iconName = null
} catch (e) {
// console.error(`Unable to read the icon path from manifest: ${e}`)
}
const appIconRX = iconName && new RegExp(`[^/]*/${iconName}`)
module.exports = {
entry: {
index: './src/index.js',
onDeleteAccount: './src/onDeleteAccount.js'
},
target: 'node',
mode: 'none',
node: {
// to avoid __dirname to be defaulted to "/" and allow cozy-konnector-libs to read a JSON payload next to the current file. https://codeburst.io/use-webpack-with-dirname-correctly-4cad3b265a92
__dirname: false,
__filename: false
},
output: {
path: path.join(currentDirectory, 'build'),
filename: '[name].js',
clean: true
},
plugins: [
new CopyPlugin({
patterns: [
{ from: 'manifest.konnector' },
{ from: 'assets', transform: optimizeSVGIcon, noErrorOnMissing: true }
]
}),
new webpack.DefinePlugin({
__WEBPACK_PROVIDED_MANIFEST__: JSON.stringify(manifest)
})
],
module: {
// to ignore the warnings like :
// WARNING in ../libs/node_modules/bindings/bindings.js 76:22-40
// Critical dependency: the request of a dependency is an expression
// Since we cannot change this dependency. I think it won't hide more important messages
exprContextCritical: false
},
externals: {
canvas: 'commonjs canvas'
}
}
function optimizeSVGIcon(buffer, path) {
if (appIconRX && path.match(appIconRX)) {
return svgo.optimize(buffer).then(resp => resp.data)
} else {
return buffer
}
}