Skip to content

Commit e93d86e

Browse files
committed
prod webpack config
1 parent acfc39a commit e93d86e

File tree

6 files changed

+236
-24
lines changed

6 files changed

+236
-24
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# react-typescripts
22

3-
[![Build Status](https://travis-ci.org/ewgenius/react-typescripts.svg?branch=master)](https://travis-ci.org/ewgenius/react-typescripts) [![dependencies Status](https://david-dm.org/ewgenius/react-typescripts/status.svg)](https://david-dm.org/ewgenius/react-typescripts)
3+
[![Build Status](https://travis-ci.org/ewgenius/react-typescripts.svg?branch=master)](https://travis-ci.org/ewgenius/react-typescripts) [![dependencies Status](https://david-dm.org/ewgenius/react-typescripts/status.svg)](https://david-dm.org/ewgenius/react-typescripts)
4+
5+
Strongly based on [create-react-app](https://github.com/facebookincubator/create-react-app).

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"chalk": "1.1.3",
3030
"detect-port": "1.1.0",
3131
"dotenv": "4.0.0",
32+
"extract-text-webpack-plugin": "^2.1.0",
3233
"fs-extra": "2.0.0",
3334
"html-webpack-plugin": "2.28.0",
3435
"object-assign": "4.1.1",
@@ -40,12 +41,14 @@
4041
"typescript": "2.2.1",
4142
"webpack": "2.2.1",
4243
"webpack-dev-server": "2.4.1",
44+
"webpack-manifest-plugin": "^1.1.0",
4345
"whatwg-fetch": "2.0.3"
4446
},
4547
"devDependencies": {
4648
"@types/autoprefixer": "6.7.0",
4749
"@types/chalk": "0.4.31",
4850
"@types/dotenv": "2.0.20",
51+
"@types/extract-text-webpack-plugin": "^2.0.1",
4952
"@types/fs-extra": "0.0.37",
5053
"@types/html-webpack-plugin": "2.11.2",
5154
"@types/node": "7.0.8",

src/config/webpack.config.dev.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// import * as path from 'path';
21
import * as autoprefixer from 'autoprefixer';
32
import * as CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';
43
import * as HtmlWebpackPlugin from 'html-webpack-plugin';

src/config/webpack.config.prod.ts

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import * as autoprefixer from 'autoprefixer';
2+
import * as ExtractTextPlugin from 'extract-text-webpack-plugin';
3+
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
4+
import * as InterpolateHtmlPlugin from 'react-dev-utils/InterpolateHtmlPlugin';
5+
import * as Webpack from 'webpack';
6+
import { Configuration } from 'webpack';
7+
import * as ManifestPlugin from 'webpack-manifest-plugin';
8+
9+
import { getClientEnvironment } from './env';
10+
import paths from './paths';
11+
12+
const publicPath = paths.servedPath;
13+
const shouldUseRelativeAssetPaths = publicPath === './';
14+
const publicUrl = publicPath.slice(0, -1);
15+
const env = getClientEnvironment(publicUrl);
16+
17+
if (env.stringified['process.env'].NODE_ENV !== '"production"') {
18+
throw new Error('Production builds must have NODE_ENV=production.');
19+
}
20+
21+
const cssFilename = 'static/css/[name].[contenthash:8].css';
22+
23+
const extractTextPluginOptions = shouldUseRelativeAssetPaths
24+
? { publicPath: Array(cssFilename.split('/').length).join('../') }
25+
: {};
26+
27+
const config: Configuration = {
28+
bail: true,
29+
devtool: 'source-map',
30+
entry: [
31+
require.resolve('./polyfills'),
32+
paths.appIndexJs
33+
],
34+
output: {
35+
path: paths.appBuild,
36+
filename: 'static/js/[name].[chunkhash:8].js',
37+
chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js',
38+
publicPath
39+
},
40+
resolve: {
41+
extensions: ['.ts', '.tsx', '.js', '.json', '.jsx'],
42+
modules: ['node_modules'].concat(paths.nodePaths)
43+
},
44+
resolveLoader: {
45+
modules: [
46+
paths.ownNodeModules,
47+
paths.appNodeModules
48+
]
49+
},
50+
module: {
51+
rules: [{
52+
test: /\.(ts|tsx)$/,
53+
enforce: 'pre',
54+
use: [{
55+
loader: 'tslint-loader'
56+
}],
57+
include: paths.appSrc
58+
}, {
59+
test: /\(.*)$/,
60+
exclude: [
61+
/\.html$/,
62+
/\.(ts|tsx)$/,
63+
/\.css$/,
64+
/\.scss$/,
65+
/\.json$/,
66+
/\.svg$/
67+
],
68+
loader: 'url-loader',
69+
options: {
70+
limit: 10000,
71+
name: 'static/media/[name].[hash:8].[ext]'
72+
}
73+
}, {
74+
test: /\.(ts|tsx)$/,
75+
include: paths.appSrc,
76+
loader: 'ts-loader'
77+
}, {
78+
test: /\.css$/,
79+
loader: ExtractTextPlugin.extract(Object.assign({
80+
fallback: 'style-loader',
81+
use: [
82+
{
83+
loader: 'css-loader',
84+
options: {
85+
importLoaders: 1
86+
}
87+
}, {
88+
loader: 'postcss-loader',
89+
options: {
90+
ident: 'postcss',
91+
plugins: (): any[] => [autoprefixer({
92+
browsers: [
93+
'>1%',
94+
'last 4 versions',
95+
'Firefox ESR',
96+
'not ie < 9',
97+
]
98+
})]
99+
}
100+
}
101+
]
102+
}, extractTextPluginOptions))
103+
// Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
104+
}, {
105+
test: /\.svg$/,
106+
loader: 'file-loader',
107+
options: {
108+
name: 'static/media/[name].[hash:8].[ext]'
109+
}
110+
}
111+
]
112+
},
113+
plugins: [
114+
new InterpolateHtmlPlugin(env.raw),
115+
// Generates an `index.html` file with the <script> injected.
116+
new HtmlWebpackPlugin({
117+
inject: true,
118+
template: paths.appHtml,
119+
minify: {
120+
removeComments: true,
121+
collapseWhitespace: true,
122+
removeRedundantAttributes: true,
123+
useShortDoctype: true,
124+
removeEmptyAttributes: true,
125+
removeStyleLinkTypeAttributes: true,
126+
keepClosingSlash: true,
127+
minifyJS: true,
128+
minifyCSS: true,
129+
minifyURLs: true
130+
}
131+
}),
132+
// Makes some environment variables available to the JS code, for example:
133+
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
134+
// It is absolutely essential that NODE_ENV was set to production here.
135+
// Otherwise React will be compiled in the very slow development mode.
136+
new Webpack.DefinePlugin(env.stringified),
137+
// Minify the code.
138+
new Webpack.optimize.UglifyJsPlugin({
139+
compress: {
140+
screw_ie8: true, // React doesn't support IE8
141+
warnings: false
142+
},
143+
mangle: {
144+
screw_ie8: true
145+
},
146+
output: {
147+
comments: false,
148+
screw_ie8: true
149+
} as any,
150+
sourceMap: true
151+
}),
152+
new ExtractTextPlugin({
153+
filename: cssFilename
154+
}),
155+
new ManifestPlugin({
156+
fileName: 'asset-manifest.json'
157+
})
158+
],
159+
node: {
160+
fs: 'empty',
161+
net: 'empty',
162+
tls: 'empty'
163+
},
164+
};
165+
166+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module 'webpack-manifest-plugin' {
2+
const ManifestPlugin: any;
3+
export = ManifestPlugin;
4+
}

0 commit comments

Comments
 (0)