I created a Things folder that has many Thing files and then create an index inside the Thing folder that acts as an "intermediate module".
Like this...
// things/thing1.js
console.log('thing1 loaded');
const thing1 = () => {
console.log('Hi from thing1!');
};
export default thing1;
// things/thing2.js
console.log('thing2 loaded');
const thing2 = () => {
console.log('Hi from thing2!');
};
export default thing2;
// things/index.js
export { default as thing1 } from './thing1';
export { default as thing2 } from './thing2';
When I import any of the files from the intermediate module...
// anotherFile.js
import { thing1 } from '../things';
thing1();
...all the files are loaded.
# console output
thing1 loaded
thing2 loaded # <-- don't want this to load.
Hi from thing1!
Other than...
import thing1 from './things/thing1';
import thing2 from './things/thing2';
...is it possible to structure the intermediate module or the import so only the file being imported is loaded?
things/index.jshas to load all of its modules in order to export them. The only way would probably be to use some bundler to minimize your JavaScript.