Skip to main content
100%

Fork of Hello Package.json

✓ Published0🌍 Public
Ttim10133222
Last edited Jan 1, 2023
Created on Jan 1, 2023
Forked from Hello Package.json

This example showcases a custom package.json setup in VizHub that enables ES6 imports of React and D3 libraries without manual script tags. The app renders a large "Hello package.json" message using ReactDOM, while the package.json defines dependency versions and maps library names to browser globals via the vizhub.libraries field. It also specifies an ISC license.

AI-generated description

An example of VizHub's package.json features.

Wait, what!?

That's right! You can now use package.json in VizHub. It's a way to declare your dependencies. If you use this, there's no longer any need to add any <script> tags yourself to index.html. VizHub will automatically append these when the page runs, based on the dependencies defined in package.json. To be clear, the dependencies are not included in the bundle. They are still pulled in from a CDN.

Because dependencies are pulled in from a CDN via <script> tags, and those tags introduce browser globals that define the packages, VizHub somehow needs to know how to resolve package names (like the 'react' part of import React from 'react') to browser globals (such as window.React). To date, this mapping has been defined in vizhub-libraries, which is a big list of mappings. This approach is not ideal, because if a library is not on that list, you can't use it with ES6 import syntax. But all that is about to change.

To support usage of any package with ES6 import syntax, we've added a special vizhub.libraries field in package.json. This field lets you define the mappings from package names to browser globals yourself. Also, since the CDN path to the browser build is sometimes not at the package root, and can be different for each package (and there's no way of knowing programmatically what it is), you can also specify what path to use.

You can also specify a custom license in the license field of package.json. This field is expected to be a valid SPDX license identifier, as described in the NPM Package.json documentation. If a license field is not present, the default MIT License is used.

Here's an example:

<iframe height="450" width="100%" src="https://vizhub.com/curran/161a5fcfe2e148f583cc75efb8ab3255?mode=snippet&file=package.json#" title="Hello Package.json" frameborder="0" ></iframe>

In the above example, the vizhub.libraries field specifies the global and path for react and react-dom. For React, the global tells VizHub to resolve the 'react' package to window.React, and the path field tells VizHub to append /umd/react-dom.production.min.js to the CDN path used in the <script> tag that pulls in the library. The CDN paths used are of the form https://unpkg.com/${name}@${version}${path}.

By the way, the above snippet is embedded using VizHub's snippet embed feature.

ISC Licensed

Similar vizzes