SwaggerEditor
SwaggerEditor is using forked Create React App as it’s building infrastructure.
Anonymized analytics
Swagger Editor uses Scarf to collect anonymized installation analytics. These analytics help support the maintainers of this library and ONLY run during installation. To opt out, you can set the scarfSettings.enabled
field to false
in your project’s package.json
:
1{2 // ...3 "scarfSettings": {4 "enabled": false5 }6 // ...7}
Alternatively, you can set the environment variable SCARF_ANALYTICS
to false
as part of the environment that installs your npm packages, e.g., SCARF_ANALYTICS=false npm install
.
Getting started
Prerequisites
These prerequisites are required both for installing SwaggerEditor as a npm package and local development setup.
- node-gyp with Python 3.x
- GLIBC
>=2.29
- emscripten or docker needs to be installed, we recommend going with a docker option
Installation
Assuming prerequisites are already installed, SwaggerEditor npm package is installable and works with Node.js >= 12.22.0
.
You can install SwaggerEditor via npm CLI by running the following command:
1 $ npm install swagger-editor@alpha
NOTE: when using bundler to build your project which is using swagger-editor@5 npm package, you might run into following Node.js error:
Reached heap limit Allocation failed - JavaScript heap out of memory
. It is caused by significant amount of code that needs to be bundled. This error can be resolved by extending the Node.js max heap limit:export NODE_OPTIONS="--max_old_space_size=4096"
.
Usage
Use the package in you application:
index.js:
1import React from 'react';2import ReactDOM from 'react-dom';3import SwaggerEditor from 'swagger-editor';4import 'swagger-editor/swagger-editor.css';5
6const url = "https://raw.githubusercontent.com/asyncapi/spec/v2.2.0/examples/streetlights-kafka.yml";7
8const MyApp = () => (9 <div>10 <h1>SwaggerEditor Integration</h1>11 <SwaggerEditor url={url} />12 </div>13);14
15self.MonacoEnvironment = {16 /**17 * We're building into the dist/ folder. When application starts on18 * URL=https://example.com then SwaggerEditor will look for19 * `apidom.worker.js` on https://example.com/dist/apidom.worker.js and20 * `editor.worker` on https://example.com/dist/editor.worker.js.21 */22 baseUrl: `${document.baseURI || location.href}dist/`,23}24
25ReactDOM.render(<MyApp />, document.getElementById('swagger-editor'));
webpack.config.js (webpack@5)
Install dependencies needed for webpack@5 to properly build SwaggerEditor.
1 $ npm i stream-browserify --save-dev2 $ npm i https-browserify --save-dev3 $ npm i stream-http --save-dev4 $ npm i util --save-dev
1const path = require('path');2const webpack = require('webpack');3
4module.exports = {5 mode: 'production',6 entry: {7 app: './index.js',8 'apidom.worker': 'swagger-editor/apidom.worker',9 'editor.worker': 'swagger-editor/editor.worker',10 },11 output: {12 globalObject: 'self',13 filename: '[name].js',14 path: path.resolve(__dirname, 'dist')15 },16 resolve: {17 fallback: {18 path: false,19 fs: false,20 http: require.resolve('stream-http'), // required for asyncapi parser21 https: require.resolve('https-browserify'), // required for asyncapi parser22 stream: require.resolve('stream-browserify'),23 util: require.resolve('util'),24 url: require.resolve('url'),25 zlib: false,26 },27 alias: {28 // This alias make sure we don't pull two different versions of monaco-editor29 'monaco-editor': '/node_modules/monaco-editor',30 // This alias makes sure we're avoiding a runtime error related to this package31 '@stoplight/ordered-object-literal$': '/node_modules/@stoplight/ordered-object-literal/src/index.mjs',32 },33 },34 plugins: [35 new webpack.ProvidePlugin({36 Buffer: ['buffer', 'Buffer'],37 }),38 ],39 module: {40 rules: [41 {42 test: /\.css$/,43 use: ['style-loader', 'css-loader']44 },45 /**46 * The default way in which webpack loads wasm files won’t work in a worker,47 * so we will have to disable webpack’s default handling of wasm files and48 * then fetch the wasm file by using the file path that we get using file-loader.49 *50 * Resource: https://pspdfkit.com/blog/2020/webassembly-in-a-web-worker/51 *52 * Alternatively, WASM file can be bundled directly into JavaScript bundle as data URLs.53 * This configuration reduces the complexity of WASM file loading54 * but increases the overal bundle size:55 *56 * {57 * test: /\.wasm$/,58 * type: 'asset/inline',59 * }60 */61 {62 test: /\.wasm$/,63 loader: 'file-loader',64 type: 'javascript/auto', // this disables webpacks default handling of wasm65 },66 ]67 }68};
Alternative webpack.config.js (webpack@5)
We’ve already built Web Workers fragments for you, and they’re located inside our npm distribution
package in dist/umd/
directory. In order to avoid complexity of building the Web Worker fragments you can
use those fragments directly. This setup will work both for production and development (webpack-dev-server)
and will significantly shorten your build process.
Install copy-webpack-plugin
and other needed dependencies.
1 $ npm i copy-webpack-plugin --save-dev2 $ npm i stream-browserify --save-dev3 $ npm i https-browserify --save-dev4 $ npm i stream-http --save-dev5 $ npm i util --save-dev
1const path = require('path');2const webpack = require('webpack');3const CopyWebpackPlugin = require('copy-webpack-plugin');4
5module.exports = {6 mode: 'production',7 entry: {8 app: './index.js',9 },10 output: {11 globalObject: 'self',12 filename: 'static/js/[name].js',13 path: path.resolve(__dirname, 'dist')14 },15 resolve: {16 fallback: {17 path: false,18 fs: false,19 http: require.resolve('stream-http'), // required for asyncapi parser20 https: require.resolve('https-browserify'), // required for asyncapi parser21 stream: require.resolve('stream-browserify'),22 util: require.resolve('util'),23 url: require.resolve('url'),24 zlib: false,25 },26 alias: {27 // This alias make sure we don't pull two different versions of monaco-editor28 'monaco-editor': '/node_modules/monaco-editor',29 // This alias makes sure we're avoiding a runtime error related to this package30 '@stoplight/ordered-object-literal$': '/node_modules/@stoplight/ordered-object-literal/src/index.mjs',31 }32 },33 plugins: [34 new webpack.ProvidePlugin({35 Buffer: ['buffer', 'Buffer'],36 }),37 new CopyWebpackPlugin({38 patterns: [39 {40 from: 'node_modules/swagger-editor/dist/umd/apidom.worker.js',41 to: 'static/js',42 },43 {44 from: 'node_modules/swagger-editor/dist/umd/editor.worker.js',45 to: 'static/js',46 }47 ]48 }),49 ],50 module: {51 rules: [52 {53 test: /\.css$/,54 use: ['style-loader', 'css-loader']55 },56 ]57 }58};
Development
Prerequisites
Assuming prerequisites are already installed, Node.js >=20.3.0
and npm >=9.6.7
are the minimum required versions that this repo runs on, but we recommend using the latest version of Node.js@20.
Setting up
If you use nvm, running following command inside this repository will automatically pick the right Node.js version for you:
1 $ nvm use
Run the following commands to set up the repository for local development:
1 $ git clone https://github.com/swagger-api/swagger-editor.git2 $ cd swagger-editor3 $ git checkout next4 $ git submodule init5 $ git submodule update6 $ npm i7 $ npm start
npm scripts
Lint
1 $ npm run lint
Runs unit and integration tests
1 $ npm test
Runs E2E Cypress tests
Usage in development environment:
1 $ npm run cy:dev
Usage in Continuous Integration (CI) environment:
1 $ npm run cy:ci
Build
1 $ npm run build
This script will build all the SwaggerEditor build artifacts - app
, esm
and umd
.
Build artifacts
After building artifacts, every two new directories will be created: build/
and dist/
.
build/
1$ npm run build:app2$ npm run build:app:serve
Builds and serves standalone SwaggerEditor application and all it’s assets on http://localhost:3050/
.
dist/esm/
1$ npm run build:bundle:esm
This bundle is suited for consumption by 3rd parties, which want to use SwaggerEditor as a library in their own applications and have their own build process.
dist/umd/
1$ npm run build:bundle:umd
SwaggerEditor UMD bundle exports SwaggerEditor symbol on global object. It’s bundled with React defined as external. This allows consumer to use his own version of React + ReactDOM and mount SwaggerEditor lazily.
1<!DOCTYPE html>2<html lang="en">3<head>4 <meta charset="utf-8" />5 <meta name="viewport" content="width=device-width, initial-scale=1" />6 <meta7 name="description"8 content="SwaggerEditor"9 />10 <title>SwaggerEditor</title>11 <link rel="stylesheet" href="./swagger-editor.css" />12</head>13<body>14 <div id="swagger-editor"></div>15 <script src="https://unpkg.com/react@18/umd/react.production.min.js" crossorigin></script>16 <script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js" crossorigin></script>17 <script src="./dist/umd/swagger-editor.js"></script>18 <script>19 const props = {20 url: 'https://raw.githubusercontent.com/asyncapi/spec/v2.2.0/examples/streetlights-kafka.yml',21 };22 const element = React.createElement(SwaggerEditor, props);23 const domContainer = document.querySelector('#swagger-editor');24
25 ReactDOM.render(element, domContainer);26 </script>27</body>28</html>