Skip to content

Commit

Permalink
fix: Remove webpack imports in npm package (#925)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhousley authored Mar 25, 2024
1 parent fb4e36b commit 7b35238
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 50 deletions.
27 changes: 3 additions & 24 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,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,15 +65,6 @@ module.exports = function (api, ...args) {
}
}
]
],
plugins: [
[
'./tools/babel/plugins/transform-import',
{
'(constants/)env$': '$1env.cdn',
'polyfill-detection$': 'polyfill-detection.es5'
}
]
]
},
'npm-cjs': {
Expand All @@ -97,9 +80,7 @@ module.exports = function (api, ...args) {
[
'./tools/babel/plugins/transform-import',
{
'(/constants/|^\\./)env$': '$1env.npm',
'(/configure/|^\\./)public-path$': '$1public-path.npm',
'(/configure/|^\\./)nonce$': '$1nonce.npm'
'(/constants/|^\\./)env$': '$1env.npm'
}
]
]
Expand All @@ -117,9 +98,7 @@ module.exports = function (api, ...args) {
[
'./tools/babel/plugins/transform-import',
{
'(/constants/|^\\./)env$': '$1env.npm',
'(/configure/|^\\./)public-path$': '$1public-path.npm',
'(/configure/|^\\./)nonce$': '$1nonce.npm'
'(/constants/|^\\./)env$': '$1env.npm'
}
]
]
Expand Down
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
} catch (ex) {
// Swallow error and proceed like nonce is not defined
// This will happen when the agent is loaded in a worker scope
}

return ''
})()
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')
// 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
}
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.

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 7b35238

Please sign in to comment.