Skip to content

Commit 4c787f0

Browse files
committed
wip
1 parent f8a9497 commit 4c787f0

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ _pkgroll_ automatically handles native Node.js addons (`.node` files) when you d
255255

256256
```js
257257
// src/index.js
258-
import nativeAddon from './native.node';
258+
import nativeAddon from './native.node'
259259
```
260260

261261
After bundling, the `.node` file will be copied to `dist/natives/` and the import will be automatically rewritten to load from the correct location at runtime.
@@ -264,6 +264,25 @@ After bundling, the `.node` file will be copied to `dist/natives/` and the impor
264264
> - Native modules are platform and architecture-specific. Make sure to distribute the correct `.node` files for your target platforms.
265265
> - This only works with direct `.node` imports. If you're using packages that dynamically load native modules via `bindings` or `node-pre-gyp`, you'll need to handle them separately.
266266
267+
#### Handling dependencies with native modules
268+
269+
If you're using packages with native modules (like `chokidar` which depends on `fsevents`):
270+
271+
- **If in `dependencies`/`peerDependencies`**: ✅ Works automatically - these are externalized (not bundled)
272+
- **If in `devDependencies`**: ⚠️ Will be bundled. If they use `bindings()` or `node-pre-gyp` patterns, move them to `dependencies` instead, or use `optionalDependencies` if they're optional.
273+
274+
Example - if you have `chokidar` in `devDependencies` and get build errors, move it to `dependencies`:
275+
276+
```json
277+
{
278+
"dependencies": {
279+
"chokidar": "^3.0.0"
280+
}
281+
}
282+
```
283+
284+
This externalizes it (users will need to install it), avoiding the need to bundle its native modules.
285+
267286
### Environment variables
268287
Pass in compile-time environment variables with the `--env` flag.
269288

src/rollup/plugins/native-modules.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ const PREFIX = '\0natives:';
1313
export const nativeModules = (
1414
distDirectory: string,
1515
): Plugin => {
16-
const nativeLibsDir = `${distDirectory}/natives`;
16+
const nativeLibsDirectory = `${distDirectory}/natives`;
1717
const copiedModules = new Map<string, string>();
1818

1919
return {
2020
name: 'native-modules',
2121

22-
buildStart() {
22+
buildStart: () => {
2323
// Reset for watch mode
2424
copiedModules.clear();
2525
},
@@ -53,21 +53,21 @@ export const nativeModules = (
5353

5454
// Handle name collisions
5555
while (Array.from(copiedModules.values()).includes(outputName)) {
56-
const ext = path.extname(basename);
57-
const name = path.basename(basename, ext);
58-
outputName = `${name}_${counter}${ext}`;
59-
counter++;
56+
const extension = path.extname(basename);
57+
const name = path.basename(basename, extension);
58+
outputName = `${name}_${counter}${extension}`;
59+
counter += 1;
6060
}
6161

62-
const destPath = path.join(nativeLibsDir, outputName);
62+
const destinationPath = path.join(nativeLibsDirectory, outputName);
6363
const relativePath = `./natives/${outputName}`;
6464

6565
// Store mapping
6666
copiedModules.set(resolvedPath, relativePath);
6767

6868
// Create directory and copy file
69-
fs.mkdirSync(nativeLibsDir, { recursive: true });
70-
fs.copyFileSync(resolvedPath, destPath);
69+
fs.mkdirSync(nativeLibsDirectory, { recursive: true });
70+
fs.copyFileSync(resolvedPath, destinationPath);
7171

7272
// Return prefixed ID for the load hook
7373
return PREFIX + relativePath;

tests/specs/builds/native-modules.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ export default testSuite(({ describe }, nodePath: string) => {
7878
);
7979

8080
const pkgrollProcess = await pkgroll([
81-
'--srcdist', 'src-a:dist-a',
82-
'--srcdist', 'src-b:dist-b',
81+
'--srcdist',
82+
'src-a:dist-a',
83+
'--srcdist',
84+
'src-b:dist-b',
8385
], {
8486
cwd: fixture.path,
8587
nodePath,

0 commit comments

Comments
 (0)