5

I'm facing problems after update @atalaskit/form from version 2.1.2 to the latest version (5.2.7).

This new update makes use of export { default } from './File' which I think my babel and/or webpack doesn't support.

So far I found out that create-react-app 2.1.8 runs well this syntax.

Also, I found this kind of syntax here: https://github.com/tc39/proposal-export-default-from

I tried to include this proposal in my .babelrc file but the error stands.

@atlaskit/form/index.js (Where the error comes)

export { default } from './Form';
export { default as FormHeader } from './FormHeader';
export { default as FormFooter } from './FormFooter';
export { default as FormSection } from './FormSection';
export { default as Field } from './Field';
export { default as CheckboxField } from './CheckboxField';
export { HelperMessage, ErrorMessage, ValidMessage } from 
'./Messages';
export { default as Fieldset } from './Fieldset';

Error:

(function (exports, require, module, __filename, __dirname) { export 
{ default } from './Form';
                                                          ^^^^^^

SyntaxError: Unexpected token export
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js 
(internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function._load ```(/home/userX/appX/node_modules/@sentry/node/src/integrations/console.ts 
:37:43)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.@atlaskit/form (/home/userX/appX/webpack:/external 
    "@atlaskit/form":1:1)
    at __webpack_require__ 
(/home/userX/appX/webpack:/webpack/bootstrap:19:1)

.babelrc:

{
"presets": [
    "env",
    "react",
    "stage-2"
]
}

webpack.dev.js

const merge = require('webpack-merge');
const css = require('./webpack.css.js');
const server = require('./webpack.server.js');
const client = require('./webpack.client.js');

const devConfigs = {
    mode: 'development',
    devtool: 'source-map',
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            }
        ]
    }
};

module.exports =
    [ css, merge(client, devConfigs), merge(server, devConfigs) ];

webpack.client.js

const webpack = require('webpack');
const path = require('path');

module.exports = {
    entry: {
        file1: ['babel-polyfill', './src/containers/file1/index.js'],
        file2: ['babel-polyfill', './src/containers/file2/index.js'],
        file3: ['babel-polyfill', './src/containers/file3/index.js'],
        },
    output: {
        path: path.resolve(__dirname, 'public/js/react'),
        filename: '[name].js',
        publicPath: '/'
    }
};

webpack.server.js

const nodeExternals = require('webpack-node-externals');
const webpack = require('webpack');

module.exports = {
    entry: {
        server: ['babel-polyfill', './app.js']
    },
    target: 'node',
    node: {
        __dirname: false,
        __filename: false
    },
    output: {
        path: __dirname,
        filename: 'server.js',
        publicPath: '/'
    },
    externals: nodeExternals(),
    plugins: [
        new webpack.BannerPlugin({
            banner: 'require("source-map-support").install();',
            raw: true,
            entryOnly: false
        })
    ]
};

Package.json

{
  ...
  "private": true,
  "scripts": {
    "start": "node server.js",
    "build:watch": "webpack --config webpack.dev.js --watch",
    "build:prod": "webpack --config webpack.prod.js && rm public/js/react/*.map",
    "lint": "./node_modules/.bin/eslint routes/api/ routes/services/"
  },
  "dependencies": {
    "@atlaskit/badge": "^8.1.0",
    "@atlaskit/breadcrumbs": "^6.0.5",
    "@atlaskit/button": "^8.2.1",
    "@atlaskit/checkbox": "^3.0.3",
    "@atlaskit/dynamic-table": "^9.2.6",
    "@atlaskit/field-base": "^10.2.0",
    "@atlaskit/field-text": "^6.0.1",
    "@atlaskit/field-text-area": "^3.2.1",
    "@atlaskit/flag": "^8.1.1",
    "@atlaskit/form": "^2.1.2",
    "@atlaskit/icon": "^12.8.0",
    "@atlaskit/inline-dialog": "^7.1.3",
    "@atlaskit/inline-message": "^5.1.2",
    "@atlaskit/lozenge": "^6.2.2",
    "@atlaskit/modal-dialog": "^7.2.3",
    "@atlaskit/page": "^7.2.0",
    "@atlaskit/page-header": "^5.1.4",
    "@atlaskit/quick-search": "^3.0.2",
    "@atlaskit/section-message": "^1.0.7",
    "@atlaskit/select": "^4.0.1",
    "@atlaskit/spinner": "^7.1.1",
    "@atlaskit/table-tree": "^3.1.3",
    "@atlaskit/tag": "^6.0.3",
    "@atlaskit/tag-group": "^6.0.6",
    "@atlaskit/theme": "^4.0.4",
    "@atlaskit/toggle": "^4.0.1",
    "@atlaskit/tooltip": "^10.3.1",
    "@sentry/node": "^4.4.1",
    "async": "^2.6.1",
    "atlassian-connect-express": "~3.3.0",
    "atlassian-jwt": "^0.1.5",
    "aws-sdk": "^2.429.0",
    "axios": "^0.18.0",
    "bluebird": "^3.5.1",
    "body-parser": "^1.18.2",
    "busboy": "^0.2.14",
    "compression": "^1.7.1",
    "cookie-parser": "^1.4.3",
    "dateformat": "^3.0.3",
    "errorhandler": "^1.5.0",
    "express": "^4.16.2",
    "express-hbs": "^1.0.4",
    "express-state": "^1.4.0",
    "feedme": "^1.1.1",
    "form-data": "^2.3.2",
    "ftp": "^0.3.10",
    "glob": "^7.1.2",
    "handlebars": "^4.0.11",
    "i18n": "^0.8.3",
    "is-base64": "^0.1.0",
    "is-image": "^2.0.0",
    "js-base64": "^2.5.1",
    "lodash": "^4.17.5",
    "mime": "^2.2.0",
    "mkdirp": "^0.5.1",
    "moment": "^2.20.1",
    "moment-timezone": "^0.5.14",
    "mongodb": "^3.0.1",
    "mongoose": "^5.0.1",
    "morgan": "^1.9.0",
    "nodemailer": "^5.1.1",
    "nodemailer-ses-transport": "^1.5.1",
    "pretty-bytes": "^4.0.2",
    "react": "^16.4.0",
    "react-ckeditor-component": "^1.1.0",
    "react-dom": "^16.4.0",
    "react-dropzone": "^4.2.12",
    "react-image-gallery": "^0.8.12",
    "react-infinite-scroller": "^1.2.2",
    "react-intl": "^2.4.0",
    "react-throttle": "^0.3.0",
    "request": "^2.83.0",
    "request-promise": "^4.2.4",
    "sanitize-filename": "^1.6.1",
    "source-map-support": "^0.5.9",
    "sqs-consumer": "^5.2.0",
    "ssh2-sftp-client": "^2.0.1",
    "static-expiry": "^0.0.11",
    "styled-components": "^3.3.2",
    "uglifyjs-webpack-plugin": "^2.1.1",
    "uuid": "^3.2.1",
    "winston": "^2.4.0",
    "winston-aws-cloudwatch": "^2.0.0"
  },
  "devDependencies": {
    "@sentry/webpack-plugin": "^1.6.2",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "css-loader": "^1.0.0",
    "debug": "^3.1.0",
    "eslint": "^5.7.0",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-config-standard": "^12.0.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-node": "^7.0.1",
    "eslint-plugin-promise": "^4.0.1",
    "eslint-plugin-sonarjs": "^0.2.0",
    "eslint-plugin-standard": "^4.0.0",
    "extract-text-webpack-plugin": "4.0.0-beta.0",
    "husky": "^1.1.2",
    "style-loader": "^0.21.0",
    "webpack": "^4.27.1",
    "webpack-cli": "^3.2.1",
    "webpack-merge": "^4.1.4",
    "webpack-node-externals": "^1.7.2"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm run lint"
    }
  }
}
LuisMendes535
  • 526
  • 7
  • 18
  • Looks like you used export instead of import { default } from './Form'; – henrik123 Apr 15 '19 at 13:58
  • it is clearly saying `SyntaxError: Unexpected token export` why have you modified babel ? what does `export` `from` mean? – Amir-Mousavi Apr 15 '19 at 14:21
  • Atlassian defined the export in that way. I'm not sure but I think that's a new feature from ES6 and .babelrc could transpile that syntax into common JS. – LuisMendes535 Apr 15 '19 at 14:26
  • I tried to run the same library with Create-react-app 2.1.8 and everything worked fine. So I guess it is not a problem with @atlaskit/form/index.js syntax. – LuisMendes535 Apr 15 '19 at 16:15
  • https://babeljs.io/docs/en/v7-migration-api#export-changes. Did you try adding back add-module-exports plugin to see if it will resolve the problem? – Thierry Prost Apr 23 '19 at 11:49
  • Yes, I already tried that Thierry. I'm wondering if is something related to polyfill since the build gets done fine and only when I try to run the server file it fails. – LuisMendes535 Apr 23 '19 at 13:38

4 Answers4

4

default is a preserved key word in javascript you can not use it as a export namespace or variable.

Yash Pokar
  • 4,939
  • 1
  • 12
  • 25
  • I believe so but why it works with create-react-app 2.18 webpack configuration? It also works with this webpack.config.js: module : { rules: [{ test: /\.js?$/, exclude : /node_modules/, loader: "babel-loader", query: { presets:["@babel/preset-env", "@babel/preset-react"] } }] } – LuisMendes535 Apr 21 '19 at 16:19
3

Solved

I solved the issue. Here it is how I manage to do it:

I noticed that I already was making use of this syntax in @atlaskit/flag package and everything was working fine. This package though was using a different folder structure as you can see in the screenshot:

enter image description here

The previous package has the index.js inside the esm folder. I didn't know the esm purpose so I searched for it and found this great article explaining it.

https://dev.to/bennypowers/you-should-be-using-esm-kn3?fbclid=IwAR3iUen7KXISkgXi_GcfIhHHbmBWrDyD-LG3t7wh6RMbJ9BNjDEJK5zxbK8#esm-a-better-solution

Solution (only one so far):

npm install esm

node -r esm server.js

Questions

1) Shouldn't Atlassian write their packages preventing this kind of situations?

2) Is there any other solution to setup esm package in the webpack or babel files?

I hope this helps someone.

Cheers.

LuisMendes535
  • 526
  • 7
  • 18
  • Hey Luis, thanks for sharing! I'm facing a similar issue with electron-webpack and an @atlaskit dependency. Can you elaborate a bit more of what you did with esm to make it work? How does this integrate into the webpack build? See https://stackoverflow.com/questions/56858139/using-a-dependency-with-an-export-statement-breaks-at-electron-app-startup for more details about my setup. – fstephany Jul 02 '19 at 22:54
  • This is great. Any idea how to compile with this for production? – Neil Jul 18 '19 at 05:05
2

Your .babelrc has the babel presets loaded in a mismatching way from the official documentation.

Should look like this:

{
  "presets": [
      "@babel/preset-env",
      "@babel/preset-react",
      "@babel/preset-stage-2"
  ]
}

Give it a try and let me know how it went, since there isn't much information to go, it does seem like the failure point.

Rafael Rocha
  • 508
  • 1
  • 3
  • 16
  • I'm using babel-core 6.26.3 here so I believe .babelrc is valid in this case. I already upgraded to babel-core 7.0.0 through npx babel-upgrade --write and .babelrc file automatically updates to that syntax. The error stands with babel-core 7.0.0. – LuisMendes535 Apr 17 '19 at 15:39
0

I'm assuming that you're using babel 6, due to the presets structure in your .babelrc file.

I believe the plugin you are looking for is babel-plugin-transform-export-extensions and it's part of the stage-1 preset, which is not included in your project.

If you were using babel 7, the corresponding plugin would have been @babel/plugin-proposal-export-default-from

iMoses
  • 4,338
  • 1
  • 24
  • 39
  • I already tried that plugin whether using babel 6 or 7. Thanks for helping iMoses. P.S: I edited the initial post with package.json. – LuisMendes535 Apr 24 '19 at 09:38