-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a3e91f3
Showing
27 changed files
with
6,686 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
name: Bug report | ||
about: Create a report to help us improve | ||
labels: 'bug: pending triage' | ||
--- | ||
|
||
## Bug description | ||
<!-- | ||
A clear and concise description of what the expected behavior is and what happened instead. | ||
--> | ||
|
||
## Reproduction steps | ||
<!-- | ||
Providing reproduction steps are crucial for communicating the problem. | ||
Please open a PR with a failing test case or provide a link to a repo that can reproduce the problem you ran into. | ||
Keep your reproductions minimal. Follow guidelines here: https://stackoverflow.com/help/minimal-reproducible-example | ||
--> | ||
|
||
## Environment | ||
|
||
- webpack-json-access-optimizer version: | ||
- Webpack version: | ||
- Operating System: | ||
- Node version: | ||
- Package manager (npm/yarn/pnpm) and version: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
name: Feature request | ||
about: Suggest an idea for this project | ||
labels: 'feature request' | ||
--- | ||
|
||
## Is your feature request related to a problem? Please describe. | ||
<!-- | ||
A clear and concise description of what the problem is. Ex. I'm frustrated when [...] | ||
--> | ||
|
||
## Describe the solution you'd like | ||
<!-- | ||
A clear and concise description of what you want to happen. | ||
--> | ||
|
||
## Describe alternatives you've considered | ||
<!-- | ||
A clear and concise description of any alternative solutions or features you've considered. | ||
--> | ||
|
||
## Additional context | ||
<!-- | ||
Add any other context or screenshots about the feature request here. | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Release | ||
|
||
on: | ||
push: | ||
branches: master | ||
|
||
jobs: | ||
release: | ||
name: Release | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: 14.x | ||
- name: Install dependencies | ||
run: npx ci | ||
- name: Build | ||
run: npm run build | ||
- name: Lint | ||
run: npm run lint | ||
- name: Test | ||
run: npm run test --if-present | ||
- name: Release | ||
env: | ||
GH_TOKEN: ${{ secrets.GH_TOKEN }} | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
run: npx semantic-release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: [develop] | ||
pull_request: | ||
branches: [master, develop] | ||
|
||
jobs: | ||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
node-version: [12.x, 14.x] | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- name: Install dependencies | ||
run: npx ci | ||
- name: Build | ||
run: npm run build | ||
- name: Lint | ||
run: npm run lint | ||
- name: Test | ||
run: npm run test --if-present |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# macOS | ||
.DS_Store | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
|
||
# Dependency directories | ||
node_modules/ | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# dotenv environment variables file | ||
.env | ||
.env.test | ||
|
||
# VSCode | ||
.vscode | ||
|
||
# Distribution | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v14.16.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Hiroki Osame <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# webpack-json-access-optimizer <a href="https://npm.im/webpack-json-access-optimizer"><img src="https://badgen.net/npm/v/webpack-json-access-optimizer"></a> <a href="https://npm.im/webpack-json-access-optimizer"><img src="https://badgen.net/npm/dm/webpack-json-access-optimizer"></a> <a href="https://packagephobia.now.sh/result?p=webpack-json-access-optimizer"><img src="https://packagephobia.now.sh/badge?p=webpack-json-access-optimizer"></a> | ||
|
||
Optimize JSON modules that are referenced via accessor function. For example, i18n locale JSONs. | ||
|
||
### Features | ||
- **Tree shaking** Remove unused JSON entries | ||
- **Optimize JSON structure** Minify JSON by converting to an array | ||
- **Developer friendly** Warn on invalid JSON keys and invalid accessor usage | ||
- **Persistent caching support** Designed to support Webpack 5 disk cache | ||
|
||
<sub>Support this project by ⭐️ starring and sharing it. [Follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️</sub> | ||
|
||
## Example | ||
### Before | ||
Given a "global accessor function" `$t` that retruns values from `locale.json`: | ||
|
||
**index.js** | ||
```js | ||
console.log($t('helloWorld')) // logs "Hello world!" | ||
``` | ||
|
||
**locale.json** | ||
```json | ||
{ | ||
"helloWorld": "Hello world!", | ||
"unusedString": "I'm never accessed" | ||
} | ||
``` | ||
|
||
### After optimization <sup>✨</sup> | ||
**index.js** | ||
```js | ||
console.log($t(0)) // logs "Hello world!" | ||
``` | ||
|
||
**locale.json** | ||
```json | ||
["Hello world!"] | ||
``` | ||
|
||
Note: | ||
- The JSON is minified into an array, and the accessor now uses the array indices to access values | ||
- Unused entries are removed from the JSON | ||
|
||
## 🚀 Install | ||
```sh | ||
npm i -D webpack-json-access-optimizer | ||
``` | ||
|
||
## 🚦 Quick setup | ||
|
||
Assuming you have some sort of "global accessor function" that takes a JSON key and returns the JSON value (eg. via [`ProvidePlugin`](https://webpack.js.org/plugins/provide-plugin/)): | ||
|
||
1. Import the `JsonAccessOptimizer` plugin from `webpack-json-access-optimizer`. | ||
2. Register the plugin with the "global accessor function" name | ||
3. Add the `webpack-json-access-optimizer` loader to the JSON files. Note, all JSON files must have identical keys. | ||
|
||
In `webpack.config.js`: | ||
|
||
```diff | ||
+ const { JsonAccessOptimizer } = require('webpack-json-access-optimizer') | ||
|
||
module.exports = { | ||
..., | ||
|
||
module: { | ||
rules: [ | ||
..., | ||
+ { | ||
+ test: /locale\.json$/, // match JSON files to optimize | ||
+ loader: 'webpack-json-access-optimizer' | ||
+ }, | ||
] | ||
}, | ||
|
||
plugins: [ | ||
..., | ||
+ new JsonAccessOptimizer({ | ||
+ accessorFunctionName: '$t', // localization function name | ||
+ }) | ||
] | ||
} | ||
``` | ||
|
||
### JS loader | ||
If the JSON needs to be transformed to JavaScript via another loader, you can chain them: | ||
|
||
In `webpack.config.js`: | ||
|
||
```diff | ||
module.exports = { | ||
..., | ||
|
||
module: { | ||
rules: [ | ||
..., | ||
{ | ||
test: /locale\.json$/, // match JSON files to optimize | ||
use: [ | ||
+ 'some-other-json-transformer-loader', // any loader to transform JSON to JS | ||
'webpack-json-access-optimizer' | ||
], | ||
+ type: 'javascript/auto' | ||
}, | ||
] | ||
}, | ||
} | ||
``` | ||
|
||
## ⚙️ Plugin API | ||
|
||
### accessorFunctionName | ||
Type: `string` | ||
|
||
Required | ||
|
||
The name of the "global accessor function" that takes a JSON key and returns the JSON value. This function is typically provided via another plugin like [`ProvidePlugin`](https://webpack.js.org/plugins/provide-plugin/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
preset: 'es-jest', | ||
transformIgnorePatterns: [ | ||
'node_modules/.pnpm(?!/(aggregate-error|indent-string|clean-stack|escape-string-regexp))', | ||
], | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"name": "webpack-json-access-optimizer", | ||
"version": "0.0.0-semantic-release", | ||
"description": "Tree-shake and minify JSON modules", | ||
"keywords": [ | ||
"webpack", | ||
"plugin", | ||
"optimize", | ||
"json", | ||
"minify", | ||
"treeshake" | ||
], | ||
"license": "MIT", | ||
"repository": "privatenumber/webpack-json-access-optimizer", | ||
"funding": "https://github.com/privatenumber/webpack-json-access-optimizer?sponsor=1", | ||
"author": { | ||
"name": "Hiroki Osame", | ||
"email": "[email protected]" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"build": "tsc", | ||
"pretest": "npm run build", | ||
"test": "jest" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.{js,ts}": [ | ||
"eslint", | ||
"jest --bail --findRelatedTests" | ||
] | ||
}, | ||
"peerDependencies": { | ||
"webpack": "^5.37.0" | ||
}, | ||
"dependencies": { | ||
"@types/estree": "^0.0.50" | ||
}, | ||
"devDependencies": { | ||
"@pvtnbr/eslint-config": "^0.2.1", | ||
"@types/jest": "^27.0.2", | ||
"aggregate-error": "^4.0.0", | ||
"es-jest": "^1.3.0", | ||
"eslint": "^7.32.0", | ||
"esno": "^0.10.0", | ||
"fs-require": "^1.1.0", | ||
"husky": "^4.3.8", | ||
"jest": "^27.2.2", | ||
"lint-staged": "^10.5.4", | ||
"memfs": "^3.3.0", | ||
"source-map": "^0.7.3", | ||
"typescript": "^4.4.3", | ||
"unionfs": "^4.4.0", | ||
"webpack": "5.37.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": "@pvtnbr" | ||
} | ||
} |
Oops, something went wrong.