-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Spread operator in dist/index.es.js causes "Unexpected token" #95
Comments
Hi @hohenp , I have a feeling both of your issues you filed just now are related to your build process. Pullstate uses somewhat modern JS, but if you need older JS in your final build- you need to transpile that to the older versions (ES5 and the like). It provides two entry files for this in
It appears, from this and your other issue, that your build is making use of the The spread operator, and excluding I think you might just be missing a final build step. Could you share a little more about how you are building your code? And where is the error happening, during the second build, or in the client? |
Just to follow up on this, modern JavaScript has its advantages where things are more efficiently executed with the newer syntaxes in the modern engine. Its easy to transpile to an older JS version, but its not that easy to do it the other way around. Transpiling to an older JS version would cause a lot of other code optimizations to become bloated to accommodate the older syntax as well- which would in turn bloat the library size for the majority of people who don't need it. |
Hi @lostpebble , thanks for your answer. The error happens at the second build. The first build runs well, and then I have my compiled .js file with "your" spread operators in. Then I "require" the .js file in another project, and when I build that one, I get the issue. So, can it be, that an old webpack version in the 2nd project is causing that issue? I cant update the versions, so is there a chance for me to have a build step that compiles your spread operators to an older runtime? |
I think you need to then look at the first build, and what target you are setting for the output there. Is there any further information you can give me about it, like which tooling are you using? Webpack, babel etc.? Generally if you're using Webpack with babel-loader - and you're transpiling for ES5 or older - you need to ensure that any ES6 libraries are being transpiled as well (pullstate in this case). Take a closer look here- its a common occurrence that people run into and unfortunately doesn't have great solutions for yet. I don't know what your build process looks like in the first step though- but the gist of it, is that you need to make sure that initial build step is transpiling |
Ah okay cool. Well I think what you might need to add then, is a
You may have to configure the preset a bit more- but it looks like I'm not sure if |
ok, thanks a lot for your help! Now I know where to look at. |
Okay great, good luck! :) (JS config has always been my least favourite thing about dev) |
Like to give an update on that. Your way to use babel to transpale a specific mode-module was the right glue. I had to go a bit deeper and find the other modules (used by pullstate) and handle them by babel as well. Those are: pullstate, fast-deep-equal and immer I ended up with following webpack rule: {
test: /\.m?js$/,
include: [/pullstate/, /fast-deep-equal/, /immer/],
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
// useBuiltIns: 'entry',
// corejs: 3, // or 2,
targets: {
ie: '11', // or whatever target to choose .
},
},
],
],
},
},
}, Within the root component you then have to enable ES5 for immer. import 'regenerator-runtime/runtime';
import 'core-js/stable'; // if polyfills are also needed
import '@pnp/polyfill-ie11';
import 'whatwg-fetch';
import { enableES5 } from 'immer';
enableES5(); |
I am using pullstate in a separate project.
When I build the project that goes fine.
When I then include that project into another project and build that, I get "Unexpected token" for all the spread operators.
When I check the build .js File the error comes from the spread operator
Is it possible to tranform the spread operator, when you build the pullstate?
The text was updated successfully, but these errors were encountered: