diff --git a/scripts/prepare-swagger-ui.js b/scripts/prepare-swagger-ui.js index b3127d7..48acd5e 100644 --- a/scripts/prepare-swagger-ui.js +++ b/scripts/prepare-swagger-ui.js @@ -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 = [ diff --git a/test/prepare.test.js b/test/prepare.test.js new file mode 100644 index 0000000..54c7fed --- /dev/null +++ b/test/prepare.test.js @@ -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') +})