UNPKG

react-compiler-webpack

Version:
54 lines (50 loc) 1.78 kB
'use strict'; var babel = require('@babel/core'); var BabelPluginReactCompiler = require('babel-plugin-react-compiler'); const defaultBabelParsePlugins = [ 'jsx', 'typescript' ]; async function reactCompilerLoader(input, _inputSourceMap) { const callback = this.async(); // TODO: is it possible to bail out early if the input doesn't contain a react component? try { const { babelTransFormOpt, ...reactCompilerConfig } = this.getOptions(); const result = await babel.transformAsync(input, { sourceFileName: this.resourcePath, filename: this.resourcePath, cloneInputAst: false, // user configured babel option ...babelTransFormOpt, // override babel plugins plugins: [ [ BabelPluginReactCompiler, reactCompilerConfig ], ...babelTransFormOpt?.plugins || [] ], // override babel parserOpts parserOpts: { ...babelTransFormOpt?.parserOpts, // override babel parserOpts plugins and add jsx plugins: [ ...babelTransFormOpt?.parserOpts?.plugins || [], ...defaultBabelParsePlugins ] }, ast: false, sourceMaps: true, configFile: false, babelrc: false }); if (!result) { throw new TypeError('babel.transformAsync with react compiler plugin returns null'); } const { code, map } = result; callback(null, code ?? undefined, map ?? undefined); } catch (e) { callback(e); } } module.exports = reactCompilerLoader;