|
| 1 | +import type { Plugin } from 'vite' |
| 2 | +import * as babel from '@babel/core' |
| 3 | +import type { PluginItem } from '@babel/core' |
| 4 | + |
| 5 | +import * as t from '@babel/types' |
| 6 | + |
| 7 | +import type { |
| 8 | + Identifier, |
| 9 | + ImportDeclaration, |
| 10 | + CallExpression, |
| 11 | + ArrowFunctionExpression, |
| 12 | + StringLiteral, |
| 13 | +} from '@babel/types' |
| 14 | + |
| 15 | +/** |
| 16 | + * This plugin just removes the `lazy` imported function from any tuono import |
| 17 | + */ |
| 18 | +const RemoveTuonoLazyImport: PluginItem = { |
| 19 | + name: 'remove-tuono-lazy-import-plugin', |
| 20 | + visitor: { |
| 21 | + ImportSpecifier: (path) => { |
| 22 | + if ((path.node.imported as Identifier).name === 'lazy') { |
| 23 | + if ( |
| 24 | + (path.parentPath.node as ImportDeclaration).source.value === 'tuono' |
| 25 | + ) { |
| 26 | + path.remove() |
| 27 | + } |
| 28 | + } |
| 29 | + }, |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Import { lazy } from 'react' |
| 35 | + */ |
| 36 | +const ImportReactLazy: PluginItem = { |
| 37 | + name: 'import-react-lazy-plugin', |
| 38 | + visitor: { |
| 39 | + Program: (path: any) => { |
| 40 | + let isReactImported = false |
| 41 | + |
| 42 | + path.node.body.forEach((val: any) => { |
| 43 | + if (val.type === 'ImportDeclaration' && val.source.value === 'react') { |
| 44 | + isReactImported = true |
| 45 | + // TODO: Handle also here case of already imported react |
| 46 | + // Right now works just for the main routes file |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
| 51 | + if (!isReactImported) { |
| 52 | + const importDeclaration = t.importDeclaration( |
| 53 | + [t.importSpecifier(t.identifier('lazy'), t.identifier('lazy'))], |
| 54 | + t.stringLiteral('react'), |
| 55 | + ) |
| 56 | + path.unshiftContainer('body', importDeclaration) |
| 57 | + } |
| 58 | + }, |
| 59 | + }, |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * For the server side we need to statically import the lazy loaded components |
| 64 | + */ |
| 65 | +const TurnLazyToStaticImport: PluginItem = { |
| 66 | + name: 'turn-lazy-to-static-import-plugin', |
| 67 | + visitor: { |
| 68 | + VariableDeclaration: (path) => { |
| 69 | + path.node.declarations.forEach((el) => { |
| 70 | + const init = el.init as CallExpression |
| 71 | + if ((init.callee as Identifier).name === 'lazy') { |
| 72 | + const importName = (el.id as Identifier).name |
| 73 | + const importPath = ( |
| 74 | + ( |
| 75 | + (init.arguments[0] as ArrowFunctionExpression) |
| 76 | + .body as CallExpression |
| 77 | + ).arguments[0] as StringLiteral |
| 78 | + ).value |
| 79 | + |
| 80 | + if (importName && importPath) { |
| 81 | + const importDeclaration = t.importDeclaration( |
| 82 | + [t.importDefaultSpecifier(t.identifier(importName))], |
| 83 | + t.stringLiteral(importPath), |
| 84 | + ) |
| 85 | + |
| 86 | + path.replaceWith(importDeclaration) |
| 87 | + } |
| 88 | + } |
| 89 | + }) |
| 90 | + }, |
| 91 | + }, |
| 92 | +} |
| 93 | + |
| 94 | +export function LazyLoadingPlugin(): Plugin { |
| 95 | + return { |
| 96 | + name: 'vite-plugin-tuono-lazy-loading', |
| 97 | + enforce: 'pre', |
| 98 | + transform(code, _id, opts): string | undefined | null { |
| 99 | + if (code.includes('lazy') && code.includes('tuono')) { |
| 100 | + const res = babel.transformSync(code, { |
| 101 | + plugins: [ |
| 102 | + ['@babel/plugin-syntax-jsx', {}], |
| 103 | + ['@babel/plugin-syntax-typescript', { isTSX: true }], |
| 104 | + [RemoveTuonoLazyImport], |
| 105 | + [!opts?.ssr ? ImportReactLazy : []], |
| 106 | + [opts?.ssr ? TurnLazyToStaticImport : []], |
| 107 | + ], |
| 108 | + sourceMaps: true, |
| 109 | + }) |
| 110 | + |
| 111 | + return res?.code |
| 112 | + } |
| 113 | + return code |
| 114 | + }, |
| 115 | + } |
| 116 | +} |
0 commit comments