0

我对 Preact 和 TypeScript 很陌生,并且使用parcel-preact-typescript-boilerplate.

到目前为止一切正常,但我意识到,在重新加载时,我有时会得到一个空白页面。打开页面时也会不时发生这种情况。空白页是指我的Hello组件不会在document.body. 但是,当我在 Google Chrome 中禁用缓存时不会发生这种情况,当我在 Firefox 中禁用缓存时仍然会发生这种情况。

我认为造成这种情况的原因可能是我的代码更改和介绍,但是在克隆parcel-preact-typescript-boilerplate并开始使用它时,npm run start我最终会遇到同样奇怪的行为。

这可能是什么原因?

索引.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>PARCEL-PREACT-TYPESCRIPT-BOILERPLATE</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link async rel="stylesheet" href="./src/style.css" />
        <script async src="./src/index.tsx"></script>
    </head>
    <body>
    </body>
</html>

索引.tsx:

import { h, render } from 'preact';

const Hello = () => (<div><h1 className="hello" >Hello World</h1></div>);

render(<Hello/>, document.body);

包.json:

{
  "name": "parcel-preact-typescript-boilerplate",
  "version": "1.0.6",
  "description": "simple parcel preact typescript project without preact-compat",
  "main": "index.html",
  "scripts": {
    "start": "parcel index.html",
    "watch": "parcel watch index.html",
    "build": "parcel build index.html",
    "rebuild": "npm run clean && npm run build",
    "clean": "rimraf dist/ && rimraf .cache"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/benevolarX/parcel-preact-typescript-boilerplate.git"
  },
  "keywords": [
    "parcel",
    "preact",
    "typescript",
    "boilerplate",
    "project"
  ],
  "author": "benevolar",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/benevolarX/parcel-preact-typescript-boilerplate/issues"
  },
  "homepage": "https://github.com/benevolarX/parcel-preact-typescript-boilerplate#readme",
  "devDependencies": {
    "@babel/core": "^7.2.0",
    "@types/node": "^10.12.12",
    "babel-preset-preact": "^1.1.0",
    "parcel-bundler": "^1.10.3",
    "rimraf": "^2.6.2",
    "typescript": "^3.2.2"
  },
  "dependencies": {
    "preact": "^8.4.2",
    "preact-compat": "^3.18.4"
  }
}

tsconfig.json:

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "baseUrl": ".",
        "checkJs": true,
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "jsx": "react",
        "jsxFactory": "h",
        "module": "commonjs",
        "outDir": "dist",
        "paths": {
            "~*": ["./src/*"]
        },
        "pretty": true,
        "removeComments": true,
        "strict": true,
        "target": "esnext",
    },
    "include": [
        "src/**/*"
    ],
    "parcelTsPluginOptions": {
        "transpileOnly": false
    }
}

编辑:玩了一会儿后,我意识到在使用 parcel 构建所有内容并通过preact serve dist/preact watch dist/index.html而不是启动应用程序后不会出现问题parcel index.html

4

1 回答 1

0

您(或包裹)正在尝试加载浏览器不喜欢的 Typescript (TSX) 文件。改变

<script async src="./src/index.tsx"></script>

<script async src="./[build folder]/index.js"></script>

它应该工作。我相信 parcel 将构建的脚本放在一个名为的文件夹build中,所以把它放在[build folder]上面的代码片段中。

于 2018-12-18T16:15:29.107 回答