代码之家  ›  专栏  ›  技术社区  ›  7urkm3n Neeraj Kumar

Webpack和Typescript(ts加载程序)上的安装程序问题

  •  0
  • 7urkm3n Neeraj Kumar  · 技术社区  · 5 年前

    尝试使用Webpack和ts加载程序设置SolidJS。

    继续关注问题 return 功能。

    我在解决问题的加载器中到底缺少什么?

    import { render } from "solid-js/web";
    const App = () => {
      // return "Hello World"             // this works!
      return (<div> Hello World </div>)   // this doesnt work!
    };
    render(App, document.getElementById("app"));
    
    

    错误日志输出:

    > webpack --watch --progress --config webpack.config.js
    
    asset index.js 1.56 KiB [emitted] (name: main)
    ./src/test.tsx 290 bytes [built] [code generated]
    
    ERROR in ./src/test.tsx 6:12
    Module parse failed: Unexpected token (6:12)
    File was processed with these loaders:
     * ./node_modules/ts-loader/index.js
    You may need an additional loader to handle the result of these loaders.
    | const App = () => {
    |     // return "Hello World"            // this works!
    >     return (<div> Hello World </div>); // this doesnt work!
    | };
    | render(App, document.getElementById("app"));
    
    webpack 5.47.0 compiled with 1 error in 2081 ms
    

    webpack.config

    const path = require('path');
    
    module.exports = {
      mode: 'development',
      entry: './src/test.tsx',
      module: {
        rules: [
          {
            use: 'ts-loader',
            test: /\.(ts|tsx)?$/,
            exclude: /node_modules/
          }
        ]
      },
      resolve: {
        extensions: ['.tsx', '.ts', '.js']
      },
      output: {
        filename: 'index.js',
        path: path.resolve(__dirname, 'dist')
      }
    }
    

    更新:

    # tsconfig.json
    
    {
      "compilerOptions": {
        "jsx": "preserve",
        "jsxImportSource": "solid-js",
       
        "target": "ESNext",
        "module": "ESNext",
        "moduleResolution": "node",
        "esModuleInterop": false
      },
    
      "include": ["src/**/*.ts", "src/**/*.tsx"],
      "exclude": ["node_modules"]
    }
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   Ryan Carniato    5 年前

    你需要加上巴贝尔。从TypeScript中保留JSX是可以的,但在最终绑定之前仍需要对其进行转换。babel preset solid解决了这个问题。否则,Webpack不知道该如何处理那个JSX。

    一般来说,我只是将Babel与TypeScript预设一起使用。我知道这不会给你实时的类型检查,只是去掉类型。Create React App使用fork-ts-checker webpack插件来解决这个问题,并与Babel转换并行处理类型检查。

        2
  •  1
  •   7urkm3n Neeraj Kumar    5 年前

    解决了的:

    #webpack module rules:
    
    module: {
        rules: [
          {
            test: /\.tsx?$/,
            exclude: /node_modules/,
            use: [
              {
                loader: 'babel-loader',
                options: {
                  presets: ['solid'],
                },
              },
              {
                loader: 'ts-loader',
              },
           ]
         }
       ]
    },
    
    
    推荐文章