forked from aws-samples/aws-lex-web-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
186 lines (180 loc) · 5 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
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
185
186
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const eslintFormatterFriendly = require('eslint-formatter-friendly');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const fs = require('fs');
function getAssetPath(filePath, defaultPath) {
const fileExists = fs.existsSync(filePath);
return fileExists ? filePath : defaultPath;
}
const VERSION = require('./package.json').version;
const basePath = __dirname;
const distDir = path.join(basePath, 'dist');
const assetsDir = path.resolve(__dirname, 'lex-web-ui/dist/bundle');
const devServerPort = (process.env.PORT) ? Number(process.env.PORT) : 8000;
module.exports = (env) => {
const isProd = (env.production === true);
return {
mode: (isProd) ? 'production' : 'development',
context: path.join(basePath, 'src/lex-web-ui-loader/js'),
entry: {
'lex-web-ui-loader': './index.js',
},
output: {
path: distDir,
filename: (isProd) ? '[name].min.js' : '[name].js',
library: 'ChatBotUiLoader',
libraryExport: 'ChatBotUiLoader',
libraryTarget: 'umd',
},
resolve: {
fallback: {
util: require.resolve('util/'),
crypto: require.resolve('crypto-browserify'),
buffer: require.resolve('buffer/'),
stream: require.resolve('stream-browserify'),
'process/browser': require.resolve('process/browser'),
"vm": require.resolve("vm-browserify"),
},
},
module: {
rules: [
/* TODO pending clean-up to re-enable
{
test: /\.js$/,
exclude: /[\\/]node_modules[\\/]/,
enforce: 'pre',
loader: 'eslint-loader',
options: {
formatter: eslintFormatterFriendly,
},
},
*/
{
test: /\.js$/,
exclude: /[\\/]node_modules[\\/]/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
},
{
test: /\.css$/,
use: (isProd) ? [
{
loader: MiniCssExtractPlugin.loader,
},
'css-loader',
'postcss-loader',
] : [
{
loader: MiniCssExtractPlugin.loader,
},
'css-loader',
],
},
],
},
devtool: (isProd) ? 'source-map' : 'cheap-module-source-map',
performance: {
hints: false,
},
devServer: {
static: [
{
directory: path.join(__dirname, 'src/config'),
},
{
directory: path.join(__dirname, 'src/website'),
},
{
directory: distDir,
},
],
client: {
logging: 'warn',
overlay: {
errors: true,
warnings: false,
runtimeErrors: true,
},
},
hot: true,
port: devServerPort,
},
stats: {
modules: false,
logging: 'error',
},
optimization: {
minimize: false,
},
plugins: [
new webpack.ProvidePlugin({
process: "process/browser",
Buffer: ["buffer", "Buffer"],
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: path.join(basePath, 'src/website/index.html'),
// script is included in template
inject: false,
scriptLoading: 'blocking',
}),
new HtmlWebpackPlugin({
filename: 'parent.html',
template: path.join(basePath, 'src/website/parent.html'),
// script is included in template
inject: false,
scriptLoading: 'blocking',
}),
isProd && new webpack.BannerPlugin({
banner: `/*!
* lex-web-ui v${VERSION}
* (c) 2017-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Released under the Amazon Software License.
*/ `,
raw: true,
entryOnly: true,
exclude: /[\\/]node_modules[\\/]/,
}),
new MiniCssExtractPlugin({
filename: (isProd) ? '[name].min.css' : '[name].css',
}),
new CopyPlugin(
{
patterns: [
// copy parent page
//{
// from: path.join(basePath, 'src/website/parent.html'),
// to: distDir,
//},
// copy custom css
{
from: path.join(basePath, 'src/website/custom-chatbot-style.css'),
to: distDir,
},
// copy lex-web-ui library
{
from: getAssetPath(path.join(basePath, 'lex-web-ui/dist/bundle/**/*'), assetsDir),
to: path.resolve(distDir, '[path][name][ext]'),
globOptions: {
ignore: [
"**/*.html",
"**.*.txt",
],
},
},
]
}
),
].filter(Boolean),
performance: {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000
},
};
};