@babel/core#PluginItem TypeScript Examples

The following examples show how to use @babel/core#PluginItem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From reskript with MIT License 6 votes vote down vote up
getBabelConfig = (input?: BabelConfigOptions): TransformOptions => {
    const options = fillBabelConfigOptions(input);
    const {mode, hot, hostType, cwd, srcDirectory} = options;
    const transform = getTransformBabelConfig(options);
    const requireReactOptimization = mode === 'production' && hostType === 'application';
    const plugins: Array<PluginItem | false> = [
        // 这东西必须放在最前面,不然其它插件会转义出如`function Wrapper()`这样的函数,这个插件再插入代码就会出问题
        requireFileName(options) && [
            debugReactComponentFileName,
            {
                srcDirectory: path.resolve(cwd, srcDirectory),
                fullPathPrefix: options.openInEditorPrefix,
            },
        ],
        ...transform.plugins || [],
        requireReactOptimization && pluginRemovePropTypes,
        hot && [pluginReactRefresh, {skipEnvCheck: true}],
    ];

    return {presets: transform.presets, plugins: compact(plugins)};
}
Example #2
Source File: plugin.ts    From vite-react-jsx with MIT License 5 votes vote down vote up
function getTransformer(opts: { sourceMaps?: boolean }) {
  const babelImport = import('@babel/core')
  const babelTransformJsx = import('@babel/plugin-transform-react-jsx')

  return async function transform(code: string, id: string) {
    if (/.+\/node_modules\/.+\.jsx?$/.test(id)) {
      const babel = await babelImport

      // Reverse-compile any React.createElement calls
      let [ast, isCommonJS] = await viteReactJsx.restoreJSX(babel, code)

      // Then apply the JSX automatic runtime transform
      if (ast) {
        const plugins: PluginItem[] = [
          [await babelTransformJsx, { runtime: 'automatic' }],
        ]
        if (isCommonJS) {
          plugins.push(babelImportToRequire)
        }
        const result = await babel.transformFromAstAsync(ast, undefined, {
          plugins,
          sourceMaps: opts.sourceMaps,
        })
        if (result?.code) {
          return {
            code: result.code,
            map: result.map,
          }
        }
      }
    } else if (/\.[tj]sx$/.test(id)) {
      const syntaxPlugins: PluginItem[] = []
      if (id.endsWith('.tsx')) {
        syntaxPlugins.push(await babelTSX())
      }
      const babel = await babelImport
      const res = await babel.transformAsync(code, {
        plugins: [
          ...syntaxPlugins,
          [await babelTransformJsx, { runtime: 'automatic' }],
        ],
        sourceMaps: opts.sourceMaps,
      })
      if (res?.code) {
        return {
          code: res.code,
          map: res.map,
        }
      }
    }
  }

  async function babelTSX() {
    return [
      await import('@babel/plugin-syntax-typescript').then(m => m.default),
      { isTSX: true },
    ]
  }
}
Example #3
Source File: parseOnly.ts    From reskript with MIT License 5 votes vote down vote up
hasValue = (value: PluginItem | false): value is PluginItem => !!value
Example #4
Source File: transformMinimal.ts    From reskript with MIT License 5 votes vote down vote up
hasValue = (value: PluginItem | false): value is PluginItem => !!value