UNPKG

1 kBJavaScriptView Raw
1const resolvePath = require('./resolve-component-import-path');
2const resolveImportStatement = require('./resolve-import-statement');
3
4const LAYOUT_COMPONENT_NAME = '___DefaultLayout';
5
6module.exports = (options) => (tree, file) => {
7 const filePath = file.history.length ? file.history[0] : undefined;
8
9 if (options.layout) {
10 if (!tree.children.find((item) => item.type === 'export')) {
11 tree.children.push({
12 type: 'import',
13 value: `import ${LAYOUT_COMPONENT_NAME} from "${resolvePath(filePath, options.layout)}";`,
14 });
15 tree.children.push({
16 type: 'export',
17 default: true,
18 value: `export default ${LAYOUT_COMPONENT_NAME}`,
19 });
20 }
21 }
22
23 if (options.imports) {
24 const imports = Array.isArray(options.imports) ? options.imports : [options.imports];
25 imports.forEach((importStatement) => {
26 tree.children.unshift({
27 type: 'import',
28 value: resolveImportStatement(importStatement),
29 });
30 });
31 }
32};