|
| 1 | +import babel from '@babel/core'; |
| 2 | + |
| 3 | +const options = { |
| 4 | + babelrc: false, |
| 5 | + ignore: [/\/(build|node_modules)\//], |
| 6 | + plugins: [ |
| 7 | + '@babel/plugin-syntax-import-meta', |
| 8 | + '@babel/plugin-transform-react-jsx', |
| 9 | + ], |
| 10 | +}; |
| 11 | + |
| 12 | +const optionsCommonJS = { |
| 13 | + ignore: [/\/(build|node_modules)\//], |
| 14 | + presets: ['react-app'], |
| 15 | + plugins: ['@babel/transform-modules-commonjs'], |
| 16 | +}; |
| 17 | + |
| 18 | +export async function transformSource(source, context, defaultTransformSource) { |
| 19 | + const {format} = context; |
| 20 | + if (format === 'module' || format === 'commonjs') { |
| 21 | + const opt = Object.assign( |
| 22 | + {filename: context.url}, |
| 23 | + format === 'commonjs' ? optionsCommonJS : options |
| 24 | + ); |
| 25 | + const {code} = await babel.transformAsync(source, opt); |
| 26 | + return {source: code}; |
| 27 | + } |
| 28 | + return defaultTransformSource(source, context); |
| 29 | +} |
| 30 | + |
| 31 | +export async function getSource(url, context, defaultGetSource) { |
| 32 | + if (url.endsWith('.client.js')) { |
| 33 | + const name = url; |
| 34 | + return { |
| 35 | + source: |
| 36 | + "export default { $$typeof: Symbol.for('react.module.reference'), name: " + |
| 37 | + JSON.stringify(name) + |
| 38 | + '}', |
| 39 | + }; |
| 40 | + } |
| 41 | + return defaultGetSource(url, context, defaultGetSource); |
| 42 | +} |
0 commit comments