Skip to content

Commit 61885dc

Browse files
authored
Merge pull request #29 from MatrixAI/pkg-integration
Pkg integration
2 parents 6af33a8 + 6d0210a commit 61885dc

16 files changed

+1458
-1143
lines changed

README.md

+66-2
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ $ npm run ts-node
7373

7474
You can also create test files in `./src`, and run them with `npm run ts-node ./src/test.ts`.
7575

76-
This allows you to test individual pieces of typescript code and it makes it easier when doing large scale rearchitecting of TypeScript code.
76+
This allows you to test individual pieces of typescript code, and it makes it easier when doing large scale architecting of TypeScript code.
7777

7878
### Path Aliases
7979

8080
Due to https://github.com/microsoft/TypeScript/issues/10866, you cannot use path aliases without a bundler like Webpack to further transform the generated JavaScript code in order to resolve the path aliases. Because this is a simple library demonstration, there's no need to use a bundler. In fact, for such libraries, it is far more efficient to not bundle the code.
8181

82-
However we have left the path alias configuration in `tsconfig.json`, `jest.config.js` and in the tests we are making use of the `@` alias.
82+
However, we have left the path alias configuration in `tsconfig.json`, `jest.config.js` and in the tests we are making use of the `@` alias.
8383

8484
#### VSCode Path Aliases
8585

@@ -99,6 +99,70 @@ This will however make `tsc` build the `tests` into the `dist` output.
9999

100100
**Therefore this fix should only be done in your own workspace, do not commit or push this change up.**
101101

102+
### Native Module Toolchain
103+
104+
There are some nuances when packaging with native modules.
105+
Included native modules are level witch include leveldown and utp-native.
106+
107+
If a module is not set to public then pkg defaults to including it as bytecode.
108+
To avoid this breaking with the `--no-bytecode` flag we need to add `--public-packages "*"`
109+
110+
#### leveldown
111+
112+
To get leveldown to work with pkg we need to include the prebuilds with the executable.
113+
after building with pkg you need to copy from `node_modules/leveldown/prebuilds` -> `path_to_executable/prebuilds`
114+
You only need to include the prebuilds for the arch you are targeting. e.g. for linux-x64 you need `prebuild/linux-x64`.
115+
116+
The folder structure for the executable should look like this.
117+
- linux_executable_elf
118+
- prebuilds
119+
- linux-x64
120+
- (node files)
121+
122+
#### utp-native
123+
124+
Including utp-native is simpler, you just need to add it as an asset for pkg.
125+
Add the following lines to the package.json.
126+
```json
127+
"pkg": {
128+
"assets": "node_modules/utp-native/**/*"
129+
}
130+
```
131+
132+
#### threads.js
133+
134+
To make sure that the worker threads work properly you need to include the compiled worker scripts as an asset.
135+
This can be fixed by adding the following to `package.json`
136+
137+
```json
138+
"pkg": {
139+
"assets": "dist/bin/worker.js"
140+
}
141+
```
142+
143+
If you need to include multiple assets then add them as an array.
144+
145+
```json
146+
"pkg": {
147+
"assets": [
148+
"node_modules/utp-native/**/*",
149+
"dist/bin/worker.js"
150+
]
151+
}
152+
```
153+
154+
#### Integration into Nix
155+
156+
Nix build uses node2nix to create the dependencies of the node modules. this does a fairly good job of detecting dependencies but with native modules it may fail to detect CLI tools like `node-gyp-build`.
157+
158+
To ensure the proper dependencies exist while building we need to bring in these utilities during the build:
159+
160+
```
161+
buildInputs = attrs.buildInputs ++ [ nodePackages.node-gyp-build ];
162+
```
163+
164+
This has to be done to both `node2nixProd` and `node2nixDev`.
165+
102166
### Docs Generation
103167

104168
```sh

nix/leveldown.patch

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--- dictionary/leveldown.js 2021-08-04 15:43:31.836337623 +1000
2+
+++ dictionary/leveldown_.js 2021-08-04 15:43:11.977266316 +1000
3+
@@ -1,10 +1,3 @@
4+
'use strict';
5+
6+
-module.exports = {
7+
- pkg: {
8+
- patches: {
9+
- 'binding.js': ['__dirname', "require('path').dirname(process.execPath)"],
10+
- },
11+
- deployFiles: [['prebuilds', 'prebuilds', 'directory']],
12+
- },
13+
-};
14+
+module.exports = {};

0 commit comments

Comments
 (0)