Skip to content

Latest commit

History

History
96 lines (67 loc) 路 2.15 KB

README.md

File metadata and controls

96 lines (67 loc) 路 2.15 KB

馃獎 Soori

Build your own compile-time library.

Getting Started

Install Soori as a dependency.

npm install soori

pnpm install soori

yarn add soori

Modify your build script:

"scripts": {
  "build": "soori build && <DO-YOUR-THING>"
}

vite

If you're using vite, you don鈥檛 have to run soori build manually.

// vite.config.js
import { defineConfig } from 'vite';
import { soori } from 'soori/vite';

export default defineConfig({
  plugins: [soori()],
});

How it works

This is an example config:

// soori.config.js
import { defineSooriPlugin } from 'soori';
import fs from 'node:fs/promises';

export default {
  plugins: [
    defineSooriPlugin({
      name: 'my-json-loader',
      watch: ['src/data/*.json'],
      output: 'my-json',
      build: async ({ filePath, filenameWithoutExt }) => {
        const fileContent = (await fs.readFile(filePath)).toString();
        return {
          id: filenameWithoutExt,
          content: `export const ${filenameWithoutExt} = ${fileContent};`,
        };
      },
    }),
  ],
};

Imagine you have the following files under src/data/:

  • abc.json
  • def.json

Then you will be able to import them in your application:

import { abc, def } from 'soori/my-json';

console.log('# my jsons', { abc, def });

How does it work?

That build function runs on each of those files. In case of abc.json, the parameters to the build function will be:

  • filePath: src/data/abc.json
  • filenameWithoutExt: abc

Soori uses the return of the build function to generate an output file with the filename being ${id}.ts, and the content being content. It means this sample plugin generates the following files:

  • node_modules/soori/submodules/my-json/abc.ts
  • node_modules/soori/submodules/my-json/def.ts
  • node_modules/soori/submodules/my-json/index.ts

And that index.ts is an entry file which is automatically generated by Soori.

API References

The API design is in progress, so there is no finalized documentation. However, you can check out the type declarations.