Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140762

ES6: How to reuse a const variable in multiple files of the library BUT WITHOUT exposing it to client code?

$
0
0

I am writing a library which is made up of several files:

./lib:
 - core.js
 - file1.js
 - file2.js
 - file3.js

lib/core.js contains common variables which I need to reuse in file1.js, file2.js and file3.js.

E.g., core.js:


/**
 * I would like to reuse this constant in file1.js, file2.js and file3.js,
 * but I don't want client code to be able to access the value of this constant
 * when importing components of my library, like "aFunction" below.
 */
export const CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY = 'Some value';

/**
 * I would like to expose this function to client code
 * as well as to the other files of the library (file1.js, file2.js and file3.js).
 */
export const aFunction = () => {
}

...

Then file1.js (file2.js and file3.js are similar):

import { CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY } from './core'

/**
 * This function should be available to the client code (as "aFunction" of core.js),
 * but CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY should not.
 */
export const file1Function = () => {
    // Need to use `CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY` here
    console.log(CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY)
}

How can I achieve this using ES6 modules? Right now, with this setup, CONSTANT_I_WANT_TO_REUSE_IN_OTHER_FILES_OF_THE_LIBRARY is also available to client code.

But I would like to keep it "module-private", i.e. only available to my library code.

Thank you for the attention.


Viewing all articles
Browse latest Browse all 140762

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>