From 12f03b86c88f2ca7d0f80c322dd7daea67976223 Mon Sep 17 00:00:00 2001 From: Alfredo Salzillo Date: Sun, 3 Jan 2021 02:00:14 +0100 Subject: [PATCH 1/3] feat: add env plugin --- plugins/env | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 plugins/env diff --git a/plugins/env b/plugins/env new file mode 100644 index 000000000..584c54376 --- /dev/null +++ b/plugins/env @@ -0,0 +1,31 @@ +import { Plugin } from '../types.ts'; + +export const defaultMatcher = /{{([^}]*)}}/ig; + +type Options = { + matcher?: string | RegExp, +} +const defaultOptions: Options = { + matcher: defaultMatcher, +} +const env = (options: Options = defaultOptions): Plugin => ({ + name: 'env', + test: /\.[tj]s[x]?$/, + acceptHMR: true, + async transform(content: Uint8Array) { + const parameters = Deno.env.toObject(); + const string = (new TextDecoder()).decode(content); + const code = string + .replaceAll(options.matcher!, (raw, match) => { + if (match in parameters) { + return String(parameters[match]); + } + return raw; + }); + return { + code, + } + } +}) + +export default env; From 0a79e272674d8611ebc69ed7ef5e1ef1e3b72673 Mon Sep 17 00:00:00 2001 From: Alfredo Salzillo Date: Sun, 3 Jan 2021 02:00:47 +0100 Subject: [PATCH 2/3] feat: add env plugin --- plugins/env_test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 plugins/env_test.ts diff --git a/plugins/env_test.ts b/plugins/env_test.ts new file mode 100644 index 000000000..29fb2e598 --- /dev/null +++ b/plugins/env_test.ts @@ -0,0 +1,38 @@ +import { assertEquals, assert } from 'https://deno.land/std/testing/asserts.ts'; +import env from './env.ts'; + +Deno.env.set('TEST_ENV_VAR', '1') + +Deno.test('env loader should accept ts files', async () => { + const plugin = env(); + assert(plugin.test.test('mod.ts')); +}) + +Deno.test('env loader should accept tsx files', async () => { + const plugin = env(); + assert(plugin.test.test('mod.tsx')); +}) + +Deno.test('env loader should accept js files', async () => { + const plugin = env(); + assert(plugin.test.test('mod.js')); +}) + +Deno.test('env loader should accept jsx files', async () => { + const plugin = env(); + assert(plugin.test.test('mod.jsx')); +}) + +Deno.test('env loader should be accept HMR files', async () => { + const plugin = env(); + assert(plugin.acceptHMR); +}) + +Deno.test('env loader should replace env variables', async () => { + const plugin = env(); + const { code, loader } = await plugin.transform?.( + (new TextEncoder).encode('const start = {{TEST_ENV_VAR}};'), + 'mod.ts' + )! + assertEquals(code, 'const start = 1;') +}) From ba2d9ac462d0c332cab1685f9af0ab601408d3e6 Mon Sep 17 00:00:00 2001 From: Alfredo Salzillo Date: Sun, 3 Jan 2021 02:01:18 +0100 Subject: [PATCH 3/3] feat: add env plugin --- plugins/{env => env.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename plugins/{env => env.ts} (100%) diff --git a/plugins/env b/plugins/env.ts similarity index 100% rename from plugins/env rename to plugins/env.ts