1

将 JSX 编译到h()测试中的问题。所有配置文件都类似于 create-react-app,排除对TypeScript和的更改preact

我通过create-react-app my-app --script=react-scripts-ts- 为 TypeScript 项目创建应用程序。然后弹出并更改reactpreact(不要使用preact-compat)。

为了迁移到preact我添加package.jsonbabel.plugins section新插件["babel-plugin-transform-react-jsx", { pragma: "h"}]中 - 用于转换<JSX />h(JSX)函数调用,而不是默认React.createElement(JSX)(迁移指南https://preactjs.com/guide/switching-to-preact)。

这很好用。

但是 test 有不同的转译配置<JSX />:它React.createElement(JSX)是默认转译。在测试中我犯了一个错误ReferenceError: React is not defined at Object.<anonymous> (src/Linkify/Linkify.test.tsx:39:21)。如果我手动更改<JSX />h(SomeComponent)在测试和测试文件中 - 它可以工作。

如何转译<JSX />h(JSX)测试?

// typescriptTransform.js
// Copyright 2004-present Facebook. All Rights Reserved.

'use strict';

const fs = require('fs');
const crypto = require('crypto');
const tsc = require('typescript');
const tsconfigPath = require('app-root-path').resolve('/tsconfig.json');
const THIS_FILE = fs.readFileSync(__filename);

let compilerConfig = {
  module: tsc.ModuleKind.CommonJS,
  jsx: tsc.JsxEmit.React,
};

if (fs.existsSync(tsconfigPath)) {
  try {
    const tsconfig = tsc.readConfigFile(tsconfigPath).config;

    if (tsconfig && tsconfig.compilerOptions) {
      compilerConfig = tsconfig.compilerOptions;
    }
  } catch (e) {
    /* Do nothing - default is set */
  }
}

module.exports = {
  process(src, path, config, options) {
    if (path.endsWith('.ts') || path.endsWith('.tsx')) {
      let compilerOptions = compilerConfig;
      if (options.instrument) {
        // inline source with source map for remapping coverage
        compilerOptions = Object.assign({}, compilerConfig);
        delete compilerOptions.sourceMap;
        compilerOptions.inlineSourceMap = true;
        compilerOptions.inlineSources = true;
        // fix broken paths in coverage report if `.outDir` is set
        delete compilerOptions.outDir;
      }

      const tsTranspiled = tsc.transpileModule(src, {
        compilerOptions: compilerOptions,
        fileName: path,
      });
      return tsTranspiled.outputText;
    }
    return src;
  },
  getCacheKey(fileData, filePath, configStr, options) {
    return crypto
      .createHash('md5')
      .update(THIS_FILE)
      .update('\0', 'utf8')
      .update(fileData)
      .update('\0', 'utf8')
      .update(filePath)
      .update('\0', 'utf8')
      .update(configStr)
      .update('\0', 'utf8')
      .update(JSON.stringify(compilerConfig))
      .update('\0', 'utf8')
      .update(options.instrument ? 'instrument' : '')
      .digest('hex');
  },
};

测试样品:

import { h, render } from 'preact';
import Linkify from './Linkify';

it('renders without crashing', () => {
  const div = document.createElement('div');
  render(<Linkify children={'text'} />, div);
});
4

1 回答 1

1

我找到了解决方案。

我在babel.plugins部分错了new plugin ["babel-plugin-transform-react-jsx", { pragma: "h"}]- 它没有使用。真的这个编译指示使用tsconfig.json-"jsxFactory": "h"

但是,该指令不用于typescriptTransform.js.

我正在扩展编译器选项

let compilerConfig = {
  module: tsc.ModuleKind.CommonJS,
  jsx: tsc.JsxEmit.React,
  jsxFactory: "h" // <-- duplicate option in jest transform config file
};

希望它会有用。

于 2017-07-28T08:05:07.977 回答