fix: oauth2 not defined by exports
Webpack 5 generates an error when using the CopyWebpackPlugin to copy the oauth2 redirect page.
new CopyWebpackPlugin({
patterns: [{
// Copy the Swagger OAuth2 redirect file to the project root;
// that file handles the OAuth2 redirect after authenticating the end-user.
from: require.resolve('swagger-ui/dist/oauth2-redirect.html'),
to: './'
}]
})
Error
Failed to load '/app/src/docs/webpack.config.js' config
[webpack-cli] Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/oauth2-redirect.html' is not defined by "exports" in /app/src/docs/node_modules/swagger-ui/package.json
at new NodeError (node:internal/errors:371:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:453:9)
at packageExportsResolve (node:internal/modules/esm/resolve:731:3)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.resolve (node:internal/modules/cjs/helpers:108:19)
at Object.<anonymous> (/app/src/docs/webpack.config.js:43:23)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
Description
Marking the file as allowed to be exported in package.json.
Motivation and Context
This change fixes webpack5 with oauth.
How Has This Been Tested?
I changed the package.json in my node modules and reran the build and it succedded.
root@5a8544c102c9:/app/src/docs# npm start
> [email protected] start
> webpack-dev-server --open
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:8080/
<i> [webpack-dev-server] Content not from webpack is served from '/app/src/docs/public' directory
<i> [webpack-dev-middleware] wait until bundle finished: /
asset app.bundle.js 11.2 MiB [emitted] (name: app)
asset oauth2-redirect.html 2.63 KiB [emitted] [from: node_modules/swagger-ui/dist/oauth2-redirect.html] [copied]
Screenshots (if appropriate):
Checklist
My PR contains...
- [x] No code changes (
src/is unmodified: changes to documentation, CI, metadata, etc.) - [] Dependency changes (any modification to dependencies in
package.json) - [x] Bug fixes (non-breaking change which fixes an issue)
- [ ] Improvements (misc. changes to existing features)
- [ ] Features (non-breaking change which adds functionality)
My changes...
- [ ] are breaking changes to a public API (config options, System API, major UI change, etc).
- [ ] are breaking changes to a private API (Redux, component props, utility functions, etc.).
- [ ] are breaking changes to a developer API (npm script behavior changes, new dev system dependencies, etc).
- [x] are not breaking changes.
Documentation
- [x] My changes do not require a change to the project documentation.
- [ ] My changes require a change to the project documentation.
- [ ] If yes to above: I have updated the documentation accordingly.
Automated tests
- [x] My changes can not or do not need to be tested.
- [ ] My changes can and should be tested by unit and/or integration tests.
- [ ] If yes to above: I have added tests to cover my changes.
- [ ] If yes to above: I have taken care to cover edge cases in my tests.
- [ ] All new and existing tests passed.
For others that hit this you can do this (dodgy?) workaround.
{
// Copy the Swagger OAuth2 redirect file to the project root;
// that file handles the OAuth2 redirect after authenticating the end-user.
from: path.join(__dirname, 'node_modules', 'swagger-ui', 'dist', 'oauth2-redirect.html'),
to: './'
},
For the maintainers, if we can decide on a solution I will happily update the web-pack-getting-started sample to use the latest dependencies.
@ted-marozzi Thanks for the PR! I would prefer updating the samples/webpack-getting-started/webpack.config.js that you cited in the comments, rather than adding the oauth2-redirect.html directly to package.json. The reason is because it appears that most(?) consumers of SwaggerUI do not need a change to package.json, therefore we can limit the change to the sample configuration documentation.
I think I wasn't clear, this change would need to be merged to update the sample, webpack 5 is incompatible with the sample until this is merged. When it is merged I can update the sample config to use webpack 5
@ted-marozzi Ah, thanks for the clarification. PR is now merged! Thanks for the contribution!