-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebpack.config.js
134 lines (121 loc) · 3.58 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
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
/**
* webpack.config.js
*
* This config serves as both the development and production
* Webpack config. The difference is that it's consumed by
* either webpack-dev-server (development) or webpack itself
* (production)
*/
const webpack = require('webpack');
const path = require('path');
const fse = require('fs-extra');
const fs = require('fs');
const _ = require('lodash')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyWebpackPlugin = require('uglifyjs-webpack-plugin');
const DefinePlugin = require('webpack/lib/DefinePlugin');
const noEmitOnErrorsPlugin = new webpack.NoEmitOnErrorsPlugin();
const htmlWebpackPlugin = new HtmlWebpackPlugin({
title: 'Sample TypeScript App',
inject: true
});
const uglifyWebpackPlugin = new UglifyWebpackPlugin();
/**
* Export config
*/
module.exports = (env) => {
env = env || {};
fixWebpackEnv(env)
const { watch = false, outFolder = 'dist', moduleName = undefined, port = 9000 } = env;
// console.log('env', env)
const browserOutFolder = `browser-for-${moduleName}`;
const srcDirRelative = _.isString(moduleName) ? `./tmp-src-${outFolder}-${browserOutFolder}` : `./tmp-src-${outFolder}`;
const distDirRelative = _.isString(moduleName) ? `./${browserOutFolder}` : `./${outFolder}-browser`;
const srcDir = path.join(__dirname, srcDirRelative);
const distDir = path.join(__dirname, distDirRelative);
let ENV = fse.existsSync('./tmp-environment.json') ? fse.readJSONSync('./tmp-environment.json', {
'encoding': 'utf8'
}) : {}
return {
devtool: !watch ? 'source-map' : 'eval-source-map',
entry: `${srcDirRelative}/app.ts`,
output: {
path: distDir,
filename: '[name].[hash:5].js'
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
// {
// enforce: 'pre',
// test: /\.tsx?$/,
// loader: 'tslint-loader',
// include: srcDir,
// exclude: /node_modules/,
// options: {
// configFile: './tslint.json',
// failOnHint: true
// }
// },
{
test: /\.tsx?$/,
include: srcDir,
exclude: /node_modules/,
use: [
// {
// /**
// * 2. Transpile ES6 + dynamic imports into ES5
// * (smaller bundle sizes than ts-loader alone)
// */
// loader: 'babel-loader',
// options: {
// presets: ['es2015'],
// plugins: ['babel-plugin-syntax-dynamic-import']
// }
// },
{
/**
* 1. Transpile TypeScript into ES6 + dynamic imports
*/
loader: 'ts-loader',
options: {
// compilerOptions: {
// module: 'esnext' , // allows bundle splitting via dynamic imports!,
// },
// configFile: "tmp-src-dist-browser/tsconfig.json"
}
}
]
}
]
},
plugins: [
new DefinePlugin({
"ENV": JSON.stringify(ENV)
}),
noEmitOnErrorsPlugin,
htmlWebpackPlugin,
...(!watch
? [uglifyWebpackPlugin]
: [])
],
devServer: {
contentBase: srcDir,
compress: true,
port
}
};
}
function fixWebpackEnv(env = {}) {
_.forIn(env, (v, k) => {
const value = v;
if (value === 'true') env[k] = true;
if (value === 'false') env[k] = false;
})
}