I have attached 2 libs with the same global obj names.
<script src="http://url-to-third-party-lib.com/sdk1"></script>
<script src="http://url-to-my-lib.com/sdk2"></script>
- lib:
Access bywindow.DummyGlobalObject.init();
- lib:
Access by:
var PortalInstance = window.DummyGlobalObject.Instance;
var someInputOptions = { ... }
var dummyobj = new PortalInstance(someInputOptions);
I've included two libs that overwrite each other.
We can see two global objects with the same window.DummyGlobalObject
Example:
Actually i have an access to both libs but first is deployed via CDN.
<script src="http://url-to-third-party-lib.com/sdk1"></script>
First lib body:
For building this i am using webpack and i want to find out some solution about merging both if both exist.
Maybe there is webpack plugin/hack that is able to handle this problem (merging both libs).
Webpack.config.js
module.exports = function (x, env) {
return {
target: 'web',
mode: 'development',
entry: {
Instance: './src/DummyGlobalScript.ts',
},
node: {
global: true,
},
output: {
path: path.resolve('./dist'),
filename: '[name].js',
sourceMapFilename: '[name].js.map',
library: 'DummyGlobalObject', // <------- GLOBAL
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
devtool: false,
module: {
rules: [
{
test: /\.ts$/,
loader: 'ts-loader',
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'less-loader',
options: {
sourceMap: true,
},
},
],
},
],
},
optimization: {
minimizer: [
new TerserJSPlugin({}),
new OptimizeCSSAssetsPlugin({})
],
},
};
};
Second lib body definition:
export interface SomeGlobalOptions {
name: string;
url: string;
}
declare global {
interface Window {
DummyGlobalObject: SomeGlobalOptions;
}
const DummyGlobalObject: SomeGlobalOptions;
}
The question how to use both libs if both scripts attached on website? (by merging both ???)
/// some pseudocode
if(window.DummyGlobalObject !== undefined){
/// ????????? acutally don't have idea...
Object.assign(window.DummyGlobalObject, ??????????????)
}