为什么Webpack尝试解析未导入文件?

时间:2018-10-18 15:52:00

标签: node.js typescript webpack

为什么Webpack尝试解析error.ts文件?我没有在任何地方导入此文件

这是我的应用程序结构:

myapp / src / app.ts

Required = Required.Always

myapp / src / some.ts

import {some} from './some';

console.log('from some', some);

myapp / src / error.ts

export const some = 1;

myapp / src / tsconfig.json

import { exist } from 'libnotexist';

console.log('not exist', exist);

myapp / webpack.config.js

{
  "compilerOptions": {
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
  }
}

myapp / package.json

const path = require('path');
const {
  CheckerPlugin,
  TsConfigPathsPlugin
} = require('awesome-typescript-loader');

const configFileName = path.resolve(__dirname, './src/tsconfig.json');

module.exports = () => [
  {
    mode: 'development',
    entry: path.resolve(__dirname, './src/app.ts'),
    output: {
      path: path.resolve(__dirname, './dist')
    },
    target: 'node',
    devtool: 'source-map',
    resolve: {
      extensions: ['.ts', '.tsx', '.js', '.jsx'],
      plugins: [new TsConfigPathsPlugin({ configFileName })]
    },
    module: {
      rules: [
        {
          test: /\.tsx?$/,
          loader: 'awesome-typescript-loader',
          query: { configFileName }
        }
      ]
    },
    plugins: [new CheckerPlugin()]
  }
];

我收到了Webpack错误:

 "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack"
  },
"dependencies": {
    "awesome-typescript-loader": "^5.2.1",
    "typescript": "^3.1.3",
    "webpack": "^4.21.0"
  },
  "devDependencies": {
    "webpack-cli": "^3.1.2"
  }

任何想法为何webpack都会出现此错误?我没有在任何地方包含此文件

1 个答案:

答案 0 :(得分:2)

根据tsconfig docs

  

如果未指定“文件”和“包含”,则编译器   默认情况下将所有TypeScript(.ts,.d.ts和.tsx)文件包含在   包含的目录和子目录(排除的目录和子目录除外)   使用“排除”属性。

因此,您应该将files: []添加到tsconfig.json中,所有其他文件都将被忽略。