babel-plugin-transform-import-to-read-file-sync

Use import to read any file contents to local variable in Node.js. Filename is resolved like regular import source.

Usage no npm install needed!

<script type="module">
  import babelPluginTransformImportToReadFileSync from 'https://cdn.skypack.dev/babel-plugin-transform-import-to-read-file-sync';
</script>

README

babel-plugin-transform-import-to-read-file-sync

Use import to read any file contents to local variable in Node.js. Filename is resolved like regular import source.

Example

import file from "./file.txt";
// => const file = _readFileSync(require.resolve('./file.txt'), "utf8");
// readFileSync is imported from 'fs' as some unique identifier, if not already available.

console.log(require("./file.txt"));
// => console.log(require('fs').readFileSync(require.resolve('./file.txt'), "utf8"));
.babelrc
{
  "plugins": [
    [
      "transform-import-to-read-file-sync",
      {
        "test": "\\.txtquot;,
        "options": "utf8"
      }
    ]
  ]
}

Options

Plugin expects options object. test property is pattern to match files. options property is passed directly to readFileSync as second argument. If no options is specified, then readFileSync returns a buffer.

You can use "options" array property to specify multiple test patterns and readFileSync options:

{
  "plugins": [
    [
      "transform-import-to-read-file-sync",
      {
        "options": [
          {
            "test": "\\.txtquot;,
            "options": "utf8"
          },
          {
            "test": "\\.pngquot;
          }
        ]
      }
    ]
  ]
}