Skip to content

Commit

Permalink
fix: Remove webpack imports in npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhousley committed Mar 22, 2024
1 parent 9482e8f commit ab29644
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 174 deletions.
49 changes: 9 additions & 40 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ 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 @@ -45,15 +51,7 @@ module.exports = function (api, ...args) {
]
},
webpack: {
ignore,
plugins: [
[
'./tools/babel/plugins/transform-import',
{
'(constants/)env$': '$1env.cdn'
}
]
]
ignore
},
'webpack-ie11': {
ignore,
Expand All @@ -73,55 +71,26 @@ module.exports = function (api, ...args) {
}
}
]
],
plugins: [
[
'./tools/babel/plugins/transform-import',
{
'(constants/)env$': '$1env.cdn',
'polyfill-detection$': 'polyfill-detection.es5'
}
]
]
},
'npm-cjs': {
ignore,
ignore: npmIgnore,
presets: [
[
'@babel/preset-env', {
modules: 'commonjs'
}
]
],
plugins: [
[
'./tools/babel/plugins/transform-import',
{
'(/constants/|^\\./)env$': '$1env.npm',
'(/configure/|^\\./)public-path$': '$1public-path.npm',
'(/configure/|^\\./)nonce$': '$1nonce.npm'
}
]
]
},
'npm-esm': {
ignore,
ignore: npmIgnore,
presets: [
[
'@babel/preset-env', {
modules: false
}
]
],
plugins: [
[
'./tools/babel/plugins/transform-import',
{
'(/constants/|^\\./)env$': '$1env.npm',
'(/configure/|^\\./)public-path$': '$1public-path.npm',
'(/configure/|^\\./)nonce$': '$1nonce.npm'
}
]
]
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/common/constants/env.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
/**
* @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.
* @file This file exposes NPM build environment variables. These variables will
* be overridden with babel.
*/

import pkgJSON from '../../../package.json'

/**
* Exposes the version of the agent
*/
export const VERSION = pkgJSON.version
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 = pkgJSON.dependencies.rrweb
export const RRWEB_VERSION = process.env.RRWEB_VERSION
26 changes: 0 additions & 26 deletions src/common/constants/env.npm.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/common/constants/env.npm.test.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/loaders/configure/nonce.cdn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* global __webpack_require__ */

__webpack_require__.nc = (() => {
try {
return document?.currentScript?.nonce

Check warning on line 5 in src/loaders/configure/nonce.cdn.js

View check run for this annotation

Codecov / codecov/patch

src/loaders/configure/nonce.cdn.js#L3-L5

Added lines #L3 - L5 were not covered by tests
} catch (ex) {
// Swallow error and proceed like nonce is not defined
// This will happen when the agent is loaded in a worker scope
}

return ''

Check warning on line 11 in src/loaders/configure/nonce.cdn.js

View check run for this annotation

Codecov / codecov/patch

src/loaders/configure/nonce.cdn.js#L11

Added line #L11 was not covered by tests
})()
13 changes: 1 addition & 12 deletions src/loaders/configure/nonce.js
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
/* global __webpack_require__ */

__webpack_require__.nc = (() => {
try {
return document?.currentScript?.nonce
} catch (ex) {
// Swallow error and proceed like nonce is not defined
// This will happen when the agent is loaded in a worker scope
}

return ''
})()
// We don't support setting automating the nonce attribute in the npm package
1 change: 0 additions & 1 deletion src/loaders/configure/nonce.npm.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/loaders/configure/public-path.cdn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Set the default CDN or remote for fetching the assets; NPM shouldn't change this var.

export const redefinePublicPath = (urlString) => {
const isOrigin = urlString.startsWith('http')

Check warning on line 4 in src/loaders/configure/public-path.cdn.js

View check run for this annotation

Codecov / codecov/patch

src/loaders/configure/public-path.cdn.js#L3-L4

Added lines #L3 - L4 were not covered by tests
// Input is not expected to end in a slash, but webpack concats as-is, so one is inserted.
urlString += '/'

Check warning on line 6 in src/loaders/configure/public-path.cdn.js

View check run for this annotation

Codecov / codecov/patch

src/loaders/configure/public-path.cdn.js#L6

Added line #L6 was not covered by tests
// If there's no existing HTTP scheme, the secure protocol is prepended by default.
__webpack_public_path__ = isOrigin ? urlString : 'https://' + urlString // eslint-disable-line
}
10 changes: 2 additions & 8 deletions src/loaders/configure/public-path.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
// Set the default CDN or remote for fetching the assets; NPM shouldn't change this var.

export const redefinePublicPath = (urlString) => {
const isOrigin = urlString.startsWith('http')
// Input is not expected to end in a slash, but webpack concats as-is, so one is inserted.
urlString += '/'
// If there's no existing HTTP scheme, the secure protocol is prepended by default.
__webpack_public_path__ = isOrigin ? urlString : 'https://' + urlString // eslint-disable-line
export const redefinePublicPath = () => {
// We don't support setting public path in webpack via NPM build.
}
3 changes: 0 additions & 3 deletions src/loaders/configure/public-path.npm.js

This file was deleted.

67 changes: 0 additions & 67 deletions tools/babel/plugins/transform-import.js

This file was deleted.

11 changes: 10 additions & 1 deletion tools/webpack/configs/common.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@ export default (env, asyncChunkName) => {
new NRBARemoveNonAsciiPlugin(),
new NRBALoaderApmCheckPlugin(),
new NRBASubresourceIntegrityPlugin(),
new NRBAFuzzyLoadersPlugin()
new NRBAFuzzyLoadersPlugin(),
new webpack.NormalModuleReplacementPlugin(/(?:[\\/]constants)?[\\/]env/, (resource) => {
resource.request = resource.request.replace(/env$/, 'env.cdn')
}),
new webpack.NormalModuleReplacementPlugin(/(?:[\\/]configure)?[\\/]public-path/, (resource) => {
resource.request = resource.request.replace(/public-path$/, 'public-path.cdn')
}),
new webpack.NormalModuleReplacementPlugin(/(?:[\\/]configure)?[\\/]nonce/, (resource) => {
resource.request = resource.request.replace(/nonce$/, 'nonce.cdn')
})
]
}
}
9 changes: 8 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["**/*.test.js", "**/*.component-test.js","**/__mocks__/*.js"]
"exclude": [
"**/*.test.js",
"**/*.component-test.js",
"**/__mocks__/*.js",
"src/common/config/env.cdn.js",
"src/loaders/configure/nonce.cdn.js",
"src/loaders/configure/public-path.cdn.js"
]
}

0 comments on commit ab29644

Please sign in to comment.