0

I have a React application which is written in ES6/JSX and transpiled by the Webpack. Part of this application I want to extract into a separate repository (github) and include as a dependency in package.json. What I do not understand is if I need to create /dist directory with ES5 compiled version of that dependency before pushing it to github.

I thought in case I use Webpack for building the main application my dependency can also be written in ES6 without additional transpyling to ES5. And when I import something from that dependency it's included and transpiled in the main build. Am I right?

3
  • Not unless you specifically tell Webpack/Babel to transpile it. Commented Aug 9, 2017 at 17:42
  • @Scimonster :( so it means that dependency should have an entry point and provide transpiled version of itself in order to be consumed by other apps? Commented Aug 9, 2017 at 17:46
  • 1
    Yes. It ends up being simpler that way not to special case a dependency. Commented Aug 9, 2017 at 17:48

2 Answers 2

2

Typically Webpack configs ignore anything in your package.json. I would suggest building the other library as a totally independent library. If for some reason that's not an option, you could change your Webpack config to allow the other package to be transpiled. Something like this:

loaders: [
  {
    test: /\.jsx?$/,
    exclude: /node_modules\/(?!moduleName)/
    loader: 'babel',
    query: {
      presets: ['react', 'es2015']
    }
  }
],

This would stop Babel from transpiling all node modules except moduleName. Obviously, change that to the correct module name.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you nice. so it means each app that wants to use this dependency is responsible for tuning its webpack config to make transpiling work. hm, now I see why you suggest providing transpiled version of this lib
1

You normally don't. I recommend you to extract your functionality as an independent library (already transpiled), and export every React component (or whatever functionality you are exporting), and then importing it from all the projects you need to.

Using npm you can import git based projects (including github) without the need of publish them into the npm database.

To do so, you can have an index.js with everything imported (from different files) and exported.

2 Comments

Yes, it's something I'm trying to achieve. Do you know if it possible to build npm dependency without explicit entry point? I mean this lib I'm trying to build is just a set of reusable functions and I'd like to keep them separate.
I don't know what do you mean. You can have in your package.json an entry point exporting every functionality you want to. Check the end of async library for example: github.com/caolan/async/blob/master/dist/async.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.