Using deno in deveopment

I have a script (although it runs in node alas) that converts elm generated javascript files into es modules:

const fs = require('fs');
const {promisify} = require('util');

const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

(async () => {
  const elmPath = process.argv[2];

  console.log(`Making ${elmPath} into a es module...`);

  const elmEs3 = await readFile(elmPath, 'utf8');

  const elmEsm =
    '\n' +
    'const scope = {};\n' +
    elmEs3.replace('}(this));', '}(scope));') +
    'export const { Elm } = scope;\n' +
    '\n';

  await writeFile(elmPath, elmEsm);

  console.log('Finished.');
})();

I think this would fix the scope issues. (Elm uses the this === window thing to do global exports but deno runs everything as an es module so this === undefined at the module level).


Edit: maybe this (untested) works in deno

import { readFileStr, writeFileStr } from 'https://deno.land/std/fs/mod.ts';

const elmPath = Deno.args[0];

console.log(`Making ${elmPath} into a es module...`);

const elmEs3 = await readFileStr(elmPath);

const elmEsm = `
const scope = {}
${elmEs3.replace('}(this));', '}(scope));')}

export const { Elm } = scope;
`;

await writeFileStr(elmPath, elmEsm);
3 Likes