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

Support es modules syntax in .config & webpack.config #155

Closed
jxodwyer opened this issue May 2, 2016 · 16 comments
Closed

Support es modules syntax in .config & webpack.config #155

jxodwyer opened this issue May 2, 2016 · 16 comments

Comments

@jxodwyer
Copy link

jxodwyer commented May 2, 2016

It seems as though babel isn't working properly for me. Here's my .babelrc file:

{
  "presets": ["es2015", "react", "stage-0"],
  "plugins": ["transform-runtime", "add-module-exports"],
  "env": {
    "development": {
      "plugins": [
        ["react-intl", {
          "messagesDir": "./build/messages/",
          "enforceDescriptions": false
        }]
      ]
    }
  }
}

and my .storybook/webpack.config.js file:

import config from '../config';
import base from '../build/webpack-environments/_base';

export default require(`../build/webpack-environments/${config.env}`)(base);

Error I'm seeing:

> start-storybook -p 9001

=> Loading custom .babelrc
.../.storybook/webpack.config.js:1
import config from '../config';
^^^^^^

SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:54:16)
    at Module._compile (module.js:375:25)
    at Object.Module._extensions..js (module.js:406:10)
    at Module.load (module.js:345:32)
    at Function.Module._load (module.js:302:12)
    at Module.require (module.js:355:17)
    at require (internal/module.js:13:17)
    at exports.default (.../node_modules/@kadira/storybook/dist/server/config.js:41:22)
    at Object.<anonymous> (.../node_modules/@kadira/storybook/dist/server/index.js:84:35)
    at Module._compile (module.js:399:26)
@arunoda
Copy link
Member

arunoda commented May 2, 2016

What's the node version you are running? I hope it's not 5.x.x
The file .storybook/webpack.config.js is not runs with a babel loader, so it uses the normal node binary.
You may need to use ES5 in that file.

Otherwise, use the latest node version.

@jxodwyer
Copy link
Author

jxodwyer commented May 2, 2016

$ node -v
v5.2.0
$ npm -v
3.3.12

is this not compatible with 5.x.x? So eitherway I will have to stick to es5 in webpack.config.js?

@arunoda
Copy link
Member

arunoda commented May 2, 2016

Hmm. That's weird.
Try using ES5 in the webpack.config.js.

This is the place which loads, babel config for client apps.

@moroshko
Copy link

moroshko commented May 6, 2016

@arunoda It would be nice to be able to use things like import and the destructuring operator in .storybook/webpack.config.js the same way you use it in server/config.js or server/webpack.config.js.
Is there any reason why can't we use Babel in .storybook/webpack.config.js?

@loudwinston
Copy link

My solution to this was use babel-register. I created 2 files, webpack.config.js and webpack.config.es6.js. webpack.config.js is responsible for loading babel-register then requiring webpack.config.es6.js.

webpack.config.js

require('babel-register');
module.exports = require('./webpack.config.es6.js');

webpack.config.es6.js

import config from '../config';
//whatever other es6 code you need
export default config;

@TigerC10
Copy link
Contributor

TigerC10 commented Jun 3, 2016

@jxodwyer / @moroshko / @loudwinston - Rename webpack.config.js to webpack.config.babel.js and make sure your webpack (and/or webpack-dev-server) is updated to the latest version. It should obey your .babelrc file and allow ES2015 syntax once you do.

@vvo
Copy link

vvo commented Dec 2, 2016

@arunoda Does storybook automatically load webpack.config.babel.js? Seems like it does not, it only tries to read webpack.config.js

@arunoda
Copy link
Member

arunoda commented Dec 2, 2016

@vvo we only support .babelrc for the app project. Check here: https://getstorybook.io/docs/react-storybook/configurations/custom-babel-config

For the webpack config, we need to use ES5. (or something supported by your Node version).

@vvo
Copy link

vvo commented Dec 2, 2016

@arunoda webpack is able to either load webpack.config.babel.js if present (and will use .babelrc) or load webpack.config.js otherwise. Would you want to support this in storybook too?

@arunoda
Copy link
Member

arunoda commented Dec 2, 2016

@vvo That's not in our plans yet. If we are going to do so, we'll do it for the webpack.config.js.

@ndelangen ndelangen self-assigned this Mar 28, 2017
@ndelangen ndelangen changed the title Babel load issue Support es modules syntax in .config & webpack.config Jul 7, 2017
@ndelangen
Copy link
Member

A PR for this is very much welcome!

@ndelangen ndelangen removed their assignment Aug 28, 2017
@stale
Copy link

stale bot commented Nov 14, 2017

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 60 days. Thanks!

@stale stale bot added the inactive label Nov 14, 2017
@ndelangen
Copy link
Member

Will be implemented in #1209

@stale stale bot removed the inactive label Nov 15, 2017
@stale
Copy link

stale bot commented Dec 30, 2017

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 60 days. Thanks!

@stale stale bot added the inactive label Dec 30, 2017
@stale
Copy link

stale bot commented Jan 14, 2018

Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook!

@stale stale bot closed this as completed Jan 14, 2018
@eric-burel
Copy link
Contributor

eric-burel commented Jan 6, 2020

Hi, this issue is still a problem in my app. The webpack config is rather complex and need ES6 (coming from electron-react-boilerplate). Having a separate config causes discrepancies between Storybook and the app. What could be a possible solution in current Storybook version?

Edit: using @babel/register solves this issue for me.

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

8 participants