Skip to content

Commit f290c37

Browse files
committed
first commit
0 parents  commit f290c37

19 files changed

+7283
-0
lines changed

Diff for: .editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
insert_final_newline = true
13+
trim_trailing_whitespace = false

Diff for: .gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
node_modules
2+
logs
3+
*.log
4+
npm-debug.log*
5+
.eslintcache
6+
/coverage
7+
/reports
8+
/node_modules
9+
.DS_Store
10+
Thumbs.db
11+
.idea
12+
*.sublime-project
13+
*.sublime-workspace
14+
*.iml

Diff for: .prettierignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/coverage
2+
/dist
3+
/node_modules
4+
/test/fixtures
5+
CHANGELOG.md

Diff for: .prettierrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"semi": false,
5+
"overrides": [
6+
{
7+
"files": ".prettierrc",
8+
"options": { "parser": "json" }
9+
}
10+
]
11+
}

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Changelog
2+

Diff for: README.md

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# json-perf-loader
2+
3+
[![npm][npm]][npm-url]
4+
[![node][node]][node-url]
5+
[![deps][deps]][deps-url]
6+
[![size][size]][size-url]
7+
8+
A loader for webpack to load JSON with performance advice.
9+
10+
## The cost of parsing JSON
11+
12+
See [The cost of parsing JSON - V8](https://v8.dev/blog/cost-of-javascript-2019#json)
13+
14+
> Because the JSON grammar is much simpler than JavaScript’s grammar, JSON can be parsed more efficiently than JavaScript.
15+
> This knowledge can be applied to improve start-up performance for web apps that ship large JSON-like configuration object literals (such as inline Redux stores).
16+
> Instead of inlining the data as a JavaScript object literal.
17+
>
18+
> As long as the JSON string is only evaluated once, the `JSON.parse` approach is much faster compared to the JavaScript object literal, especially for cold loads.
19+
> A good rule of thumb is to apply this technique for objects of **10 kB or larger** — but as always with performance advice, measure the actual impact before making any changes.
20+
21+
## Getting Started
22+
23+
To begin, you'll need to install `json-perf-loader`:
24+
25+
```shell
26+
$ npm install json-perf-loader --save-dev
27+
```
28+
29+
`json-perf-loader` works like
30+
[`json-loader`](https://github.com/justjavac/json-loader), but much faster.
31+
32+
**index.js**
33+
34+
```js
35+
import json from './file.json';
36+
```
37+
38+
**webpack.config.js**
39+
40+
```js
41+
module.exports = {
42+
module: {
43+
rules: [
44+
{
45+
test: /\.json$/i,
46+
type: "javascript/auto",
47+
use: [
48+
{
49+
loader: 'json-perf-loader',
50+
options: {
51+
limit: 4096,
52+
},
53+
},
54+
],
55+
},
56+
],
57+
},
58+
};
59+
```
60+
61+
And run `webpack` via your preferred method.
62+
63+
**Note: `type: "javascript/auto"` is require**. See https://webpack.js.org/configuration/module/#ruletype
64+
65+
> `Rule.type` sets the type for a matching module.
66+
> This prevents defaultRules and their default importing behaviors from occurring.
67+
> For example, if you want to load a `.json` file through a custom loader, you'd need to set the `type` to `javascript/auto` to bypass webpack's built-in json importing.
68+
69+
## Options
70+
71+
### `limit`
72+
73+
Type: `Number|String`
74+
Default: `1024 * 10`
75+
76+
The limit can be specified via loader options and defaults to `1024 * 10`. This is the recommended value for the V8 team.
77+
78+
#### `Number`
79+
80+
A `Number` specifying the maximum size of a file in bytes. If the file size is
81+
**equal** or **greater** than the limit `JSON.parse` will be used.
82+
83+
**webpack.config.js**
84+
85+
```js
86+
module.exports = {
87+
module: {
88+
rules: [
89+
{
90+
test: /\.json$/i,
91+
type: "javascript/auto",
92+
use: [
93+
{
94+
loader: 'json-perf-loader',
95+
options: {
96+
limit: 10,
97+
},
98+
},
99+
],
100+
},
101+
],
102+
},
103+
};
104+
```
105+
106+
## License
107+
108+
[MIT](./LICENSE)
109+
110+
[npm]: https://img.shields.io/npm/v/json-perf-loader.svg
111+
[npm-url]: https://npmjs.com/package/json-perf-loader
112+
[node]: https://img.shields.io/node/v/json-perf-loader.svg
113+
[node-url]: https://nodejs.org
114+
[deps]: https://david-dm.org/justjavac/json-perf-loader.svg
115+
[deps-url]: https://david-dm.org/justjavac/json-perf-loader
116+
[tests]: https://dev.azure.com/justjavac/json-perf-loader/_apis/build/status/justjavac.json-perf-loader?branchName=master
117+
[tests-url]: https://dev.azure.com/justjavac/json-perf-loader/_build/latest?definitionId=2&branchName=master
118+
[cover]: https://codecov.io/gh/justjavac/json-perf-loader/branch/master/graph/badge.svg
119+
[cover-url]: https://codecov.io/gh/justjavac/json-perf-loader
120+
[size]: https://packagephobia.now.sh/badge?p=json-perf-loader
121+
[size-url]: https://packagephobia.now.sh/result?p=json-perf-loader

Diff for: package.json

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "json-perf-loader",
3+
"version": "1.0.2",
4+
"description": "A loader for webpack to load JSON with performance advice",
5+
"license": "MIT",
6+
"repository": "justjavac/json-perf-loader",
7+
"author": "justjavac",
8+
"homepage": "https://github.com/justjavac/json-perf-loader",
9+
"bugs": "https://github.com/justjavac/json-perf-loader/issues",
10+
"main": "src/index.js",
11+
"engines": {
12+
"node": ">= 8.0.0"
13+
},
14+
"scripts": {
15+
"clean": "rimraf -rf coverage",
16+
"lint": "prettier \"{**/*,*}.{js,json,md,yml}\" --list-different",
17+
"test": "cross-env NODE_ENV=test jest",
18+
"test:coverage": "cross-env NODE_ENV=test jest --collectCoverageFrom=\"src/**/*.js\" --coverage"
19+
},
20+
"husky": {
21+
"hooks": {
22+
"pre-commit": "lint-staged"
23+
}
24+
},
25+
"lint-staged": {
26+
"**/*.{js,json,md,yml}": [
27+
"prettier --write",
28+
"git add"
29+
]
30+
},
31+
"files": [
32+
"src"
33+
],
34+
"peerDependencies": {
35+
"webpack": "^4.0.0"
36+
},
37+
"dependencies": {
38+
"loader-utils": "^1.1.0",
39+
"schema-utils": "^1.0.0"
40+
},
41+
"devDependencies": {
42+
"cross-env": "^5.2.0",
43+
"husky": "^2.5.0",
44+
"jest": "^24.8.0",
45+
"lint-staged": "^8.2.1",
46+
"memory-fs": "^0.4.1",
47+
"prettier": "^1.18.2",
48+
"rimraf": "^2.6.3",
49+
"webpack": "^4.35.0"
50+
},
51+
"keywords": [
52+
"webpack",
53+
"webpack-loader",
54+
"json",
55+
"perf"
56+
]
57+
}

Diff for: src/index.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { getOptions } = require('loader-utils')
2+
const validateOptions = require('schema-utils')
3+
4+
const schema = require('./options.json')
5+
6+
function shouldInline(limit = 10240, size) {
7+
return size <= parseInt(limit, 10)
8+
}
9+
10+
// https://v8.dev/blog/cost-of-javascript-2019#json
11+
module.exports = function(source) {
12+
const options = getOptions(this) || {}
13+
14+
validateOptions(schema, options, 'JSON Perf Loader')
15+
16+
17+
let value
18+
19+
try {
20+
value = typeof source === 'string' ? JSON.parse(source) : source
21+
} catch (error) {
22+
this.emitError(error)
23+
}
24+
25+
if (shouldInline(options.limit, source.length)) {
26+
value = JSON.stringify(value)
27+
.replace(/\u2028/g, '\\u2028')
28+
.replace(/\u2029/g, '\\u2029')
29+
30+
return `module.exports = ${value}`
31+
}
32+
33+
return `module.exports = JSON.parse('${JSON.stringify(value)}')`
34+
}
35+
36+
exports.raw = true;

Diff for: src/options.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"limit": {
5+
"type": ["number", "string"]
6+
}
7+
},
8+
"additionalProperties": true
9+
}

0 commit comments

Comments
 (0)