Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/web/browser/webpack-min.config.js
1067 views
1
/*
2
This is an attempt to make the bundle
3
smaller by enabling measuring, dangerously removing some node
4
polyfills, etc. It doesn't help much.
5
6
The Javascript download is about about 1.8MB gziped, and with these
7
optimizations it gets it down to about 1.6MB.
8
9
For comparison, emscripten's Javascript shipped with pyodide seems
10
to be on the order of 0.3MB.
11
12
The difference is that we use a bunch of standard modules, e.g., memfs,
13
unionfs, modules from node.js for buffers, process, streams, events, etc.
14
This is all very nice, documented, modern, and fits into a big ecosystem.
15
But it is also 1MB more Javascript. It's a tradeoff.
16
*/
17
18
const { resolve } = require("path");
19
const HtmlWebpackPlugin = require("html-webpack-plugin");
20
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
21
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
22
23
const NODE_MODULES = [
24
"Buffer",
25
"buffer",
26
"events",
27
"path",
28
"process",
29
"stream",
30
"util",
31
];
32
33
module.exports = {
34
mode: process.env.NODE_ENV == "production" ? "production" : "development",
35
entry: "./src/index.ts",
36
devtool: "inline-source-map",
37
output: {
38
filename: "[name].bundle.js",
39
path: resolve(__dirname, "dist"),
40
clean: true,
41
},
42
plugins: [
43
new NodePolyfillPlugin({
44
includeAliases: NODE_MODULES,
45
}) /* required for python-wasm */,
46
new HtmlWebpackPlugin({
47
title: "Web Python Examples",
48
}),
49
new BundleAnalyzerPlugin({
50
analyzerMode: "static",
51
reportFilename: "measure.html",
52
}),
53
],
54
module: {
55
rules: [
56
{
57
test: /\.wasm$|\.zip$/ /* required for python-wasm */,
58
type: "asset/resource",
59
},
60
{
61
test: /\.tsx?$/,
62
use: "ts-loader",
63
exclude: /node_modules/,
64
},
65
],
66
},
67
resolve: {
68
alias: { assert: false, url: false },
69
extensions: [".tsx", ".ts", ".js"],
70
},
71
devServer: {
72
headers: {
73
"Cross-Origin-Opener-Policy": "same-origin",
74
"Cross-Origin-Embedder-Policy": "require-corp",
75
},
76
},
77
};
78
79
// Refactor same code in terminal and webpack packages.
80
if (process.env.COCALC_PROJECT_ID && process.env.NODE_ENV != "production") {
81
const port = 8080;
82
const basePath = `/${process.env.COCALC_PROJECT_ID}/port/${port}/`;
83
// Working in a cocalc project, so do a bit more to support the base path under.
84
module.exports.output.publicPath = basePath;
85
module.exports.devServer.port = port;
86
module.exports.devServer.allowedHosts = "all";
87
module.exports.devServer.host = "0.0.0.0";
88
module.exports.devServer.client = {
89
webSocketURL: `auto://cocalc.com${basePath}ws`,
90
};
91
console.log(`https://cocalc.com${basePath}\n`);
92
}
93
94