Skip to content

Commit

Permalink
feat: webpack dotenv support
Browse files Browse the repository at this point in the history
(cherry picked from commit 6b2f41a)
  • Loading branch information
hayes committed Feb 19, 2022
1 parent 42b359e commit fd00413
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/docs/features/bundles.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: bundles
title: Bundles
sidebar_label: Bundles
slug: /features
slug: /features/
---

Template and layout bundles are a build-time optimization which ensures
Expand Down Expand Up @@ -134,4 +134,4 @@ where the layout option value is the name of the layout.
### Bundle analyzer
Packer's [build script](/docs/commands#build) can use Webpack Bundle Analyzer to see what's inside each bundle for further optimization

![Bundle Analyzer](../../static/img/webpack-bundle-analyzer.png)
![Bundle Analyzer](../../static/img/webpack-bundle-analyzer.png)
24 changes: 24 additions & 0 deletions docs/docs/features/dotenv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
id: dotenv
title: Dotenv
sidebar_label: Dotenv
slug: /features/dotenv
---

Packer uses dotenv and webpack to load any environment values you have stored in ``.env``

## Usage

Create an .env file

```dotenv
S3_BUCKET="YOURS3BUCKET"
SECRET_KEY="YOURSECRETKEYGOESHERE"
```

Use it in your code

```javascript
console.log(process.env.S3_BUCKET)
// YOURS3BUCKET
```
1 change: 1 addition & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
'config/webpack',
],
Features: [
'features/dotenv',
'features/bundles',
'features/liquid-styles',
'features/admin-api',
Expand Down
7 changes: 5 additions & 2 deletions src/webpack/config/dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const css = require('../parts/css');
const scss = require('../parts/scss');
const assets = require('../parts/assets');
const liquidStyles = require('../parts/liquid-styles');
const env = require('../parts/env');
const copy = require('../parts/copy');

const mergeDev = customConfigCheck(config.get('merge.dev'));
Expand All @@ -40,6 +41,7 @@ module.exports = merge([
scss,
css,
copy,
env,
{
mode: 'development',
devtool: 'eval-source-map',
Expand All @@ -65,8 +67,9 @@ module.exports = merge([
],
}),

new webpack.DefinePlugin({
'process.env': {NODE_ENV: '"development"'},
new webpack.EnvironmentPlugin({
NODE_ENV: 'development',
DEBUG: false,
}),

new webpack.HotModuleReplacementPlugin(),
Expand Down
7 changes: 5 additions & 2 deletions src/webpack/config/prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const assets = require('../parts/assets');
const copy = require('../parts/copy');
const optimization = require('../parts/optimization');
const liquidStyles = require('../parts/liquid-styles');
const env = require('../parts/env');
const mergeProd = customConfigCheck(config.get('merge.prod'));

core.entry = {
Expand All @@ -38,6 +39,7 @@ const output = merge([
scss,
css,
copy,
env,
{
mode: 'production',
devtool: false,
Expand All @@ -55,8 +57,9 @@ const output = merge([
filename: '[name].css',
}),

new webpack.DefinePlugin({
'process.env': {NODE_ENV: '"production"'},
new webpack.EnvironmentPlugin({
NODE_ENV: 'production',
DEBUG: false,
}),

new HtmlWebpackPlugin({
Expand Down
14 changes: 14 additions & 0 deletions src/webpack/parts/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const PackerConfig = require('../../config');
const config = new PackerConfig(require('../../../packer.schema'));
require('dotenv').config({path: `${config.get('root')}/.env`});
const webpack = require('webpack');

const env = {
plugins: [
new webpack.DefinePlugin({
'process.env': JSON.stringify(process.env),
}),
],
};

module.exports = env;

0 comments on commit fd00413

Please sign in to comment.