typescript - 模块构建失败 : Error: Could not find file: '[my-project-directory]\src\App.vue'

标签 typescript webpack vue.js

[my-project-directory] ​​是我项目的完整路径。

根据官方 Vue.js 文档,我通过“vue init webpack my-project”开始了 webpack 项目。

我正在尝试通过添加 vuetype 来切换到 TypeScript。

我已将 resources.d.ts 文件添加到我的 src 目录的根目录中,但我不确定它是如何被引用的:

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

我已经在 webpack.base.conf.js 中将条目切换为 ts:

    var path = require('path')
    var utils = require('./utils')
    var config = require('../config')
    var vueLoaderConfig = require('./vue-loader.conf')

    function resolve (dir) {
      return path.join(__dirname, '..', dir)
    }

    module.exports = {
      entry: {
        app: './src/main.ts'
      },
      output: {
        path: config.build.assetsRoot,
        filename: '[name].js',
        publicPath: process.env.NODE_ENV === 'production'
          ? config.build.assetsPublicPath
          : config.dev.assetsPublicPath
      },
      resolve: {
        extensions: ['.js', '.ts', '.vue', '.json'],
        alias: {
          '@': resolve('src')
        }
      },
      module: {
        rules: [
          {
            test: /\.vue$/,
            loader: 'vue-loader',
            options: vueLoaderConfig
          },
          {
            test: /\.js$/,
            loader: 'babel-loader',
            include: [resolve('src'), resolve('test')]
          },
          {
            test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
            loader: 'url-loader',
            options: {
              limit: 10000,
              name: utils.assetsPath('img/[name].[hash:7].[ext]')
            }
          },
          {
            test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
            loader: 'url-loader',
            options: {
              limit: 10000,
              name: utils.assetsPath('media/[name].[hash:7].[ext]')
            }
          },
          {
            test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
            loader: 'url-loader',
            options: {
              limit: 10000,
              name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
            }
          }
        ]
      }
    }

这是我的 main.ts 文件。请注意,如果我将导入更改为'./App'(没有.vue,它会给我“(2,17):错误TS2307:找不到模块'./App'。”:

import Vue from 'vue'
import App from './App.vue'

new Vue({
  el: '#app',
  render: h => h(App)
})

在 App.vue 中,我添加了 lang="ts",使用了 vuetype 库中的组件:

<template>
   <div>TEST</div>
</template>

<script lang="ts">
import Vue from 'vue'
import { Component } from 'vue-property-decorator'

@Component
export default class App extends Vue  {
  name: 'app'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

App.vue.d.ts 输出:

import Vue from 'vue';
export default class App extends Vue {
    name: 'app';
    components: {
        Hello;
        Game;
    };
}

我的 package.json:

  {
    "name": "dominant-species",
    "version": "1.0.0",
    "description": "A Game",
    "author": "Frank Rizzi",
    "private": true,
    "scripts": {
      "dev": "node build/dev-server.js",
      "start": "node build/dev-server.js",
      "build": "node build/build.js"
    },
    "dependencies": {
      "@types/core-js": "^0.9.42",
      "core-js": "^2.5.0",
      "ts-loader": "^2.3.2",
      "vue": "^2.4.2"
    },
    "devDependencies": {
      "autoprefixer": "^7.1.2",
      "babel-core": "^6.22.1",
      "babel-loader": "^7.1.1",
      "babel-plugin-transform-runtime": "^6.22.0",
      "babel-preset-env": "^1.3.2",
      "babel-preset-stage-2": "^6.22.0",
      "babel-register": "^6.22.0",
      "chalk": "^2.0.1",
      "connect-history-api-fallback": "^1.3.0",
      "copy-webpack-plugin": "^4.0.1",
      "css-loader": "^0.28.0",
      "cssnano": "^3.10.0",
      "eventsource-polyfill": "^0.9.6",
      "express": "^4.14.1",
      "extract-text-webpack-plugin": "^2.0.0",
      "file-loader": "^0.11.1",
      "friendly-errors-webpack-plugin": "^1.1.3",
      "html-webpack-plugin": "^2.28.0",
      "http-proxy-middleware": "^0.17.3",
      "opn": "^5.1.0",
      "optimize-css-assets-webpack-plugin": "^2.0.0",
      "ora": "^1.2.0",
      "rimraf": "^2.6.0",
      "semver": "^5.3.0",
      "shelljs": "^0.7.6",
      "url-loader": "^0.5.8",
      "vue-class-component": "^5.0.2",
      "vue-loader": "^12.1.0",
      "vue-property-decorator": "^5.2.1",
      "vue-style-loader": "^3.0.1",
      "vue-template-compiler": "2.4.2",
      "webpack": "^2.6.1",
      "webpack-bundle-analyzer": "^2.9.0",
      "webpack-dev-middleware": "^1.10.0",
      "webpack-hot-middleware": "^2.18.0",
      "webpack-merge": "^4.1.0"
    },
    "engines": {
      "node": ">= 4.0.0",
      "npm": ">= 3.0.0"
    },
    "browserslist": [
      "> 1%",
      "last 2 versions",
      "not ie <= 8"
    ]
  }

我的 tsconfig.json:

  {
    "compilerOptions": {
      // ... other options omitted
    "experimentalDecorators": true,
      "allowSyntheticDefaultImports": true,
      "lib": [
        "es6", "es2015.promise", "es2015.iterable", "dom","dom.iterable","es2015"
      ],
      "module": "commonjs",
      "moduleResolution": "node",
      "sourceMap": true, 
      "target": "es6",
      "noImplicitAny": false
    },
    "exclude": [
      "node_modules"
    ]
  }

这是完整的错误:

Module build failed: Error: Could not find file: '[MyDirectory]\src\App.vue'.



error  in ./src/App.vue

Module build failed: Error: Could not find file: '[my-project-directory]\
src\App.vue'.
    at getValidSourceFile ([my-user-directory]\npm\node_modules
\typescript\lib\typescript.js:89078:23)
    at Object.getEmitOutput ([my-user-directory]\npm\node_modul
es\typescript\lib\typescript.js:89448:30)
    at getEmit ([my-project-directory]\node_modules\ts-loader\dist\index.
js:114:43)
    at successLoader ([my-project-directory]\node_modules\ts-loader\dist\
index.js:34:11)
    at Object.loader ([my-project-directory]\node_modules\ts-loader\dist\
index.js:21:12)

@ ./src/App.vue 8:2-94
@ ./src/main.ts
@ multi ./build/dev-client ./src/main.ts

版本:

node -v
v6.11.2

npm -v
3.10.10

tsc -v
Version 2.4.2

最佳答案

你用过ts-loader吗?

 {
    test: /\.ts$/,
    exclude: /node_modules|vue\/src/,
    loader: 'ts-loader',
    options: {
      appendTsSuffixTo: [/\.vue$/]
    }
  }

把它放在 webpack.base.conf.js 中

关于typescript - 模块构建失败 : Error: Could not find file: '[my-project-directory]\src\App.vue' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45573106/

相关文章:

angular - 以 Angular 访问此对象时未定义

javascript - 点后仅保留两位小数( Angular )

javascript - 如何在html中使用webpack构建的js文件中声明的函数?

javascript - 使用 Vue.js 在 v-for 中设置背景图片

reactjs - 如何使用 ESLint 捕获 React 中未声明的变量?

json - 如何使用 VueJS 和 AXIOS 映射 JSON 对象

angular - 如何从我的组件内容中将 columnDefinitions 添加到 md-table

reactjs - 将 React.FunctionComponent<React.SVGProps<SVGSVGElement>> 作为 Prop 发送到另一个组件

vue.js - 使用 Axios baseURL 选项时远程服务器上的空白页面

vue.js - 当一个项目有一些变化时更新 vuex 数组的状态