2

Hi Im removing html tags from a richtext input in order to make it a raw string, the string will be a code snippet. However I could not achieve it through using the standalone.js of prettier library. since it throws an error indicating that a babel-parser.js is needed or something like that, and when I upload that as a static resource too, the LWC just throws "undefined" as error for that babel-parser.js loadscript process.

So I dont know if there is a way to call a public API or something in the internet that returns the string correctly code formatted. Also why Does salesforce fail to load some specific .js files from static resources to LWC.

It happened me with ChartJS and now its happening with Prettier.

This is what I've tried so far:

Apex Controller =>

public with sharing class CustomChatterFeedController { 

    @AuraEnabled
    public static String plainText(String text){
        return text.stripHtmlTags();
    }

}

LWC .js =>

import { LightningElement } from "lwc";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
import { loadScript } from "lightning/platformResourceLoader";
import bundle from '@salesforce/resourceUrl/bundle';
import plainText from "@salesforce/apex/CustomChatterFeedController.plainText";

export default class CodeFormatter extends LightningElement {
    myLibInitialized = false;
    textValue = '';

    async renderedCallback() {
        if (this.myLibInitialized) {
            return;
        }
        this.myLibInitialized = true;

        try {
            await Promise.all([
                loadScript(this, bundle + '/standalone.js'),
                loadScript(this, bundle + '/plugins/babel.js'),
            ]);
            console.log('success!');
        } catch (error) {
            console.log(error);
            this.dispatchEvent(
                new ShowToastEvent({
                    title: "Error loading prettier",
                    message: error,
                    variant: "error",
                }),
            );
        }
    }

    handleTextChange(evt) {
        this.textValue = evt.target.value;
    }

    async formatCode(evt) {
        const text = this.refs.textBox.value;
        const cleanedText = await plainText({ text }); // al the String comes in one line
        console.log('cleanedText');
        console.log(cleanedText);
        const formattedCode = await prettier.format(cleanedText, { parser: 'babel' });
        console.log(formattedCode);
    }
}

and keeps throwing errors like this: [Couldn't resolve parser "babel". Plugins must be explicitly added to the standalone bundle.]. Which I know its caused because this standalone.min.js file does not contain the parser bundles.

So Im kind of giving up with this kind of solution, and wanted to ask, is there a way to make this through a public API from prettier or other formatter, that I can call from salesforce?

All of this is because I need to mimic the chatter </> button functionality.

Edit: This is How I have structured the zip that I use as a static resource: enter image description here

the zip name is "bundle" and its is standalone.js along with a folder which contains the babel.js.

My code is the same above (plus the loadscript for the babel.js). The standAlone.js loads fine, but the babel throws Error: undefined

1 Answer 1

0

By default, Prettier expects to find the parser in a particular file location. It's not enough to just upload the file and hope for the best.

It appears that you should create a ZIP (Compressed Folder) that has the following structure:

Failing that, you may want to use something like webpack, rollup, or browserify to compile Prettier into a bundle you can then upload as a Static Resource, and load it that way.

4
  • Im downloading the babel parser .js files from the cdn pages, and its latest version is for 2.8.8 Is there a problem if I use that version with the 3.3.1 standalone.js prettier? Also does it have to be named exactly babel.js? can't it be parser-babel.min.js Commented Jun 7 at 19:38
  • Im sorry, also. How should I structure the zip a top folder should contain both (standalone.min.js and plugins folder) before compressing? And if that is the case, How should I import them in the lwc? Commented Jun 7 at 19:46
  • @RProgrammer The plugins folder is already minified (see the link in my answer), but the documentation suggests these files should be used as-is. Create a new folder, move the standalone.min.js into it, create a plugins folder, move babel.js into that folder, go back to the parent folder, select both the file and folder, and compress it to a ZIP.
    – sfdcfox
    Commented Jun 7 at 20:38
  • I have edited the code and the zip. Please see my question updates. However when I use the loadscript for the babel.js file, it throws error and the error is undefined (just that). If I remove the babel.js loadscript, no error is thrown untill I call the formatCode method, where I get the same than before: [Couldn't resolve parser "babel". Plugins must be explicitly added to the standalone bundle.]. I dont know if salesforce just blocks that for some reason. It just does not want to load or use the babel.js Commented Jun 7 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .