Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove webpack imports in npm package #925

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ''

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#L10-L11

Added lines #L10 - L11 were 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')
// 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"
]
}
Loading