All Projects → privatenumber → webpack-json-access-optimizer

privatenumber / webpack-json-access-optimizer

Licence: MIT license
Webpack plugin to tree-shake and minify JSON modules

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to webpack-json-access-optimizer

frontend-starter-kit-with-gulp
Frontend Starter Kit with Gulp for either Themeforest Projects or customizable projects.
Stars: ✭ 34 (+0%)
Mutual labels:  minify, optimize
gatsby-plugin-optimize-svgs
A Gatsby Plugin to minify SVGs output to the filesystem during the build.
Stars: ✭ 39 (+14.71%)
Mutual labels:  minify, optimize
Tinyify
a browserify plugin that runs various optimizations, so you don't have to install them all manually. makes your bundles tiny!
Stars: ✭ 392 (+1052.94%)
Mutual labels:  minify, optimize
Tiny Html Minifier
Minify HTML in PHP with just a single class
Stars: ✭ 114 (+235.29%)
Mutual labels:  minify
Serverless Plugin Optimize
Bundle with Browserify, transpile and minify with Babel automatically to your NodeJS runtime compatible JavaScript
Stars: ✭ 122 (+258.82%)
Mutual labels:  minify
Yui Css Compressor Php Port
A PHP port of the YUI CSS compressor.
Stars: ✭ 220 (+547.06%)
Mutual labels:  minify
MinifyAll
A 𝗩𝗦𝗖𝗼𝗱𝗲 𝗺𝗶𝗻𝗶𝗳𝗶𝗲𝗿 for JS, JSON/C, CSS, and HTML, you will love its simplicity! 🌟 𝘾𝙤𝙢𝙥𝙧𝙚𝙨𝙨 and 𝙜𝙯𝙞𝙥 files and folders 📦 Reduce your bundle and file sizes with lightning speed ⚡
Stars: ✭ 54 (+58.82%)
Mutual labels:  minify
Html Minifier Terser
actively maintained fork of html-minifier - minify HTML, CSS and JS code using terser - supports ES6 code
Stars: ✭ 106 (+211.76%)
Mutual labels:  minify
rollup-plugin-closure-compiler-js
Rollup plugin for optimizing JavaScript with google-closure-compiler-js.
Stars: ✭ 31 (-8.82%)
Mutual labels:  minify
Twelvety
An Eleventy starter project built to be fast
Stars: ✭ 195 (+473.53%)
Mutual labels:  minify
Yii2 Minify View
Yii2 View component with minification css & js
Stars: ✭ 186 (+447.06%)
Mutual labels:  minify
Terser Webpack Plugin
Terser Plugin
Stars: ✭ 1,687 (+4861.76%)
Mutual labels:  minify
Minify
Go minifiers for web formats
Stars: ✭ 2,824 (+8205.88%)
Mutual labels:  minify
Minify
Minifier of js, css, html and img
Stars: ✭ 120 (+252.94%)
Mutual labels:  minify
flask minify
A Flask extension to minify request's response for html, js, css and less.
Stars: ✭ 59 (+73.53%)
Mutual labels:  minify
Minify
DEPRECATED A simple plugin that allows you to minify blocks of HTML, CSS, and JS inline in Craft CMS templates
Stars: ✭ 111 (+226.47%)
Mutual labels:  minify
ipx
High performance, secure and easy to use image proxy based on Sharp and libvips.
Stars: ✭ 683 (+1908.82%)
Mutual labels:  optimize
Glup
Some of the gulp tutorial -《gulp笔记》
Stars: ✭ 136 (+300%)
Mutual labels:  minify
Instapack
All-in-one TypeScript and Sass compiler for web applications! 📦 🚀
Stars: ✭ 131 (+285.29%)
Mutual labels:  minify
Image Shrinker
App for macOS. Minify your images and graphics with just one drop. Autorenamed in the same place where it comes from. Immediately!
Stars: ✭ 217 (+538.24%)
Mutual labels:  minify

webpack-json-access-optimizer

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

Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️

Example

Before

Given a "global accessor function" $t that retruns values from locale.json:

index.js

console.log($t('helloWorld')) // logs "Hello world!"

locale.json

{
    "helloWorld": "Hello world!",
    "unusedString": "I'm never accessed"
}

After optimization

index.js

console.log($t(0)) // logs "Hello world!"

locale.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

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):

  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:

+ 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:

  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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].