Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@braintree/asset-loader

braintree1.8mMIT2.0.3TypeScript support: included

A module to load frontend assets.

readme

asset-loader

Installation

npm install --save @braintree/asset-loader

Usage

var loadScript = require("@braintree/asset-loader/load-script");

loadScript({
  src: "https://example.com/some-js-file.js",
  container: domNodeToAddScriptTagTo, // optional, defaults to document.head
  id: "id-to-give-script-tag", // optional
  crossorigin: "anonymous", // optional
  dataAttributes: {
    // optional
    foo: "value", // adds data-foo="value" to script tag
  },
  // optional, by default the module will resolve with an existing script
  // node if a script tag with the same src exists on the page, by setting
  // this value to `true`, it will add the script to the page even if an
  // identical script tag already exists on the page.
  forceScriptReload: true,
})
  .then(function (script) {
    script; // a reference to the dom node
  })
  .catch(function (err) {
    // occurs when script fails to load or is aborted
  });
var loadStylesheet = require("@braintree/asset-loader/load-stylesheet");

loadStylesheet({
  href: "https://example.com/some-css-file.css",
  container: domNodeToAddScriptTagTo, // optional, defaults to document.head and always puts it at the top of the container
  id: "id-to-give-to-stylesheet-element", // optional
}).then(function (stylesheet) {
  stylesheet; // a reference to the dom node
});

Both methods will resolve immediately if the script or link element is already on the page.

Development

Run tests:

npm test

changelog

CHANGELOG

2.0.3

  • Add ability to set script integrity attribute

2.0.2

  • Updates dev dependencies

2.0.1

  • Updates braces to 3.0.3
  • Updates was to 8.17.1

2.0.0

  • Remove promise polyfill

1.0.1

  • Dependabot Updates

1.0.0-beta.1

  • BREAKING CHANGES

    • Update Node to v18
  • DevDependency Updates

    • Prettier to v3
    • eslint-plugin-prettier to v5
    • Typescript to v5

0.4.4

  • Fix issue where server side rendering would not work with sdk

0.4.3

  • Remove use of export default in promise lib

0.4.2

  • Remove use of export default in module

0.4.1

  • Fix paths when publishing

0.4.0

  • Add typescript types

0.3.1

  • Fix issue where crossorigin would not be set

0.3.0

  • Add ability to set crossorigin attribute

0.2.1

  • Set loadScript to cache the promise used to load the script to eliiminate a race condition where the script could be on the page, but not ready to use

0.2.0

  • Add ability to force reload a script tag if it already exists on the page

0.1.0

  • Initial version