Skip to content

Commit 75a2b9a

Browse files
authoredJun 15, 2020
Demonstrate a simple Webpack setup (#5185)
1 parent f3b253a commit 75a2b9a

File tree

8 files changed

+6957
-0
lines changed

8 files changed

+6957
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
Demo of Swagger UI with Webpack.
3+
4+
Includes CSS and OAuth configuration.
5+
6+
Usage
7+
8+
npm install
9+
npm start
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Getting Started</title>
6+
</head>
7+
<body>
8+
<div id="swagger"></div>
9+
</body>
10+
</html>

‎docs/samples/webpack-getting-started/package-lock.json

+6,814
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "swagger-ui-webpack-getting-started",
3+
"version": "0.0.1",
4+
"description": "A simple setup of Swagger UI with Webpack",
5+
"scripts": {
6+
"build": "webpack",
7+
"start": "webpack-dev-server --open"
8+
},
9+
"author": "Shaun Luttin",
10+
"license": "Apache-2.0",
11+
"devDependencies": {
12+
"clean-webpack-plugin": "^1.0.1",
13+
"copy-webpack-plugin": "^4.6.0",
14+
"html-webpack-plugin": "^3.2.0",
15+
"webpack": "^4.29.3",
16+
"webpack-cli": "^3.2.3",
17+
"webpack-dev-server": "^3.1.14"
18+
},
19+
"dependencies": {
20+
"css-loader": "^2.1.0",
21+
"json-loader": "^0.5.7",
22+
"style-loader": "^0.23.1",
23+
"swagger-ui": "^3.20.7",
24+
"yaml-loader": "^0.5.0"
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import SwaggerUI from 'swagger-ui'
2+
import 'swagger-ui/dist/swagger-ui.css';
3+
4+
const spec = require('./swagger-config.yaml');
5+
6+
const ui = SwaggerUI({
7+
spec,
8+
dom_id: '#swagger',
9+
});
10+
11+
ui.initOAuth({
12+
appName: "Swagger UI Webpack Demo",
13+
// See https://demo.identityserver.io/ for configuration details.
14+
clientId: 'implicit'
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: "0.0.1"
4+
title: "Swagger UI Webpack Setup"
5+
description: "Demonstrates Swagger UI with Webpack including CSS and OAuth"
6+
servers:
7+
- url: "https://demo.identityserver.io/api"
8+
description: "Identity Server test API"
9+
components:
10+
securitySchemes:
11+
# See https://demo.identityserver.io/ for configuration details.
12+
identity_server_auth:
13+
type: oauth2
14+
flows:
15+
implicit:
16+
authorizationUrl: "https://demo.identityserver.io/connect/authorize"
17+
scopes:
18+
api: "api"
19+
security:
20+
- identity_server_auth:
21+
- api
22+
paths:
23+
/test:
24+
get:
25+
summary: "Runs a test request against the Identity Server demo API"
26+
responses:
27+
401:
28+
description: "Unauthorized"
29+
200:
30+
description: "OK"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const path = require('path');
2+
const HtmlWebpackPlugin = require('html-webpack-plugin');
3+
const CleanWebpackPlugin = require('clean-webpack-plugin');
4+
const CopyWebpackPlugin = require('copy-webpack-plugin');
5+
6+
const outputPath = path.resolve(__dirname, 'dist');
7+
8+
module.exports = {
9+
mode: 'development',
10+
entry: {
11+
app: './src/index.js',
12+
},
13+
module: {
14+
rules: [
15+
{
16+
test: /\.yaml$/,
17+
use: [
18+
{ loader: 'json-loader' },
19+
{ loader: 'yaml-loader' }
20+
]
21+
},
22+
{
23+
test: /\.css$/,
24+
use: [
25+
{ loader: 'style-loader' },
26+
{ loader: 'css-loader' },
27+
]
28+
}
29+
]
30+
},
31+
plugins: [
32+
new CleanWebpackPlugin([
33+
outputPath
34+
]),
35+
new CopyWebpackPlugin([
36+
{
37+
// Copy the Swagger OAuth2 redirect file to the project root;
38+
// that file handles the OAuth2 redirect after authenticating the end-user.
39+
from: 'node_modules/swagger-ui/dist/oauth2-redirect.html',
40+
to: './'
41+
}
42+
]),
43+
new HtmlWebpackPlugin({
44+
template: 'index.html'
45+
})
46+
],
47+
output: {
48+
filename: '[name].bundle.js',
49+
path: outputPath,
50+
}
51+
};

‎docs/usage/installation.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ SwaggerUI({
1818
})
1919
```
2020

21+
See the [Webpack Getting Started](../samples/webpack-getting-started) sample for details.
22+
2123
In contrast, **`swagger-ui-dist`** is meant for server-side projects that need assets to serve to clients. The module, when imported, includes an `absolutePath` helper function that returns the absolute filesystem path to where the `swagger-ui-dist` module is installed.
2224

2325
_Note: we suggest using `swagger-ui` when your tooling makes it possible, as `swagger-ui-dist`

0 commit comments

Comments
 (0)
Please sign in to comment.