├── package.json ├── index.js └── README.md /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "src/index.js", 3 | "name": "@jsplumb/docusaurus-plugin-env-loader-json", 4 | "version": "1.0.0" 5 | } 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const fs = require("fs") 2 | const path = require('path') 3 | 4 | const DEFAULT_FILENAME = 'env.json' 5 | 6 | module.exports = function(context, options) { 7 | 8 | options = options || {} 9 | const sourceFile = options.filename || DEFAULT_FILENAME 10 | 11 | const environment = process.env.NODE_ENV 12 | const customFields = context.siteConfig.customFields 13 | const configPath = path.resolve(context.siteDir, sourceFile) 14 | 15 | try { 16 | const configContents = fs.readFileSync(configPath).toString() 17 | const config = JSON.parse(configContents) 18 | 19 | const envConfig = config[environment] || {} 20 | if (envConfig) { 21 | console.log(`INFO: loading config for current environment ${environment}`) 22 | for (let key in envConfig) { 23 | customFields[key] = envConfig[key] 24 | } 25 | } else { 26 | console.log(`WARN: no config found for current environment ${environment}`) 27 | } 28 | } catch (e) { 29 | console.log(`ERROR: could not initialise env-loader-json plugin: ${e}`) 30 | } 31 | 32 | return { 33 | name: 'env-loader-json' 34 | }; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## env-loader-json 2 | 3 | This Docusaurus plugin loads configuration from a JSON file and sets the given values in the `customFields` section of the Docusaurus siteConfig. It is designed to run both in development mode (ie `docusaurus start`) and in production mode. 4 | 5 | The current environment is read from the `NODE_ENV` environment variable, which is `development` when running the dev server and `production` when running a build. 6 | 7 | 8 | ### Installation 9 | 10 | ``` 11 | npm i --save-dev @jsplumb/docusaurus-plugin-env-loader-json 12 | ``` 13 | 14 | ### Setup 15 | 16 | Add the plugin to the `plugins` section of `docusaurus.config.js`: 17 | 18 | ``` 19 | plugins:[ 20 | ..., 21 | "@jsplumb/docusaurus-plugin-env-loader-json", 22 | ... 23 | ], 24 | ``` 25 | 26 | By default, the plugin will look for a file called `env.json` in the Docusaurus project directory. You can change the name of the file the plugin looks for via the `sourceFile` option: 27 | 28 | ``` 29 | plugins:[ 30 | ..., 31 | [ 32 | "@jsplumb/docusaurus-plugin-env-loader-json", 33 | { 34 | "sourceFile":"aFolder/myEnv.json" 35 | } 36 | ] 37 | ... 38 | ], 39 | ``` 40 | 41 | Note that the path to the file is resolved relative to the Docusaurus project directory - a leading slash is not required. 42 | 43 | ### Configuration 44 | 45 | Inside the environment config file, you should declare a section for each environment: 46 | 47 | ``` 48 | { 49 | "production":{ 50 | "SERVER_URL":"https://some.server.com/anEndpoint" 51 | }, 52 | "development":{ 53 | "SERVER_URL":"http://localhost:4200/anEndpoint" 54 | } 55 | } 56 | ``` 57 | 58 | ### Accessing values 59 | 60 | You can access these values via the `customFields` section of the Docusaurus site config: 61 | 62 | ``` 63 | import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; 64 | 65 | export function MyApp { 66 | 67 | const {siteConfig} = useDocusaurusContext(); 68 | const serverUrl = siteConfig.customFields.SERVER_URL 69 | 70 | ... 71 | 72 | } 73 | ``` 74 | 75 | 76 | 77 | --------------------------------------------------------------------------------