-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.round1.js
173 lines (150 loc) · 5 KB
/
webpack.round1.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
'use strict';
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
const dev = process.env.BUILD_DEV === 'true';
const entry = { app: ['./src/round1/Index.tsx'] };
// make sure the cdn links are always versioned
const vendorList = [
{
moduleName: 'react',
namespace: 'React',
link: 'https://unpkg.com/[email protected]/dist/react.js'
},
{
moduleName: 'react-dom',
namespace: 'ReactDOM',
link: 'https://unpkg.com/[email protected]/dist/react-dom.js'
},
{
moduleName: 'IntlMessageFormat',
namespace: 'IntlMessageFormat',
link: 'https://unpkg.com/[email protected]/dist/intl-messageformat.js'
},
{
moduleName: 'mobx',
namespace: 'mobx',
link: 'https://unpkg.com/[email protected]/lib/mobx.umd.js'
},
{
moduleName: 'mobx-react',
namespace: 'mobxReact',
link: 'https://unpkg.com/[email protected]/index.js'
},
{
moduleName: 'semantic-ui-react',
namespace: 'semanticUIReact',
link: 'https://unpkg.com/[email protected]/dist/umd/semantic-ui-react.min.js'
}
];
const vendorMap = vendorList.reduce((total, cur) => {
total[cur.moduleName] = cur.namespace;
return total;
}, {});
const vendorCdnJs = vendorList.map(v => v.link);
vendorCdnJs.push(
'https://unpkg.com/[email protected]/dist/jquery.js',
'https://unpkg.com/[email protected]/dist/js/materialize.js'
);
// Make sure to update storybook whenever update semantic.min.css
const vendorCdnCss = ['https://unpkg.com/[email protected]/dist/css/materialize.css'];
const getVendorCdnMinJs = () => {
return vendorCdnJs.map(js => {
if (/.min.js$/g.test(js)) {
return js;
}
if (!/.js$/g.test(js)) {
throw new Error(`${js} is not a link to a javascript file.`);
}
return js.replace(/.js$/g, '.min.js');
});
};
const plugins = [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
__DEV__: JSON.stringify(JSON.parse(process.env.BUILD_DEV || 'true'))
}),
new HtmlWebpackPlugin({
template: 'src/round1/index.html',
inject: true,
filename: dev ? 'index.html' : 'round1.html',
vendorJs: getVendorCdnMinJs(),
vendorCss: vendorCdnCss
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function(module) {
// this assumes your vendor imports exist in the node_modules directory
return module.context && module.context.indexOf('node_modules') !== -1;
}
}),
new ScriptExtHtmlWebpackPlugin({
inline: /\.js$/
})
];
let cssLoader;
if (process.env.NODE_ENV === 'production') {
const ExtractTextPlugin = require('extract-text-webpack-plugin');
cssLoader = {
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: 'css-loader'
})
};
plugins.push(new ExtractTextPlugin('styles.[hash].css'));
} else {
const WebpackNotifierPlugin = require('webpack-notifier');
entry.app.unshift('webpack-hot-middleware/client');
cssLoader = {
test: /\.css$/,
use: ['style-loader', 'css-loader']
};
plugins.push(new WebpackNotifierPlugin(), new webpack.HotModuleReplacementPlugin());
}
module.exports = {
devtool: '',
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js'],
modules: [path.resolve('.'), 'node_modules']
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: vendorMap,
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
loader: 'source-map-loader',
exclude: path.resolve(__dirname, 'node_modules')
},
{
enforce: 'pre',
test: /\.tsx?$/,
loader: 'tslint-loader',
exclude: [path.resolve(__dirname, 'node_modules')],
options: {
emitErrors: true,
failOnHint: true
}
},
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader',
exclude: path.resolve(__dirname, 'node_modules')
},
{ test: /\.(png|svg)$/, loader: 'url-loader', options: { limit: 5000 } },
cssLoader
]
},
entry,
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].[hash].js'
},
plugins
};