-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
109 lines (105 loc) · 3.31 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
const path = require('path')
const webpack = require('webpack')
const capitalize = require('lodash.capitalize')
const { name } = require('./package')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const watch = !!process.env.WATCH
const isExample = !!process.env.EXAMPLE
function conf({ define, isBuild, entry, globalObject, watch, externals, mini, suffix = '', format } = {}) {
const NODE_ENV = isBuild ? 'production' : 'development'
const filename = `${name}${suffix}.${format}${mini ? '.min' : ''}.js`
return {
name: filename,
mode: NODE_ENV,
entry: entry ? entry : isExample ? './example.js' : ['./standalone.js'],
target: format === 'commonjs2' ? 'node' : 'web',
externals:
format === 'commonjs2'
? [
function(context, request, callback) {
if (['.less', '.css', '.sass'].includes(path.extname(request))) {
return callback()
}
const dep = Object.keys(require('./package').dependencies).concat(
Object.keys(require('./package').peerDependencies)
)
const f = dep.find(name => request.startsWith(name))
if (f) {
return callback(null, 'commonjs ' + request)
}
callback()
}
].concat(externals)
: externals,
output: {
globalObject,
path: isExample ? path.resolve(__dirname, 'public') : path.resolve(__dirname, 'dist'),
filename,
library: !isExample
? {
root: name.replace(/^([a-z])|(?:-([a-z]))/gi, (m, headLetter, letter) =>
(headLetter || letter).toUpperCase()
),
amd: name,
commonjs: name
}
: void 0,
libraryTarget: !isExample ? format : void 0
},
resolve: {
alias: {
react: 'preact-compat',
'react-dom': 'preact-compat',
// Not necessary unless you consume a module using `createClass`
'create-react-class': 'preact-compat/lib/create-react-class',
// Not necessary unless you consume a module requiring `react-dom-factories`
'react-dom-factories': 'preact-compat/lib/react-dom-factories'
}
},
devtool: 'source-map',
module: require('./webpack.module'),
plugins: [
isExample &&
new HtmlWebpackPlugin({
templateParameters: {},
template: './example.html',
filename: 'index.html'
}),
new ExtractTextPlugin({
filename: 'style.css',
allChunks: false
}),
new webpack.DefinePlugin(
Object.assign(
{
'process.env.NODE_ENV': JSON.stringify(NODE_ENV)
},
define
)
),
// !isBuild && watch && new webpack.HotModuleReplacementPlugin(),
isBuild &&
!isExample &&
!watch &&
new BundleAnalyzerPlugin({
analyzerMode: 'static'
})
].filter(Boolean),
watch
}
}
module.exports = [
watch
? conf({
watch: watch,
isBuild: false,
format: 'umd'
})
: conf({
watch: watch,
isBuild: true,
format: 'umd'
})
].filter(Boolean)