Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/web/cowasm.org/webpack.config.js
1067 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
const mode =
18
process.env.NODE_ENV == "production" ? "production" : "development";
19
20
module.exports = {
21
mode,
22
entry: "./src/index.ts",
23
devtool: "inline-source-map",
24
output: {
25
filename: "[name].bundle.js",
26
path: resolve(__dirname, "dist"),
27
clean: true,
28
filename:
29
mode == "production" ? "[name]-[chunkhash].js" : "[id]-[chunkhash].js",
30
chunkFilename:
31
mode == "production" ? "[chunkhash].js" : "[id]-[chunkhash].js",
32
hashFunction: "sha256",
33
},
34
plugins: [
35
new NodePolyfillPlugin() /* required for python-wasm */,
36
new HtmlWebpackPlugin({
37
title: "CoWasm: Collaborative WebAssembly for Servers and Browsers",
38
}),
39
],
40
module: {
41
rules: [
42
{
43
test: /\.wasm$|\.zip$|\.tar.xz$|\.ico$/ /* required for python-wasm */,
44
type: "asset/resource",
45
},
46
{
47
test: /\.tsx?$/,
48
use: "ts-loader",
49
exclude: /node_modules/,
50
},
51
{
52
test: /\.css$/i,
53
use: ["style-loader", "css-loader"],
54
},
55
],
56
},
57
resolve: {
58
extensions: [".tsx", ".ts", ".js"],
59
},
60
devServer: {
61
headers: {
62
"Cross-Origin-Opener-Policy": "same-origin",
63
"Cross-Origin-Embedder-Policy": "require-corp",
64
},
65
},
66
};
67
68
// Refactor same code in terminal and webpack packages.
69
if (process.env.COCALC_PROJECT_ID && process.env.NODE_ENV != "production") {
70
const port = 8080;
71
const basePath = `/${process.env.COCALC_PROJECT_ID}/port/${port}/`;
72
// Working in a cocalc project, so do a bit more to support the base path under.
73
module.exports.output.publicPath = basePath;
74
module.exports.devServer.port = port;
75
module.exports.devServer.allowedHosts = "all";
76
module.exports.devServer.host = "0.0.0.0";
77
module.exports.devServer.client = {
78
webSocketURL: `auto://cocalc.com${basePath}ws`,
79
};
80
console.log(`https://cocalc.com${basePath}\n`);
81
}
82
83