vite#transformWithEsbuild TypeScript Examples

The following examples show how to use vite#transformWithEsbuild. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From reskript with MIT License 6 votes vote down vote up
export default function cssBindPlugin({classNamesModule = 'classnames/bind'}: Options = {}): Plugin {
    return {
        name: 'reskript:css-bind',
        enforce: 'post',
        resolveId(id) {
            if (id === ASSIGN_MODULE_ID || id === BIND_MODULE_ID) {
                return id;
            }
        },
        transform(code, id) {
            if (isStyle(id)) {
                return transformCssModulesExport(code);
            }
        },
        async load(id) {
            if (id === ASSIGN_MODULE_ID) {
                return ASSIGN_MODULE;
            }

            // 在`classnames`完成ES化以前,不得不在这里转换一下
            if (id === BIND_MODULE_ID) {
                const resolved = await this.resolve(classNamesModule);
                if (resolved) {
                    const code = await fs.readFile(resolved.id, 'utf-8');
                    const transformed = await transformWithEsbuild(code, resolved.id, {format: 'esm'});
                    return transformed.code;
                }
            }
        },
    };
}