From e75ef50b292677b25e90cb003f768f013da48675 Mon Sep 17 00:00:00 2001 From: Andrey Polischuk Date: Tue, 17 Oct 2023 16:13:56 +0300 Subject: [PATCH] feat(environment): add plugin --- packages/environment/README.md | 37 +++++++++++++++++++++++++++++++ packages/environment/index.js | 18 +++++++++++++++ packages/environment/package.json | 18 +++++++++++++++ yarn.lock | 21 +++++++++++++++++- 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 packages/environment/README.md create mode 100644 packages/environment/index.js create mode 100644 packages/environment/package.json diff --git a/packages/environment/README.md b/packages/environment/README.md new file mode 100644 index 0000000..b354b85 --- /dev/null +++ b/packages/environment/README.md @@ -0,0 +1,37 @@ +# Environment Plugin + +Pass environment variables to app with [`dotenv`](https://github.com/motdotla/dotenv) and custom variables + +## Install + +``` +npm install -D @rambler-tech/razzle-environment +``` + +or + +``` +yarn add -D @rambler-tech/razzle-environment +``` + +## Usage + +Add the plugin to `razzle.config.js` + +```js +const EnvironmentPlugin = require('@rambler-tech/razzle-environment') + +const VERSION = '...' + +module.exports = { + plugins: [ + EnvironmentPlugin({ + VERSION + }) + ], + modifyWebpackConfig({webpackConfig}) { + // ... + return webpackConfig + } +} +``` diff --git a/packages/environment/index.js b/packages/environment/index.js new file mode 100644 index 0000000..eae9541 --- /dev/null +++ b/packages/environment/index.js @@ -0,0 +1,18 @@ +require('dotenv').config() + +const webpack = require('webpack') +const DotenvPlugin = require('dotenv-webpack') + +module.exports = (variables) => ({ + modifyWebpackConfig({webpackConfig}) { + webpackConfig.plugins = [ + ...webpackConfig.plugins, + new webpack.EnvironmentPlugin(variables), + new DotenvPlugin({ + systemvars: true + }) + ] + + return webpackConfig + } +}) diff --git a/packages/environment/package.json b/packages/environment/package.json new file mode 100644 index 0000000..0c8e1d2 --- /dev/null +++ b/packages/environment/package.json @@ -0,0 +1,18 @@ +{ + "name": "@rambler-tech/razzle-environment", + "version": "0.0.0", + "main": "index.js", + "license": "MIT", + "sideEffects": false, + "publishConfig": { + "access": "public" + }, + "dependencies": { + "dotenv": "^16.3.1", + "dotenv-webpack": "^8.0.1" + }, + "peerDependencies": { + "razzle": ">=4", + "webpack": ">=5" + } +} diff --git a/yarn.lock b/yarn.lock index 6a8d993..f9d9247 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2109,16 +2109,35 @@ dot-prop@^5.1.0: dependencies: is-obj "^2.0.0" +dotenv-defaults@^2.0.2: + version "2.0.2" + resolved "https://registry.yarnpkg.com/dotenv-defaults/-/dotenv-defaults-2.0.2.tgz#6b3ec2e4319aafb70940abda72d3856770ee77ac" + integrity sha512-iOIzovWfsUHU91L5i8bJce3NYK5JXeAwH50Jh6+ARUdLiiGlYWfGw6UkzsYqaXZH/hjE/eCd/PlfM/qqyK0AMg== + dependencies: + dotenv "^8.2.0" + dotenv-expand@~10.0.0: version "10.0.0" resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-10.0.0.tgz#12605d00fb0af6d0a592e6558585784032e4ef37" integrity sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A== -dotenv@~16.3.1: +dotenv-webpack@^8.0.1: + version "8.0.1" + resolved "https://registry.yarnpkg.com/dotenv-webpack/-/dotenv-webpack-8.0.1.tgz#6656550460a8076fab20e5ac2eac867e72478645" + integrity sha512-CdrgfhZOnx4uB18SgaoP9XHRN2v48BbjuXQsZY5ixs5A8579NxQkmMxRtI7aTwSiSQcM2ao12Fdu+L3ZS3bG4w== + dependencies: + dotenv-defaults "^2.0.2" + +dotenv@^16.3.1, dotenv@~16.3.1: version "16.3.1" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.3.1.tgz#369034de7d7e5b120972693352a3bf112172cc3e" integrity sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ== +dotenv@^8.2.0: + version "8.6.0" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.6.0.tgz#061af664d19f7f4d8fc6e4ff9b584ce237adcb8b" + integrity sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g== + duplexer@^0.1.1: version "0.1.2" resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6"