json-perf-loader
Advanced tools
Comparing version
# Changelog | ||
## 1.0.3 - [2019-09-10] | ||
- Upgrade schema-utils to 2.x [#11](https://github.com/justjavac/json-perf-loader/pull/11) | ||
- 2.x drop support for Node.js < 8.9.0 |
{ | ||
"name": "json-perf-loader", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A loader for webpack to load JSON with performance advice", | ||
@@ -12,3 +12,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">= 8.0.0" | ||
"node": "> 8.9.0" | ||
}, | ||
@@ -18,2 +18,3 @@ "scripts": { | ||
"lint": "prettier \"{**/*,*}.{js,json,md,yml}\" --list-different", | ||
"format": "prettier \"{**/*,*}.{js,json,md,yml}\" --write", | ||
"test": "cross-env NODE_ENV=test jest", | ||
@@ -41,13 +42,14 @@ "test:coverage": "cross-env NODE_ENV=test jest --collectCoverageFrom=\"src/**/*.js\" --coverage" | ||
"loader-utils": "^1.1.0", | ||
"schema-utils": "^1.0.0" | ||
"schema-utils": "^2.2.0" | ||
}, | ||
"devDependencies": { | ||
"cross-env": "^5.2.0", | ||
"husky": "^2.5.0", | ||
"jest": "^24.8.0", | ||
"lint-staged": "^8.2.1", | ||
"codecov": "^3.5.0", | ||
"cross-env": "^5.2.1", | ||
"husky": "^3.0.5", | ||
"jest": "^24.9.0", | ||
"lint-staged": "^9.2.5", | ||
"memory-fs": "^0.4.1", | ||
"prettier": "^1.18.2", | ||
"rimraf": "^2.6.3", | ||
"webpack": "^4.35.0" | ||
"rimraf": "^3.0.0", | ||
"webpack": "^4.39.3" | ||
}, | ||
@@ -54,0 +56,0 @@ "keywords": [ |
# json-perf-loader | ||
[](https://nodei.co/npm/json-perf-loader/) | ||
[![build][build]][build-url] | ||
[![cover][cover]][cover-url] | ||
[![npm][npm]][npm-url] | ||
[![node][node]][node-url] | ||
[![download][download]][download-url] | ||
[![deps][deps]][deps-url] | ||
@@ -17,3 +22,3 @@ [![size][size]][size-url] | ||
> Instead of inlining the data as a JavaScript object literal. | ||
> | ||
> | ||
> 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. | ||
@@ -36,3 +41,3 @@ > 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. | ||
```js | ||
import json from './file.json'; | ||
import json from './file.json' | ||
``` | ||
@@ -48,3 +53,3 @@ | ||
test: /\.json$/i, | ||
type: "javascript/auto", | ||
type: 'javascript/auto', | ||
use: [ | ||
@@ -61,3 +66,3 @@ { | ||
}, | ||
}; | ||
} | ||
``` | ||
@@ -95,3 +100,3 @@ | ||
test: /\.json$/i, | ||
type: "javascript/auto", | ||
type: 'javascript/auto', | ||
use: [ | ||
@@ -108,3 +113,3 @@ { | ||
}, | ||
}; | ||
} | ||
``` | ||
@@ -116,2 +121,4 @@ | ||
[build]: https://travis-ci.com/justjavac/json-perf-loader.svg?branch=master | ||
[build-url]: https://travis-ci.com/justjavac/json-perf-loader | ||
[npm]: https://img.shields.io/npm/v/json-perf-loader.svg | ||
@@ -123,2 +130,4 @@ [npm-url]: https://npmjs.com/package/json-perf-loader | ||
[deps-url]: https://david-dm.org/justjavac/json-perf-loader | ||
[download]: https://img.shields.io/npm/dm/json-perf-loader.svg?style=flat | ||
[download-url]: https://npmcharts.com/compare/json-perf-loader?minimal=true | ||
[tests]: https://dev.azure.com/justjavac/json-perf-loader/_apis/build/status/justjavac.json-perf-loader?branchName=master | ||
@@ -125,0 +134,0 @@ [tests-url]: https://dev.azure.com/justjavac/json-perf-loader/_build/latest?definitionId=2&branchName=master |
@@ -6,3 +6,7 @@ const { getOptions } = require('loader-utils') | ||
function shouldInline(limit = 10240, size) { | ||
const DEFAULT_OPTIONS = { | ||
limit: 10240, | ||
} | ||
function shouldInline(limit, size) { | ||
return size <= parseInt(limit, 10) | ||
@@ -13,9 +17,8 @@ } | ||
module.exports = function(source) { | ||
const options = getOptions(this) || {} | ||
const options = Object.assign({}, DEFAULT_OPTIONS, getOptions(this)) | ||
validateOptions(schema, options, 'JSON Perf Loader') | ||
let value | ||
try { | ||
@@ -26,14 +29,14 @@ value = typeof source === 'string' ? JSON.parse(source) : source | ||
} | ||
if (shouldInline(options.limit, source.length)) { | ||
value = JSON.stringify(value) | ||
.replace(/\u2028/g, '\\u2028') | ||
.replace(/\u2029/g, '\\u2029') | ||
.replace(/\u2028/g, '\\u2028') | ||
.replace(/\u2029/g, '\\u2029') | ||
return `module.exports = ${value}` | ||
} | ||
return `module.exports = JSON.parse('${JSON.stringify(value)}')` | ||
} | ||
exports.raw = true; | ||
exports.raw = true |
7855
31.73%6
20%37
12.12%131
7.38%9
12.5%+ Added
+ Added
Updated