Giter VIP home page Giter VIP logo

Comments (14)

lukeed avatar lukeed commented on June 20, 2024 1

Cool, will do 👍 I'm likely to extract this register script into its own package since I've needed it a number of times.

from worktop.

lukeed avatar lukeed commented on June 20, 2024

Hey, nope, will not add conditional exports for this project.
This is a TS limitation that is still being worked through.

For the time being, you can either precompile your TS into JS with ESM format, and then use a test runner that supports ESM (tap does); or you can use a require hook that transforms everything on the fly into ESM or CommonJS, depending on your runtime. For example, something like this would work everywhere: uvu -r esbuild-register tests (configured via tsconfig.json file).

Hope that helps~!

from worktop.

StarpTech avatar StarpTech commented on June 20, 2024

Still doesn't work same error mesage, any idea?

    "test": "tap --node-arg=\"--require=esbuild-register\"",

from worktop.

StarpTech avatar StarpTech commented on June 20, 2024

with uvu

    "test": "uvu src \".test.ts$\" -r esbuild-register",
.../node_modules/worktop/request/index.mjs:18
src/routes/foo.test.ts 2> export {
SyntaxError: Unexpected token 'export'

from worktop.

lukeed avatar lukeed commented on June 20, 2024

Here's an example setup:

// src/index.ts
import { encode } from 'worktop/utils';

export function howdy(input: string) {
  return encode(input);
}

// test/index.ts
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import * as app from '../src';

test('should be a function', () => {
  assert.type(app.howdy, 'function');
});

test('should be encoded', () => {
  let output = app.howdy('hello');

  assert.is.not(output, 'hello');
  assert.instance(output, Uint8Array);
});

test.run();
// package.json
{
  "scripts": {
    "test": "uvu -r ./bin/register.js test"
  },
  "devDependencies": {
    "esbuild": "0.11.12",
    "uvu": "0.5.1",
    "worktop": "0.4.2"
  }
}
// bin/esbuild.js
const esbuild = require('esbuild');

/** @type {esbuild.CommonOptions} */
const options = {
  target: 'es2019',
  sourcemap: false,
  treeShaking: true,
  minifySyntax: true,
  minifyIdentifiers: true,
}

/**
 * @param {string} input
 * @param {string} output
 * @param {string[]} [externals]
 */
exports.build = function (input, output, externals=[]) {
  return esbuild.build({
    ...options,
    bundle: true,
    format: 'esm',
    outfile: output,
    entryPoints: [input],
    external: externals,
  });
}

/**
 * @param {string} source
 * @param {esbuild.TransformOptions} [overrides]
 */
exports.transform = function (source, overrides={}) {
  return esbuild.transformSync(source, {
    ...options,
    format: 'cjs',
    ...overrides
  });
}

// bin/register.js
const { transform } = require('./esbuild');

const loadCJS = require.extensions['.js'];

/**
 * @param {string} extn
 * @param {string} loader
 */
function loader(extn, loader) {
  require.extensions[extn] = function (Module, filename) {
    const pitch = Module._compile.bind(Module);

    Module._compile = source => {
      const { code, warnings } = transform(source, {
        sourcefile: filename,
        loader: loader,
      });

      warnings.forEach(msg => {
        console.warn(`\nesbuild warning in ${filename}:`);
        console.warn(msg.location);
        console.warn(msg.text);
      });

      return pitch(code, filename);
    };

    loadCJS(Module, filename);
  }
}

loader('.ts', 'ts');
loader('.mjs', 'js');
loader('.cjs', 'js');

Note: The two /bin files are (practically) cloned from worktop's testing setup

But yeah – sorry, forgot that esbuild-register ignores .mjs files by default. I don't know of a way around it – I always just have my own bin/register script.

from worktop.

StarpTech avatar StarpTech commented on June 20, 2024

@lukeed thank you! I'll try it out. Are you sure you won't support cjs in worktop? I'd create a PR 😄

from worktop.

lukeed avatar lukeed commented on June 20, 2024

Yup, 100% sure. Thank you though

from worktop.

StarpTech avatar StarpTech commented on June 20, 2024

Works! We should document it.

from worktop.

StarpTech avatar StarpTech commented on June 20, 2024

Do you have type-check when running the tests?

from worktop.

StarpTech avatar StarpTech commented on June 20, 2024

I see you run it explicitly.

from worktop.

lukeed avatar lukeed commented on June 20, 2024

No, in every project I always have it as a separate process. My IDE type checks for live-feedback. I don't need it to run again & again & again for tests – catastrophic errors will fail compilation.

I'll also add a pretest npm-script that runs tsc for me, most of the time.

from worktop.

StarpTech avatar StarpTech commented on June 20, 2024

Make sense.

from worktop.

StarpTech avatar StarpTech commented on June 20, 2024

It looks like that this step breaks debugging entirely. The breakpoints aren't longer mapped to the right code location.

from worktop.

lukeed avatar lukeed commented on June 20, 2024

Yeah, that's the missing part of the bin/*.js scripts. I don't have sourcemap support in here yet but it should be fairly straightforward. TBH that's the missing piece before actually extracting this as a separate module.

from worktop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.