This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetWebpackConfig.js
184 lines (170 loc) · 5.25 KB
/
getWebpackConfig.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
'use strict';
const path = require('path');
const modulePaths = [
// check webpack config folder first
path.join(__dirname, 'node_modules'),
// fall back to project folder
path.join(process.env.PWD, 'node_modules'),
];
process.env.NODE_PATH = modulePaths.join(':');
require('module').Module._initPaths();
const invariant = require('invariant');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const EvaluateBundlePlugin = require('./EvaluateBundlePlugin');
const entrypointPath = require.resolve('./entrypoint');
function getWebpackConfig({
componentPath,
buildPath,
jsxstyleLoaderOptions,
emitCallback,
htmlTemplate = path.resolve(__dirname, 'template.html'),
reactID = '.react-root',
cssFileName = 'bundle.css',
bundleFileName = 'bundle.js',
browsers = ['last 2 versions', '> 5%'],
}) {
invariant(
typeof componentPath === 'string' &&
path.isAbsolute(componentPath) &&
path.extname(componentPath) === '.js',
'`componentPath` is expected to be an absolute path to a JS file'
);
invariant(
typeof buildPath === 'string' && path.isAbsolute(buildPath),
'`buildPath` is expected to be an absolute path to a directory'
);
invariant(
typeof htmlTemplate === 'string' &&
path.isAbsolute(htmlTemplate) &&
path.extname(htmlTemplate) === '.html',
'`htmlTemplate` is expected to be an absolute path to a HTML file'
);
if (typeof emitCallback !== 'undefined') {
invariant(
typeof emitCallback === 'function',
'`emitCallback` is expected to be a function'
);
}
const postcssLoaderObject = {
loader: require.resolve('postcss-loader'),
options: {
plugins: loader => [require('postcss-cssnext')({ browsers })],
},
};
const cssLoaderObject = {
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
};
return function(idk, env) {
return {
entry: [
// patch is a noop in non-hot environments, but let's filter it out anyway
env.hot && require.resolve('react-hot-loader/patch'),
entrypointPath,
].filter(f => f),
output: {
path: buildPath,
filename: bundleFileName,
},
plugins: [
!env.hot &&
emitCallback &&
new EvaluateBundlePlugin({ callback: emitCallback }),
env.hot && new webpack.NamedModulesPlugin(),
!env.hot && new ExtractTextPlugin(cssFileName),
new HtmlWebpackPlugin({
template: htmlTemplate,
inject: false,
_react_id: reactID,
}),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin({
__REACT_ROOT_ID__: JSON.stringify(reactID),
__ROOT_COMPONENT__: JSON.stringify(componentPath),
}),
].filter(f => f),
resolve: {
alias: {
__ROOT_COMPONENT__: componentPath,
},
modules: [].concat(modulePaths, 'node_modules'),
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: require.resolve('babel-loader'),
options: {
// ignoring babelrc
babelrc: false,
presets: [
[
require.resolve('babel-preset-env'),
{ targets: { browsers } },
],
require.resolve('babel-preset-react'),
],
plugins: [
env.hot && require.resolve('react-hot-loader/babel'),
require.resolve(
'babel-plugin-transform-object-rest-spread'
),
require.resolve('babel-plugin-transform-object-assign'),
].filter(f => f),
},
},
jsxstyleLoaderOptions
? {
loader: require.resolve('jsxstyle-loader'),
options: jsxstyleLoaderOptions,
}
: null,
].filter(f => f),
},
{
test: /\.css$/,
use: env.hot
? [
require.resolve('style-loader'),
cssLoaderObject,
postcssLoaderObject,
]
: ExtractTextPlugin.extract({
use: [cssLoaderObject, postcssLoaderObject],
fallback: require.resolve('style-loader'),
}),
},
{
test: /fonts\/[^/]+\.(eot|svg|ttf|woff|woff2)$/,
use: {
loader: require.resolve('file-loader'),
options: {
name: 'webfonts/[name].[ext]',
},
},
},
{
test: /\.svg$/,
use: [
require.resolve('raw-loader'),
{
loader: require.resolve('svgo-loader'),
query: {
plugins: [{ removeTitle: true }, { convertPathData: false }],
},
},
],
},
],
},
};
};
}
module.exports = getWebpackConfig;