我对Preact&TypeScript很陌生,并使用parcel-preact-typescript-boilerplate
引导了应用程序。
到目前为止,一切正常,但是我意识到,不时进行重新加载时,我最终会得到空白页。打开页面时也会不时发生。空白页是指我的Hello
组件未在document.body
中呈现。但是,在禁用Google Chrome浏览器中的缓存时不会发生这种情况,而在禁用Firefox中的缓存时仍会发生这种情况。
我认为这样做的原因可能是我的代码更改和介绍,但是在克隆parcel-preact-typescript-boilerplate
并以npm run start
开头时,我却得到了同样的怪异行为。
这可能是什么原因?
index.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>
index.tsx:
import { h, render } from 'preact';
const Hello = () => (<div><h1 className="hello" >Hello World</h1></div>);
render(<Hello/>, document.body);
package.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
}
}
编辑:
经过一番尝试之后,我意识到用宗地构建所有内容并通过preact serve dist
/ preact watch dist/index.html
而不是parcel index.html
答案 0 :(得分:0)
您(或包裹)正试图加载浏览器不喜欢的Typescript(TSX)文件。更改
<script async src="./src/index.tsx"></script>
到
<script async src="./[build folder]/index.js"></script>
它应该工作。我相信包裹会将已构建的脚本放在名为build
的文件夹中,因此将其替换为上述片段中的[build folder]
。