Minor edits, mostly cosmetic

This commit is contained in:
Stephan Schreiber
2024-09-18 17:57:48 +02:00
parent a68a00060b
commit 01d08dd125
5 changed files with 43 additions and 15 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
#### > Use the lib in your code
1. Install the lib in your project: `npm install libwin32`
1. Import the functions and types you need. You may either import from `libwin32` or from `libwin32/<dllname>` (without the `.dll` extension) if you know which dll a function beglons to. Currently, only two dlls are available: `kernel32` and `user32`.
1. Import the functions and types you need. You may either import from `libwin32` or from `libwin32/<dllname>` (without the `.dll` extension) if you know which dll a function belongs to. For example, `import { MessageBox } from 'libwin32'` and `import { MessageBox } from 'libwin32/user2'` will both work.
1. (optional) Import some complementary constants. They greatly simplify calls to the Win32 API.
* All constants are available via the `libwin32/consts` import.
* Logically grouped constants are exported as `enum`s, where the prefix is the name of the enum. For instance, `WM_DESTROY` and `WM_KEYDOWN` are exported as `WM_.DESTROY` and `WM_.KEYDOWN`, respectively.
-1
View File
@@ -24,7 +24,6 @@ function makeConfig(which) {
output: {
file: `demos/${which}/${which}.js`,
format: 'esm',
assetFileNames: 'assets/[name].[ext]',
generatedCode: {
preset: 'es2015',
symbols: false
+30 -4
View File
@@ -3,15 +3,40 @@ import MagicString from 'magic-string'
import type { Plugin } from 'rollup'
/**
* A plugin that rewrites TypeScript's `enum`s into a tree-shakeable
* A plugin that rewrites libwin32's enums into a tree-shakeable
* form --the same as esbuid.
*
* TypeScript:
* ````
* export enum X { a, b=10, c }
* ````
*
* tsc:
* ````
* export var X;
* (function (X) {
* X[X["a"] = 0] = "a";
* X[X["b"] = 10] = "b";
* X[X["c"] = 11] = "c";
* })(GA_ || (GA_ = {}));
* ````
*
* this plugin:
* ````
* export var X=(X=>{ // Note: there is a #__PURE__ annotation that can't be shown here
* X[X["a"] = 0] = "a";
* X[X["b"] = 10] = "b";
* X[X["c"] = 11] = "c";
* return X;
* })(X || {});
* ````
*/
export function enums(): Plugin {
const constsBase = fileURLToPath(import.meta.resolve('#consts-base'))
// https://regex101.com/r/1nyPC3/3
const enumRx = /\b(?<intro>(?:export\s+)?var\s+(?<name>[^;\s]+);?\s*\(function\s*\(\k<name>\)\s*{).*(?<outro>}\)\(\k<name>\s*\|\|\s*\(\k<name>\s*=\s{}\)\);?)/gsmd
const enumRx = /\b(?<intro>export\s+var\s+(?<name>[^;\s]+);?\s*\(function\s*\(\k<name>\)\s*{).*(?<outro>}\)\(\k<name>\s*\|\|\s*\(\k<name>\s*=\s{}\)\);?)/gsmd
return {
name: 'libwin32-tree-skakeable-enums',
@@ -28,12 +53,13 @@ export function enums(): Plugin {
const indices = match.indices!.groups
ms ??= new MagicString(code)
ms.update(indices!.intro[0], indices!.intro[1], `export var ${varName}= /*@__PURE__*/(${varName} => {`)
ms.update(indices!.intro[0], indices!.intro[1], `export var ${varName}=/*@__PURE__*/(${varName}=>{`)
ms.update(indices!.outro[0], indices!.outro[1], `${ms.getIndentString()}return ${varName};\n})(${varName} || {});`)
}
if (ms) {
ms.replace(/\/\/#\s*sourceMappingURL=(?:.*).map\s*$/, '')
if (match = /\/\/#\s*sourceMappingURL=(?:.*).map\s*$/d.exec(code))
ms.remove(match.indices![0][0], match.indices![0][1])
return {
code: ms.toString(),
map: ms.generateMap()
+11 -8
View File
@@ -29,7 +29,7 @@ export function koffi(): Plugin {
// Make sure Koffi's installed and accessible.
const err = await fs.access(koffiSource, fs.constants.R_OK).catch((err: NodeJS.ErrnoException) => err)
if (err instanceof Error)
if (err)
this.error({ message: `Cannot access "${koffiSource}": ${err.code}. Did you install koffi as a dependency?`, stack: undefined })
},
@@ -51,7 +51,7 @@ export function koffi(): Plugin {
const outFile = path.join(outDir, koffi_win32_x64)
let err = await fs.copyFile(koffiSource, outFile).catch((err: NodeJS.ErrnoException) => err)
if (err)
this.error({ message: `Cannot write "${outFile}": ${err.code}.`, stack: undefined })
this.error({ message: `Cannot write binary: ${err.code}.`, stack: undefined })
// Write index.cjs
err = await fs.writeFile(
@@ -62,12 +62,15 @@ export function koffi(): Plugin {
this.error({ message: `Cannot write index.cjs: ${err.code}.`, stack: undefined })
// Write package.json
err = await fs.writeFile(path.join(outDir, 'package.json'), JSON.stringify({
name: 'koffi',
description: `koffi binary for ${platform} ${arch} systems`,
type: 'commonjs',
main: './index.cjs'
}, undefined, 2)).catch((err: NodeJS.ErrnoException) => err)
err = await fs.writeFile(
path.join(outDir, 'package.json'),
JSON.stringify({
name: 'koffi',
description: `koffi binary for ${platform} ${arch} systems`,
type: 'commonjs',
main: './index.cjs'
}, undefined, 2)
).catch((err: NodeJS.ErrnoException) => err)
if (err)
this.error({ message: `Cannot write package.json: ${err.code}.`, stack: undefined })
}
+1 -1
View File
@@ -17,7 +17,7 @@
// Output
"outDir": "dist",
"target": "ESNext",
"target": "ES2023",
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,