Skip to content

Commit 2ba1f02

Browse files
committed
fix(website): plugin usage according to v2
1 parent ea0ebb6 commit 2ba1f02

11 files changed

Lines changed: 75 additions & 189 deletions

File tree

cppjs-plugins/cppjs-plugin-webpack/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,51 @@ export default class CppjsWebpackPlugin {
6868
getFilteredBuildTargets
6969
};
7070
}
71+
72+
getRule() {
73+
return {
74+
test: new RegExp(`\\.(${state.config.ext.header.join('|')})$`),
75+
loader: '@cpp.js/plugin-webpack-loader',
76+
options: { ...this.getLoaderOptions() },
77+
};
78+
}
79+
80+
setDevServerMiddleware(middlewares, devServer) {
81+
if (!devServer) {
82+
throw new Error('devServer is not defined');
83+
}
84+
85+
middlewares.unshift({
86+
name: '/cpp.js',
87+
path: '/cpp.js',
88+
middleware: (req, res) => {
89+
const filePath = `${state.config.paths.build}/${buildTargetDebug.jsName}`;
90+
res.setHeader('Content-Type', 'application/javascript');
91+
fs.createReadStream(filePath).pipe(res);
92+
},
93+
});
94+
95+
middlewares.unshift({
96+
name: '/cpp.wasm',
97+
path: '/cpp.wasm',
98+
middleware: (req, res) => {
99+
const filePath = `${state.config.paths.build}/${buildTargetDebug.wasmName}`;
100+
res.setHeader('Content-Type', 'application/wasm');
101+
fs.createReadStream(filePath).pipe(res);
102+
},
103+
});
104+
105+
return middlewares;
106+
}
107+
108+
getDevServerConfig() {
109+
return {
110+
watchFiles: state.config.paths.native,
111+
hot: true,
112+
liveReload: true,
113+
setupMiddlewares: (middlewares, devServer) => {
114+
return this.setDevServerMiddleware(middlewares, devServer);
115+
},
116+
};
117+
}
71118
}

cppjs-plugins/cppjs-plugin-webpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cpp.js/plugin-webpack",
3-
"version": "2.0.0-beta.12",
3+
"version": "2.0.0-beta.13",
44
"description": "Cpp.js Webpack plugin: A tool for seamless C++ integration with the Webpack bundler.",
55
"homepage": "https://github.com/bugra9/cpp.js/tree/main/packages/cppjs-plugin-rollup#readme",
66
"repository": "https://github.com/bugra9/cpp.js.git",

cppjs-samples/cppjs-sample-web-react-rspack/rspack.config.mjs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ export default defineConfig({
3535
},
3636
module: {
3737
rules: [
38-
{
39-
test: /\.h$/,
40-
loader: '@cpp.js/plugin-webpack-loader',
41-
options: { ...cppjsLoaderOptions },
42-
},
38+
cppjsWebpackPlugin.getRule(),
4339
{
4440
test: /\.svg$/,
4541
type: 'asset',
@@ -78,41 +74,5 @@ export default defineConfig({
7874
}),
7975
isDev && new ReactRefreshRspackPlugin(),
8076
],
81-
devServer: {
82-
watchFiles: {
83-
paths: ['src/**/*'],
84-
options: {
85-
ignored: /node_modules/,
86-
},
87-
},
88-
hot: true,
89-
liveReload: true,
90-
setupMiddlewares: (middlewares, devServer) => {
91-
if (!devServer) {
92-
throw new Error('@rspack/dev-server is not defined');
93-
}
94-
95-
middlewares.unshift({
96-
name: '/cpp.js',
97-
path: '/cpp.js',
98-
middleware: (req, res) => {
99-
const filePath = `${state.config.paths.build}/${buildTargetDebug.jsName}`;
100-
res.setHeader('Content-Type', 'application/javascript');
101-
fs.createReadStream(filePath).pipe(res);
102-
},
103-
});
104-
105-
middlewares.unshift({
106-
name: '/cpp.wasm',
107-
path: '/cpp.wasm',
108-
middleware: (req, res) => {
109-
const filePath = `${state.config.paths.build}/${buildTargetDebug.wasmName}`;
110-
res.setHeader('Content-Type', 'application/wasm');
111-
fs.createReadStream(filePath).pipe(res);
112-
},
113-
});
114-
115-
return middlewares;
116-
},
117-
},
77+
devServer: cppjsWebpackPlugin.getDevServerConfig(),
11878
});

website/docs/guide/integrate-into-existing-project/cloudflare-worker.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You can use cpp.js to compile native code from your project into WebAssembly. To
66
{
77
"name": "myapp",
88
"scripts": {
9-
+ "build": "cppjs build -p wasm",
9+
+ "build": "cppjs build -p wasm -e edge -r st",
1010
"dev": "wrangler dev",
1111
"deploy": "wrangler dev"
1212
},
@@ -63,17 +63,17 @@ This command will generate myapp.wasm, myapp.browser.js, and myapp.node.js files
6363
| └── MySampleClass.h
6464
|
6565
├── dist
66-
│ └── myapp.wasm
67-
| └── myapp.browser.js
68-
| └── myapp.node.js
66+
│ └── myapp-wasm-wasm32-st-release.edge.js
67+
└── myapp-wasm-wasm32-st-release.edge.wasm
68+
|
6969
├── ...
7070
```
7171

72-
You can now access your native code by importing **dist/myapp.browser.js** into your JavaScript file. For a minimal setup, create a index.js and add the following content.
72+
You can now access your native code by importing **dist/myapp-wasm-wasm32-st-release.edge.js** into your JavaScript file. For a minimal setup, create a index.js and add the following content.
7373

7474
```js title="index.js"
75-
import initCppJs from './dist/myapp.browser.js';
76-
import wasmContent from './dist/myapp.wasm';
75+
import initCppJs from './dist/myapp-wasm-wasm32-st-release.edge.js';
76+
import wasmContent from './dist/myapp-wasm-wasm32-st-release.edge.wasm';
7777

7878
const { MySampleClass } = await initCppJs({ getWasmFunction: () => wasmContent });
7979

website/docs/guide/integrate-into-existing-project/create-react-app.md

Lines changed: 0 additions & 86 deletions
This file was deleted.

website/docs/guide/integrate-into-existing-project/nodejs.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You can use cpp.js to compile native code from your project into WebAssembly. To
66
{
77
"name": "myapp",
88
"scripts": {
9-
+ "build": "cppjs build -p wasm"
9+
+ "build": "cppjs build -p wasm -e node -r st"
1010
},
1111
"devDependencies": {
1212
+ "cpp.js": "^2.0.0-beta"
@@ -60,16 +60,15 @@ This command will generate myapp.wasm, myapp.browser.js, and myapp.node.js files
6060
| └── MySampleClass.h
6161
|
6262
├── dist
63-
│ └── myapp.wasm
64-
| └── myapp.browser.js
65-
| └── myapp.node.js
63+
│ └── myapp-wasm-wasm32-st-release.node.js
64+
│ └── myapp-wasm-wasm32-st-release.node.wasm
6665
├── ...
6766
```
6867

69-
You can now access your native code by importing **dist/myapp.node.js** into your JavaScript file. For a minimal setup, create a src/index.js and add the following content.
68+
You can now access your native code by importing **dist/myapp-wasm-wasm32-st-release.node.js** into your JavaScript file. For a minimal setup, create a src/index.js and add the following content.
7069

7170
```js title="src/index.js"
72-
const initCppJs = require('../dist/myapp.node.js');
71+
const initCppJs = require('../dist/myapp-wasm-wasm32-st-release.node.js');
7372

7473
initCppJs().then(({ MySampleClass }) => {
7574
console.log(`Response from c++ : ${MySampleClass.sample()}`);
@@ -86,7 +85,7 @@ node ./src/index.js
8685
For module (ES) usage, create a src/index.mjs and add the following content.
8786

8887
```js title="src/index.mjs"
89-
import initCppJs from '../dist/myapp.node.js';
88+
import initCppJs from '../dist/myapp-wasm-wasm32-st-release.node.js';
9089

9190
initCppJs().then(({ MySampleClass }) => {
9291
console.log(`Matrix multiplier with c++ => ${MySampleClass.sample()}`);

website/docs/guide/integrate-into-existing-project/overview.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ If you are using a bundler you can continue with its page. If not, you can integ
2020
| [Rollup](rollup) | | | |
2121
| [Vite](vite) | | | |
2222
| [Rspack](rspack) |
23-
| [CRA(Create React App)](create-react-app) |
2423
:::warning
2524
Before proceeding, ensure that you have met all the [prerequisites](/docs/guide/getting-started/prerequisites) for setting up a working development environment.
2625
:::

website/docs/guide/integrate-into-existing-project/rspack.md

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,13 @@ To enable the plugin, modify the `rspack.config.mjs` file as shown below.
1717
export default defineConfig({
1818
module: {
1919
rules: [
20-
+ {
21-
+ test: /\.h$/,
22-
+ loader: '@cpp.js/plugin-webpack-loader',
23-
+ options: { compiler },
24-
+ }
20+
+ cppjsWebpackPlugin.getRule(),
2521
]
2622
},
2723
plugins: [
2824
+ cppjsWebpackPlugin,
2925
].filter(Boolean),
30-
+ devServer: {
31-
+ watchFiles: compiler.config.paths.native,
32-
+ setupMiddlewares: (middlewares, devServer) => {
33-
+ if (!devServer) {
34-
+ throw new Error('@rspack/dev-server is not defined');
35-
+ }
36-
+
37-
+ middlewares.unshift({
38-
+ name: '/cpp.js',
39-
+ path: '/cpp.js',
40-
+ middleware: (req, res) => {
41-
+ res.sendFile(`${compiler.config.paths.temp}/${compiler.config.general.name}.browser.js`);
42-
+ },
43-
+ });
44-
+ middlewares.unshift({
45-
+ name: '/cpp.wasm',
46-
+ path: '/cpp.wasm',
47-
+ middleware: (req, res) => {
48-
+ res.send(fs.readFileSync(`${compiler.config.paths.temp}/${compiler.config.general.name}.wasm`));
49-
+ },
50-
+ });
51-
+
52-
+ return middlewares;
53-
+ },
54-
+ },
26+
+ devServer: cppjsWebpackPlugin.getDevServerConfig(),
5527
});
5628
```
5729

website/docs/guide/integrate-into-existing-project/standalone.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ You can use cpp.js to compile native code from your project into WebAssembly. To
1010
{
1111
"name": "myapp",
1212
"scripts": {
13-
+ "build": "cppjs build -p wasm"
13+
+ "build": "cppjs build -p wasm -a wasm32 -r st -e browser -b release"
1414
},
1515
"devDependencies": {
1616
+ "cpp.js": "^2.0.0-beta"
@@ -56,29 +56,28 @@ Before proceeding, ensure that you have met all the [prerequisites](/docs/guide/
5656
npm run build
5757
```
5858

59-
This command will generate myapp.wasm, myapp.browser.js, and myapp.node.js files inside the dist folder.
59+
This command will generate myapp-wasm-wasm32-st-release.browser.js and myapp-wasm-wasm32-st-release.browser.wasm files inside the dist folder.
6060

6161
```
6262
├── src
6363
│ └── native
6464
| └── MySampleClass.h
6565
|
6666
├── dist
67-
│ └── myapp.wasm
68-
| └── myapp.browser.js
69-
| └── myapp.node.js
67+
│ └── myapp-wasm-wasm32-st-release.browser.js
68+
| └── myapp-wasm-wasm32-st-release.browser.wasm
7069
├── ...
7170
```
7271

73-
You can now access your native code by importing **dist/myapp.browser.js** into your JavaScript file. For a minimal setup, create a index.html and add the following content.
72+
You can now access your native code by importing **dist/myapp-wasm-wasm32-st-release.browser.js** into your JavaScript file. For a minimal setup, create a index.html and add the following content.
7473

7574
```html title="index.html"
7675
<!DOCTYPE html>
7776
<html>
7877
<head>
7978
<meta charset = "utf-8">
8079
<title>Cpp.js Vanilla sample</title>
81-
<script src="./dist/myapp.browser.js"></script>
80+
<script src="./dist/myapp-wasm-wasm32-st-release.browser.js"></script>
8281
<script>
8382
initCppJs({ path: './dist' }).then(({ MySampleClass }) => {
8483
document.querySelector('#cppMessage').innerHTML = MySampleClass.sample();
@@ -103,7 +102,7 @@ To add `serve` as a project dependency, follow these steps:
103102
"build": "cppjs build -p wasm",
104103
},
105104
"devDependencies": {
106-
+ "serve": "^14.2.3",
105+
+ "serve": "^14.2.6",
107106
"cpp.js": "^2.0.0-beta"
108107
}
109108
}

website/docs/guide/integrate-into-existing-project/webpack.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ module.exports = {
2020
],
2121
module: {
2222
rules: [
23-
+ {
24-
+ test: /\.h$/,
25-
+ loader: '@cpp.js/plugin-webpack-loader',
26-
+ options: { compiler },
27-
+ }
23+
+ cppjsWebpackPlugin.getRule(),
2824
],
2925
},
26+
+ devServer: cppjsWebpackPlugin.getDevServerConfig(),
3027
};
3128
```
3229

0 commit comments

Comments
 (0)