30 lines
1.1 KiB
JavaScript
30 lines
1.1 KiB
JavaScript
import Debug from 'debug';
|
|
import {EOL} from 'node:os';
|
|
|
|
const pivot = 'export default function(app) {';
|
|
|
|
export default function(layouts = [], {debug = Debug('@lando/vite-plugin')}) { // eslint-disable-line
|
|
return {
|
|
name: 'inject-layouts',
|
|
enforce: 'pre',
|
|
transform: (code, id) => {
|
|
const layoutfile = 'enhance-app-with-layouts.js';
|
|
if (id.includes(layoutfile) && layouts.length > 0) {
|
|
// get lines and pindex
|
|
const lines = code.split(EOL);
|
|
// get pivot line
|
|
let pindex = lines.findIndex(line => line.startsWith(pivot));
|
|
// loop through and add imports
|
|
for (const layout of layouts.reverse()) lines.splice(pindex, 0, layout.import);
|
|
// get pivot again
|
|
pindex = lines.findIndex(line => line.startsWith(pivot)) + 1;
|
|
// loop through again and add components
|
|
for (const layout of layouts.reverse()) lines.splice(pindex, 0, layout.add);
|
|
// debug
|
|
debug('autogenerated layout import file %o with content %O', layoutfile, lines);
|
|
return lines.join(EOL);
|
|
}
|
|
},
|
|
};
|
|
};
|