Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

babel prevents ESM import #77

Open
noraj opened this issue Oct 9, 2022 · 9 comments
Open

babel prevents ESM import #77

noraj opened this issue Oct 9, 2022 · 9 comments

Comments

@noraj
Copy link

noraj commented Oct 9, 2022

I'm trying to port the CommonJS (CJS) import to a ECMAScript Modules (ESM) import. cf. https://gist.github.com/noraj/007a943dc781dc8dd3198a29205bae04

From:

var jest = require('gulp-jest').default;

to

import gulpJest from 'gulp-jest';

But it fails with the following error when running gulp:

[18:00:50] TypeError: gulpJest is not a function
    at test_data (file:///home/noraj/Dev/rawsec-cybersecurity-inventory/gulpfile.mjs:178:15)
    at bound (node:domain:433:15)
    at runBound (node:domain:444:12)
    at asyncRunner (/home/noraj/Dev/rawsec-cybersecurity-inventory/node_modules/async-done/index.js:55:18)
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11

However node_modules/gulp-jest/lib/index.js seems to have an uncommon way to export.

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

...

var _default = function _default() {...};

exports["default"] = _default;

Could you provide me an example to how to import gulp-jest in a .mjs file or maybe patch the export if it's the cause.

@noraj
Copy link
Author

noraj commented Oct 9, 2022

I just tested and adding module.exports = _default; at the end of the script made it work, like it's done in https://github.com/colynb/gulp-data/blob/master/index.js or https://github.com/gulp-community/gulp-pug/blob/master/index.js.

ESM export example: https://github.com/sindresorhus/del/blob/main/index.js.

@noraj
Copy link
Author

noraj commented Oct 9, 2022

Seems to be fixed in https://github.com/Nerajno/gulp-jest/blob/master/src/index.js, it just lacks a new release. The reported repository on the last yarn / npmjs (4.0.4) release is https://github.com/aarontrank/gulp-jest and it's not using ESM at all.

but it seems other package are redirecting to your repository

@noraj
Copy link
Author

noraj commented Oct 9, 2022

yarn add gulp-jest

node_modules/gulp-jest/lib/index.js

"use strict";

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

var _pluginError = _interopRequireDefault(require("plugin-error"));

var _through = _interopRequireDefault(require("through2"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var _require = require('@jest/core'),
    runCLI = _require.runCLI;

var _default = function _default() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return _through["default"].obj(function (file, enc, cb) {
    options = Object.assign({
      rootDir: file ? process.cwd() : undefined
    }, options);
    runCLI(options, [options.rootDir]).then(function (_ref) {
      var results = _ref.results;

      if (results.numFailedTests || results.numFailedTestSuites) {
        cb(new _pluginError["default"]('gulp-jest', {
          message: 'Tests Failed'
        }));
      } else if (typeof results.success !== 'undefined' && !results.success) {
        if (results.snapshot && results.snapshot.failure) {
          cb(new _pluginError["default"]('gulp-jest', {
            message: 'Tests Failed due to coverage snapshot failure'
          }));
        } else {
          cb(new _pluginError["default"]('gulp-jest', {
            message: 'Tests Failed due to coverage threshold breaches'
          }));
        }
      } else {
        cb();
      }
    });
  });
};

exports["default"] = _default;

@noraj
Copy link
Author

noraj commented Oct 9, 2022

gulp-js 4.0.4 package is up to date from this repository. It's babel that is generating this horrible code.

$ git clone https://github.com/Nerajno/gulp-jest
$ cd gulp-jest
$ yarn install
$ yarn run build
$ cat lib/index.js

@noraj noraj changed the title ESM import babel prevent ESM import Oct 9, 2022
@noraj noraj changed the title babel prevent ESM import babel prevents ESM import Oct 9, 2022
@noraj
Copy link
Author

noraj commented Oct 9, 2022

In https://www.bigbinary.com/blog/helping-babel-move-to-esm, they give an example where

const rollupBabel = require("@rollup/plugin-babel").default;

is replaced with

import { babel as rollupBabel } from "@rollup/plugin-babel";

But replacing

var jest = require('gulp-jest').default;

with

import { jest as gulpJest } from 'gulp-jest';

But it always end in:

SyntaxError: Named export 'jest' not found. The requested module 'gulp-jest' is a CommonJS module, which may not support all module.exports as named exports.

I can't figure what is the export name.

This doesn't work either.

import * as gulpJest from 'gulp-jest';

@noraj
Copy link
Author

noraj commented Oct 9, 2022

This seems to be related to the usage of @babel/plugin-transform-modules-commonjs that is included in @babel/preset-env.

@noraj
Copy link
Author

noraj commented Oct 9, 2022

Some projects like babel/babel#11701 has made some work to publish both as ESM or CJS cf. https://github.com/babel/babel/pull/13414/files

@SamesJeabrook
Copy link

SamesJeabrook commented Mar 13, 2023

Has there been any update on this as I'm also stuck at this point and may have to look at using different tools.

@noraj
Copy link
Author

noraj commented Oct 22, 2023

No, the project seems abandoned. The author seems to only merge dependabot PR from times to times but as no been committing since April and has never given any answer for this issue for one year. The last npmjs release is 3 years old. I think I'll drop this dependency and replace the script:

  "scripts": {
-    "test": "gulp test"
+    "test": "jest"
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants