npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

esbuild-raw-plugin

v0.1.1

Published

An ESBuild and TSUP plugin that allows importing files as raw text. Useful for loading code files in documentation, interactive demos, or tools like react-live.

Downloads

263

Readme

Esbuild Raw Plugin

test Maintainability codecov Version Downloads npm bundle size Gitpod ready-to-code

An ESBuild/TSUP plugin to import files as raw text.
Ideal for scenarios like importing code files for documentation, interactive tools like react-live, or other text-based use cases.

Star this repository and share it with your friends.


Features

  • Import any file (e.g., .js, .ts, .css, etc.) as raw text.
  • Works seamlessly with ESBuild and TSUP.
  • Perfect for documentation generators, live code editors, and similar tools.

Installation

Using npm:

npm install esbuild-raw-plugin --save-dev

Using yarn:

yarn add esbuild-raw-plugin --dev

Using pnpm:

pnpm add esbuild-raw-plugin --save-dev

Usage

ESBuild Configuration

Add the plugin to your ESBuild configuration:

import { build } from "esbuild";
import { raw } from "esbuild-raw-plugin";

build({
  entryPoints: ["src/index.js"],
  bundle: true,
  outfile: "out.js",
  plugins: [raw()],
});

TSUP Configuration

Add the plugin to your TSUP configuration:

import { defineConfig } from "tsup";
import { raw } from "esbuild-raw-plugin";

export default defineConfig({
  entry: ["src/index.ts"],
  outDir: "dist",
  plugins: [raw()],
});

IDE Setup for IntelliSense and Type Checking

Add following to your declaration file. If you do not have one, create declarations.d.ts file and add following.

declare module "*?raw" {
  const value: string;
  export default value;
}

Importing Files as Raw Text

With the plugin enabled, you can import files as raw text directly:

import myCode from "./example.js?raw";

console.log(myCode);
// Outputs the content of 'example.js' as a string.

Good News:

With the latest update, you no longer need to specify the file extension explicitly.

import myCode from "./example?raw";

This works seamlessly! Additionally, if you're exporting from files like index.tsx, index.jsx, etc., you can simplify imports. For example, if your file path is my-lib/index.ts, you can import the raw content like this:

import myCode from "./my-lib?raw";

Extension Options (Optional)

export interface RawPluginOptions {
  /**
   * Extensions to check in order if the file does not exist.
   * If it's a directory, the plugin will look for `dir/index.[ext]`.
   * @defaultValue ["tsx", "ts", "jsx", "js", "mjs", "mts", "module.css", "module.scss", "css", "scss"]
   *
   * You can provide your own extensions to optimize build performance or extend the list based on your use case.
   */
  ext?: string[];
}

Supported File Types

You can use ?raw with any file type, including:

  • .js, .ts, .jsx, .tsx
  • .css, .scss
  • .html
  • .md
  • and more!

Example Use Case

Live Code Preview with react-live

import React from "react";
import { LiveProvider, LiveEditor, LiveError, LivePreview } from "react-live";
import exampleCode from "./example.js?raw";

const App = () => (
  <LiveProvider code={exampleCode}>
    <LiveEditor />
    <LiveError />
    <LivePreview />
  </LiveProvider>
);

export default App;

Why Use esbuild-raw-plugin?

  • Simplifies importing files as raw text for documentation and live previews.
  • Seamlessly integrates with modern build tools like ESBuild and TSUP.
  • Lightweight and easy to configure.

Keywords

esbuild, esbuild-plugin, tsup-plugin, raw-text-import, import-as-text, file-loader, react-live, documentation-tools, frontend-tooling


Contributing

Contributions are welcome!
Feel free to open issues or pull requests to improve the plugin.


Let me know if you'd like further tweaks! 🚀

Alt


License

This library is licensed under the MPL-2.0 open-source license.

Please enroll in our courses or sponsor our work.