Skip to content

Commit

Permalink
Add and configure git-revision-webpack-plugin
Browse files Browse the repository at this point in the history
Configured `git-revision-webpack-plugin` (1) to provide the version,
commit hash and branch as environment variables through through the
`webpack.DefinePlugin` (2).
The data is exposed through the following environment variables:

- `NORD_DOCS_GIT_VERSION`
- `NORD_DOCS_GIT_COMMITHASH`
- `NORD_DOCS_GIT_BRANCH`

References:
  (1) https://github.com/pirelenito/git-revision-webpack-plugin
  (2) https://webpack.js.org/plugins/define-plugin

GH-31
  • Loading branch information
arcticicestudio committed Nov 19, 2018
1 parent 87fb27a commit 60cff32
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
40 changes: 39 additions & 1 deletion .gatsby/onCreateWebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

const { resolve: resolvePath } = require("path");
/* eslint-disable import/no-extraneous-dependencies */
const webpack = require("webpack");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const GitRevisionPlugin = require("git-revision-webpack-plugin");
/* eslint-enable import/no-extraneous-dependencies */

const r = m => resolvePath(__dirname, m);
Expand All @@ -27,6 +29,7 @@ const r = m => resolvePath(__dirname, m);
* Configuration for the `webpack-bundle-analyzer` plugin.
*
* @type {object}
* @see https://github.com/webpack-contrib/webpack-bundle-analyzer
* @since 0.1.0
*/
const bundleAnalyzerPluginConfig = {
Expand All @@ -37,6 +40,32 @@ const bundleAnalyzerPluginConfig = {
statsFilename: r("../build/reports/webpack-bundle-analyzer/stats.json")
};

/**
* Configuration for the `git-revision-webpack-plugin` plugin.
*
* @type {object}
* @see https://github.com/pirelenito/git-revision-webpack-plugin
* @since 0.1.0
*/
const gitRevisionPluginConfig = {
branch: true
};

const gitRevisionPlugin = new GitRevisionPlugin(gitRevisionPluginConfig);

/**
* Configuration for `webpack.DefinePlugin`.
*
* @type {object}
* @see https://webpack.js.org/plugins/define-plugin
* @since 0.1.0
*/
const definePluginConfig = {
"process.env.NORD_DOCS_GIT_VERSION": JSON.stringify(gitRevisionPlugin.version()),
"process.env.NORD_DOCS_GIT_COMMITHASH": JSON.stringify(gitRevisionPlugin.commithash()),
"process.env.NORD_DOCS_GIT_BRANCH": JSON.stringify(gitRevisionPlugin.branch())
};

/**
* Implementation of the Gatsby Node `onCreateWebpackConfig` API.
*
Expand Down Expand Up @@ -72,7 +101,16 @@ const onCreateWebpackConfig = ({ actions, stage }) => {
case "build-html":
case "build-javascript":
actions.setWebpackConfig({
plugins: [new BundleAnalyzerPlugin(bundleAnalyzerPluginConfig)]
plugins: [
new BundleAnalyzerPlugin(bundleAnalyzerPluginConfig),
new GitRevisionPlugin(),
new webpack.DefinePlugin(definePluginConfig)
]
});
break;
case "develop":
actions.setWebpackConfig({
plugins: [new webpack.DefinePlugin(definePluginConfig)]
});
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/config/internal/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* The absolute path of the content base directory starting from the project root.
*
* @constant {String}
* @constant {string}
* @since 0.1.0
*/
const BASE_DIR_CONTENT = "content";
Expand Down Expand Up @@ -65,9 +65,9 @@ const BASE_DIR_PAGES = `${BASE_DIR_SRC}/pages`;
/**
* The internal type for MDX nodes.
*
* @constant {String}
* @since 0.1.0
* @constant {string}
* @see https://github.com/mdx-js/mdx
* @since 0.1.0
*/
const NODE_TYPE_MDX = "Mdx";

Expand Down

0 comments on commit 60cff32

Please sign in to comment.