@crossbind/plugin-webpack 2.0.0-beta.60 → 2.0.0-beta.63

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.
Files changed (3) hide show
  1. package/README.md +4 -4
  2. package/index.js +35 -8
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -3,20 +3,20 @@
3
3
  A tool for seamless C++ integration with the Webpack bundler.
4
4
 
5
5
  <a href="https://www.npmjs.com/package/@crossbind/plugin-webpack">
6
- <img alt="NPM version" src="https://img.shields.io/npm/v/@crossbind/plugin-webpack?style=for-the-badge" />
6
+ <img alt="NPM version" src="https://img.shields.io/npm/v/@crossbind/plugin-webpack/beta?style=for-the-badge" />
7
7
  </a>
8
8
  <a href="https://github.com/crossbind/crossbind/blob/main/LICENSE">
9
9
  <img alt="License" src="https://img.shields.io/github/license/crossbind/crossbind?style=for-the-badge" />
10
10
  </a>
11
- <a href="https://crossbind.js.org/docs/guide/integrate-into-existing-project/webpack">
12
- <img alt="Docs - React Native" src="https://img.shields.io/badge/Docs_-_Webpack-20B2AA?style=for-the-badge" />
11
+ <a href="https://crossbind.dev/docs/guide/integrate-into-existing-project/webpack">
12
+ <img alt="Docs - Webpack" src="https://img.shields.io/badge/Docs_-_Webpack-20B2AA?style=for-the-badge" />
13
13
  </a>
14
14
 
15
15
  ## Integration
16
16
  To integrate crossbind into your project using Webpack as a bundler, you can utilize the @crossbind/plugin-webpack plugin. Start by installing these package with the following command:
17
17
 
18
18
  ```sh
19
- npm install @crossbind/plugin-webpack @crossbind/plugin-webpack-loader --save-dev
19
+ npm install @crossbind/plugin-webpack@beta @crossbind/plugin-webpack-loader@beta --save-dev
20
20
  ```
21
21
 
22
22
  **Webpack**
package/index.js CHANGED
@@ -3,6 +3,7 @@ import fs from 'node:fs';
3
3
  import path from 'node:path';
4
4
  import {
5
5
  state, createLib, buildWasm, createBridgeFile, getData, getCrossbindScript, getRustJsScript, getDependFilePath, buildDependencies, getTargetParams, getFilteredBuildTargets, isSourceNewer,
6
+ appSourceFiles,
6
7
  } from 'crossbind';
7
8
 
8
9
  const targetParams = getTargetParams({ platform: ['wasm'], arch: ['wasm32'], runtime: ['st'], runtimeEnv: ['browser'] }, true);
@@ -19,6 +20,8 @@ if (!buildTargetDebug) {
19
20
  buildTargetRelease = buildTargetDebug;
20
21
  }
21
22
 
23
+ const buildTargetFor = (mode) => (mode === 'development' ? buildTargetDebug : buildTargetRelease);
24
+
22
25
  export default class CrossbindWebpackPlugin {
23
26
  static defaultOptions = {};
24
27
 
@@ -38,6 +41,12 @@ export default class CrossbindWebpackPlugin {
38
41
  const marker = getDependFilePath(resource.request, buildTargetRelease);
39
42
  if (marker) resource.request = marker;
40
43
  }).apply(compiler);
44
+ // A dependency's header (`@crossbind/port-zlib/zlib.h`) names the package, not a file: it lives in the
45
+ // package's prebuilt include directory for the target, as the Vite and Metro plugins resolve it.
46
+ new compiler.webpack.NormalModuleReplacementPlugin(new RegExp(`\\.(${state.config.ext.header.join('|')})$`), (resource) => {
47
+ const header = getDependFilePath(resource.request, buildTargetRelease);
48
+ if (header) resource.request = header;
49
+ }).apply(compiler);
41
50
  // Rust packages ship no JS entry at all (their package.json is just a name), so node
42
51
  // resolution can never find them. Each known cargo-type dependency gets an exact-match
43
52
  // alias onto its crate root; the loader's .rs branch turns that into the proxy module.
@@ -48,6 +57,12 @@ export default class CrossbindWebpackPlugin {
48
57
  compiler.options.resolve.alias[`${dep.package.name}$`] = libRs;
49
58
  }
50
59
  }
60
+ // Before any loader runs: a header's bridge reads the include roots of every dependency, conan's staged
61
+ // packages included, and a cargo dependency built for another target only is there but lacks this one
62
+ // until the build makes it.
63
+ const buildDeps = () => buildDependencies({ targetParams: { ...targetParams, buildType: [buildTargetFor(compiler.options.mode).buildType] } });
64
+ compiler.hooks.beforeRun.tapPromise(pluginName, buildDeps);
65
+ compiler.hooks.watchRun.tapPromise(pluginName, buildDeps);
51
66
  // tapPromise (not tap) so webpack awaits the native C++/wasm build and a build
52
67
  // failure surfaces as a compilation error instead of an unhandled rejection.
53
68
  compiler.hooks.done.tapPromise(pluginName, this.onDone.bind(this));
@@ -61,8 +76,7 @@ export default class CrossbindWebpackPlugin {
61
76
 
62
77
  async onDone({ compilation }) {
63
78
  const isDev = compilation.options.mode === 'development';
64
- const buildTarget = isDev ? buildTargetDebug : buildTargetRelease;
65
- await buildDependencies({ targetParams: { ...targetParams, buildType: [buildTarget.buildType] } });
79
+ const buildTarget = buildTargetFor(compilation.options.mode);
66
80
  const force = isSourceNewer(buildTarget);
67
81
  const sourceBuilt = createLib(buildTarget, 'Source', { force, buildSource: true });
68
82
  // Bridge cache is keyed on the nativeGlob fingerprint: a changed bridge
@@ -92,6 +106,7 @@ export default class CrossbindWebpackPlugin {
92
106
  getLoaderOptions() {
93
107
  return {
94
108
  bridges: this.bridges,
109
+ appSourceFiles,
95
110
  createBridgeFile,
96
111
  getData,
97
112
  state,
@@ -116,10 +131,16 @@ export default class CrossbindWebpackPlugin {
116
131
  throw new Error('devServer is not defined');
117
132
  }
118
133
 
134
+ // The boot code asks under webpack's public path, which need not be the root.
135
+ const asksFor = (req, name) => req.url.split('?')[0].endsWith(`/${name}`);
136
+
119
137
  middlewares.unshift({
120
138
  name: '/crossbind.js',
121
- path: '/crossbind.js',
122
- middleware: (req, res) => {
139
+ middleware: (req, res, next) => {
140
+ if (!asksFor(req, 'crossbind.js')) {
141
+ next();
142
+ return;
143
+ }
123
144
  const filePath = `${state.config.paths.build}/${buildTargetDebug.jsName}`;
124
145
  res.setHeader('Content-Type', 'application/javascript');
125
146
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
@@ -130,8 +151,11 @@ export default class CrossbindWebpackPlugin {
130
151
 
131
152
  middlewares.unshift({
132
153
  name: '/crossbind.wasm',
133
- path: '/crossbind.wasm',
134
- middleware: (req, res) => {
154
+ middleware: (req, res, next) => {
155
+ if (!asksFor(req, 'crossbind.wasm')) {
156
+ next();
157
+ return;
158
+ }
135
159
  const filePath = `${state.config.paths.build}/${buildTargetDebug.wasmName}`;
136
160
  res.setHeader('Content-Type', 'application/wasm');
137
161
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
@@ -142,8 +166,11 @@ export default class CrossbindWebpackPlugin {
142
166
 
143
167
  middlewares.unshift({
144
168
  name: '/crossbind.data.txt',
145
- path: '/crossbind.data.txt',
146
- middleware: (req, res) => {
169
+ middleware: (req, res, next) => {
170
+ if (!asksFor(req, 'crossbind.data.txt')) {
171
+ next();
172
+ return;
173
+ }
147
174
  const filePath = `${state.config.paths.build}/${buildTargetDebug.dataTxtName}`;
148
175
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
149
176
  res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crossbind/plugin-webpack",
3
- "version": "2.0.0-beta.60",
3
+ "version": "2.0.0-beta.63",
4
4
  "description": "crossbind Webpack plugin: A tool for seamless C++ integration with the Webpack bundler.",
5
5
  "homepage": "https://github.com/crossbind/crossbind/tree/main/plugins/webpack#readme",
6
6
  "repository": {
@@ -11,8 +11,8 @@
11
11
  "type": "module",
12
12
  "main": "index.js",
13
13
  "dependencies": {
14
- "crossbind": "^2.0.0-beta.60",
15
- "@crossbind/core-embind-rust": "^2.0.0-beta.60"
14
+ "crossbind": "^2.0.0-beta.63",
15
+ "@crossbind/core-embind-rust": "^2.0.0-beta.63"
16
16
  },
17
17
  "peerDependencies": {
18
18
  "webpack": ">=5.0.0"