-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
648e6d7
commit 0e1a128
Showing
5 changed files
with
126 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
/** | ||
* @file This file exposes NPM build environment variables. These variables will | ||
* be overridden with babel. | ||
* @file This file exposes build time environment variables that will be included in the | ||
* build output of the agent. This file specifically contains the normal environment variables | ||
* for the NPM agent build and will be overridden by webpack/babel during the build based on the | ||
* type of build being performed. | ||
*/ | ||
|
||
import pkgJSON from '../../../package.json' | ||
|
||
/** | ||
* Exposes the version of the agent | ||
*/ | ||
export const VERSION = process.env.BUILD_VERSION | ||
export const VERSION = pkgJSON.version | ||
|
||
/** | ||
* Exposes the build type of the agent | ||
* Valid values are LOCAL, PROD, DEV | ||
*/ | ||
export const BUILD_ENV = 'NPM' | ||
|
||
/** | ||
* Exposes the distribution method of the agent | ||
* Valid valuse are CDN, NPM | ||
*/ | ||
export const DIST_METHOD = 'NPM' | ||
|
||
/** | ||
* Exposes the lib version of rrweb | ||
*/ | ||
export const RRWEB_VERSION = process.env.RRWEB_VERSION | ||
export const RRWEB_VERSION = pkgJSON.dependencies.rrweb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @file This file exposes NPM build environment variables. These variables will | ||
* be overridden with babel. | ||
*/ | ||
|
||
/** | ||
* Exposes the version of the agent | ||
*/ | ||
export const VERSION = process.env.BUILD_VERSION | ||
|
||
/** | ||
* Exposes the build type of the agent | ||
* Valid values are LOCAL, PROD, DEV | ||
*/ | ||
export const BUILD_ENV = 'NPM' | ||
|
||
/** | ||
* Exposes the distribution method of the agent | ||
* Valid valuse are CDN, NPM | ||
*/ | ||
export const DIST_METHOD = 'NPM' | ||
|
||
/** | ||
* Exposes the lib version of rrweb | ||
*/ | ||
export const RRWEB_VERSION = process.env.RRWEB_VERSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as env from './env.npm' | ||
|
||
test('should default environment variables to NPM values', () => { | ||
expect(env.VERSION).toMatch(/\d{1,3}\.\d{1,3}\.\d{1,3}/) | ||
expect(env.BUILD_ENV).toEqual('NPM') | ||
expect(env.DIST_METHOD).toEqual('NPM') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @file This custom babel plugin swaps out import statements. Whenever an import path is encountered, it | ||
* is checked against key-value pairs of RegEx patterns defined in the plugin config options block. | ||
* To understand the code (which uses the Visitor pattern to traverse an Abstract Source Tree), see the Babel | ||
* [plugin handbook](https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md). | ||
*/ | ||
|
||
/** | ||
* Replaces part(s) of an import path based on key-value RegEx pairs from plugin state options. | ||
* | ||
* @param {string} importPath - The original import path to modify. | ||
* @param {Object} state - A state object containing options to use for replacement. | ||
* @returns {string} - The modified import path with the replaced value. | ||
*/ | ||
function replacePath (importPath, state) { | ||
if (!importPath) return importPath | ||
|
||
let newPath = importPath | ||
|
||
for (let key in state.opts) { | ||
const regex = new RegExp(key) | ||
if (importPath.match(regex)) { | ||
newPath = importPath.replace(regex, state.opts[key]) | ||
} | ||
} | ||
|
||
return newPath | ||
} | ||
|
||
module.exports = function ({ types: t }) { | ||
/** | ||
* A visitor object containing methods for modifying specific types of nodes encountered in the Abstract Source Tree. | ||
* The state parameter is used to pass along the current state of the plugin as it traverses the AST. We use it | ||
* specifically for access to plugin options. | ||
* | ||
* @type {Object} | ||
* @property {Function} CallExpression - A function to modify CallExpression nodes. | ||
* @property {Function} ImportDeclaration - A function to modify ImportDeclaration nodes. | ||
* @property {Function} ExportNamedDeclaration - A function to modify ExportNamedDeclaration nodes. | ||
* @property {Function} ExportAllDeclaration - A function to modify ExportAllDeclaration nodes. | ||
*/ | ||
const visitor = { | ||
ImportDeclaration (path, state) { | ||
if (path.node.source) { | ||
path.node.source.value = replacePath(path.node.source.value, state) | ||
} | ||
}, | ||
ExportNamedDeclaration (path, state) { | ||
if (path.node.source) { | ||
path.node.source.value = replacePath(path.node.source.value, state) | ||
} | ||
}, | ||
ExportAllDeclaration (path, state) { | ||
if (path.node.source) { | ||
path.node.source.value = replacePath(path.node.source.value, state) | ||
} | ||
} | ||
} | ||
|
||
return { | ||
visitor: { | ||
Program (path, state) { | ||
path.traverse(visitor, state) | ||
} | ||
} | ||
} | ||
} |