gpt4 book ai didi

laravel - npm run watch/hot 仅在第一次运行时成功

转载 作者:行者123 更新时间:2023-12-05 01:57:34 25 4
gpt4 key购买 nike

背景:

我在现有项目中添加了 TypeScript 支持,因此我添加了 ts-loadertypescript。我想,我正确配置了一切,它在 devprod 模式下工作正常。

我想逐步更新,保留所有 JavaScript 代码并在所有新内容或需要重构的地方使用 TypeScript。因此,可能需要注意 TableValue.vue 是一个旧的 js 组件。

问题:

编辑:它也发生在 npm run watch

当我运行 npm run hotpackage.json: "scripts": { ..., "hot": "mix watch --hot", ...} 它只适用于第一个尝试。一旦我更改任何文件并触发重新编译,我就会得到:

√ Mix: Compiled successfully in 19.15s
webpack compiled successfully

// Here the recompile is triggered
i Compiling Mix
√ Mix: Compiled with some errors in 509.01ms
ERROR in C:\fakepath\resources\js\components\test\component.vue.ts
24:23-41
[tsl] ERROR in C:\fakepath\resources\js\components\test\component.vue.ts(24,24)
TS2307: Cannot find module './TableValue.vue' or its corresponding type declarations.

webpack compiled with 1 error

我怀疑这个错误来自 ts-loader,但为什么第一次尝试一切正常?

我可以忽略这个错误,但是 hot module replacement无法使用,因为无论如何我每次都必须手动触发新的构建过程。

  • 有人有这样的设置吗?
  • 我该怎么做才能解决这个错误?

信息:

我正在与:

  • Laravel 8.58
  • Laravel 混合 6.0.25
  • Vue 2.6.14
  • ts-loader 9.2.5
  • typescript 4.4.2

这里是来自测试组件的脚本标签:

<script lang="ts">
import Vue, { PropType } from 'vue';

import TableValue from "./TableValue.vue";
import Model from "@/js/types/model.js";

export default Vue.extend({
name: "TestComponent",
components: {
TableValue
},
props: {
'model': {
type: Object as PropType<Model>,
required: true
}
},
data() {
return {};
},
});
</script>

项目结构:

app/
bootstrap/
config/
database/
node_modules/
public/
resources/
js/
components/
store/
types/
views/
app.js
bootstrap.js
routes.js
shims-vue.d.ts
lang/
sass/
views/
routes/
storage/
tests/
vendor/
composer.json
composer.lock
tsconfig.json
package-lock.json
package.json
phpunit.xml
vs.code-workspace
webpack.mix.js

webpack.mix.js:

const mix = require('laravel-mix');
const ResolveTypeScriptPlugin = require("resolve-typescript-plugin").default;

mix.webpackConfig({
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: { appendTsSuffixTo: [/\.vue$/] },
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.js', '.ts', '.vue'],
alias: {
'@': __dirname + '/resources'
},
fullySpecified: false,
plugins: [new ResolveTypeScriptPlugin()]
},
devtool: 'source-map'
}).sourceMaps();

mix.ts('resources/js/app.js', 'public/js')
.sass('resources/sass/app.sass', 'public/css').sourceMaps()
.vue();

mix.extract();

tsconfig.json:

{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"noImplicitAny": false,
"importHelpers": true,
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"allowJs": true,
"checkJs": false,
"sourceMap": true,
"baseUrl": ".",
"paths": {
"@/*": [
"resources/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"files": [
"resources/js/shims-vue.d.ts"
],
"include": [
"resources/js/**/*.ts",
"resources/js/**/*.vue",
],
"exclude": [
"node_modules",
".vscode",
"app",
"bootstrap",
"config",
"database",
"public",
"routes",
"storage",
"tests",
"vendor"
]
}

更新:

当我删除 shims-vue.d.ts 时,我立即收到错误。

declare module "*.vue" {
import Vue from "vue";
export default Vue;
}

看起来这个文件只被读取/应用了一次,而不是之后?不确定。

最佳答案

看起来 ts-loader 不支持 HMR还没有。

我安装了 fork-ts-checker-webpack-plugin并将 webpack.mix.js 更新为:

const mix = require('laravel-mix');
const path = require('path');

const ResolveTypeScriptPlugin = require("resolve-typescript-plugin").default;
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

mix.webpackConfig({
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: {
appendTsSuffixTo: [/\.vue$/],
transpileOnly: true
},
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.js', '.ts', '.tsx', '.vue'],
alias: {
'@': path.resolve(__dirname + '/resources'),
'@store': path.resolve(__dirname + '/resources/js/store'),
'@components': path.resolve(__dirname + '/resources/js/components')
},
fullySpecified: false,
plugins: [new ResolveTypeScriptPlugin()]
},
plugins: [new ForkTsCheckerWebpackPlugin()],
devtool: 'source-map'
}).sourceMaps();

mix.ts('resources/js/app.js', 'public/js')
.sass('resources/sass/app.sass', 'public/css').sourceMaps()
.vue();

mix.extract();

现在一切正常,但我仍然不确定为什么 watch 也受到影响以及问题到底出在哪里。

关于laravel - npm run watch/hot 仅在第一次运行时成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69120779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com