LearnNewsExamplesServices
Frontmatter
id467
titleES2020: Add optional chaining once possible (Webpack issue)
stateClosed
labels
enhancement
assignees[]
createdAtApr 13, 2020, 12:43 PM
updatedAtJun 28, 2021, 10:39 AM
githubUrlhttps://github.com/neomjs/neo/issues/467
authortobiu
commentsCount4
parentIssuenull
subIssues[]
subIssuesCompleted0
subIssuesTotal0
blockedBy[]
blocking[]
closedAtJun 28, 2021, 10:39 AM

ES2020: Add optional chaining once possible (Webpack issue)

tobiu
tobiu commented on Apr 13, 2020, 12:43 PM

In short:

const obj = {
  prop1: {
    prop2: {
      prop3: {
        prop4: {
          prop5: 5
        }
      }
    }
  }
}

obj.prop1 &&
  obj.prop1.prop2 &&
  obj.prop1.prop2 &&
  obj.prop1.prop2.prop3 &&
  obj.prop1.prop2.prop3.prop4 &&
  console.log(obj.prop1.prop2.prop3.prop4.prop5);

VS

const obj = {
  prop1: {
    prop2: {
      prop3: {
        prop4: {
          prop5: 5
        }
      }
    }
  }
}

console.log(obj?.prop1?.prop2?.prop3?.prop4?.prop5);

This can some boiler-plate code, but unfortunately, webpack can not handle it yet. Browsers already support it, but webpack wants to parse it.

ERROR in ./apps/covid/view/TableContainerController.mjs 191:37 Module parse failed: Unexpected token (191:37) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders | } |

    me.loadHistoricalData(record?.countryInfo?.iso2 || 'all');

Since Babel is not an option for neo, we need to wait for this PR:

https://github.com/acornjs/acorn/pull/891

(Webpack relies on acornjs)

tobiu added the enhancement label on Apr 13, 2020, 12:43 PM
tobiu referenced in commit 2531822 - "Covid.view.TableContainerController: removed optional chaining again for now, see #467" on Apr 13, 2020, 12:55 PM
dreyks
dreyks Jun 11, 2020, 9:46 AM

https://github.com/acornjs/acorn/pull/891 is merged, let's keep an eye on the releases

tobiu
tobiu Jun 11, 2020, 10:17 AM

Thanks for the heads up!

There are probably many spots inside the neo.mjs framework & demo apps code base where we can use it. Optional chaining can definitely help removing some boiler plate code (smaller file-size) as well as making the spots cleaner & easier to read.

Looking forward to it.

tobiu
tobiu Jun 28, 2021, 10:39 AM

just double-checked this one again and we are now finally good to use optional chaining.

will create new tickets in case i see spots.

tobiu closed this issue on Jun 28, 2021, 10:39 AM