0

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>
  1. lib:
    Access by window.DummyGlobalObject.init();
  2. 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, ??????????????)
}
eckopage
  • 1
  • 1
  • Please try to clarify the question. It isn't quite clear what is your problem. You have `typescript` in tags. Does your problem relate to types or ts compiler? It isn't clear what was meant by `Access by` section Error messages and/or expected results would also help – Guria Apr 26 '23 at 21:11
  • Main problem is that i want to run app with 2 scripts that have the same global name `window.DummyGlobalObject`. Should i merge both objects? How to handle it via webpack? Maybe it possible to handle it by `declare glabal` modification. I found similar problem https://stackoverflow.com/a/30740935/5358061 – eckopage Apr 27 '23 at 06:21

0 Answers0