logo@krutoo/utils

pluginRawImport

Rspack plugin that that allows importing file source as a string.

You can import source text by ?raw query:

import sourceText from './my-module.js?raw';

Or by import attribute:

import sourceText from './my-module.js' with { type: 'text' };

Basic usage

rspack.config.js
import { pluginRawImport } from '@krutoo/utils/rspack';

export default {
  plugins: [
    // just use plugin function
    pluginRawImport(),
  ],
};

By query

If you want to change query behavior you can provide byQuery option:

rspack.config.js
import { pluginRawImport } from '@krutoo/utils/rspack';

export default {
  plugins: [
    pluginRawImport({
      byQuery: {
        ruleOverride: {
          // custom query
          resourceQuery: /source/,
        },
      },
    }),
  ],
};

If you want to disable importing source by query you can pass byQuery: false.

By import attribute

If you want to change import attribute behavior you can provide byImportAttr option:

rspack.config.js
import { pluginRawImport } from '@krutoo/utils/rspack';

export default {
  plugins: [
    pluginRawImport({
      byImportAttr: {
        ruleOverride: {
          // custom attribute
          with: {
            type: 'source',
          },
        },
      },
    }),
  ],
};

If you want to disable importing source by query you can pass byImportAttr: false.

Rule insertion

Under the hood this plugin adds item to module.rules in your configuration.

You can configure how to add rule by set ruleInsert option:

rspack.config.js
import { pluginRawImport } from '@krutoo/utils/rspack';

export default {
  plugins: [
    pluginRawImport({
      // 'to-start' | 'to-end', default: 'to-end'
      ruleInsert: 'to-end',
    }),
  ],
};