|
| 1 | +export function removeLegacyLayout(babel) { |
| 2 | + const shouldContinue = (state) => { |
| 3 | + const fName = state.file.opts.filename; |
| 4 | + return ( |
| 5 | + fName.includes('node_modules') && |
| 6 | + (fName.includes('addon') || fName.includes('app')) |
| 7 | + ); |
| 8 | + }; |
| 9 | + const { types: t } = babel; |
| 10 | + return { |
| 11 | + name: 'remove-layout', |
| 12 | + visitor: { |
| 13 | + Program: { |
| 14 | + exit(path, state) { |
| 15 | + if (state.shouldAppendLayout) { |
| 16 | + path.node.body.push( |
| 17 | + t.variableDeclaration('var', [ |
| 18 | + t.variableDeclarator( |
| 19 | + t.identifier('layout'), |
| 20 | + t.stringLiteral('') |
| 21 | + ), |
| 22 | + ]) |
| 23 | + ); |
| 24 | + } |
| 25 | + }, |
| 26 | + }, |
| 27 | + ImportDeclaration(path, state) { |
| 28 | + if (!shouldContinue(state)) { |
| 29 | + return; |
| 30 | + } |
| 31 | + if (path.node.source.value.endsWith('/config/environment')) { |
| 32 | + path.node.source.value = '@/config/env'; |
| 33 | + } |
| 34 | + if (path.node.source.value === '@ember-decorators/component') { |
| 35 | + // path.node.specifiers = path.node.specifiers.filter((el) => { |
| 36 | + // return el.imported.name !== 'layout'; |
| 37 | + // }); |
| 38 | + // if (!path.node.specifiers.length) { |
| 39 | + // path.remove(); |
| 40 | + // } |
| 41 | + path.node.source.value = 'compat-ember-decorators/component'; |
| 42 | + } else if (path.node.source.value.includes('/templates/')) { |
| 43 | + path.remove(); |
| 44 | + // add `layout` variable to programm |
| 45 | + state.shouldAppendLayout = true; |
| 46 | + } |
| 47 | + }, |
| 48 | + ObjectProperty(path, state) { |
| 49 | + if (!shouldContinue(state)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + if ( |
| 53 | + path.node.value.name === 'layout' && |
| 54 | + path.node.key.name === 'layout' |
| 55 | + ) { |
| 56 | + path.remove(); |
| 57 | + } |
| 58 | + }, |
| 59 | + ClassProperty(path, state) { |
| 60 | + if (!shouldContinue(state)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + if ( |
| 64 | + path.node.value && |
| 65 | + path.node.value.name === 'layout' && |
| 66 | + path.node.key && |
| 67 | + path.node.key.name === 'layout' |
| 68 | + ) { |
| 69 | + path.remove(); |
| 70 | + } |
| 71 | + }, |
| 72 | + // Decorator(path, state) { |
| 73 | + // if (!shouldContinue(state)) { |
| 74 | + // return; |
| 75 | + // } |
| 76 | + // if (path.node.expression && path.node.expression.callee) { |
| 77 | + // if ( |
| 78 | + // path.node.expression.callee.name === 'templateLayout' || |
| 79 | + // path.node.expression.callee.name === 'layout' |
| 80 | + // ) { |
| 81 | + // path.remove(); |
| 82 | + // } else { |
| 83 | + // console.log(path.node.expression.callee.name); |
| 84 | + // } |
| 85 | + // } |
| 86 | + // }, |
| 87 | + }, |
| 88 | + }; |
| 89 | +} |
0 commit comments