npm install --save @desco/env-require
Note that it will be necessary to have NPM installed for the command to work.
const configEnvRequire = require("@desco/env-require")
import configEnvRequire from "@desco/env-require"
module.exports = {
'@desco/atlas': () => require('../../atlas'),
}
module.exports = {
'@desco/atlas': () => require('@desco/atlas'),
...require('./dynamicImports_dev')
}
const envRequire = require('@desco/env-require')(packagesDynamicImports)
envRequire('@desco/atlas')
In
dynamicImports_dev.js
export an object containing in your keys the names of the packages and in their values a function containing the loading of the local version of the package.
In
dynamicImports.js
export an object containing the names of the packages in its keys and in its values a function containing the loading of the NPM version of the package.
At the end of the
dynamicImports.js
object, concatenate the importer values ofdynamicImports_dev.js
, so the existing packages will overwrite the previous ones.
Import
envRequire
already running and passing the imported object fromdynamicImports.js
, that way it will already know which packages it will work with.
DynamicImports_dev.js
should only be versioned with an empty object so that NPM packages are always used in production. After empty versioning, adddynamicImports_dev.js
to.gitignore
so that the changes for each programmer are not sent to production.
Make sure the local packages have the packages installed! (
npm install
)
Now just use
envRequire
instead ofrequire
/import
!
Eventually you may want to have two instances of envRequire
, to do this just pass a second parameter with the name of the scope of each instance!
const envRequire = require('@desco/env-require')(packagesDynamicImports, 'main)
const envRequire = require('@desco/env-require')(packagesDynamicImports, 'second')
envRequire('@desco/atlas')
envRequire2('@desco/urano')
By default the scope name is
default
, so there is no need to pass a scope if you are using a single instance.
Rafael A. R. Dias |