/** * alias plugin * @description * config example: * ``` * { * '@lib': '/some/absolute/path' * } * ``` * then `import { something } from '@tamagui/core'` will be transformed to * `import { something } from '/some/absolute/path/xxx'` * @param {object} config */ export const esbuildAliasPlugin = (config) => { const alias = config && Object.keys(config) return { name: 'path-alias', setup(build) { if (!alias || !alias.length) { return } const main = (k, args) => { const targetPath = config[k].replace(/\/$/, '') return { path: targetPath, } } alias.forEach((k) => { build.onResolve({ filter: new RegExp(`^.*${k}$`) }, (args) => { return main(k, args) }) build.onResolve({ filter: new RegExp(`^.*\\/${k}\\/.*$`) }, (args) => { return main(k, args) }) }) }, } }