Newer
Older
/* eslint-env node */
/* eslint-disable import/no-commonjs */
/* eslint-disable import/order */
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const webpack = require("webpack");
const BundleAnalyzerPlugin =
require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const path = require("path");
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
const mainConfig = require("./webpack.config");
const SDK_SRC_PATH = __dirname + "/enterprise/frontend/src/embedding-sdk";
const BUILD_PATH = __dirname + "/resources/embedding-sdk";
// default WEBPACK_BUNDLE to development
const WEBPACK_BUNDLE = process.env.WEBPACK_BUNDLE || "development";
const isDevMode = WEBPACK_BUNDLE !== "production";
// TODO: Reuse babel and css configs from webpack.config.js
// Babel:
const BABEL_CONFIG = {
cacheDirectory: process.env.BABEL_DISABLE_CACHE ? false : ".babel_cache",
};
const CSS_CONFIG = {
modules: {
auto: filename =>
!filename.includes("node_modules") && !filename.includes("vendor.css"),
localIdentName: isDevMode
? "[name]__[local]___[hash:base64:5]"
: "[hash:base64:5]",
},
importLoaders: 1,
};
const shouldAnalyzeBundles = process.env.SHOULD_ANALYZE_BUNDLES === "true";
module.exports = env => {
return {
...mainConfig,
context: SDK_SRC_PATH,
entry: "./index.ts",
output: {
path: BUILD_PATH + "/dist",
publicPath: "",
filename: "[name].bundle.js",
library: {
type: "commonjs2",
},
},
module: {
rules: [
{
test: /\.(tsx?|jsx?)$/,
exclude: /node_modules|cljs/,
use: [{ loader: "babel-loader", options: BABEL_CONFIG }],
},
{
test: /\.(svg|png|eot|woff2?|ttf)$/,
type: "asset/inline",
resourceQuery: { not: [/component|source/] },
},
{
test: /\.css$/,
use: [
{
loader: "style-loader",
},
{ loader: "css-loader", options: CSS_CONFIG },
{ loader: "postcss-loader" },
],
},
{
test: /\.js$/,
exclude: /node_modules/,
enforce: "pre",
use: ["source-map-loader"],
},
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
{
test: /\.svg/,
type: "asset/source",
resourceQuery: /source/, // *.svg?source
},
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
resourceQuery: /component/, // *.svg?component
use: [
{
loader: "@svgr/webpack",
options: {
ref: true,
},
},
],
},
],
},
externals: {
...mainConfig.externals,
react: "react",
"react-dom": "react-dom",
"react/jsx-runtime": "react/jsx-runtime",
},
optimization: !isDevMode
? {
minimizer: [
new TerserPlugin({
minify: TerserPlugin.swcMinify,
parallel: true,
test: /\.(tsx?|jsx?)($|\?)/i,
}),
],
}
: undefined,
plugins: [
new webpack.BannerPlugin({
banner:
"/*\n* This file is subject to the terms and conditions defined in\n * file 'LICENSE.txt', which is part of this source code package.\n */\n",
}),
new NodePolyfillPlugin(), // for crypto, among others
// https://github.com/remarkjs/remark/discussions/903
new webpack.ProvidePlugin({
process: "process/browser.js",
}),
new ForkTsCheckerWebpackPlugin({
async: isDevMode,
typescript: {
configFile: path.resolve(__dirname, "./tsconfig.sdk.json"),
mode: "write-dts",
memoryLimit: 4096,
},
}),
shouldAnalyzeBundles &&
new BundleAnalyzerPlugin({
analyzerMode: "static",
reportFilename: BUILD_PATH + "/dist/report.html",
}),
].filter(Boolean),
};
};