Skip to content

Commit 9e17198

Browse files
authored
Merge pull request #10 from hankei6km:topic/implement
feat: Implement
2 parents 6e0ed55 + 53ed1d8 commit 9e17198

File tree

25 files changed

+449
-58
lines changed

25 files changed

+449
-58
lines changed

.github/workflows/gh_pkg.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
- name: Run tests
5151
run: |
5252
npm run lint:type-check
53-
npm run test
53+
npm run test:run
5454
5555
- name: Build
5656
run: npm run build

.github/workflows/npm_pkg.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
- name: Run tests
5757
run: |
5858
npm run lint:type-check
59-
npm run test
59+
npm run test:run
6060
6161
- name: Build
6262
run: npm run build

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ jobs:
4343
- name: Run tests
4444
run: |
4545
npm run lint:type-check
46-
npm run test
46+
npm run test:run

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,6 @@ dist
116116
.vscode-test
117117

118118
# End of https://www.toptal.com/developers/gitignore/api/node
119+
120+
# exclude fixture files
121+
!/test/fixtures/walk/node_modules/

package-lock.json

Lines changed: 63 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"keywords": [
1212
"rollup-plugin",
13+
"vite-plugin",
1314
"nodebox"
1415
],
1516
"main": "dist/index.js",
@@ -22,9 +23,10 @@
2223
"scripts": {
2324
"build": "npm run clean && tsc && rimraf dist/test && mv dist/src/* dist/ && rimraf dist/src",
2425
"test": "vitest",
26+
"test:run": "vitest run",
2527
"test:debug": "vitest run --inspect-brk --threads false --coverage false",
2628
"lint:type-check": "tsc --noEmit",
27-
"clean": "rimraf \"dist/*\"",
29+
"clean": "rimraf dist/",
2830
"upgrade-interactive": "npm-check --update",
2931
"csb:test": "npm test -- --runInBand --watchAll"
3032
},
@@ -35,6 +37,11 @@
3537
"rimraf": "^5.0.0",
3638
"ts-node": "^10.9.1",
3739
"typescript": "^5.0.4",
40+
"vite": "^4.3.8",
3841
"vitest": "^0.31.0"
42+
},
43+
"dependencies": {
44+
"@rollup/pluginutils": "^5.0.2",
45+
"rollup": "^3.22.0"
3946
}
4047
}

src/count.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/files.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { readFile } from 'node:fs/promises'
2+
import { resolve } from 'node:path'
3+
import type { PluginImpl } from 'rollup'
4+
import { createFilter, dataToEsm } from '@rollup/pluginutils'
5+
import { walk } from './walk.js'
6+
7+
export const defultExcludeFrom = [
8+
'**/.DS_Store',
9+
'**/Thumbs.db',
10+
'**/.git/**',
11+
'**/.gitignore',
12+
'**/.vscode/**',
13+
'**/node_modules/**',
14+
'**/dist/**',
15+
'**/build/**',
16+
'**/out/**'
17+
]
18+
19+
export const defultBinaryFile = [
20+
'**/*.png',
21+
'**/*.jpg',
22+
'**/*.jpeg',
23+
'**/*.gif',
24+
'**/*.ico',
25+
'**/*.wasm'
26+
]
27+
28+
export type NodeboxFsFilesOptions = {
29+
from: string
30+
insertTo: string
31+
excludeFrom?: string | string[]
32+
binaryFile?: string | string[]
33+
}
34+
35+
export function makeFilesFilterOptsExclude(
36+
excludeFrom?: NodeboxFsFilesOptions['excludeFrom']
37+
): Parameters<typeof createFilter>[1] {
38+
return typeof excludeFrom === 'string' && excludeFrom.length > 0
39+
? [...defultExcludeFrom, excludeFrom]
40+
: [...defultExcludeFrom, ...(excludeFrom || [])]
41+
}
42+
43+
export function makeBinaryFilterOptsInclude(
44+
binaryFile?: NodeboxFsFilesOptions['binaryFile']
45+
): Parameters<typeof createFilter>[1] {
46+
return typeof binaryFile === 'string' && binaryFile.length > 0
47+
? [...defultBinaryFile, binaryFile]
48+
: [...defultBinaryFile, ...(binaryFile || [])]
49+
}
50+
51+
export function makeFilesFiter(
52+
excludeFrom?: NodeboxFsFilesOptions['excludeFrom']
53+
): ReturnType<typeof createFilter> {
54+
return createFilter([], makeFilesFilterOptsExclude(excludeFrom))
55+
}
56+
export function makebinaryFilesFiter(
57+
binaryFile?: NodeboxFsFilesOptions['binaryFile']
58+
): ReturnType<typeof createFilter> {
59+
return createFilter(makeBinaryFilterOptsInclude(binaryFile), [])
60+
}
61+
62+
export const nodeboxFsFiles: PluginImpl<NodeboxFsFilesOptions> =
63+
function nodeboxFsFiles(opts?: NodeboxFsFilesOptions) {
64+
if (!opts?.from) {
65+
throw new Error('from option should be specified')
66+
}
67+
if (!opts?.insertTo) {
68+
throw new Error('insertTo option should be specified')
69+
}
70+
71+
const filter = createFilter(opts.insertTo, [])
72+
const filesFilter = makeFilesFiter(opts.excludeFrom)
73+
const binaryFilter = makebinaryFilesFiter(opts.binaryFile)
74+
let curFiles: Record<string, any> = {}
75+
76+
return {
77+
name: 'nodebox-fs-files',
78+
79+
async buildStart() {
80+
curFiles = {}
81+
const dir = resolve('.', opts.from)
82+
for await (const [entry, relEntry] of walk(filesFilter, dir, [])) {
83+
const content = await readFile(entry)
84+
curFiles[relEntry] = binaryFilter(entry)
85+
? content
86+
: content.toString('utf-8')
87+
}
88+
},
89+
90+
async transform(code, id) {
91+
if (filter(id)) {
92+
// Once JSON.stringify and parse to make it available in fs.init of Nodebox.
93+
// It becomes { type: "Buffer", data: [ 97, 98, 99 ] }.
94+
// If you don't do this, it will be { type: "Buffer", 1: 97, 2: 98, 3: 99 }.
95+
return dataToEsm(
96+
{ files: JSON.parse(JSON.stringify(curFiles)) },
97+
{
98+
compact: false,
99+
indent: '\t',
100+
preferConst: false,
101+
objectShorthand: false,
102+
namedExports: true
103+
}
104+
)
105+
}
106+
return null
107+
}
108+
}
109+
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import countChars from './count.js'
2-
export { countChars }
1+
export { nodeboxFsFiles } from './files.js'
2+
export type { NodeboxFsFilesOptions } from './files.js'

0 commit comments

Comments
 (0)