Skip to content

Commit 85c2c91

Browse files
committed
perf: use JSON.stringify on data
1 parent 35f69da commit 85c2c91

File tree

12 files changed

+301
-17
lines changed

12 files changed

+301
-17
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
run: yarn install --frozen-lockfile
1919

2020
- name: build
21-
run: yarn tsc
21+
run: yarn workspaces run tsc
2222

2323
- name: lint
2424
run: yarn lint

.github/workflows/publish.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
- name: install deps
1717
run: yarn install --frozen-lockfile
1818

19+
- name: build plugins
20+
run: yarn workspace rollup-plugins run build
21+
1922
- name: build
2023
run: yarn vsce package
2124

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules
55
*.vsix
66
lib
77
processing-vscode.js
8+
.eslintcache

.vscodeignore

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
*.log
2+
13
.github
2-
.vscode/**
4+
.vscode/
35
data/
4-
src/**
6+
legacy/
7+
media/
8+
node_modules/**
9+
rollup/
10+
scripts/
11+
src/
512
.editorconfig
13+
.eslintcache
14+
.eslintignore
15+
.eslintrc.js
16+
.gitattributes
617
.gitignore
18+
.prettierignore
719
.prettierrc.yml
8-
tsconfig.json
9-
tslint.json
10-
**/*.map
11-
yarn.lock
20+
.vscodeignore
21+
pnpm-lock.yaml
22+
pnpm-workspace.yaml
1223
rollup.config.js
13-
node_modules/
14-
scripts/
15-
*.log
16-
legacy/
17-
media/
24+
tsconfig.json

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "processing-vscode",
33
"version": "2.4.0",
4+
"private": true,
45
"description": "Processing Language Support for VSCode",
56
"license": "MIT",
67
"repository": {
@@ -33,7 +34,6 @@
3334
"devDependencies": {
3435
"@rollup/plugin-node-resolve": "^13.0.0",
3536
"@rollup/plugin-typescript": "^8.2.1",
36-
"@rollup/plugin-yaml": "^3.0.0",
3737
"@types/glob": "^7.1.4",
3838
"@types/jsdom": "^16.2.13",
3939
"@types/node": "^16.3.1",
@@ -53,7 +53,7 @@
5353
"rollup-plugin-progress": "^1.1.2",
5454
"rollup-plugin-terser": "^7.0.2",
5555
"tslib": "^2.3.0",
56-
"typescript": "^4.3.5",
56+
"typescript": "~4.3.5",
5757
"vsce": "^1.95.1",
5858
"yaml": "^1.10.2"
5959
},
@@ -236,5 +236,9 @@
236236
},
237237
"displayName": "Processing VSCode",
238238
"icon": "images/processing.png",
239-
"publisher": "Luke-zhang-04"
239+
"publisher": "Luke-zhang-04",
240+
"workspaces": [
241+
".",
242+
"./rollup/plugins"
243+
]
240244
}

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import yaml from "@rollup/plugin-yaml"
1+
import {yaml} from "./rollup/plugins/lib/index.js"
22
import progress from "rollup-plugin-progress"
33
import resolve from "@rollup/plugin-node-resolve"
44
import {terser} from "rollup-plugin-terser"

rollup/plugins/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "rollup-plugins",
3+
"version": "0.0.0",
4+
"license": "MIT",
5+
"main": "./lib/index.js",
6+
"scripts": {
7+
"build": "tsc"
8+
},
9+
"dependencies": {
10+
"@rollup/pluginutils": "^4.1.0",
11+
"js-yaml": "^4.1.0"
12+
},
13+
"devDependencies": {
14+
"@types/js-yaml": "^4.0.1",
15+
"rollup": "^2.53.0",
16+
"typescript": "^4.3.2"
17+
},
18+
"types": "./lib/index.d.ts"
19+
}

rollup/plugins/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default as yaml} from "./json-and-yaml"
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
* @license MIT
3+
* @file improved Version of @rollup/plugin-json and @rollup/plugin-yaml that combines the two and
4+
* provides the option for JSON.stringify()
5+
* @copyright (c) 2019 RollupJS Plugin Contributors
6+
* (https://github.com/rollup/plugins/graphs/contributors), 2021 Luke Zhang
7+
* @see {@link https://github.com/rollup/plugins/tree/master/packages/json}
8+
* @see {@link https://github.com/rollup/plugins/tree/master/packages/yaml}
9+
*/
10+
11+
import {FilterPattern, createFilter, dataToEsm} from "@rollup/pluginutils"
12+
import yaml from "js-yaml"
13+
import type {PluginFunc} from "./types"
14+
15+
type ValidYamlType =
16+
| number
17+
| string
18+
| boolean
19+
| null
20+
| undefined
21+
| {[key: string]: ValidYamlType}
22+
| ValidYamlType[]
23+
24+
type RollupJsonOptions = {
25+
/**
26+
* All JSON and YAML files will be parsed by default, but you can also specifically include files
27+
*
28+
* @default ["**‍/*.json", "**‍/*.y(a)?ml"]
29+
*/
30+
include?: FilterPattern
31+
/**
32+
* All JSON and YAML files will be parsed by default, but you can also specifically exclude files
33+
*
34+
* @default
35+
*/
36+
exclude?: FilterPattern
37+
/**
38+
* For tree-shaking, properties will be declared as variables, using either `var` or `const`.
39+
*
40+
* @default true
41+
*/
42+
preferConst?: boolean
43+
/**
44+
* Specify indentation for the generated default export
45+
*
46+
* @default
47+
*/
48+
indent?: string
49+
/**
50+
* Ignores indent and generates the smallest code
51+
*
52+
* @default false
53+
*/
54+
compact?: boolean
55+
/**
56+
* Generate a named export for every property of the JSON object
57+
*
58+
* @default true
59+
*/
60+
namedExports?: boolean
61+
/**
62+
* Character for when json should be stringified and then parsed at runtime
63+
*
64+
* @default 14 * 1024 (14kb)
65+
* @see {@link https://v8.dev/blog/cost-of-javascript-2019#json}
66+
*/
67+
stringifyLimit?: number
68+
/**
69+
* A function which can optionally mutate parsed YAML. The function should return the mutated
70+
* `object`, or `undefined` which will make no changes to the parsed YAML.
71+
*
72+
* @default undefined
73+
*/
74+
transform?: (data: ValidYamlType, filePath: string) => ValidYamlType | undefined
75+
/**
76+
* - If `single`, specifies that the target YAML documents contain only one document in the target file(s).
77+
* - If more than one [document stream](https://yaml.org/spec/1.2/spec.html#id2801681) exists in
78+
* the target YAML file(s), set `documentMode: 'multi'`.
79+
*
80+
* @default 'single'
81+
*/
82+
documentMode?: "single" | "multi"
83+
}
84+
85+
const json: PluginFunc<RollupJsonOptions> = ({
86+
include = ["**/*.y(a)?ml", "**/*.json"],
87+
exclude,
88+
preferConst = true,
89+
indent = " ",
90+
compact = false,
91+
namedExports = true,
92+
stringifyLimit = 14 * 1024,
93+
transform,
94+
documentMode = "single",
95+
} = {}) => {
96+
const filter = createFilter(include, exclude)
97+
98+
let loadMethod = (documentMode === "multi" ? yaml.loadAll : yaml.load) as (
99+
str: string,
100+
iterator?: (doc: any) => void,
101+
opts?: yaml.LoadOptions,
102+
) => ValidYamlType
103+
104+
const plugin: ReturnType<PluginFunc<RollupJsonOptions>> = {
105+
name: "json/yaml",
106+
transform(code, id) {
107+
if (!filter(id)) {
108+
return null
109+
}
110+
111+
try {
112+
const parsed = transform?.(loadMethod(code), id) ?? loadMethod(code)
113+
const stringified = JSON.stringify(parsed, null, 0)
114+
115+
return {
116+
code:
117+
stringified.length > stringifyLimit
118+
? `export default JSON.parse(${JSON.stringify(stringified)})`
119+
: dataToEsm(parsed, {
120+
preferConst,
121+
compact,
122+
namedExports,
123+
indent,
124+
}),
125+
map: {mappings: ""},
126+
}
127+
} catch (err) {
128+
const message = `Could not parse JSON file: ${
129+
err instanceof Error ? err.toString() : JSON.stringify(err)
130+
}`
131+
132+
this.error({message, id})
133+
}
134+
},
135+
}
136+
137+
return plugin
138+
}
139+
140+
export default json

rollup/plugins/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type PluginFunc<T> = (options: T) => import("rollup").Plugin | undefined

0 commit comments

Comments
 (0)