I
I
its2easyy2020-05-19 12:31:03
JavaScript
its2easyy, 2020-05-19 12:31:03

How to properly import a library in umd format?

There is a library that is built by webpack in umd format:

output: {
        path: path.resolve(__dirname, '../build'),
        filename: 'my-lib.min.js',
        library: 'myLib',
        globalObject: 'this',
        libraryTarget: 'umd',
        umdNamedDefine: true
    },

It needs to work in the browser when connected by a script, and so that it can be imported in applications that use the assembly.
In the browser, it works fine . And when I try to import in a project that is built via webpack, the build works without errors, but the error occurs in the browser
<script src="my-lib.min.js"></script>
myLib.init();
Uncaught TypeError: Cannot set property 'myLib' of undefined

Tried to import in different ways, no difference
const myLib = require('./my-lib.min');
import './my-lib.min';
import { myLib }  from './my-lib.min'
import myLib  from './my-lib.min'

Is it possible to somehow connect this or am I misunderstanding the meaning of the umd format?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
its2easyy, 2020-05-19
@its2easyy

The problem was babel-leader trying to process the library. It is necessary that it be in the exclude webpack, usually it is if the file is loaded from node_modules, and if not, then you must manually add the file to exclude

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question