push
This commit is contained in:
39
build/plugin/theme/esbuild/less.ts
Normal file
39
build/plugin/theme/esbuild/less.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import path from 'path';
|
||||
import { Plugin } from 'esbuild';
|
||||
import less from 'less';
|
||||
|
||||
/** Less-loader for esbuild */
|
||||
export const lessLoader = (content, options: Less.Options = {}): Plugin => {
|
||||
return {
|
||||
name: 'less-loader',
|
||||
setup: (build) => {
|
||||
build.onResolve({ filter: /\.less$/, namespace: 'file' }, (args) => {
|
||||
const filePath = path.resolve(
|
||||
process.cwd(),
|
||||
path.relative(process.cwd(), args.resolveDir),
|
||||
args.path
|
||||
);
|
||||
return {
|
||||
path: filePath,
|
||||
};
|
||||
});
|
||||
|
||||
// Build .less files
|
||||
build.onLoad({ filter: /\.less$/, namespace: 'file' }, async (args) => {
|
||||
const dir = path.dirname(args.path);
|
||||
try {
|
||||
const result = await less.render(content, {
|
||||
...options,
|
||||
paths: [...(options.paths || []), dir],
|
||||
});
|
||||
|
||||
return {
|
||||
contents: result.css,
|
||||
loader: 'css',
|
||||
resolveDir: dir,
|
||||
};
|
||||
} catch (e) {}
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user