javiertury/babel-plugin-transform-import-meta

Strange self-reference

tobewisebeforeiamold opened this issue · 6 comments

The problem

babel-plugin-transform-import-meta combined with createRequire seems to create a situation where _require is being used before it's defined.

Example

I have simple simple mjs:

import { createRequire } from 'module';
const require = createRequire( import.meta.url );
export default require( '../package.json' ).version;

When I use babel-plugin-transform-import-meta in my babel --config-file ./.babelrcForBuild.json src -d dist command I get a weird self-reference for _require:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _module = require("module");

var _require = (0, _module.createRequire)(_require('url').pathToFileURL(__filename).toString());

var _default = _require('../package.json').version;

exports["default"] = _default;

The problem is that _require is being used before it's defined.

Work Around

I can manually edit that file to change (_require('url'). to (require('url')., but I'd like to avoid haivng to manually make changes if possible

Question

Any tips for me here? Thanks!

I have an idea, add support for ES6 targets.

Usage:

['babel-plugin-transform-import-meta', { target: 'ES6' }]

Output:

import url from 'url';
console.log(url.pathToFileURL(__filename).toString());

Would something like the above solve the problem? Could you try the branch javiertury/babel-plugin-transform-import-meta#add_es6_support and see if this change palys nice with the rest of your stack?

Thanks for the quick response!

I just tried to give that a shot, but it looks like pnpm is a new dependency? I ask because when I try to install that branch via npm using "babel-plugin-transform-import-meta": "git+https://github.com/javiertury/babel-plugin-transform-import-meta#add_es6_support", I get this error:

> babel-plugin-transform-import-meta@2.1.1 prepack \AppData\Roaming\npm-cache\_cacache\tmp\git-clone-0af25f6e
> pnpm run lint && pnpm run clean && pnpm run build

/usr/bin/bash: pnpm: command not found
←[37;40mnpm←[0m ←[0m←[30;43mWARN←[0m←[35m←[0m Local package.json exists, but node_modules missing, did you mean to install?
←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m premature close
←[0m
←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m A complete log of this run can be found in:
←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m     \AppData\Roaming\npm-cache\_logs\2022-06-19T18_01_09_105Z-debug.log
←[0m

pnpm is an alternative package manager to npm.

I've published a new version 2.2.0-alpha.1 with the changes to make testing easier for you.

If you want to try it out

npm install babel-plugin-transform-import-meta@2.2.0-alpha.1

And then change your babel configuration. Notice that usage has changed since the last proposal.

['babel-plugin-transform-import-meta', { module: 'ES6' }]

Sweet, thanks! That seems to have eliminated the need for my dumb sed work-around to munge the file after the compile!

And thanks again for the quick turn-around too!

I'll keep an eye pealed for when this gets a non-alpha release :)

I'll leave this issue open in case you want me to test anything else, like the "official" release. But if that's not necessary please feel free to close this issue.

I just released a new version with the changes

📦 2.2.0