Build your own compile-time library.
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>"
}
If you're using vite, you don’t have to run soori build
manually.
// vite.config.js
import { defineConfig } from 'vite';
import { soori } from 'soori/vite';
export default defineConfig({
plugins: [soori()],
});
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 });
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.
The API design is in progress, so there is no finalized documentation. However, you can check out the type declarations.