Skip to content

Commit d137ede

Browse files
committed
Creating build app
1 parent bb6138c commit d137ede

File tree

4 files changed

+120
-49
lines changed

4 files changed

+120
-49
lines changed
Lines changed: 91 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,98 @@
1-
"use strict";
1+
'use strict';
22

3-
const HtmlWebpackPlugin = require("html-webpack-plugin");
4-
const resolverPath = require("../utils/resolverPath");
3+
const HtmlWebpackPlugin = require('html-webpack-plugin');
4+
const resolverPath = require('../utils/resolverPath');
55

6-
const APP_PATH = resolverPath("src/dev");
6+
const APP_PATH = resolverPath('src/dev');
7+
const BUILD_PATH = resolverPath('build');
78

8-
const config = {
9-
entry: APP_PATH,
10-
mode: "development",
11-
resolve: {
12-
modules: ["node_modules", "src/lib"],
13-
extensions: [".ts", ".tsx", ".js", ".json"]
14-
},
15-
module: {
16-
rules: [
17-
{
18-
test: /\.(ts|js)x?$/,
19-
loader: "babel-loader",
20-
exclude: /node_modules/,
21-
options: {
22-
presets: [require.resolve("babel-preset-react-app")]
23-
}
24-
},
25-
{
26-
test: /\.(gif|png|jpe?g|svg)$/i,
27-
use: [
28-
"file-loader",
29-
{
30-
loader: "image-webpack-loader"
9+
const configWebpack = ({ mode }) => {
10+
const isEnvProduction = mode === 'production';
11+
12+
return {
13+
entry: APP_PATH,
14+
output: {
15+
path: BUILD_PATH,
16+
pathinfo: !isEnvProduction,
17+
filename: isEnvProduction
18+
? 'static/js/[name].[contenthash:8].js'
19+
: 'bundle.js',
20+
chunkFilename: isEnvProduction
21+
? 'static/js/[name].[contenthash:8].chunk.js'
22+
: 'static/js/[name].chunk.js',
23+
globalObject: 'this'
24+
},
25+
mode,
26+
bail: isEnvProduction,
27+
resolve: {
28+
modules: ['node_modules', 'src'],
29+
extensions: ['.ts', '.tsx', '.js', '.json']
30+
},
31+
module: {
32+
rules: [
33+
{
34+
test: /\.(ts|js)x?$/,
35+
loader: require.resolve('babel-loader'),
36+
exclude: /node_modules/,
37+
options: {
38+
babelrc: false,
39+
configFile: false,
40+
compact: isEnvProduction,
41+
sourceMaps: false,
42+
inputSourceMap: false,
43+
presets: [require.resolve('babel-preset-react-app')]
3144
}
32-
]
33-
},
34-
{
35-
test: /\.scss$/,
36-
use: ["style-loader", "css-loader", "sass-loader"]
37-
},
38-
{
39-
test: /\.css$/,
40-
use: ["style-loader", "css-loader"]
41-
}
45+
},
46+
{
47+
test: /\.(gif|png|jpe?g|svg)$/i,
48+
use: ['file-loader', { loader: 'image-webpack-loader' }]
49+
},
50+
{
51+
test: /\.scss$/,
52+
use: ['style-loader', 'css-loader', 'sass-loader']
53+
},
54+
{
55+
test: /\.css$/,
56+
use: ['style-loader', 'css-loader']
57+
}
58+
]
59+
},
60+
optimization: { minimize: isEnvProduction },
61+
node: {
62+
module: 'empty',
63+
dgram: 'empty',
64+
dns: 'mock',
65+
fs: 'empty',
66+
http2: 'empty',
67+
net: 'empty',
68+
tls: 'empty',
69+
child_process: 'empty'
70+
},
71+
plugins: [
72+
new HtmlWebpackPlugin(
73+
Object.assign(
74+
{},
75+
{ inject: true, template: `${APP_PATH}/index.html` },
76+
isEnvProduction
77+
? {
78+
minify: {
79+
removeComments: true,
80+
collapseWhitespace: true,
81+
removeRedundantAttributes: true,
82+
useShortDoctype: true,
83+
removeEmptyAttributes: true,
84+
removeStyleLinkTypeAttributes: true,
85+
keepClosingSlash: true,
86+
minifyJS: true,
87+
minifyCSS: true,
88+
minifyURLs: true
89+
}
90+
}
91+
: undefined
92+
)
93+
)
4294
]
43-
},
44-
45-
plugins: [
46-
new HtmlWebpackPlugin({
47-
inject: true,
48-
template: `${APP_PATH}/index.html`
49-
})
50-
],
51-
52-
optimization: { minimize: false }
95+
};
5396
};
5497

55-
module.exports = config;
98+
module.exports = configWebpack;

packages/react-dependency-scripts/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ process.on('unhandledRejection', err => {
99
const { spawnSync } = require('child_process');
1010

1111
const args = process.argv.slice(2);
12-
const scripts = ['start', 'build', 'test'];
12+
const scripts = ['start', 'build', 'test', 'build-app'];
1313

1414
if (args.length === 0) {
1515
console.log('\x1b[31mEmpty script.');

packages/react-dependency-scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"choose-port": "^0.1.0",
3737
"css-loader": "^3.4.2",
3838
"file-loader": "^5.0.2",
39+
"fs-extra": "^8.1.0",
3940
"html-webpack-plugin": "^3.2.0",
4041
"image-webpack-loader": "^6.0.0",
4142
"jest": "^25.1.0",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
process.env.NODE_ENV = 'production';
4+
process.env.BABEL_ENV = 'production';
5+
6+
const Webpack = require('webpack');
7+
const fs = require('fs-extra');
8+
const resolverPath = require('../utils/resolverPath');
9+
const configWebpack = require('../configs/webpack.config.js');
10+
11+
const BUILD_PATH = resolverPath('build');
12+
13+
const config = configWebpack({
14+
mode: 'production'
15+
});
16+
17+
fs.emptyDirSync(BUILD_PATH);
18+
19+
const compiler = Webpack(config);
20+
21+
compiler.run((err, stats) => {
22+
if (err) {
23+
return console.log(err);
24+
}
25+
26+
return console.log(stats);
27+
});

0 commit comments

Comments
 (0)