forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.embedding-sdk.config.js
186 lines (165 loc) · 4.86 KB
/
webpack.embedding-sdk.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
/* 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 mainConfig = require("./webpack.config");
const { resolve } = require("path");
const fs = require("fs");
const path = require("path");
const SDK_SRC_PATH = __dirname + "/enterprise/frontend/src/embedding-sdk";
const BUILD_PATH = __dirname + "/resources/embedding-sdk";
const ENTERPRISE_SRC_PATH =
__dirname + "/enterprise/frontend/src/metabase-enterprise";
// default WEBPACK_BUNDLE to development
const WEBPACK_BUNDLE = process.env.WEBPACK_BUNDLE || "development";
const isDevMode = WEBPACK_BUNDLE !== "production";
const sdkPackageTemplateJson = fs.readFileSync(
path.resolve("./enterprise/frontend/src/embedding-sdk/package.template.json"),
"utf-8",
);
const sdkPackageTemplateJsonContent = JSON.parse(sdkPackageTemplateJson);
const EMBEDDING_SDK_VERSION = JSON.stringify(
sdkPackageTemplateJsonContent.version,
);
// 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 => {
const config = {
...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)$/,
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"],
},
{
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 webpack.EnvironmentPlugin({
EMBEDDING_SDK_VERSION,
IS_EMBEDDING_SDK_BUILD: true,
}),
new ForkTsCheckerWebpackPlugin({
async: isDevMode,
typescript: {
configFile: resolve(__dirname, "./tsconfig.sdk.json"),
mode: "write-dts",
memoryLimit: 4096,
},
}),
shouldAnalyzeBundles &&
new BundleAnalyzerPlugin({
analyzerMode: "static",
reportFilename: BUILD_PATH + "/dist/report.html",
}),
].filter(Boolean),
};
config.resolve.alias = {
...mainConfig.resolve.alias,
"ee-plugins": ENTERPRISE_SRC_PATH + "/plugins",
"ee-overrides": ENTERPRISE_SRC_PATH + "/overrides",
};
if (config.cache) {
config.cache.cacheDirectory = resolve(
__dirname,
"node_modules/.cache/",
"webpack-ee",
);
}
return config;
};