Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/web/browser/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: "Web Python Examples",
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
},
46
resolve: {
47
extensions: [".tsx", ".ts", ".js"],
48
},
49
devServer: {
50
headers: {
51
"Cross-Origin-Opener-Policy": "same-origin",
52
"Cross-Origin-Embedder-Policy": "require-corp",
53
},
54
},
55
};
56
57
// Refactor same code in terminal and webpack packages.
58
if (process.env.COCALC_PROJECT_ID && process.env.NODE_ENV != "production") {
59
const port = 8080;
60
const basePath = `/${process.env.COCALC_PROJECT_ID}/port/${port}/`;
61
// Working in a cocalc project, so do a bit more to support the base path under.
62
module.exports.output.publicPath = basePath;
63
module.exports.devServer.port = port;
64
module.exports.devServer.allowedHosts = "all";
65
module.exports.devServer.host = "0.0.0.0";
66
module.exports.devServer.client = {
67
webSocketURL: `auto://cocalc.com${basePath}ws`,
68
};
69
console.log(`https://cocalc.com${basePath}\n`);
70
}
71
72