这是项目的代码。
src/index.ts
import { SAXParser, StartTag, EndTag } from "parse5-sax-parser"
import fs from "fs"
const parse = (stream: fs.ReadStream): Promise<void> =>{
const parser = new SAXParser()
parser.on("startTag", (elem: StartTag)=>{
if(elem.tagName === "LINK"){
console.log(elem)
}
})
parser.on("closeTag", (elem: EndTag)=>{
if(elem.tagName === "LINK"){
console.log(elem)
}
})
parser.on("text", (text: string) =>{
console.log(text)
})
return new Promise<void>((resolve)=>{
stream.on("end", ()=>{
console.log("end")
})
stream.pipe(parser)
})
}
export default parse
.mocharc.json
{
"extension": ["ts"],
"spec": "test/**/*.spec.ts",
"require": "ts-node/register"
}
package.json
{
"name": "parse-error-testing",
"version": "1.0.0",
"description": "",
"main": "lib/index.js",
"type":"module",
"module":"commonjs",
"directories": {
"test": "test",
"lib":"lib"
},
"scripts": {
"compile": "tsc --declaration",
"lint": "tslint --project tsconfig.json",
"prepublish": "npm run compile",
"test": "nyc --reporter=lcov --reporter=text mocha --reporter-option maxDiffSize=0 "
},
"files": [
"/lib"
],
"author": "[email protected]",
"license": "MIT",
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"chai": "^4.3.7",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
},
"dependencies": {
"parse5-sax-parser": "^7.0.0"
}
}
tsconfig.json
{
"compilerOptions": {
"outDir": "./lib",
"rootDir": "./src",
"target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "CommonJS", /* Specify what module code is generated. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": ["src/**/*"],
}
测试/测试.spec.ts
import chai from "chai"
import fs from "fs"
import parse from "../src/index"
const expect = chai.expect
describe("parse5 bug", function(){
const html_str_1 = fs.createReadStream("./test/1.txt", 'utf8') //For bug replication, just replace this with any HTML stored as a .txt file in the same directory as test.spec.ts
describe("Attempting to parse", function(){
it("should parse", function(){
parse(html_str_1) //When I don't include this line of code, eveything runs just fine
})
})
})
我运行了“npm run test”,这导致了错误。我尝试在 tsconfig.json 中使用 "loader": "ts-node/esm",但这只会导致一个新错误,即 ReferenceError: exports is not defined in ES module scope。我尝试删除“type”:“module”和“module”:“commonjs”,但这样做导致了另一个错误,如下所示: