Closed alimsadeghi1 closed 1 year ago
Microbundle does offer a programmatic API which should fit your needs, but that's in place of the CLI.
// build.js
const microbundle = require('microbundle');
(async function () {
await microbundle({ ... });
})();
Then you'd build by using:
$ node build.js
Is there any best practice or example for that?
Using it programmatically? Not that I know of, it's pretty uncommon use I think.
If you provide the format of your config file I can write up a more specific example, but likely will look something like:
// build.js
const microbundle = require('microbundle');
const fs = require('fs/promises');
(async function () {
const alias = await fs.readFile('aliases.txt', 'utf-8');
await microbundle({
cwd: '.',
input: './src/index.js',
output: './dist/index.js',
format: 'cjs,esm',
alias,
});
})();
How many aliases are we talking about here? If it's just a few, then adding a layer of indirection with a custom build script really doesn't make much sense.
Thank you for responding so quickly. I actually want to have something like this:
@components: ./src/components
I want ./src
be dynamic. for example somewhere it should be: ./src
and somewhere else it should be ../../../src
// aliases.txt
@components: ./src/components
@routes: ./src/routes
// build.js
const microbundle = require('microbundle');
const fs = require('fs/promises');
(async function () {
let alias = await fs.readFile('aliases.txt', 'utf-8');
alias = alias.replace(/:\s/g, '=').replace(/\n/g, ',');
await microbundle({
cwd: '.',
input: './src/index.js',
output: './dist/index.js',
format: 'cjs,esm',
alias,
});
})();
This should do the trick.
It simply reads your alias file, replaces the semi-colon and proceeding space with an equals sign, then converts all new lines into commas so you have a list of comma-separated entries.
Edit: Of course if you're fine with writing your aliases in this JS file, you could skip the whole readFile thing and just do alias: '@components=./src/components,@routes=./src/routes'
, etc.
Thank you very much. it works.
but I think it would be great if microbundle could read aliases from tsconfig
using --tsconfig
in CLI.
Type aliases aren't always module aliases. Doing so would cause more issues that's it's worth, IMO.
I have alias in my project and I rather define them in a file and not cli. Is there any way for doing this?