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

Parcel reports "Export 'X' is not defined" if a ESM module tries to export a function #2820

Closed
mweststrate opened this issue Mar 20, 2019 · 16 comments
Labels
Babel ✖️ Non-Parcel bug Bugs related to dependencies or plugins Stale Inactive issues

Comments

@mweststrate
Copy link

mweststrate commented Mar 20, 2019

🐛 bug report

If in an ESM module the export statement refers to a function, this consistently fails the build.

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

To reproduce, clone: https://github.com/mweststrate/parcel-bug, yarn install, yarn build

This happens with an out-of-the-box configuration with parcel, no .babelrc, package.json:

{
  "name": "parcel-bug",
  "version": "1.0.0",
  "main": "index.js",
  "author": "Michel Weststrate <mweststrate@gmail.com>",
  "license": "MIT",
  "devDependencies": {
    "parcel-bundler": "^1.12.2"
  },
  "scripts": {
    "build": "parcel public/index.html"
  },
  "dependencies": {
    "immer": "^2.1.4"
  }
}

🤔 Expected Behavior

The project should build without error, even when an export statement references a function

😯 Current Behavior

Build failure, any referred function is reported as Export '...' is not defined

When using the immer package with parcel I consistently get the following build error:

🚨  /home/michel/Dropbox/presentaties/parcel-bug/node_modules/immer/dist/immer.module.js:1065:113: Export 'original' is not defined (1065:113)
  1063 | 
  1064 | export default produce;
> 1065 | export { produce, setAutoFreeze, setUseProxies, applyPatches$1 as applyPatches, createDraft, finishDraft, Immer, original, isDraft, isDraftable, NOTHING as nothing, DRAFTABLE as immerable };
       |                                                                                                                 ^
  1066 | 
  1067 | 

The package does bundle correctly when original, isDraft and isDraftable are removed from the export statement. Unlike all other exports, which are defined as var, these three are defined as functions in the module scope in node_modules/immer/dist/immer.module.js, like:

function original(value) {
  if (value && value[DRAFT_STATE]) {
    return value[DRAFT_STATE].base;
  } // otherwise return undefined
}

💁 Possible Solution

🔦 Context

I'm converting a CRA app to parcel, however this is withholding me from that.

💻 Code Sample

https://github.com/mweststrate/parcel-bug

🌍 Your Environment

Software Version(s)
Parcel 1.12.2
Node 6.4.1
npm/Yarn 1.12.3
Operating System Linux x250 4.18.0-15-generic #16-Ubuntu SMP Thu Feb 7 10:56:39 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
@pumano
Copy link

pumano commented Mar 20, 2019

Got the same thing with mobx-react: node_modules/mobx-react/index.module.js:1450:53: Export 'observer' is not defined (1450:53)

@Zero2key
Copy link

Also got the same thing withredux :node_modules/redux/es/redux.js:636:9: Export 'createStore' is not defined (636:9)

@lorenzode
Copy link

Same issue here:

[...]node_modules/react-router-dom/esm/react-router-dom.js:247:42: Export 'NavLink' is not defined (247:42)

@Zero2key
Copy link

Also got the same thing withredux :node_modules/redux/es/redux.js:636:9: Export 'createStore' is not defined (636:9)

I lock these package version parcel used, @babel/*** < 7.4.0. Now resolve this problem.
Seems @babel/***@7.4.0 problem, it have breaking change babel/babel#7646 (comment)

    "@babel/core": "<7.4.0",
    "@babel/plugin-transform-runtime": "<7.4.0",
    "@babel/preset-env": "<7.4.0",
    "@babel/code-frame": "<7.4.0",
    "@babel/generator": "<7.4.0",
    "@babel/parser": "<7.4.0",
    "@babel/plugin-transform-flow-strip-types": "<7.4.0",
    "@babel/plugin-transform-modules-commonjs": "<7.4.0",
    "@babel/plugin-transform-react-jsx": "<7.4.0",
    "@babel/template": "<7.4.0",
    "@babel/traverse": "<7.4.0",
    "@babel/types": "<7.4.0",

@pumano
Copy link

pumano commented Mar 20, 2019

I use typescript, no babel in project, but it hurts my current build with mobx-react :(

@owbp
Copy link

owbp commented Mar 20, 2019

Following the lead from @Zero2key, the babel parser seems to be the culprit and just doing this package gets our builds working for now (as a workaround until a fix is introduced):
"@babel/parser": "<7.4.0",

@mweststrate
Copy link
Author

Confirmed that adding just the following to package.json works as a work around (not sure what other things it breaks obviously)

  "resolutions": {
    "@babel/parser": "7.3.0"
  },

@pumano
Copy link

pumano commented Mar 20, 2019

@mweststrate confirm it HELPS ME A LOT!!!

@mweststrate
Copy link
Author

There is no matching babel issue open, correct? (which isn't strange since 7.4.0 was only released yesterday).

@mweststrate
Copy link
Author

Maintainers: does this mean a new issue should be opened on babel? Or is it an API compatibility issue with the latest babel version?

@Zero2key
Copy link

I find this @babel/parser PR Allow any reserved word in export {} from specifiers #9616, maybe this PR cause problem, but I don't confirm.

@mischnic
Copy link
Member

It triggered by this line in Parcel.

Setting strictMode to null (which seems to be the default) or true works. false fails with that error.

async parse(code) {
return babelParser.parse(code, {
filename: this.name,
allowReturnOutsideFunction: true,
strictMode: false,
sourceType: 'module',
plugins: ['exportDefaultFrom', 'exportNamespaceFrom', 'dynamicImport']
});
}

Reproduction using only Babel:

const parser = require("@babel/parser");

const code = `
function original(value) {
  if (value) {
    return value.base;
  }
}

export { original };
`;

console.log(parser.parse(code, {
  strictMode: false, // <-------------------- works with `true` or `null`
  sourceType: 'module',
}));

So I guess it's a Babel bug?

@devongovett
Copy link
Member

Just released parcel v1.12.3 which downgrades Babel to <7.4 while we figure this out.

@devongovett
Copy link
Member

Filed this issue with babel: babel/babel#9720

@nicolo-ribaudo
Copy link

Fixed in Babel 7.4.2

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

@github-actions github-actions bot added the Stale Inactive issues label Jan 17, 2020
@github-actions github-actions bot closed this as completed Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Babel ✖️ Non-Parcel bug Bugs related to dependencies or plugins Stale Inactive issues
Projects
None yet
Development

No branches or pull requests

8 participants