You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When calling executables in development, use this style:
58
-
59
-
```
60
-
npm run typescript-demo-lib -- p1 p2 p3
61
-
```
62
-
63
-
The `--` is necessary to make `npm` understand that the parameters are for your own executable, and not parameters to `npm`.
64
-
65
-
### Using the REPL
66
-
67
-
```
68
-
$ npm run ts-node
69
-
> import fs from 'fs';
70
-
> fs
71
-
> import { Library } from '@';
72
-
> Library
73
-
> import Library as Library2 from './src/lib/Library';
74
-
```
75
-
76
-
You can also create test files in `./src`, and run them with `npm run ts-node ./src/test.ts`.
77
-
78
-
This allows you to test individual pieces of typescript code, and it makes it easier when doing large scale architecting of TypeScript code.
79
-
80
-
### Path Aliases
81
-
82
-
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.
83
-
84
-
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.
85
-
86
-
### Native Module Toolchain
87
-
88
-
There are some nuances when packaging with native modules.
89
-
Included native modules are level witch include leveldown and utp-native.
90
-
91
-
If a module is not set to public then pkg defaults to including it as bytecode.
92
-
To avoid this breaking with the `--no-bytecode` flag we need to add `--public-packages "*"`
93
-
94
-
#### leveldown
95
-
96
-
To get leveldown to work with pkg we need to include the prebuilds with the executable.
97
-
after building with pkg you need to copy from `node_modules/leveldown/prebuilds` -> `path_to_executable/prebuilds`
98
-
You only need to include the prebuilds for the arch you are targeting. e.g. for linux-x64 you need `prebuild/linux-x64`.
99
-
100
-
The folder structure for the executable should look like this.
101
-
- linux_executable_elf
102
-
- prebuilds
103
-
- linux-x64
104
-
- (node files)
105
-
106
-
#### utp-native
107
-
108
-
Including utp-native is simpler, you just need to add it as an asset for pkg.
109
-
Add the following lines to the package.json.
110
-
```json
111
-
"pkg": {
112
-
"assets": "node_modules/utp-native/**/*"
113
-
}
114
-
```
115
-
116
-
#### threads.js
117
-
118
-
To make sure that the worker threads work properly you need to include the compiled worker scripts as an asset.
119
-
This can be fixed by adding the following to `package.json`
120
-
121
-
```json
122
-
"pkg": {
123
-
"assets": "dist/bin/worker.js"
124
-
}
125
-
```
126
-
127
-
If you need to include multiple assets then add them as an array.
128
-
129
-
```json
130
-
"pkg": {
131
-
"assets": [
132
-
"node_modules/utp-native/**/*",
133
-
"dist/bin/worker.js"
134
-
]
135
-
}
136
-
```
137
-
138
-
#### Integration into Nix
139
-
140
-
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`.
141
-
142
-
To ensure the proper dependencies exist while building we need to bring in these utilities during the build:
0 commit comments