|
| 1 | +import path from 'path'; |
| 2 | +import { createRequire } from 'module'; |
| 3 | +import type { Plugin } from '@ice/types'; |
| 4 | +import { transformSync } from '@babel/core'; |
| 5 | + |
| 6 | +const require = createRequire(import.meta.url); |
| 7 | +const runtimePackage = 'babel-runtime-jsx-plus'; |
| 8 | +const runtimePackagePath = require.resolve(runtimePackage); |
| 9 | + |
| 10 | +const babelPlugins = [ |
| 11 | + 'babel-plugin-transform-jsx-list', |
| 12 | + 'babel-plugin-transform-jsx-condition', |
| 13 | + 'babel-plugin-transform-jsx-memo', |
| 14 | + 'babel-plugin-transform-jsx-slot', |
| 15 | + ['babel-plugin-transform-jsx-fragment', { moduleName: 'react' }], |
| 16 | + 'babel-plugin-transform-jsx-class', |
| 17 | +]; |
| 18 | + |
| 19 | +const babelTransformOptions = { |
| 20 | + babelrc: false, |
| 21 | + configFile: false, |
| 22 | + parserOpts: { |
| 23 | + sourceType: 'module', |
| 24 | + allowAwaitOutsideFunction: true, |
| 25 | + // ts syntax had already been transformed by swc plugin. |
| 26 | + plugins: [ |
| 27 | + 'jsx', |
| 28 | + 'importMeta', |
| 29 | + 'topLevelAwait', |
| 30 | + 'classProperties', |
| 31 | + 'classPrivateMethods', |
| 32 | + ], |
| 33 | + generatorOpts: { |
| 34 | + decoratorsBeforeExport: true, |
| 35 | + }, |
| 36 | + }, |
| 37 | + plugins: [], |
| 38 | +}; |
| 39 | + |
| 40 | +babelPlugins.forEach((plugin) => { |
| 41 | + if (typeof plugin === 'string') { |
| 42 | + babelTransformOptions.plugins.push(require.resolve(plugin)); |
| 43 | + } else if (Array.isArray(plugin)) { |
| 44 | + const pluginName = plugin[0] as string; |
| 45 | + const pluginOption = plugin[1]; |
| 46 | + babelTransformOptions.plugins.push([require.resolve(pluginName), pluginOption]); |
| 47 | + } |
| 48 | +}); |
| 49 | + |
| 50 | +export function idFilter(options: JSXPlusOptions, id: string): boolean { |
| 51 | + const extFilter = (id) => options.extensions.some((ext) => id.endsWith(ext)); |
| 52 | + |
| 53 | + if (options.exclude) { |
| 54 | + for (const pattern of options.exclude) { |
| 55 | + if (typeof pattern === 'string') { |
| 56 | + if (id.indexOf(pattern) > -1) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + } else if (pattern instanceof RegExp && pattern.test(id)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (options.include) { |
| 66 | + for (const pattern of options.include) { |
| 67 | + if (typeof pattern === 'string') { |
| 68 | + if (id.indexOf(pattern) > -1) { |
| 69 | + return extFilter(id); |
| 70 | + } |
| 71 | + } else if (pattern instanceof RegExp && pattern.test(id)) { |
| 72 | + return extFilter(id); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return false; |
| 78 | +} |
| 79 | + |
| 80 | +export interface JSXPlusOptions { |
| 81 | + include?: (string | RegExp)[]; |
| 82 | + exclude?: (string | RegExp)[]; |
| 83 | + extensions?: string[]; |
| 84 | +} |
| 85 | + |
| 86 | +const plugin: Plugin<JSXPlusOptions> = (options: JSXPlusOptions = {}) => ({ |
| 87 | + name: '@ice/plugin-jsx-plus', |
| 88 | + setup: ({ onGetConfig, context }) => { |
| 89 | + // Default include all files in `src`. |
| 90 | + if (!options.include) { |
| 91 | + const sourceDir = path.join(context.rootDir, 'src'); |
| 92 | + options.include = [sourceDir]; |
| 93 | + } |
| 94 | + |
| 95 | + // Default include all files with `.tsx` and `.jsx` extensions. |
| 96 | + if (!options.extensions) { |
| 97 | + options.extensions = ['.tsx', '.jsx']; |
| 98 | + } |
| 99 | + |
| 100 | + function jsxPlusTransformer(source, id) { |
| 101 | + if (idFilter(options, id)) { |
| 102 | + try { |
| 103 | + const options = Object.assign({ |
| 104 | + filename: id, |
| 105 | + sourceFileName: id, |
| 106 | + }, babelTransformOptions); |
| 107 | + if (/\.tsx?$/.test(id)) { |
| 108 | + // When routes file is a typescript file, add ts parser plugins. |
| 109 | + options.parserOpts.plugins.push('typescript'); |
| 110 | + options.parserOpts.plugins.push('decorators-legacy'); // allowing decorators by default |
| 111 | + } |
| 112 | + |
| 113 | + const { code, map } = transformSync(source, options); |
| 114 | + return { code, map }; |
| 115 | + } catch (compileError) { |
| 116 | + console.error(compileError); |
| 117 | + return { code: source, map: null }; |
| 118 | + } |
| 119 | + } |
| 120 | + return { code: source, map: null }; |
| 121 | + } |
| 122 | + |
| 123 | + onGetConfig((config) => { |
| 124 | + // Add runtime alias. |
| 125 | + if (!config.alias) { |
| 126 | + config.alias = {}; |
| 127 | + } |
| 128 | + config.alias[runtimePackage] = runtimePackagePath; |
| 129 | + |
| 130 | + // Apply babel jsx plus transformer. |
| 131 | + if (!config.transforms) { |
| 132 | + config.transforms = []; |
| 133 | + } |
| 134 | + config.transforms.push(jsxPlusTransformer); |
| 135 | + }); |
| 136 | + }, |
| 137 | +}); |
| 138 | + |
| 139 | +export default plugin; |
0 commit comments