forked from bangle-io/bangle-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
166 lines (156 loc) · 4.29 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
/* eslint-disable no-process-env */
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webpack = require('webpack');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const ejs = require('ejs');
const fs = require('fs');
process.env.WEBPACK = true;
module.exports = (env, argv) => {
const isProduction = env && env.production;
const envVars = require('@bangle.io/env-vars')({ isProduction });
const mode = isProduction ? 'production' : 'development';
const buildPath = path.resolve(__dirname, 'build');
// eslint-disable-next-line no-process-env
if (isProduction && process.env.NODE_ENV !== 'production') {
throw new Error('NODE_ENV not production');
}
let rawHtml = fs.readFileSync(path.resolve(__dirname, 'index.html'), 'utf-8');
const html = ejs.render(rawHtml, {
...envVars.htmlInjections,
});
console.log(`
====================${mode}========================
`);
const result = {
target: 'web',
mode,
entry: './app/app-entry/index.ts',
devtool: 'source-map',
resolve: {
extensions: ['.tsx', '.ts', '.jsx', '.js', '...'],
// TODO fix me punycode
fallback: { punycode: require.resolve('punycode/') },
},
resolveLoader: {},
devServer: {
contentBase: path.join(__dirname, 'build'),
disableHostCheck: true,
port: 4000,
host: '0.0.0.0',
historyApiFallback: {
disableDotRule: true,
},
},
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/](prosemirror-[a-z]*|markdown-it|markdown-it-emoji|entities|linkify-it|react|react-dom|react-router|history|@popperjs)[\\/]/,
name: 'vendor',
chunks: 'all',
},
bangledev: {
test: /[\\/]node_modules[\\/](@bangle.dev)[\\/]/,
name: 'bangledev',
chunks: 'all',
},
},
},
},
output: {
filename: '[name].[contenthash].js',
chunkFilename: '[name].bundle.[contenthash].js',
path: buildPath,
publicPath: '/',
},
plugins: [
new webpack.DefinePlugin({
...envVars.appEnvs,
}),
new CaseSensitivePathsPlugin(),
new HtmlWebpackPlugin({
title: 'Bangle',
templateContent: html,
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[id].[contenthash].css',
ignoreOrder: false,
}),
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, 'tooling', 'public'),
to: buildPath,
},
],
}),
// eslint-disable-next-line no-process-env
process.env.NETLIFY
? new webpack.SourceMapDevToolPlugin({
noSources: true,
})
: null,
// new BundleAnalyzerPlugin(),
].filter(Boolean),
module: {
rules: [
// {
// test: /\.tsx?$/,
// use: 'ts-loader',
// exclude: /node_modules/,
// },
{
test: /\.ejs$/,
loader: 'ejs-loader',
options: {
variable: 'data',
},
},
{
test: /\.(png|svg|jpg|gif)$/,
use: ['file-loader'],
},
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.css$/i,
use: [
// 'style-loader',
{
loader: MiniCssExtractPlugin.loader,
options: {},
},
{
loader: 'css-loader',
options: { importLoaders: 1, sourceMap: true },
},
{
loader: 'postcss-loader',
},
],
},
{
test: /\.worker\.(c|m)?ts$/i,
loader: 'worker-loader',
// options: {
// worker: 'Worker',
// },
},
],
},
};
if (process.env.NETLIFY) {
delete result['devtool'];
}
return result;
};