chore: make Redoc lib compatible with Webpack 5 (#1982)

This commit is contained in:
Andrew Tatomyr 2022-05-10 14:50:50 +03:00 committed by GitHub
parent 7e01a0cfe2
commit 8678615a0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 42 deletions

View File

@ -1,7 +1,6 @@
import * as React from 'react'; import * as React from 'react';
import { render } from 'react-dom'; import { render } from 'react-dom';
import styled from 'styled-components'; import styled from 'styled-components';
import { resolve as urlResolve } from 'url';
import { RedocStandalone } from '../src'; import { RedocStandalone } from '../src';
import ComboBox from './ComboBox'; import ComboBox from './ComboBox';
import FileInput from './components/FileInput'; import FileInput from './components/FileInput';
@ -87,7 +86,7 @@ class DemoApp extends React.Component<
let proxiedUrl = specUrl; let proxiedUrl = specUrl;
if (specUrl !== DEFAULT_SPEC) { if (specUrl !== DEFAULT_SPEC) {
proxiedUrl = cors proxiedUrl = cors
? '\\\\cors.redoc.ly/' + urlResolve(window.location.href, specUrl) ? '\\\\cors.redoc.ly/' + new URL(specUrl, window.location.href).href
: specUrl; : specUrl;
} }
return ( return (

42
package-lock.json generated
View File

@ -9,7 +9,7 @@
"version": "2.0.0-rc.67", "version": "2.0.0-rc.67",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@redocly/openapi-core": "^1.0.0-beta.88", "@redocly/openapi-core": "^1.0.0-beta.95",
"classnames": "^2.3.1", "classnames": "^2.3.1",
"decko": "^1.2.0", "decko": "^1.2.0",
"dompurify": "^2.2.8", "dompurify": "^2.2.8",
@ -17,7 +17,7 @@
"json-pointer": "^0.6.2", "json-pointer": "^0.6.2",
"lunr": "^2.3.9", "lunr": "^2.3.9",
"mark.js": "^8.11.1", "mark.js": "^8.11.1",
"marked": "^4.0.10", "marked": "^4.0.15",
"mobx-react": "^7.2.0", "mobx-react": "^7.2.0",
"openapi-sampler": "^1.2.1", "openapi-sampler": "^1.2.1",
"path-browserify": "^1.0.1", "path-browserify": "^1.0.1",
@ -44,7 +44,7 @@
"@types/json-pointer": "^1.0.30", "@types/json-pointer": "^1.0.30",
"@types/lunr": "^2.3.3", "@types/lunr": "^2.3.3",
"@types/mark.js": "^8.11.5", "@types/mark.js": "^8.11.5",
"@types/marked": "^4.0.1", "@types/marked": "^4.0.3",
"@types/node": "^15.6.1", "@types/node": "^15.6.1",
"@types/prismjs": "^1.16.5", "@types/prismjs": "^1.16.5",
"@types/prop-types": "^15.7.3", "@types/prop-types": "^15.7.3",
@ -2590,9 +2590,9 @@
"integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==" "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug=="
}, },
"node_modules/@redocly/openapi-core": { "node_modules/@redocly/openapi-core": {
"version": "1.0.0-beta.88", "version": "1.0.0-beta.95",
"resolved": "https://registry.npmjs.org/@redocly/openapi-core/-/openapi-core-1.0.0-beta.88.tgz", "resolved": "https://registry.npmjs.org/@redocly/openapi-core/-/openapi-core-1.0.0-beta.95.tgz",
"integrity": "sha512-E9vkLvumIkzII0ydDFGr6uYbZgI9rHMxBveefzM51OUvobvifryXb6VcnQ1T0P8VoHRiYwpgiWlmZeDsNAdZdg==", "integrity": "sha512-7Nnc4Obp/1lbrjNjD33oOnZCuoJa8awhBCEyyayPWGQFp1SkhjpZJnfnKkFuYbQzMjTIAvEeSp9DOQK/E0fgEA==",
"dependencies": { "dependencies": {
"@redocly/ajv": "^8.6.4", "@redocly/ajv": "^8.6.4",
"@types/node": "^14.11.8", "@types/node": "^14.11.8",
@ -3014,9 +3014,9 @@
} }
}, },
"node_modules/@types/marked": { "node_modules/@types/marked": {
"version": "4.0.1", "version": "4.0.3",
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.0.3.tgz",
"integrity": "sha512-ZigEmCWdNUU7IjZEuQ/iaimYdDHWHfTe3kg8ORfKjyGYd9RWumPoOJRQXB0bO+XLkNwzCthW3wUIQtANaEZ1ag==", "integrity": "sha512-HnMWQkLJEf/PnxZIfbm0yGJRRZYYMhb++O9M36UCTA9z53uPvVoSlAwJr3XOpDEryb7Hwl1qAx/MV6YIW1RXxg==",
"dev": true "dev": true
}, },
"node_modules/@types/mime": { "node_modules/@types/mime": {
@ -13154,9 +13154,9 @@
"integrity": "sha1-GA8fnr74sOY45BZq1S24eb6y/8U=" "integrity": "sha1-GA8fnr74sOY45BZq1S24eb6y/8U="
}, },
"node_modules/marked": { "node_modules/marked": {
"version": "4.0.10", "version": "4.0.15",
"resolved": "https://registry.npmjs.org/marked/-/marked-4.0.10.tgz", "resolved": "https://registry.npmjs.org/marked/-/marked-4.0.15.tgz",
"integrity": "sha512-+QvuFj0nGgO970fySghXGmuw+Fd0gD2x3+MqCWLIPf5oxdv1Ka6b2q+z9RP01P/IaKPMEramy+7cNy/Lw8c3hw==", "integrity": "sha512-esX5lPdTfG4p8LDkv+obbRCyOKzB+820ZZyMOXJZygZBHrH9b3xXR64X4kT3sPe9Nx8qQXbmcz6kFSMt4Nfk6Q==",
"bin": { "bin": {
"marked": "bin/marked.js" "marked": "bin/marked.js"
}, },
@ -20971,9 +20971,9 @@
} }
}, },
"@redocly/openapi-core": { "@redocly/openapi-core": {
"version": "1.0.0-beta.88", "version": "1.0.0-beta.95",
"resolved": "https://registry.npmjs.org/@redocly/openapi-core/-/openapi-core-1.0.0-beta.88.tgz", "resolved": "https://registry.npmjs.org/@redocly/openapi-core/-/openapi-core-1.0.0-beta.95.tgz",
"integrity": "sha512-E9vkLvumIkzII0ydDFGr6uYbZgI9rHMxBveefzM51OUvobvifryXb6VcnQ1T0P8VoHRiYwpgiWlmZeDsNAdZdg==", "integrity": "sha512-7Nnc4Obp/1lbrjNjD33oOnZCuoJa8awhBCEyyayPWGQFp1SkhjpZJnfnKkFuYbQzMjTIAvEeSp9DOQK/E0fgEA==",
"requires": { "requires": {
"@redocly/ajv": "^8.6.4", "@redocly/ajv": "^8.6.4",
"@types/node": "^14.11.8", "@types/node": "^14.11.8",
@ -21369,9 +21369,9 @@
} }
}, },
"@types/marked": { "@types/marked": {
"version": "4.0.1", "version": "4.0.3",
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.0.3.tgz",
"integrity": "sha512-ZigEmCWdNUU7IjZEuQ/iaimYdDHWHfTe3kg8ORfKjyGYd9RWumPoOJRQXB0bO+XLkNwzCthW3wUIQtANaEZ1ag==", "integrity": "sha512-HnMWQkLJEf/PnxZIfbm0yGJRRZYYMhb++O9M36UCTA9z53uPvVoSlAwJr3XOpDEryb7Hwl1qAx/MV6YIW1RXxg==",
"dev": true "dev": true
}, },
"@types/mime": { "@types/mime": {
@ -29069,9 +29069,9 @@
"integrity": "sha1-GA8fnr74sOY45BZq1S24eb6y/8U=" "integrity": "sha1-GA8fnr74sOY45BZq1S24eb6y/8U="
}, },
"marked": { "marked": {
"version": "4.0.10", "version": "4.0.15",
"resolved": "https://registry.npmjs.org/marked/-/marked-4.0.10.tgz", "resolved": "https://registry.npmjs.org/marked/-/marked-4.0.15.tgz",
"integrity": "sha512-+QvuFj0nGgO970fySghXGmuw+Fd0gD2x3+MqCWLIPf5oxdv1Ka6b2q+z9RP01P/IaKPMEramy+7cNy/Lw8c3hw==" "integrity": "sha512-esX5lPdTfG4p8LDkv+obbRCyOKzB+820ZZyMOXJZygZBHrH9b3xXR64X4kT3sPe9Nx8qQXbmcz6kFSMt4Nfk6Q=="
}, },
"media-typer": { "media-typer": {
"version": "0.3.0", "version": "0.3.0",

View File

@ -71,7 +71,7 @@
"@types/json-pointer": "^1.0.30", "@types/json-pointer": "^1.0.30",
"@types/lunr": "^2.3.3", "@types/lunr": "^2.3.3",
"@types/mark.js": "^8.11.5", "@types/mark.js": "^8.11.5",
"@types/marked": "^4.0.1", "@types/marked": "^4.0.3",
"@types/node": "^15.6.1", "@types/node": "^15.6.1",
"@types/prismjs": "^1.16.5", "@types/prismjs": "^1.16.5",
"@types/prop-types": "^15.7.3", "@types/prop-types": "^15.7.3",
@ -138,7 +138,7 @@
"styled-components": "^4.1.1 || ^5.1.1" "styled-components": "^4.1.1 || ^5.1.1"
}, },
"dependencies": { "dependencies": {
"@redocly/openapi-core": "^1.0.0-beta.88", "@redocly/openapi-core": "^1.0.0-beta.95",
"classnames": "^2.3.1", "classnames": "^2.3.1",
"decko": "^1.2.0", "decko": "^1.2.0",
"dompurify": "^2.2.8", "dompurify": "^2.2.8",
@ -146,7 +146,7 @@
"json-pointer": "^0.6.2", "json-pointer": "^0.6.2",
"lunr": "^2.3.9", "lunr": "^2.3.9",
"mark.js": "^8.11.1", "mark.js": "^8.11.1",
"marked": "^4.0.10", "marked": "^4.0.15",
"mobx-react": "^7.2.0", "mobx-react": "^7.2.0",
"openapi-sampler": "^1.2.1", "openapi-sampler": "^1.2.1",
"path-browserify": "^1.0.1", "path-browserify": "^1.0.1",

View File

@ -1,5 +1,3 @@
import { resolve as urlResolve } from 'url';
import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types'; import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types';
import { appendToMdHeading, IS_BROWSER } from '../utils/'; import { appendToMdHeading, IS_BROWSER } from '../utils/';
@ -62,7 +60,7 @@ export class OpenAPIParser {
const href = IS_BROWSER ? window.location.href : ''; const href = IS_BROWSER ? window.location.href : '';
if (typeof specUrl === 'string') { if (typeof specUrl === 'string') {
this.specUrl = urlResolve(href, specUrl); this.specUrl = new URL(specUrl, href).href;
} }
} }

View File

@ -1,5 +1,3 @@
import { resolve as urlResolve } from 'url';
import { OpenAPIEncoding, OpenAPIExample, Referenced } from '../../types'; import { OpenAPIEncoding, OpenAPIExample, Referenced } from '../../types';
import { isFormUrlEncoded, isJsonLike, urlFormEncodePayload } from '../../utils/openapi'; import { isFormUrlEncoded, isJsonLike, urlFormEncodePayload } from '../../utils/openapi';
import { OpenAPIParser } from '../OpenAPIParser'; import { OpenAPIParser } from '../OpenAPIParser';
@ -23,7 +21,7 @@ export class ExampleModel {
this.summary = example.summary; this.summary = example.summary;
this.description = example.description; this.description = example.description;
if (example.externalValue) { if (example.externalValue) {
this.externalValueUrl = urlResolve(parser.specUrl || '', example.externalValue); this.externalValueUrl = new URL(example.externalValue, parser.specUrl || '').href;
} }
parser.exitRef(infoOrRef); parser.exitRef(infoOrRef);

View File

@ -1,5 +1,4 @@
import slugify from 'slugify'; import slugify from 'slugify';
import { format, parse } from 'url';
/** /**
* Maps over array passing `isLast` bool to iterator as the second argument * Maps over array passing `isLast` bool to iterator as the second argument
@ -146,18 +145,23 @@ export function isAbsoluteUrl(url: string) {
export function resolveUrl(url: string, to: string) { export function resolveUrl(url: string, to: string) {
let res; let res;
if (to.startsWith('//')) { if (to.startsWith('//')) {
const { protocol: specProtocol } = parse(url); try {
res = `${specProtocol || 'https:'}${to}`; res = `${new URL(url).protocol || 'https:'}${to}`;
} catch {
res = `https:${to}`;
}
} else if (isAbsoluteUrl(to)) { } else if (isAbsoluteUrl(to)) {
res = to; res = to;
} else if (!to.startsWith('/')) { } else if (!to.startsWith('/')) {
res = stripTrailingSlash(url) + '/' + to; res = stripTrailingSlash(url) + '/' + to;
} else { } else {
const urlObj = parse(url); try {
res = format({ const urlObj = new URL(url);
...urlObj, urlObj.pathname = to;
pathname: to, res = urlObj.href;
}); } catch {
res = to;
}
} }
return stripTrailingSlash(res); return stripTrailingSlash(res);
} }

View File

@ -1,4 +1,6 @@
import type { Source, Document } from '@redocly/openapi-core'; import type { Source, Document } from '@redocly/openapi-core';
// eslint-disable-next-line import/no-internal-modules
import type { ResolvedConfig } from '@redocly/openapi-core/lib/config';
// eslint-disable-next-line import/no-internal-modules // eslint-disable-next-line import/no-internal-modules
import { bundle } from '@redocly/openapi-core/lib/bundle'; import { bundle } from '@redocly/openapi-core/lib/bundle';
@ -11,7 +13,7 @@ import { OpenAPISpec } from '../types';
import { IS_BROWSER } from './dom'; import { IS_BROWSER } from './dom';
export async function loadAndBundleSpec(specUrlOrObject: object | string): Promise<OpenAPISpec> { export async function loadAndBundleSpec(specUrlOrObject: object | string): Promise<OpenAPISpec> {
const config = new Config({}); const config = new Config({} as ResolvedConfig);
const bundleOpts = { const bundleOpts = {
config, config,
base: IS_BROWSER ? window.location.href : process.cwd(), base: IS_BROWSER ? window.location.href : process.cwd(),

View File

@ -8,6 +8,7 @@ const nodeExternals = require('webpack-node-externals')({
// bundle in modules that need transpiling + non-js (e.g. css) // bundle in modules that need transpiling + non-js (e.g. css)
allowlist: [ allowlist: [
'swagger2openapi', 'swagger2openapi',
'marked',
/reftools/, /reftools/,
'oas-resolver', 'oas-resolver',
'oas-kit-common', 'oas-kit-common',
@ -65,11 +66,12 @@ export default (env: { standalone?: boolean; browser?: boolean } = {}) => ({
'node-fetch': 'null', 'node-fetch': 'null',
'node-fetch-h2': 'null', 'node-fetch-h2': 'null',
yaml: 'null', yaml: 'null',
url: 'null',
'safe-json-stringify': 'null', 'safe-json-stringify': 'null',
} }
: (context, request, callback) => { : (context, request, callback) => {
// ignore node-fetch dep of swagger2openapi as it is not used // ignore node-fetch dep of swagger2openapi as it is not used
if (/esprima|node-fetch|node-fetch-h2|\/yaml|safe-json-stringify$/i.test(request)) { if (/esprima|node-fetch|node-fetch-h2|\/yaml|safe-json-stringify|url$/i.test(request)) {
return callback(null, 'var undefined'); return callback(null, 'var undefined');
} }
return nodeExternals(context, request, callback); return nodeExternals(context, request, callback);