I've got a class defined in an npm package:
export default class SomeClass{
constructor({ testA, testB }) {
this.testA = testA;
this.testB = testB;
}
log() {
console.log(this.testA + this.testB);
}
}
That then gets aggregated with another file in the source file:
import SomeClass from './some-class';
import anotherFunction from './another-function';
export { SomeClass, anotherFunction };
And then built with the esbuild config as below:
const esbuild = require('esbuild');
async function bundle() {
await esbuild.build({
logLevel: 'info',
entryPoints: ['src/js/package.js'],
bundle: true,
minify: true,
sourcemap: true,
outfile: 'dist/js/package.min.js'
})
.catch(() => process.exit(1));
}
bundle();
But when I try import that into my app's app.js as such:
import { SomeClass } from '../package.min.js';
const newClass = new SomeClass(constructorObject);
I get the aforementioned void 0 message.
If I open the minified js file I do see a message about the class not being referenced anywhere else, is there something I can do to make it export the file?
Thanks @Bergi for pointing me in the right direction. The solution was adding
format: 'esm'
to my esbuild script options. Though for projects importing libraries in the CJS format the value cjs
could presumably be placed there instead.
The full build function now looks like:
async function bundle() {
await esbuild.build({
logLevel: 'info',
entryPoints: ['src/js/employee-portal-assets.js'],
bundle: true,
minify: true,
sourcemap: true,
treeShaking: true,
format: 'esm',
outfile: 'dist/js/employee-portal-assets.min.js'
})
.catch(() => process.exit(1));
}