Socket
Socket
Sign inDemoInstall

@googlicius/load-assets

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @googlicius/load-assets

Load assets helper


Version published
Weekly downloads
7
increased by600%
Maintainers
1
Install size
14.1 kB
Created
Weekly downloads
 

Readme

Source

Load Assets

Utilities to load assets programatically: javascript, css, images,...

Test

Motivation

I am a fan of using javascript libraries from their CDN rather than include them in the application bundle. If you are building application with React.js, Angular, or Vue.js, there are many wrapper of libraries you need. But it causes a problem called bi-incompatible (that's what I call it) when you upgrade your framework to latest version. Some javascript libraries doesn't support npm installation, or you just want to load it as lazy way (when user reach a specific page).

Installation

npm install @googlicius/load-assets

Or Yarn

yarn add @googlicius/load-assets

Usage

import { loadScript } from '@googlicius/load-assets';

loadScript(
  'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js',
).then(() => {
  // Lodash available here.
});

Loads and runs 2 javascript files in parallel.

loadScript(
  'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js',
  'https://connect.facebook.net/en_US/sdk.js',
).then(() => {
  // Lodash and Facebook SDK available here.
});

Load 2 javascript files in parallel but runs in order, which means the second script depends on the first one.

loadScript([
  'https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/prism.min.js',
  'https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/plugins/autoloader/prism-autoloader.min.js',
]).then(() => {
  // Prism initialized with its autoloader.
});

Often a library is not only have javascript but also with its own css file. So to load css and js we simple use loadAsset:

import loadAsset from '@googlicius/load-assets';

loadAsset(
  'https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/themes/prism-twilight.min.css',
  'https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/prism.min.js',
).then(() => {
  // Prism initialized with its styles.
});

Load with custom attributes:

loadScript('https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/prism.min.js', {
  'data-manual': 'true',
}).then(() => {
  // ...
});

API

  • loadScript: Load one or more javascript file(s) from cdn or external sources.
  • loadCss: Load one or more css file(s) from cdn or external sources.
  • loadImage: Load one or more image(s) from cdn or external sources.
  • loadAsset: Load any file type, based on file extension. This method is a combination of all api above.

License

MIT


Two-way incompatible

When you upgradge your framework to higher version, but that version possibly incompatible with some library wrappers in your project, or you want to use the latest version of a library but it possibly haven't supported. For example, Reactstrap currently support Bootstrap 4, so you cannot use Bootstrap 5 at this time.

Keywords

FAQs

Last updated on 05 Aug 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc