Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/web/cowasm.sh/webpack.config.js
1067 views
1
/*
2
This is a minimal webpack config file for using dash-wasm in a frontend
3
Javascript project that uses WebPack 5 (and Typescript). There are two
4
small things that you must:
5
6
- The NodePolyfillPlugin is needed because dash-wasm
7
uses memfs, which requires several polyfilled libraries.
8
9
- The wasm and zip asset/resource rules are needed so dash-wasm
10
can import the dash wasm binary and zip filesystem.
11
12
*/
13
14
const { resolve } = require("path");
15
const HtmlWebpackPlugin = require("html-webpack-plugin");
16
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
17
18
module.exports = {
19
mode: process.env.NODE_ENV == "production" ? "production" : "development",
20
entry: "./src/index.ts",
21
devtool: "inline-source-map",
22
output: {
23
filename: "[name].bundle.js",
24
path: resolve(__dirname, "dist"),
25
clean: true,
26
},
27
plugins: [
28
new NodePolyfillPlugin() /* required for dash-wasm */,
29
new HtmlWebpackPlugin({ title: "CoWasm Shell" }),
30
],
31
module: {
32
rules: [
33
{
34
test: /\.wasm$|\.zip$|\.tar.xz|\.ico$/ /* required for python-wasm */,
35
type: "asset/resource",
36
},
37
{
38
test: /\.tsx?$/,
39
use: "ts-loader",
40
exclude: /node_modules/,
41
},
42
{
43
test: /\.css$/i,
44
use: ["style-loader", "css-loader"],
45
},
46
],
47
},
48
resolve: {
49
extensions: [".tsx", ".ts", ".js"],
50
},
51
devServer: {
52
headers: {
53
"Cross-Origin-Opener-Policy": "same-origin",
54
"Cross-Origin-Embedder-Policy": "require-corp",
55
},
56
},
57
};
58
59
if (process.env.PORT) {
60
console.log(
61
`PORT=${process.env.PORT}, so serving at that port instead of the default.`
62
);
63
module.exports.devServer.port = parseInt(process.env.PORT);
64
}
65
if (process.env.SW) {
66
console.log(
67
"SW variable set, so disabling cross-origin isolation to force fallback to a service worker."
68
);
69
module.exports.devServer.headers = {};
70
}
71
72
// Refactor same code in terminal and webpack packages.
73
if (process.env.COCALC_PROJECT_ID && process.env.NODE_ENV != "production") {
74
const port = module.exports.devServer.port ?? 8080;
75
const basePath = `/${process.env.COCALC_PROJECT_ID}/port/${port}/`;
76
// Working in a cocalc project, so do a bit more to support the base path under.
77
module.exports.output.publicPath = basePath;
78
module.exports.devServer.allowedHosts = "all";
79
module.exports.devServer.host = "0.0.0.0";
80
module.exports.devServer.client = {
81
webSocketURL: `auto://cocalc.com${basePath}ws`,
82
};
83
console.log(`https://cocalc.com${basePath}\n`);
84
}
85
86