npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tojson-loader

v1.0.5

Published

Serialize module exports as JSON. Cache generated static data as JSON at build time.

Downloads

88

Readme

tojson-loader

NPM NPM

Generate JSON assets at build-time

Build Status

If every client needs access to a shared chunk of data, and that data can be known at build time, a simple option is to bake that data into clientside build with tojson-loader.

Benefits

  • Having shared, static data preloaded in the client can improve performance/UX & reduce complexity.
  • Use whatever tools you need to generate the data on the serverside, no need to worry about shipping or shimming them on the client.
  • Reduce client-side processing load. Perform expensive calulations only once, on the server, at build-time.
  • Designed so tojson scripts can be transparently universal. Will run dynamic version on server & load static version on client.

Particularly useful for loading configuration and/or mock data into the client.

Caveats

  • Only supports JSON data types i.e. JSON can't natively serialise Function or Date objects.

Usage

// webpack.config.js
module.exports = {
  ...
  module: {
    loaders: [
      {
        // Use *.json.js extension to bake exported JS data into JSON
        test: /\.json\.js/,
        loader: 'tojson'
      }
      ...
    ]
  }
}

Example .json.js file

// data.json.js

// can use serverside-only dependencies
var fs = require('fs')
var readme = fs.readFileSync(__dirname + '/../../Readme.md', 'utf8')
readme = readme.split('\n')[0] // (just grab header for demo)

// any other dependencies that are only used in here won't be included in bundle
var tape = require('tape') // some random dependency

// whatever the value of module.exports is will be serialised to JSON
module.exports = {
  readme: readme,
  tape: tape, // tape happens to be a function so it won't serialise.
  random: Math.random(), // will be fixed to whatever value is generated at compile-time
}
// index.js
console.log(require('./data.json.js'))

Example Result

The transformed output after being built by webpack is below.

Note:

  • No dependencies on any of the modules (fs, tape) used.
  • Result of fs.readFileSync is baked into the output.
  • random key will always be the same value until next build.
  • tape key doesn't exist because JSON can't serialise functions.
...
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

	console.log(__webpack_require__(1))


/***/ },
/* 1 */
/***/ function(module, exports) {

	module.exports = {"readme":"# tojson-loader","random":0.5418716457206756}

/***/ }
/******/ ]);

License

ISC