Skip to content

Commit

Permalink
Remove sourceMaps (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanReece authored Feb 13, 2024
1 parent f8ebaca commit 77a7c73
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
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')
})

0 comments on commit 77a7c73

Please sign in to comment.