Skip to content

Commit

Permalink
fix src dir in package
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhousley committed Mar 25, 2024
1 parent 648e6d7 commit 0e1a128
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 14 deletions.
26 changes: 18 additions & 8 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ module.exports = function (api, ...args) {
'**/*.component-test.js',
'**/__mocks__/*.js'
]
const npmIgnore = [
...ignore,
'src/common/config/env.cdn.js',
'src/loaders/configure/nonce.cdn.js',
'src/loaders/configure/public-path.cdn.js'
]
const presets = [
'@babel/preset-env'
]
Expand Down Expand Up @@ -74,23 +68,39 @@ module.exports = function (api, ...args) {
]
},
'npm-cjs': {
ignore: npmIgnore,
ignore,
presets: [
[
'@babel/preset-env', {
modules: 'commonjs'
}
]
],
plugins: [
[
'./tools/babel/plugins/transform-import',
{
'(/constants/|^\\./)env$': '$1env.npm'
}
]
]
},
'npm-esm': {
ignore: npmIgnore,
ignore,
presets: [
[
'@babel/preset-env', {
modules: false
}
]
],
plugins: [
[
'./tools/babel/plugins/transform-import',
{
'(/constants/|^\\./)env$': '$1env.npm'
}
]
]
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/common/constants/env.js
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
26 changes: 26 additions & 0 deletions src/common/constants/env.npm.js
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
7 changes: 7 additions & 0 deletions src/common/constants/env.npm.test.js
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')
})
67 changes: 67 additions & 0 deletions tools/babel/plugins/transform-import.js
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)
}
}
}
}

0 comments on commit 0e1a128

Please sign in to comment.