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

Unexpected token, You may need an appropriate loader to handle this file type. #3685

Closed
justinlazaro-ubidy opened this issue Jan 4, 2018 · 8 comments

Comments

@justinlazaro-ubidy
Copy link

justinlazaro-ubidy commented Jan 4, 2018

Just created an npm package, and when i use it on my project created by create-react-app
got error, any thoughts?

Module parse failed: Unexpected token (8:6)
You may need an appropriate loader to handle this file type.
|   render() {
|     return (
|       <section className="FullWidthLoading-section">
|         <Spin
|           delay={props.delay}

my npm package

class FullWidthLoading extends Component {
  render() {
   return (
    <section className="FullWidthLoading-section">
      <Spin
        delay={props.delay}
        indicator={props.indicator}
        size={props.size}
        spinning={props.spinning}
        tip={props.tip}
        wrapperClassName={props.wrapperClassName}
      >
        {props.children}
      </Spin>
    </section>
    )
  }
};

export default FullWidthLoading;
@threepointone
Copy link

you should ship compiled code in your npm package, not raw jsx, since babel/other loaders don't run on anything inside node_modules.

@justinlazaro-ubidy
Copy link
Author

Solve, thanks.

 // Process JS with Babel.
          {
            test: /\.(js|jsx|mjs)$/,
            // include: paths.appSrc, remove to support reactjs in node_modules
            loader: require.resolve("babel-loader"),
            options: {
              // This is a feature of `babel-loader` for webpack (not Babel itself).
              // It enables caching results in ./node_modules/.cache/babel-loader/
              // directory for faster rebuilds.
              cacheDirectory: true
            }
          }

@miraage
Copy link

miraage commented Jan 5, 2018

Usually it is not a great idea to compile everything with babel since it will slow compilation time.

Almost all code you're using from node_modules has already been processed with babel so you don't need to do it.

I would suggest you to process your package code with Rollup to achieve smallest output possible and point main section to correct file. Also it is noteworthy to mention is how to organize exports in package in order to make tree-shaking work as efficient as possible.

@justinlazaro-ubidy
Copy link
Author

Hi @miraage thanks for the info. Im doing a UI kit in react, or maybe i can do this?

include: {
paths.appSrc,
/node_modules/custom_ui_kit/
}

or i need to build my ui kit before publish in npm.

any tips? thanks.

@gaearon
Copy link
Contributor

gaearon commented Jan 7, 2018

You need to compile code before publishing. People who use your library shouldn’t need to configure anything to use it.

Here’s a tool that you can use to do that: https://github.com/insin/nwb

@gaearon gaearon closed this as completed Jan 7, 2018
@justinlazaro-ubidy
Copy link
Author

Many thanks for the info @gaearon

@mileung
Copy link

mileung commented May 17, 2018

I've published React Native packages with JSX no problem. Guess this is just a React thing?

@umeshShindepatil
Copy link

@threepointone or @justinlazaro-ubidy

Please can you share file path where i can put my changes for this issue

thanks

tony added a commit to tony/cv that referenced this issue Dec 26, 2018
@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants