Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/web/terminal/webpack.config.js
1391 views
1
/*
2
This is a minimal webpack config file for using python-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 python-wasm
7
uses memfs, which requires several polyfilled libraries.
8
9
- The wasm and zip asset/resource rules are needed so python-wasm
10
can import the python 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 python-wasm */,
29
new HtmlWebpackPlugin({
30
title: "python-wasm Terminal",
31
}),
32
],
33
module: {
34
rules: [
35
{
36
test: /\.wasm$|\.zip$|\.tar.xz$/ /* required for python-wasm */,
37
type: "asset/resource",
38
},
39
{
40
test: /\.tsx?$/,
41
use: "ts-loader",
42
exclude: /node_modules/,
43
},
44
{
45
test: /\.css$/i,
46
use: ["style-loader", "css-loader"],
47
},
48
],
49
},
50
resolve: {
51
extensions: [".tsx", ".ts", ".js"],
52
},
53
devServer: {
54
headers: {
55
"Cross-Origin-Opener-Policy": "same-origin",
56
"Cross-Origin-Embedder-Policy": "require-corp",
57
},
58
},
59
};
60
61
if (process.env.PORT) {
62
console.log(
63
`PORT=${process.env.PORT}, so serving at that port instead of the default.`
64
);
65
module.exports.devServer.port = parseInt(process.env.PORT);
66
}
67
if (process.env.SW) {
68
console.log(
69
"SW variable set, so disabling cross-origin isolation to force fallback to a service worker."
70
);
71
module.exports.devServer.headers = {};
72
}
73
74
// Refactor same code in terminal and webpack packages.
75
if (process.env.COCALC_PROJECT_ID && process.env.NODE_ENV != "production") {
76
const port = module.exports.devServer.port ?? 8080;
77
const basePath = `/${process.env.COCALC_PROJECT_ID}/port/${port}/`;
78
// Working in a cocalc project, so do a bit more to support the base path under.
79
module.exports.output.publicPath = basePath;
80
module.exports.devServer.allowedHosts = "all";
81
module.exports.devServer.host = "0.0.0.0";
82
module.exports.devServer.client = {
83
webSocketURL: `auto://cocalc.com${basePath}ws`,
84
};
85
console.log(`https://cocalc.com${basePath}\n`);
86
}
87
88