Skip to content

export function not hoisted #4753

Closed

Description

@lukescott

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

export function foo() {} is not hoisted above import. With code below, you get:

console.log(foo) -> undefined

If the current behavior is a bug, please provide the steps to reproduce.

foo.js

import {bar} from "./bar";
export function foo() {}

bar.js

import {foo} from "./foo";
console.log(foo);

index.js

import "./foo";

.babelrc:

{
	"presets": [
		["latest", {"es2015": {"modules": false}}],
	],
}

What is the expected behavior?

console.log(foo) -> function foo() {}

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

  • Max OS X 10.11.6
  • Chrome 57
  • Node 7.8.0
  • webpack 2.3.3

When you remove ["latest", {"es2015": {"modules": false}}], from .babelrc (set modules to true) it works as expected.

Babel produces this for foo.js:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.foo = foo;

var _bar = require("./bar");

function foo() {}

If I change foo.js to this it breaks with babel:

import {bar} from "./bar";
function foo() {}
export {foo}

This is what babel produces:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.foo = undefined;

var _bar = require("./bar");

function foo() {}
exports.foo = foo;

It seems that Babel makes a distinction between export function foo{} and function foo() {}; export {foo} and webpack does not.

Activity

  1. loganfsmyth commented on Apr 20, 2017

    @loganfsmyth
    Contributor

    Looks like they are both a little wrong.

    function foo(){}
    export {foo};
    

    and

    export function foo(){}
    

    should both export foo before processing the imported modules. Functions declaration bindings are initialized with their values before evaluating their imported modules.

  2. lukescott commented on Apr 20, 2017

    @lukescott
    Author

    Is var hoisted the same way as function? So does this also apply?

    import {bar} from "./bar";
    export var foo = "foo"

    I get undefined for this case as well.

  3. loganfsmyth commented on Apr 20, 2017

    @loganfsmyth
    Contributor

    For var it would be undefined and for let/const it would be a tdz error

  4. added this to the webpack 2.4 milestone on Apr 20, 2017
  5. added 2 commits that reference this issue on May 4, 2017
    820c8f0
    a952bb9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions