2

Is there a way to load every label data and every SObject description data using only Javascript without any Apex Server-side code?

How do I import * from @salesforce/schema or @salesforce/label?

I am looking for a way to get all labels or all SObject describe information details to retrieve in LWC at once.

I can see that such syntax is provided by ES6 and is used widely by Salesforce itself, however, it is not possible to use the following syntax in LWC:

 import * as schema from '@salesforce/schema' 
 import * as label from '@salesforce/label'

or this syntax

import {label, schema} from '@salesforce';

since both versions generate error

LWC1512: Missing resource value for @salesforce"

or

LWC1512: Missing resource value for @salesforce/label

Is there a way to load every label data and every SObject description data using only Javascript without any Apex Server-side code?

By the way, if we look as Salesforce Playground we could notice that enter image description here the same syntax is used to import everything from lwc

import * as Engine from 'lwc';

however, if we try to insert such line into any of Lightning Web Component, we would receive the following error

LWC1001: Invalid import. Namespace imports are not allowed on "lwc", instead use named imports "import { LightningElement } from 'lwc'" or LWC1517: Invalid LWC import

  • 1
    +1 , SF does not support SELECT * In SOQL , I believe it would be same here to maintain consistency. – Pranay Jaiswal Mar 1 at 16:29
  • while import * from is used in Playground example, it is not possible to use the same line in the LWC component itself. – Patlatus Mar 1 at 16:44
  • Maybe there is some different solution which wouldn't require import * from – Patlatus Mar 1 at 16:45
3

An ES6 module is a JavaScript file that explicitly exports variables or functions that other modules can use. Modules make it easier to structure your code without polluting the global scope.

Everything in LWC which is imported and Exported is a ES6 module.

JavaScript files in Lightning web components are ES6 modules. By default, everything declared in a module is local—it’s scoped to the module.

As this is basically es6 and not just LWC, I tried searching import all from es6.

Surprisingly, there are many results, I tried a solution from most accepted and most voted answer that suggested exporting * first and then importing it in new component.

My code which exports all lables in AllLables module

import { LightningElement } from 'lwc';
export * from '@salesforce/label';


export default class AllLables extends LightningElement {

}

when I try saving it, I get this error:

LWC1507: Exporting * from @salesforce/label is not allowed.

Apparently, even if there is the way in ES6, SF is intentionally blocking it, probably for the same reason it does not allow SELECT * in SOQL

You have to use Apex :(

  • 2
    +1 Perhaps even more so, a single object's metadata may well exceed 1MB of raw data, so for an org with hundreds of custom objects, such a component could easily consume a GB of RAM just for itself. – sfdcfox Mar 1 at 17:37
  • 1
    @sfdcfox you are right! One of the motivation to control the amount of code that needs to sent to the client. Another motivation is static analysis, the namespaced import (via *) doesn't allow the LWC compiler to detect what piece of metadata the component relies on. – pmdartus Mar 7 at 13:36
1

Adding on top of Pranay's answer.

If you refer to the Supported JavaScript documentation, there some JS functions which are specific to the LWC programming model. Those are not part of standard ES6 modules. And @salesforce is one of those.

This developer guide explains how to develop Lightning web components and documents the JavaScript functions that are unique to the programming model.

And because those are specifically developed by Salesforce as part of the model, Salesforce will always have full authority on the usage and syntax.

And one of the reasons I would think is to not allow an import * directive is the payload it may end up with (as sfdcfox has mentioned in the comments).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.