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

Scope hoisting const { a : b } = require("") is not handled correctly #6242

Closed
aminya opened this issue May 7, 2021 · 2 comments
Closed

Comments

@aminya
Copy link
Contributor

aminya commented May 7, 2021

🐛 bug report

If I try to bundle fs-extra with scope hoisting, the resulting code will throw an error during the runtime.

The issue is with this file

'use strict'
const u = require('universalify').fromPromise
const { makeDir: _makeDir, makeDirSync } = require('./make-dir')
const makeDir = u(_makeDir)

module.exports = {
  mkdirs: makeDir,
  mkdirsSync: makeDirSync,
  // alias
  mkdirp: makeDir,
  mkdirpSync: makeDirSync,
  ensureDir: makeDir,
  ensureDirSync: makeDirSync
}

This gives this error that

ReferenceError: _makeDir is not defined

The generated code (part of it) looks like this. _makeDir isn't defined in the last line.

var $3f87dcec6c869b468883028e3d30483c$export$fromPromise = function (fn) {
  return Object.defineProperty(function (...args) {
    const cb = args[args.length - 1];
    if (typeof cb !== 'function') return fn.apply(this, args); else fn.apply(this, args.slice(0, -1)).then(r => cb(null, r), cb);
  }, 'name', {
    value: fn.name
  });
};
var $07fb696951fd6b0d685f71f9f8ea1482$export$makeDir = async (input, options) => {
  $07fb696951fd6b0d685f71f9f8ea1482$var$checkPath(input);
  options = $07fb696951fd6b0d685f71f9f8ea1482$var$processOptions(options);
  if ($07fb696951fd6b0d685f71f9f8ea1482$var$useNativeRecursiveOption) {
    const pth = resolve(input);
    return $934909dd8f4c64c205e1422cec78074c$exports.mkdir(pth, {
      mode: options.mode,
      recursive: true
    });
  }
  const make = async pth => {
    try {
      await $934909dd8f4c64c205e1422cec78074c$exports.mkdir(pth, options.mode);
    } catch (error) {
      if (error.code === 'EPERM') {
        throw error;
      }
      if (error.code === 'ENOENT') {
        if (dirname(pth) === pth) {
          throw $07fb696951fd6b0d685f71f9f8ea1482$var$permissionError(pth);
        }
        if (error.message.includes('null bytes')) {
          throw error;
        }
        await make(dirname(pth));
        return make(pth);
      }
      try {
        const stats = await $934909dd8f4c64c205e1422cec78074c$exports.stat(pth);
        if (!stats.isDirectory()) {
          // This error is never exposed to the user
          // it is caught below, and the original error is thrown
          throw new Error('The path is not a directory');
        }
      } catch {
        throw error;
      }
    }
  };
  return make(resolve(input));
};

const $cf88b871108d46ab64679d4e62654c04$var$u = $3f87dcec6c869b468883028e3d30483c$export$fromPromise;
const $cf88b871108d46ab64679d4e62654c04$var$makeDir = $cf88b871108d46ab64679d4e62654c04$var$u(_makeDir);

I tried to reproduce it with this minimal example, but the error looks different.

const u = require('util').promisify
const { makeDir: _makeDir } = require('./make-dir')  // just exports a dummy function which is called makeDir
const makeDir = u(_makeDir)

Gives:

var promisify = require("util").promisify;
var $917d73bacb9e109ab9b8dcb1a97dd4a1$var$u = {};
var $e5669b1f1b075a899289a8599ce4a0e9$export$makeDir = function () {
  return false;
};
var $917d73bacb9e109ab9b8dcb1a97dd4a1$var$_makeDir = $e5669b1f1b075a899289a8599ce4a0e9$export$makeDir;
var $917d73bacb9e109ab9b8dcb1a97dd4a1$var$makeDir = $917d73bacb9e109ab9b8dcb1a97dd4a1$var$u($917d73bacb9e109ab9b8dcb1a97dd4a1$var$_makeDir);

//# sourceMappingURL=a.js.map
var $917d73bacb9e109ab9b8dcb1a97dd4a1$var$makeDir = $917d73bacb9e109ab9b8dcb1a97dd4a1$var$u($917d73bacb9e109ab9b8dcb1a97dd4a1$var$_makeDir);
                                                    ^
TypeError: $917d73bacb9e109ab9b8dcb1a97dd4a1$var$u is not a function

🎛 Configuration (.babelrc, package.json, cli command)

{
  "test": "./a.js",
  "scripts" : {
    "build":  "parcel build --no-optimize --target test ./a.source.js"
  },
  "targets": {
    "test": {
      "context": "node"
    }
  }
}

🤔 Expected Behavior

😯 Current Behavior

💁 Possible Solution

Don't use scope hoisting on fs-extra

🔦 Context

💻 Code Sample

🌍 Your Environment

Software Version(s)
Parcel 2.0.0-nightly.653
Node 16
npm/Yarn 6
Operating System Win 10
@mischnic
Copy link
Member

mischnic commented May 7, 2021

Well, you found the folllowing out already in #6241 😄

But apart from that issue, ReferenceError: _makeDir is not defined seems to be already fixed with #6230


The first thing I get is

  if (hasOwnProperty('O_SYMLINK') && process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {
      ^

TypeError: Cannot convert undefined or null to object

because

const constants = require("constants");
console.log(constants.hasOwnProperty("abc"));	

is turned into

var {hasOwnProperty: $4g1mB$hasOwnProperty} = require("constants");
console.log($4g1mB$hasOwnProperty("abc"));

@mischnic
Copy link
Member

mischnic commented May 10, 2021

But apart from that issue, ReferenceError: _makeDir is not defined seems to be already fixed with #6230

That has been merged

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

No branches or pull requests

2 participants