Socket
Socket
Sign inDemoInstall

append-files

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    append-files

Append CSS files in head and JS files in body tag.


Version published
Weekly downloads
33
increased by13.79%
Maintainers
1
Install size
8.45 kB
Created
Weekly downloads
 

Readme

Source

append-files

Append CSS files and inline CSS in head tag whereas JS files and embed JS in body tag.

Install

Via npm

npm install append-files

Via Yarn

yarn add append-files

Usage

If like to add the styles and script files only without defining any other attributes then you can do this:

Add Script Files:

import { appendScripts } from 'append-files';

var addScripts = [
  '/js/script.js',
  'http://example.com/test.js'
];

appendScripts(addScripts);

NOTE: In this way, you can only add the script files, not applicable for inline script. To add inline scripts or add attibutes on script tags, like id, async, nomodule etc pass them as an object as shown in the example below.

Add Script Files with its dependency:

If you need to append 2 or more script files and the second script is dependent on the first script then you can define add the dependent script like this:

import { appendScripts } from 'append-files';

var addScripts = [{
  'id': 'test-forms',
  'url': 'http://example.com/test.js',
  'dependentScripts': [{
    'url': '/js/script.js',
    'async': true,
  }]
}];

appendScripts(addScripts);

NOTE: In this case, id attribute MUST be defined otherwise, dependent scripts not appended as it is suppose to be.

Add Inline Script:

Pass the script to be added as inline:

import { appendScripts } from 'append-files';

var addScripts = [{
  'inline': 'alert(1);'
}];

appendScripts(addScripts);

Add Stylesheets:

import { appendStylesheets } from 'append-files';

var addStyles = [
  'http://example.com/test.css',
  '/css/style.css'
];

appendStylesheets(addStyles);

NOTE: In this way, you can only add the stylesheet files, not applicable for inline styles. To add inline styles, pass them as an object as shown in the example below.

Add Stylesheets and Inline Style:

This is the example by which you can add inline CSS and/or stylesheet files.

import { appendStylesheets } from 'append-files';

var addStyles = [
  {
    'href': 'http://example.com/test.css'
  },
  {
    'href': '/css/style.css'
  },
  {
    'inline': 'h1 { color: red; }'
  }
];

appendStylesheets(addStyles);

Supported Script Attributes

AttributesTypeRequiredDescription
urlURLYesURI of an script.

Either url or inline is required.
inlineStringYesscript which is not loaded from the file, but embedded inside script tag. For example: .

Either url or inline is required.
asyncBooleanNoIndicating that the browser should load the script asynchronously and then execute it as soon as it's downloaded.
referrerPolicyStringNoIndicates which referrer to send when fetching the script, or resources fetched by the script.
idStringNounique id for an Script Tag. Required when using dependentScripts attribute.
nomoduleBooleanNoIndicate that the script should not be executed in browsers that support ES2015 modules.
dependentScriptsObjectNoIf you have 2 script Files and the first file has the dependency on the second file then you can use this attribute to define the dependent script.

In this case, id attribute is required with the dependency. For further information, refer to the Add Script Files with its dependency: example.

Supported Style Attributes

AttributesTypeRequiredDescription
urlURLYesURI of an stylesheet.

Either url or inline is required.
inlineStringYesstyle which is not loaded from the file, but embedded inside style tag. For example: h1 { color: red; }.

Either url or inline is required.

Tested

This package is tested with the React Application.

Keywords

FAQs

Last updated on 26 Sep 2019

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