Skip to content

Commit 301f530

Browse files
authored
Merge pull request #8 from vite-plugin/v2.0.1
V2.0.1
2 parents 18fe62a + 43caa77 commit 301f530

File tree

9 files changed

+58
-10
lines changed

9 files changed

+58
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.0.1 (2023-06-20)
2+
3+
- f80f4c9 feat(test): add `better-sqlite3`
4+
- 1716007 fix: reserve `.Interop.mjs` for build-watch mode
5+
16
## 2.0.0 (2023-06-20)
27

38
**Break**. This will be a completely new version.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-native",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Supports Node/Electron C/C++ native addons",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -30,7 +30,9 @@
3030
"webpack": "^5.70.0"
3131
},
3232
"devDependencies": {
33+
"@types/better-sqlite3": "^7.6.10",
3334
"@types/node": "^20.14.2",
35+
"better-sqlite3": "^11.0.0",
3436
"fsevents": "^2.3.3",
3537
"serialport": "^12.0.0",
3638
"sqlite3": "^5.1.7",

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default function native(options: NativeOptions): Plugin {
3434
const assetsDir = options.assetsDir ??= 'node_natives'
3535
const nativesMap = new Map<string, {
3636
built: boolean
37+
nativeFilename: string
3738
interopFilename: string
3839
}>
3940
// Webpack output(absolute path)
@@ -60,6 +61,7 @@ export default function native(options: NativeOptions): Plugin {
6061
options.natives.length && ensureDir(output)
6162

6263
for (const native of options.natives) {
64+
const nativeFilename = path.join(output, native + NativeExt)
6365
const interopFilename = path.join(output, native + InteropExt)
6466

6567
aliases.push({
@@ -76,7 +78,7 @@ export default function native(options: NativeOptions): Plugin {
7678
// because `build.emptyOutDir = true` will cause the built file to be removed.
7779

7880
// Collect modules that are explicitly used.
79-
nativesMap.set(native, { built: false, interopFilename })
81+
nativesMap.set(native, { built: false, nativeFilename, interopFilename })
8082
}
8183

8284
return { id: source }
@@ -104,7 +106,6 @@ export default function native(options: NativeOptions): Plugin {
104106
try {
105107
await webpackBundle(native, output, options.webpack)
106108
info.built = true
107-
fs.rmSync(info.interopFilename, { force: true })
108109
} catch (error: any) {
109110
console.error(`\n${TAG}`, error)
110111
process.exit(1)

test/fixtures/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/dist-native
2-
db.sqlite3
2+
/db

test/fixtures/better-sqlite3.db

Whitespace-only changes.

test/fixtures/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// TODO: https://github.com/serialport/node-serialport/issues/2464
22
// import * as serialport from 'serialport'
3-
import sqlite3 from 'sqlite3'
43
import * as fsevents from 'fsevents'
4+
import sqlite3 from 'sqlite3'
5+
import better_sqlite3 from 'better-sqlite3'
56

67
export {
7-
sqlite3,
88
fsevents,
9+
sqlite3,
10+
better_sqlite3,
911
}

test/fixtures/sqlite3.db

Whitespace-only changes.

test/index.test.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,45 @@ import {
1010
const root = path.join(__dirname, 'fixtures')
1111

1212
function initSqlite3(sqlite3: typeof import('sqlite3')) {
13-
const database = path.join(root, 'db.sqlite3')
13+
const sqlite3_db = path.join(root, 'db/sqlite3.db')
1414

1515
return new Promise<{
1616
database: import('sqlite3').Database
1717
error: Error | null
1818
}>(resolve => {
19-
const _database = new (sqlite3.verbose().Database)(database, error => {
19+
const db = new (sqlite3.verbose().Database)(sqlite3_db, error => {
2020
resolve({
21-
database: _database,
21+
database: db,
2222
error,
2323
})
2424
})
2525
})
2626
}
2727

28+
function initBetterSqlite3(BetterSqlite3: typeof import('better-sqlite3')) {
29+
const better_sqlite3_db = path.join(root, 'db/better-sqlite3.db')
30+
31+
return new Promise<{
32+
database: import('better-sqlite3').Database
33+
error: Error | null
34+
}>(resolve => {
35+
const db = new BetterSqlite3(better_sqlite3_db, { verbose: console.log })
36+
// https://github.com/WiseLibs/better-sqlite3/blob/v11.1.0/docs/api.md#pragmastring-options---results
37+
db.pragma('cache_size = 32000')
38+
resolve({
39+
database: db,
40+
error: db.pragma('cache_size', { simple: true }) === 32000
41+
? null
42+
: new Error('better-sqlite3 initialize failed'),
43+
})
44+
})
45+
}
46+
2847
beforeAll(async () => {
29-
for (const name of ['dist', 'db.sqlite3']) {
48+
for (const name of ['dist', 'db']) {
3049
fs.rmSync(path.join(root, name), { recursive: true, force: true })
3150
}
51+
fs.mkdirSync(path.join(root, 'db'), { recursive: true })
3252

3353
await build({ configFile: path.join(root, 'vite.config.ts') })
3454
})
@@ -38,6 +58,7 @@ test('vite-plugin-native', async () => {
3858
const fsevents = main.fsevents
3959
const sqlite3 = main.sqlite3
4060
const sqlite3DB = await initSqlite3(main.sqlite3)
61+
const better_sqlite3DB = await initBetterSqlite3(main.better_sqlite3)
4162

4263
expect(Object.getOwnPropertyNames(fsevents).filter(name => name !== 'default').reverse())
4364
.toEqual(Object.getOwnPropertyNames(require('fsevents')))
@@ -46,4 +67,6 @@ test('vite-plugin-native', async () => {
4667
.toEqual(Object.getOwnPropertyNames(require('sqlite3')).filter(name => name !== 'path'))
4768
expect(sqlite3DB.database && typeof sqlite3DB.database).eq('object')
4869
expect(sqlite3DB.error).null
70+
expect(better_sqlite3DB.database && typeof better_sqlite3DB.database).eq('object')
71+
expect(better_sqlite3DB.error).null
4972
})

yarn.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,13 @@
392392
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
393393
integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==
394394

395+
"@types/better-sqlite3@^7.6.10":
396+
version "7.6.10"
397+
resolved "https://registry.yarnpkg.com/@types/better-sqlite3/-/better-sqlite3-7.6.10.tgz#1818e56490953404acfd44cdde0464f201be6105"
398+
integrity sha512-TZBjD+yOsyrUJGmcUj6OS3JADk3+UZcNv3NOBqGkM09bZdi28fNZw8ODqbMOLfKCu7RYCO62/ldq1iHbzxqoPw==
399+
dependencies:
400+
"@types/node" "*"
401+
395402
"@types/eslint-scope@^3.7.3":
396403
version "3.7.7"
397404
resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.7.tgz#3108bd5f18b0cdb277c867b3dd449c9ed7079ac5"
@@ -702,6 +709,14 @@ base64-js@^1.3.1:
702709
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
703710
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==
704711

712+
better-sqlite3@^11.0.0:
713+
version "11.0.0"
714+
resolved "https://registry.yarnpkg.com/better-sqlite3/-/better-sqlite3-11.0.0.tgz#12083acfe0ded6abdba908ed73520f2003e3ea0e"
715+
integrity sha512-1NnNhmT3EZTsKtofJlMox1jkMxdedILury74PwUbQBjWgo4tL4kf7uTAjU55mgQwjdzqakSTjkf+E1imrFwjnA==
716+
dependencies:
717+
bindings "^1.5.0"
718+
prebuild-install "^7.1.1"
719+
705720
big.js@^5.2.2:
706721
version "5.2.2"
707722
resolved "https://registry.yarnpkg.com/big.js/-/big.js-5.2.2.tgz#65f0af382f578bcdc742bd9c281e9cb2d7768328"

0 commit comments

Comments
 (0)