1

Imagine I have a JavaScript file common.js

const myMethod = (arg) => {
     // do stuff
}

export {
    myMethod 
};

And I include it into a LWC:

import { LightningElement } from 'lwc';
import { myMethod } from 'c/common';

export default class MyLWC extends LightningElement {

    handleEvent() {
        myMethod('an example argument');
    }
}

I'd like to know how could I can also use common.js in a Visualforce page?

<apex:page applyHtmlTag="false" applyBodyTag="false" showHeader="false" showChat="false" standardStylesheets="false" sideBar="false"
docType="html-5.0">
    <html>
        <head>
             // What can I reference here?
             <script src="... what is the path to common.js ...."></script>
        </head>
        <body>
            // content
        </body>
    </html>
</apex:page>

Questions

  1. What is the path to common.js from a Visualforce page?
  2. Is it even possible to access it?
4

Is it even possible to access it?

No, it's not possible. The Content Security Policy shouldn't allow it, as far as I can tell, and the path to the files are not documented and could change between releases. In addition, the code is transpiled by the system when they load, so a LWC script may not be directly compatible with non-LWC code anyways.

If you want to have a common script between Visualforce, LWC, and/or Aura, upload it as a Static Resource and use the appropriate method to import the script (apex:includeScript, ltng:require, loadScript).

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.