Skip to content

Import As String Plugin

A staticbolt plugin that lets you inline the contents of any file as a string at build time by calling import_as_string(). The file content goes straight into your script, and the original file is left out of the output.

Installation

The plugin ships as part of @staticbolt/core. You do not need to install anything else.

Usage

In your .staticbolt.ts configuration file:

import { defineConfig } from "@staticbolt/core";
import { importAsStringPlugin } from "@staticbolt/core/plugins";
export default defineConfig({
plugins: [importAsStringPlugin()],
});

Inlining a file as a string

Call import_as_string() anywhere in your JavaScript or TypeScript with the path to the file you want to inline. The call is replaced with the file's content as a string at build time.

const styles = import_as_string("./component.css");
const template = import_as_string("./template.html");

To get type checking for the import_as_string function, import the type from @staticbolt/core/plugins.

import type { import_as_string } from "@staticbolt/core/plugins";
declare const import_as_string: import_as_string;
const styles = import_as_string("./component.css");

Processing options

By default the plugin runs the file through the staticbolt pipeline before inlining it. That applies any transform registered for that file type. Pass the options argument to change this and the other behaviors per call.

// Process the file through the pipeline and minify the output
const styles = import_as_string("./component.css", { minify: true });
// Read the file as-is, skipping the pipeline entirely
const raw = import_as_string("./data.txt", { process: false });
// Wrap the inlined content in a template literal
const html = import_as_string("./template.html", { templateLiteral: true });

Plugin Options

process

  • Type: boolean
  • Default: true

When true, the file goes through the staticbolt pipeline before it is inlined. When false, the raw file content is read and inlined as is.

minify

  • Type: boolean
  • Default: false

Minifies the file content before inlining. Only applies when process is true. Has no effect in development.

format

  • Type: boolean
  • Default: false

Formats the file content before inlining. Only applies when process is true. Has no effect in development.

templateLiteral

  • Type: boolean
  • Default: false

Wraps the inlined string in a template literal instead of a regular string.