Skip to main content

Docusaurus JSON environment loader

· 2 min read
Simon Porritt
VisuallyJs Development
info

This post is from JsPlumb, which is now in maintenance mode. We still use this plugin in the VisuallyJs site though.

JsPlumb uses Docusaurus, a super handy static site generator, for various parts of our ecosystem, including our Documentation and also our recently released stand alone components product.

While developing the site for JsPlumb Components, we wanted to include a couple of pieces of information that would vary depending on whether we were running in development mode locally (ie docusaurus start) or building for production. We looked into the various options available, all based on the dotenv plugin for Webpack, but couldn't find what we were looking for: a solution that worked, with minimal manual intervention, in both development mode and when building for production.

So we ended up building our own plugin. This plugin reads values from a JSON file and makes them available to your site via the customFields map in the Docusaurus siteConfig.

note

When I say "Docusaurus" in this post I am talking about v2/v3. I've not used v1.

Installation

npm i @jsplumb/docusaurus-plugin-env-loader-json

Configuration

You just have to add the plugin to the list of plugins in docusaurus.config.js:

plugins:[
"@jsplumb/docusaurus-plugin-env-loader-json"
],

and then create an env.json file in the Docusaurus project directory.

env.json

Your environment variables should be keyed under a section that identifies the environment you are targetting - "development" when using docusaurus start and "production" when running a build. The plugin reads the environment name from the environment variable NODE_ENV.

{
"production":{
"SERVER_URL":"https://some.server.com/anEndpoint"
},
"development":{
"SERVER_URL":"http://localhost:4200/anEndpoint"
}
}

Accessing values

You can access these values via the customFields section of the Docusaurus site config:

import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

export function MyApp {

const {siteConfig} = useDocusaurusContext();
const serverUrl = siteConfig.customFields.SERVER_URL

...

}

Changing the configuration file name

If you want to specify some file other than env.json in your project's root, you can do so by setting the sourceFile option of the plugin:

plugins:[
[
"@jsplumb/docusaurus-plugin-env-loader-json",
{
"sourceFile":"path/to/aFile.json"
}
]
],

This path should be specified relative to the project root. No leading slash is required.



Try VisuallyJs

VisuallyJs offers an extensive list of starter apps, diagrams, charts and dashboards to quick start your development, in React, Angular, Vue, Svelte and Typescript/Javascript.

VisuallyJs - JavaScript and Typescript diagramming library that fuels exceptional UIs
Call Flow
Use VisuallyJs to build a visual Call Flow editor
VisuallyJs - fully featured alternative to ReactFlow and ngDiagram
AI Agent Builder
Use VisuallyJs to create an advanced AI agent builder
VisuallyJs - leading alternative to GoJS, JointJS, ReactFlow and SvelteFlow
Chatbot
Use VisuallyJs to build a Chatbot editor, with actions, messages, input and choices
VisuallyJs - callflow builders, sankey charts, database schemas, ERD diagrams and more
Flowchart
Fully featured flowchart builder including support for custom shapes, edge routing to avoid vertices, shape resize/rotate, SVG/PNG/JPG export and more
VisuallyJs - leading alternative to GoJS, JointJS, ReactFlow and SvelteFlow
BPMN
BPMN editor for modelling the steps of a business process. Pools, lanes, and a full set of task, event and gateway types
When you've reached the limits with ReactFlow, VisuallyJs has what you need
ERD
ERD editor for modelling the steps of a business process
VisuallyJs - effortlessly build professional node based UIs with Javascript, Typescript, React, Svelte, Angular and Vue
Gantt
Interactive Gantt chart featuring tasks, task groups and milestones
VisuallyJs - effortlessly build professional node based UIs with Javascript, Typescript, React, Svelte, Angular and Vue
Kanban
Fully featured Kanban board. Drag items between columns and use the inspector to update items and columns
VisuallyJs - JavaScript and Typescript diagramming library that fuels exceptional UIs
Database Schema
Database Schema builder with support for tables, views, multiple columns types, and column relationships
VisuallyJs - leading alternative to GoJS, JointJS, ReactFlow and SvelteFlow
Org Chart
Uses the classic org chart layout and provides an inspector from which the user can navigate around
VisuallyJs - effortlessly build professional node based UIs with Javascript, Typescript, React, Svelte, Angular and Vue
Circuit Diagram
Fully featured starter app containing a circuit diagram builder
VisuallyJs - JavaScript and Typescript diagramming library that fuels exceptional UIs
Scada/HMI
Professional and modern Scada/HMI application with fluid, heating/cooling & instrumentation shapes, adhering to the HMI ISA-101 Design Standard
When you've reached the limits with ReactFlow, VisuallyJs has what you need
Mindmap
The mindmap builder highlights several advanced features, such as custom layouts, parsers and exporters
VisuallyJs - leading alternative to GoJS, JointJS, ReactFlow and SvelteFlow
Neighbourhood Views
Demonstrates how to include multiple views of a dataset on one page
VisuallyJs - industry standard diagramming and rich visual UI Javascript and Typescript library
Logic Gates
Fully featured starter app containing a logic gates diagram builder
VisuallyJs - effortlessly build professional node based UIs with Javascript, Typescript, React, Svelte, Angular and Vue
Template
Basic starter app demonstrating how to setup VisuallyJs and its main features
VisuallyJs - industry standard diagramming and rich visual UI Javascript and Typescript library
Area & Line charts
Use VisuallyJs to create area and line charts
When you've reached the limits with ReactFlow, VisuallyJs has what you need
Bar & Column charts
Multiple series, stacked, grouped, pivoted, and much more
VisuallyJs - flowcharts, AI Agent builders, chatbots, bar charts, decision trees, mindmaps, org charts and more
Scatter & Bubble charts
Circle, rectangle, triangle or custom markers, multiple series, fully customizable
VisuallyJs - callflow builders, sankey charts, database schemas, ERD diagrams and more
Sankey chart
Use VisuallyJs to create a professional Sankey chart, with support for pivoting
When you've reached the limits with SvelteFlow, VisuallyJs has what you need
Supply Chain Analyzer
Dashboard for managing and analyzing supply chains
VisuallyJs - callflow builders, sankey charts, database schemas, ERD diagrams and more
Network Infrastructure
Combine a network management diagram with charts showing projected cost and resource usage
VisuallyJs - JavaScript and Typescript diagramming library that fuels exceptional UIs
Scrolling Lists
Use the ListManager plugin to manage scrolling lists: as elements are scrolled out of the view, their edges are moved to the list container
When you've reached the limits with ngDiagram, VisuallyJs has what you need
FIFA World Cup
A visualizer for the FIFA World cup - group stages, team journeys and a tournament view.
VisuallyJs - industry standard diagramming and rich visual UI Javascript and Typescript library
Fault Tree Analysis
Combines a fault tree analysis diagram with charts showing risk and list of cut sets