【问题标题】:Why webpack try to resolve unimport file?为什么 webpack 尝试解析 unimport 文件?
【发布时间】:2019-03-23 11:53:09
【问题描述】:

为什么 webpack 会尝试解析 error.ts 文件?我不会在任何地方导入这个文件

这是我的应用程序结构:

myapp/src/app.ts

import {some} from './some';

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

myapp/src/some.ts

export const some = 1;

myapp/src/error.ts

import { exist } from 'libnotexist';

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

myapp/src/tsconfig.json

{
  "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/webpack.config.js

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()]
  }
];

myapp/package.json

 "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 错误:

ERROR in [at-loader] ./src/error.ts:1:23
TS2307: Cannot find module 'libnotexist'.

任何想法为什么 webpack 会给出这个错误?我没有在任何地方包含这个文件

【问题讨论】:

  • 如果从插件中删除 new CheckerPlugin() 会发生什么?
  • @axm__ 没有变化。

标签: node.js typescript webpack


【解决方案1】:

tsconfig docs

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

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

【讨论】:

    猜你喜欢
    • 2017-10-23
    • 2017-01-12
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    相关资源
    最近更新 更多