Parse Error: Unexpected token
hirasso opened this issue · 2 comments
hirasso commented
Hi there, just wanted to give animateplus a try, but I keep getting this error when importing it into my app.js:
.../node_modules/animateplus/animateplus.js:217
...rest
^
ParseError: Unexpected token
When using the spread operator directly in my main app.js, it works fine (using Babel)
bendc commented
Hi! Closing as this is related to your Babel/Webpack config, not this library.
FYI, this is not a spread operator but object rest properties.
evanfrawley commented
I was getting this same issue too. Here are the steps that I used to get this library to work with react-scripts
/ webpack / babel. Note: this should similarly work for any other webpack configuration as long as you use babel-loader
.
yarn eject
-- Unfortunately, to add a babel loader to the webpack config, I had to eject thereact-scripts
support from my project. Though a bit cumbersome, you get much more control over how your project is compiled.yarn add --dev babel-plugin-transform-object-rest-spread
so that you can get the babel plugin that will fix theUnexpected Token
error that is being encountered.
This will change yourdevDependencies
in thepackage.json
file to look something like:
"devDependencies": {
...
"babel-plugin-transform-object-rest-spread": "^6.26.0",
...
},
- Lastly, in the
webpack.config.dev.js
andwebpack.config.prod.js
, I changed two things. First, I added thebabel-loader
entry to include theanimateplus
node module and I added the plugin that we added above. Mybabel-loader
entry now looks like:
webpack.config.dev.js
...
// Process JS with Babel.
{
test: /\.(js|jsx|mjs)$/,
include: [paths.appSrc, `${paths.appNodeModules}/animateplus`],
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,
plugins: ["transform-object-rest-spread"],
},
},
...
webpack.config.prod.js
...
// Process JS with Babel.
{
test: /\.(js|jsx|mjs)$/,
include: [paths.appSrc, `${paths.appNodeModules}/animateplus`],
loader: require.resolve('babel-loader'),
options: {
plugins: ["transform-object-rest-spread"],
compact: true,
},
},
...
Other notes:
Here are the other babel
deps that may or may not be necessary:
"dependencies": {
...
"babel-core": "6.26.0",
"babel-eslint": "7.2.3",
"babel-jest": "20.0.3",
"babel-loader": "7.1.2",
"babel-preset-react-app": "^3.1.1",
"babel-runtime": "6.26.0",
...
}