@cpp.js/plugin-webpack 2.0.0-beta.13 → 2.0.0-beta.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +89 -0
- package/LICENSE +1 -1
- package/index.js +15 -4
- package/package.json +2 -2
package/AGENTS.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# AGENTS.md — @cpp.js/plugin-webpack
|
|
2
|
+
|
|
3
|
+
> Webpack **and Rspack** integration plugin. Same package serves both — Rspack reuses the Webpack plugin contract.
|
|
4
|
+
|
|
5
|
+
## What lives here
|
|
6
|
+
|
|
7
|
+
- `index.js` — exports `class CppjsWebpackPlugin`. Single class, no separate kernel.
|
|
8
|
+
- Sibling package `@cpp.js/plugin-webpack-loader` ships the loader for `.h` files; `getRule()` references it by name.
|
|
9
|
+
|
|
10
|
+
## Class API surface
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
new CppjsWebpackPlugin()
|
|
14
|
+
↓
|
|
15
|
+
.apply(compiler) ← webpack standard plugin hook
|
|
16
|
+
.getRule() ← module.rules entry for .h
|
|
17
|
+
.getDevServerConfig() ← devServer block w/ COOP+COEP + middleware
|
|
18
|
+
.getLoaderOptions() ← escape hatch: cpp.js state passed to other rules
|
|
19
|
+
.setDevServerMiddleware(...) ← internal, called by getDevServerConfig
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
User config consumes all four; see `docs/playbooks/integration/webpack-rspack.md` for the canonical wiring.
|
|
23
|
+
|
|
24
|
+
## Hooks the plugin registers
|
|
25
|
+
|
|
26
|
+
| webpack hook | What it does |
|
|
27
|
+
|--------------|--------------|
|
|
28
|
+
| `compiler.hooks.done` | After every compilation: re-run `createLib + createXCFramework + buildWasm` (mtime-aware via `isSourceNewer`) |
|
|
29
|
+
| `compiler.hooks.afterCompile` | Adds `state.config.paths.native` to `compilation.contextDependencies` so webpack watch picks up `.cpp`/`.h` edits |
|
|
30
|
+
|
|
31
|
+
## DevServer middleware
|
|
32
|
+
|
|
33
|
+
`getDevServerConfig()` returns:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
{
|
|
37
|
+
watchFiles: state.config.paths.native,
|
|
38
|
+
hot: true,
|
|
39
|
+
liveReload: true,
|
|
40
|
+
headers: {
|
|
41
|
+
'Cross-Origin-Opener-Policy': 'same-origin',
|
|
42
|
+
'Cross-Origin-Embedder-Policy': 'require-corp',
|
|
43
|
+
},
|
|
44
|
+
setupMiddlewares: (middlewares, devServer) => { ... }
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The middleware adds `/cpp.js` and `/cpp.wasm` routes that pipe the freshly built artifacts. Both routes also re-emit COOP/COEP per-response (belt-and-suspenders for caching layers that strip top-level headers).
|
|
49
|
+
|
|
50
|
+
## Rspack compatibility
|
|
51
|
+
|
|
52
|
+
Rspack copies webpack's plugin / loader shape. The plugin works without modification. The Rspack-specific wrinkle: `getRule()` returns a plain object that Rspack's TypeScript-strict config types may complain about — cast to `RspackOptions['module']['rules'][number]` if needed.
|
|
53
|
+
|
|
54
|
+
## Common pitfalls
|
|
55
|
+
|
|
56
|
+
- **Forgetting `getRule()` while passing the plugin instance.** The `.h` loader rule and the plugin instance are independent registrations; both are required.
|
|
57
|
+
- **Wrapping the plugin's `getDevServerConfig()` and dropping `headers`.** Always spread: `{ ...cppDev, ...custom, headers: { ...cppDev.headers, ...custom.headers } }`.
|
|
58
|
+
- **Trying to inject `getRule()` into `plugins:` array** instead of `module.rules:`. They are distinct.
|
|
59
|
+
- **Babel/SWC running on `.h` first.** Loader chain order matters; `getRule()` test is `/\.h$/` so other rules with broader tests can swallow it. Make sure no rule for `/\.[jt]sx?$/` accidentally matches `.h` (none do by default).
|
|
60
|
+
- **Adding `console.log` for debugging.** Route through `cpp.js` exports — `state.config` carries the logger if you need it. Plugin output should be quiet by default.
|
|
61
|
+
|
|
62
|
+
## Validation
|
|
63
|
+
|
|
64
|
+
Strict gate:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pnpm run ci:linux:build && pnpm run e2e:dev && pnpm run e2e:prod
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Smoke samples:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pnpm --filter=@cpp.js/sample-web-react-rspack run build
|
|
74
|
+
pnpm --filter=@cpp.js/playground-web-rspack run build
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
If you touch the dev-server middleware, also test:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pnpm --filter=@cpp.js/sample-web-react-rspack exec rspack serve
|
|
81
|
+
# verify in browser: COOP/COEP headers present, /cpp.wasm 200
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Reference
|
|
85
|
+
|
|
86
|
+
- Loader package: `cppjs-plugins/cppjs-plugin-webpack-loader/`
|
|
87
|
+
- Integration recipe: `docs/playbooks/integration/webpack-rspack.md`
|
|
88
|
+
- Next.js variant (no devServer): `docs/playbooks/integration/nextjs.md`
|
|
89
|
+
- Canonical samples: `cppjs-sample-web-react-rspack`, `cppjs-playground-web-rspack`
|
package/LICENSE
CHANGED
package/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
import fs from 'node:fs';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
state, createLib, buildWasm, createBridgeFile, getData, getCppJsScript, getTargetParams, getFilteredBuildTargets, isSourceNewer,
|
|
5
|
+
} from 'cpp.js';
|
|
4
6
|
|
|
5
7
|
const targetParams = getTargetParams({ platform: ['wasm'], arch: ['wasm32'], runtime: ['st'], runtimeEnv: ['browser'] }, true);
|
|
6
8
|
let buildTargetRelease = getFilteredBuildTargets(targetParams, { buildType: 'release' })?.[0];
|
|
@@ -38,9 +40,10 @@ export default class CppjsWebpackPlugin {
|
|
|
38
40
|
async onDone({ compilation }) {
|
|
39
41
|
const isDev = compilation.options.mode === 'development';
|
|
40
42
|
const buildTarget = isDev ? buildTargetDebug : buildTargetRelease;
|
|
41
|
-
|
|
42
|
-
createLib(buildTarget, '
|
|
43
|
-
|
|
43
|
+
const force = isSourceNewer(buildTarget);
|
|
44
|
+
createLib(buildTarget, 'Source', { force, buildSource: true });
|
|
45
|
+
createLib(buildTarget, 'Bridge', { force, buildSource: false, nativeGlob: [`${state.config.paths.cli}/assets/cpp-runtime/commonBridges.cpp`, ...this.bridges] });
|
|
46
|
+
await buildWasm(buildTarget, { force });
|
|
44
47
|
if (!isDev) {
|
|
45
48
|
const output = state.config.paths.output === state.config.paths.build ? compilation.options.output.path : state.config.paths.output;
|
|
46
49
|
fs.copyFileSync(`${state.config.paths.build}/${buildTarget.jsName}`, `${output}/cpp.js`);
|
|
@@ -88,6 +91,8 @@ export default class CppjsWebpackPlugin {
|
|
|
88
91
|
middleware: (req, res) => {
|
|
89
92
|
const filePath = `${state.config.paths.build}/${buildTargetDebug.jsName}`;
|
|
90
93
|
res.setHeader('Content-Type', 'application/javascript');
|
|
94
|
+
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
|
|
95
|
+
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
|
|
91
96
|
fs.createReadStream(filePath).pipe(res);
|
|
92
97
|
},
|
|
93
98
|
});
|
|
@@ -98,6 +103,8 @@ export default class CppjsWebpackPlugin {
|
|
|
98
103
|
middleware: (req, res) => {
|
|
99
104
|
const filePath = `${state.config.paths.build}/${buildTargetDebug.wasmName}`;
|
|
100
105
|
res.setHeader('Content-Type', 'application/wasm');
|
|
106
|
+
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
|
|
107
|
+
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
|
|
101
108
|
fs.createReadStream(filePath).pipe(res);
|
|
102
109
|
},
|
|
103
110
|
});
|
|
@@ -110,6 +117,10 @@ export default class CppjsWebpackPlugin {
|
|
|
110
117
|
watchFiles: state.config.paths.native,
|
|
111
118
|
hot: true,
|
|
112
119
|
liveReload: true,
|
|
120
|
+
headers: {
|
|
121
|
+
'Cross-Origin-Opener-Policy': 'same-origin',
|
|
122
|
+
'Cross-Origin-Embedder-Policy': 'require-corp',
|
|
123
|
+
},
|
|
113
124
|
setupMiddlewares: (middlewares, devServer) => {
|
|
114
125
|
return this.setDevServerMiddleware(middlewares, devServer);
|
|
115
126
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cpp.js/plugin-webpack",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.21",
|
|
4
4
|
"description": "Cpp.js Webpack plugin: A tool for seamless C++ integration with the Webpack bundler.",
|
|
5
5
|
"homepage": "https://github.com/bugra9/cpp.js/tree/main/packages/cppjs-plugin-rollup#readme",
|
|
6
6
|
"repository": "https://github.com/bugra9/cpp.js.git",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "index.js",
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"cpp.js": "^2.0.0-beta.
|
|
11
|
+
"cpp.js": "^2.0.0-beta.21"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"cpp.js-plugin",
|