I've authored a javascript library using Webpack. The entrypoint someClass.js
looks like this:
import x from './x'
/* several more imports here */
class SomeClass {}
export default SomeClass;
My webpack config that bundles this library is as follows:
module.exports = {
entry: './someClass.js',
output: {
path: __dirname,
filename: 'lib.js',
library: 'lib',
libraryTarget: 'umd',
},
I then import the generated lib.js
into a simple index.html
that is defined as follows:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script src="app.js" type="module"></script>
</body>
</html>
In app.js
, I simply try to import the file as follows:
import * as lib from './lib.js';
console.log(lib);
// Output: Module {Symbol(Symbol.toStringTag): "Module"} Symbol(Symbol.toStringTag): "Module"
However, this import statement does not seem to work as planned (I expect a module with a default
field that is my SomeClass
constructor).
The only way I can access my library's default export SomeClass
is if I do a global import statement in app.js
as follows, which sets lib
as an object on window
:
import './lib.js';
console.log(window.lib);
// Output: Module {default: ƒ, __esModule: true, Symbol(Symbol.toStringTag): "Module"} default: class SomeClass_SomeClass
I don't want my class to available at the global window
, as it makes it tough to modularise my code.
I also want to be able to use this library in a variety of places on the web (react app, standalone html files, etc), and so want to minimize dependencies.
Is there anything I can do to import the module as an es6 import?