Skip to content
This repository was archived by the owner on Sep 2, 2023. It is now read-only.
This repository was archived by the owner on Sep 2, 2023. It is now read-only.

Resolve hook source transformation #390

@coreyfarrell

Description

@coreyfarrell

I've created a proof of concept for adding source transformation support to the ESM loader resolve hooks. An example --loader script using this feature:

import {fileURLToPath} from 'url';
import babel from '@babel/core';

async function fetchSource(url, defaultFetchSource) {
  const source = await defaultFetchSource(url);
  const {code} = await babel.transformAsync(source, {
    // Protect against `data:` url
    filename: url.startsWith('file:') ? fileURLToPath(url) : undefined,
    sourceMaps: 'inline'
  });

  return code;
}

export async function resolve(spec, parentMod, baseResolver) {
  const result = baseResolver(spec, parentMod);
  if (result.format !== 'module') {
    return result;
  }

  return {
    ...result,
    fetchSource
  };
}

Thoughts on this API? Currently my patch supports the fetchSource option for module, wasm and json formats. I'm unsure what the return value of fetchSource should be. Currently a string is expected because is was easiest to implement and matches the existing getSource, but maybe it would make sense for it to allow an object {source, sourceMap}? To do that I'd need advice on what to do with the sourceMap object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions