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

perf: Reduce package size by removing sourceMaps #126

Merged
merged 1 commit into from
Feb 13, 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
17 changes: 11 additions & 6 deletions scripts/prepare-swagger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ const filesToCopy = [
'index.css',
'oauth2-redirect.html',
'swagger-ui-bundle.js',
'swagger-ui-bundle.js.map',
'swagger-ui-standalone-preset.js',
'swagger-ui-standalone-preset.js.map',
'swagger-ui.css',
'swagger-ui.css.map',
'swagger-ui.js',
'swagger-ui.js.map'
'swagger-ui.js'
]
filesToCopy.forEach(filename => {
fse.copySync(`${swaggerUiAssetPath}/${filename}`, resolve(`./static/${filename}`))
fse.ensureFileSync(resolve(`./static/${filename}`))
const readableStream = fs.createReadStream(`${swaggerUiAssetPath}/${filename}`, 'utf8')
const writableStream = fs.createWriteStream(resolve(`./static/${filename}`))
// Matches sourceMappingURL comments in .js and .css files
const sourceMapRegex = new RegExp(String.raw`\/.# sourceMappingURL=${filename}.map(\*\/)?$`)

readableStream.on('data', (chunk) => {
// Copy file while removing sourceMappingURL comments
writableStream.write(chunk.replace(sourceMapRegex, ''))
})
})

const overrides = [
Expand Down
38 changes: 38 additions & 0 deletions test/prepare.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

const { test } = require('tap')
const Fastify = require('fastify')
const fastifySwagger = require('@fastify/swagger')
const fastifySwaggerUi = require('../index')

test('Swagger source does not contain sourceMaps', async (t) => {
t.plan(2)
const fastify = Fastify()
await fastify.register(fastifySwagger)
await fastify.register(fastifySwaggerUi)

const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/swagger-ui.js'
})

const includesSourceMap = res.payload.includes('sourceMappingURL')
t.equal(includesSourceMap, false)
t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')
})

test('Swagger css does not contain sourceMaps', async (t) => {
t.plan(2)
const fastify = Fastify()
await fastify.register(fastifySwagger)
await fastify.register(fastifySwaggerUi)

const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/swagger-ui.css'
})

const includesSourceMap = res.payload.includes('sourceMappingURL')
t.equal(includesSourceMap, false)
t.equal(res.headers['content-type'], 'text/css; charset=UTF-8')
})
Loading