Socket
Book a DemoInstallSign in
Socket

inline-import

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inline-import

A tool for inlining file imports.

Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
136
257.89%
Maintainers
1
Weekly downloads
 
Created
Source

Inline Import

Build status npm version Dependencies

A tool that inlines custom file imports.

Use Case

Instead of loading external files during runtime, you may wish to integrate the raw file contents directly into your JavaScript files during build time. This can be achieved using the native import syntax:

import data from "./data.png";

The type of the external file is irrelevant. You only need to specify a preferred encoding for each file type.

Installation

npm install inline-import

Usage

Command Line Interface (CLI)

The command line tool requires a configuration in which the source path and the options are specified. You can decide whether you want to provide the configuration via package.json or as a standalone file.

If there is no configuration in package.json, the tool will look for a configuration file with the default name .inline-import.json in the current working directory.

Affected files will automatically be copied into a backup directory before they are modified. You can restore the original files by using the --restore option.

OptionShorthandDescription
--config-cSpecifies an alternative config path
--backup-bOnly copies files into a backup directory
--restore-rRestores files from the backup directory

JavaScript API

The immediate inlining process is destructive. Affected files will be changed permanently.
To inline your file imports, you need to specify the path to the JavaScript file that should be modified. Additionally, you need to define the extensions of the relevant import statements.

text.txt

hello world

index.js

import component from "module";
import text from "./text.txt";

inline.js

import InlineImport from "inline-import";

InlineImport.transform("index.js", {

	extensions: {
		".txt": "utf8"
	}

}).then(successMessage => {

	console.log(successMessage);

}).catch(e => {

	console.error(e);

});

index.js (inlined)

import component from "module";
const text = "hello world";

Options

  • When using the command line tool, src must be specified.
  • When using the command line tool, an alternative backup path may be specified.
  • You may define a specific encoding for the JavaScript files that will be processed. Use one of the possible encoding values specified in node's Buffer class. The default encoding is utf8.
  • Only imports with matching file extensions will be considered. Each extension must define its own encoding.
  • If, for some reason, you don't want to use the const statement, set useVar to true.

.inline-import.json

{
	"src": "src/**/*.js",
	"backup": "path/to/backup",
	"encoding": "utf8",
	"useVar": true,
	"extensions": {
		".html": "utf8",
		".png": "base64"
	}
}

package.json

{
	"inlineImport": {
		"src": "src/**/*.js",
		"extensions": {}
	}
}

inline.js

InlineImport.transform(filePath, {
	encoding: "utf8",
	useVar: true,
	extensions: {}
}).catch(e => console.error(e));

Build Tool Integration

Contributing

Maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Keywords

file

FAQs

Package last updated on 27 Jun 2018

Did you know?

Socket

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