Attempting to use the ES6 spread operator to include the content of another object in a component's state produces a compiler error.
component.js
module.exports = class {
onCreate() {
const innerState = {a: 1, b: 2}
this.state = {
...innerState,
c: 3
}
}
}
index.marko
div -- ${state.a}
div -- ${state.b}
div -- ${state.c}
No error
Error as follows:
/Users/me/marko-test/src/components/compound-state/component.js:6
...innerState,
^^^
SyntaxError: Unexpected token ...
at Object.exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.
at Module._compile (module.js:571:32)
This is expected since the spread operator in objects is still a Stage 3 Draft and not supported by Node. Chrome 60+ and Firefox 55+ do support it natively now.
If you want to use object spread in node and other browsers, you'll need to transpile it with babel.
@marko-js/core:
marko-starter?Thanks @mlrawlings, that does make sense. I hadn't noticed the provisional status of the spread operator in object literals and that it doesn't work in Node. Pity. I'll stick to Object.assign() for now, and close this issue.
Please _don't_ add Babel transpilation to marko-starter, at least not as a default in development. Being able to debug source code as-is (with only the "wrapper" prelude added to the first code line) is a killer feature of Marko / Lasso. Source maps "kind of" work but there are always a few glitches of one sort or another. I spent ages messing with Vue and WebPack to try and get around them, whereas Marko "just works" without the need for source maps, as the code is basically what I write. Much better. Transpilation as an option on the minified prod build could be cool though.
Hello,
NodeJS now supports the object spread operator, but Marko still gives the same error message. (Isn't it related to the JS tokenizer used in Marko?)
Most helpful comment
Hello,
NodeJS now supports the object spread operator, but Marko still gives the same error message. (Isn't it related to the JS tokenizer used in Marko?)