node-esm-loader

A webpack-like loader for Node
48,094 8
Install
npm install node-esm-loader
Latest Version:0.3.0
Vue:3
Older releases:2
License:MIT
Last Updated:Dec 14, 2023
Links: GitHub  ·  npm
Maintainer: sebamarynissen

node-esm-loader

A webpack-like loader library for Node

DISCLAIMER Loaders are still experimental in Node and may still change, which means this module is still experimental as well. Use at own risk and DO NOT rely on it in production.

This module is a simple wrapper around create-esm-loader to make it easier to set up a loader. In create-esm-loader you have to manually initialize, configure and export your loader, but this module reduces that so that all you need to do is configure the loader.

Installation

npm install --save-dev node-esm-loader

Usage

node-esm-loader will automatically export a loader based on how you configured it. You can use it by running Node as

node --import=node-esm-loader/register ./your/file.js

Or you can use the JavaScript api

// # register.js
import { register } from 'node:module';
register('node-esm-loader', import.meta.url, {

  // Optional, will search for a configuration file automatically if not specified
  data: {
    loaderConfig: './path/to/config.js',
  },

});

and then run Node as

node --import=./register.js ./your/file.js

For more info about this, see https://nodejs.org/api/module.html#customization-hooks. Note: since version 0.3.0, either Node >=18.19 or >=20.6 is required. Support for lower Node version has been dropped.

node-esm-loader loader will subsequently look for a configuration file in various places using cosmiconfig, where it also looks up the directory tree. My advice is to either use .loaderrc.js, or use a .config folder with .config/loaderrc.js, but loader.config.js will also work.

// .loaderrc.js, loader.config.js or .config/loaderrc.js
export default {
  loaders: [
    'vue-esm-loader',
    {
      test: /\.(png|gif|jpe?g)$/,
      type: 'asset/resource',
    },
    {
      transform(source, opts) {
        return { source: someTransformation(String(source)) };
      }
    },
  ],
};

For more info on what the loaders configuration should look like, have a look at create-esm-loader.

If you want to put the loader config in a different file, you can use

node --import=node-esm-loader/register ./your-file.js --loader-config=./path/to/config.js

When using this with mocha, create a .mocharc.cjs file that looks like this:

module.exports = {
  import: 'node-esm-loader/register',
};

Compatible Loaders

You can find a list of compatible loaders, here:

Related Packages

vue-esm-loader

Vue single-file component loader for Node

5,522 2
vue-button-spinner

A simple button spinner.

16,948 35
http-vue-loader

Load .vue files directly from your html/js. No node.js environment, no build ste...

52,627 1,477
mpvue-loader

mpvue single-file component loader for Webpack

20,800 46
weex-vue-loader

Vue.js component loader for Webpack

13,557 4,958

Version History

Version Released Vue License
0.3.0 ^3.3.11 MIT
0.2.5 ^2.6.14 MIT
0.2.4 ^2.6.14 MIT
0.2.3 ^2.6.14 MIT
0.2.2 ^2.6.14 MIT
Pre-releases (2)
Version Released Vue License
0.0.1-alpha.0 ^2.6.14 MIT
0.0.1-alpha MIT