Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't figure out a basic scenario working with my own CDN and packages #55

Open
shaipetel opened this issue Jan 3, 2019 · 4 comments
Open
Labels

Comments

@shaipetel
Copy link

Basic scenario working with webpack and this package.

example:

main.ts

export class Main {
    public Init(): string {
        console.log("Init was called");
        return "Init done";
    }

    public GetDelayedMessage(): Promise<string> {
        return Promise.resolve("Thanks");
    }
}

I'm hosting "es6-promise" under my own CDN: https://cdn.company.com/libs/es6-promise.js

I can't figure out how to make webpack not bundle es6-promise but instead load it from my CDN.
Better yet - I would ultimately would like to lazy-load it only when someone called GetDelayedMessage, if the code changes to this:

    public Init(): string {
        console.log("Init was called");
        return "Init done";
    }

    public GetDelayedMessage(): Promise<string> {
        return import("es6-promise").then(() => { return "Thanks!"; });
    }
}

Could someone perhaps provide a basic webpack.config.js that covers this scenario?
I read through the documentation but can't find all the info regarding:

  1. Usage with ManifestPlugin - what is the content of "manifest.json"?
  2. options.resolver - what is the signature of this function (which parameters it gets)?

I tried to set up this plugin but that didn't work:

    resolver: function (packageName, version, params) {
        switch (packageName) {
            case "es6-promise":
                return { name: packageName, var: "Promise", url: "https://apps.kwizcom.com/libs/es6-promise/es6-promise.min.js", version: version };
        }
        return null;
    }
})

Thanks!!!

@mgenev
Copy link

mgenev commented Apr 3, 2019

Same here, how do you set the urls to your own CDN? This module is not very useful if it only supports 1 CDN

@aulisius
Copy link
Collaborator

aulisius commented Apr 5, 2019

Hi,

Sorry for the delay. It is indeed possible to use your own CDNs with this plugin.

@shaipetel What error are you exactly getting with that config? The way you have provided a custom resolver looks correct to me. Can you pass verbose:true in the plugin options?

@mgenev You can provide a custom resolver such as follows.

const HtmlWebpackPlugin = require("html-webpack-plugin");
const DynamicCdnWebpackPlugin = require("dynamic-cdn-webpack-plugin");

module.exports = {
  mode: "production",
  plugins: [
    new HtmlWebpackPlugin(),
    new DynamicCdnWebpackPlugin({
      resolver: (packageName, packageVersion, options) => {
        let env = options.env || "development";
        if (packageName === "react") {
          return {
            name: packageName,
            var: "React",
            version: packageVersion,
            url: `https://cdn.jsdelivr.net/npm/${packageName}@${packageVersion}/umd/react.${env}.min.js`
          };
        } else {
          return null;
        }
      }
    })
  ]
};

Here I'm loading react package alone through the jsDelivr CDN instead of using the default unpkg. You can pass a similar function to be used with another CDN of your choice.

@aulisius
Copy link
Collaborator

aulisius commented Apr 5, 2019

@shaipetel , on closer inspection, it looks like the module loaded from https://apps.kwizcom.com/libs/es6-promise/es6-promise.min.js attaches the promise to the window under ES6Promise.

Can you change your config to something like this

resolver: function (packageName, version, params) {
  switch (packageName) {
    case "es6-promise":
      return {
        name: packageName,
        var: "ES6Promise",
        url: "https://apps.kwizcom.com/libs/es6-promise/es6-promise.min.js",
        version: version
      };
  }
  return null;
}

@shaipetel
Copy link
Author

thanks, looks like what I needed. I'll give it a try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants