From c239c3b051dc6fd57f13a288ffb2e32a40a0d547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Iv=C3=A1n=20Vieitez=20Parra?= Date: Wed, 5 Oct 2022 23:15:03 +0200 Subject: [PATCH] Updated test, bugfix for random no output error --- src/index.ts | 2 +- test/esbuild.test.ts | 6 +++++- test/external.inline.ts | 31 ++++++++++++++++++++++++++++++- test/test-path-import.ts | 10 +++++++++- test/test.d.ts | 9 +++++++++ 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 103344d..3257a68 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,7 +40,7 @@ export default ( namespace: '@exact-realty/esbuild-plugin-inline-js/loader', }, async ({ path }) => { - const outfile = randomBytes(9).toString('base64'); + const outfile = randomBytes(9).toString('base64url'); const result = await esbuild.build({ ...config, entryPoints: [path], diff --git a/test/esbuild.test.ts b/test/esbuild.test.ts index 532aed6..3f4e0ec 100644 --- a/test/esbuild.test.ts +++ b/test/esbuild.test.ts @@ -21,7 +21,11 @@ import assert from 'node:assert/strict'; describe('Test', () => { describe('Import results in the right values', () => { - for (const test of ['test-simple', 'test-default-import', 'test-path-import']) { + for (const test of [ + 'test-simple', + 'test-default-import', + 'test-path-import', + ]) { it(test, async () => { await esbuild .build({ diff --git a/test/external.inline.ts b/test/external.inline.ts index 7dc44b4..c68b989 100644 --- a/test/external.inline.ts +++ b/test/external.inline.ts @@ -1,3 +1,32 @@ (function () { - return 'Hello, World!'; + const list = [ + 'H', + 'e', + 'l', + 'l', + 'o', + ',', + ' ', + 'W', + 'o', + 'r', + 'l', + 'd', + '!', + ]; + return list + .map( + (c) => [c.toLowerCase(), c.toUpperCase()].find((d) => d !== c) ?? c, + ) + .map((c) => String.prototype.codePointAt.call(c, 0)) + .map((c) => c << 1) + .map((c) => c << 1) + .map((c) => c << 1) + .map((c) => c >> 3) + .map((c) => String.fromCodePoint(c)) + .map( + (c) => [c.toLowerCase(), c.toUpperCase()].find((d) => d !== c) ?? c, + ) + .join(';') + .replace(/;/g, ''); })(); diff --git a/test/test-path-import.ts b/test/test-path-import.ts index be54f34..1304c3b 100644 --- a/test/test-path-import.ts +++ b/test/test-path-import.ts @@ -15,6 +15,8 @@ import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; +import { join } from 'node:path'; +import { readFileSync } from 'node:fs'; import * as x from './external.inline.ts'; @@ -29,6 +31,12 @@ assert.equal( ); assert.equal(new Function('return ' + x.default)(), 'Hello, World!'); assert.equal( - x.path.replace(/external-[^.]+\.js$/, 'external.js'), + x.path.replace(/\/(external)-[^.]+\.js$/, '/$1.js'), 'http://invalid/assets/external.js', ); + +const content = readFileSync(join(__dirname, x.path.split('/').pop())); +assert.equal( + x.sri, + 'sha384-' + createHash('sha384').update(content).digest('base64'), +); diff --git a/test/test.d.ts b/test/test.d.ts index c1a5910..7b0d9d2 100644 --- a/test/test.d.ts +++ b/test/test.d.ts @@ -21,3 +21,12 @@ declare module '*.inline.ts' { export default content; } + +declare module '*.inline.js' { + const content: string; + export const contentBase64: string; + export const path: string; + export const sri: string; + + export default content; +}