This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
webpack.config.js
166 lines (144 loc) · 4.3 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
const webpack = require('webpack')
const path = require('path')
const config = require('./src/config')
const _ = require('lodash')
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const StringReplacePlugin = require('string-replace-webpack-plugin')
const { replaceInModuleSource, getAllModules } = require('svg-sprite-loader/lib/utils')
const __DEV__ = process.env.NODE_ENV === 'development'
const __PROD__ = process.env.NODE_ENV === 'production'
const paths = {
source: path.resolve(__dirname, './src'),
dist: path.resolve(__dirname, './dist'),
assets: path.resolve(__dirname, './assets'),
app: path.resolve(__dirname, './src/app/pictogram.js'),
spritePrefix: 'sprite-'
}
const appName = 'pictogrify'
const currentColors = loadReplaceColors().join('|')
function loadPlugins () {
const base = [
new webpack.DefinePlugin({
__DEV__,
__PROD__
}),
new SpriteLoaderPlugin({
plainSprite: true,
spriteAttrs: {
id: ''
}
}),
new StringReplacePlugin(),
{
apply (compiler) {
compiler.plugin('compilation', (compilation) => {
compilation.plugin('optimize-chunk-assets', (chunks, callback) => {
const { assets } = compilation
const spriteAssets = _.pickBy(assets, (value, key) => key.startsWith('sprite-'))
if (_.isEmpty(spriteAssets) || _.isNil(spriteAssets)) throw new Error('Sprites are not compiled!')
const spriteSources = _.transform(spriteAssets, (result, value, key) => result[key.replace(/(sprite-|.svg)/g, '')] = value.source())
getAllModules(compilation).forEach((module) => {
replaceInModuleSource(module, {
__SPRITE_SOURCES__: JSON.stringify(spriteSources)
})
})
callback()
})
})
}
}
]
const prod = [
new CleanWebpackPlugin([paths.dist]),
new webpack.optimize.UglifyJsPlugin()
]
if (__PROD__) {
return [ ...base, ...prod ]
} else {
return base
}
}
function loadReplaceColors () {
const colors = _(config.themes).flatMap((theme) => _.map(theme.shapes, (shape) => shape.currentColor)).compact().uniq().value()
return colors
}
const build = {
target: 'web',
entry: path.join(paths.app),
output: {
path: paths.dist,
filename: `${appName}.js`,
library: 'Pictogrify',
libraryTarget: 'umd',
},
devServer: {
open: true,
},
resolve: {
extensions: ['.js']
},
module: {
rules: [
{
test: /\.svg$/,
exclude: /node_modules/,
use: [
{
loader: 'svg-sprite-loader',
options: {
extract: true,
spriteFilename: svgPath => {
const tree = path.dirname(svgPath).split(path.sep)
const theme = tree[_.indexOf(tree, 'themes') + 1]
return `${paths.spritePrefix}${theme}.svg`
}
}
},
{
loader: StringReplacePlugin.replace({
replacements: [
{
pattern: new RegExp(`(fill)="(${currentColors})"`, 'g'),
replacement: function (match, p1, p2) {
// return `${p1}=param(${p1}) ${p2}`
return p1 + '="param(' + p1 + ') ' + p2 + '"'
}
}
]
}),
},
{
loader: 'svgo-loader',
options: {
plugins: [
{ inlineStyles: { onlyMatchedOnce: false } },
{ removeAttrs: { attrs: ['data.*', 'viewBox', 'serif.*'] } },
{ removeXMLNS: true },
{ cleanupIDs: true },
{ removeUnknownsAndDefaults: true },
{ collapseGroups: true },
]
}
},
]
},
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
},
{
test: /\.js$/,
exclude: /node_modules/,
use: [
'babel-loader',
`imports-loader?__SPRITE_DIST__=>{url: "${__PROD__ ? '/' + appName : ''}/dist/${paths.spritePrefix}"}`,
]
}
]
},
plugins: loadPlugins()
}
module.exports = build