【问题标题】:Webpack Karma cannot resolve local importWebpack Karma 无法解析本地导入
【发布时间】:2015-09-05 01:22:24
【问题描述】:

我将 Webpack 用于应用程序和测试(使用 https://github.com/webpack/karma-webpack)。该应用在 typescript 中,在 Babel 中进行测试。

在测试中从独立模块导入一些东西是可行的(使用import { cleanHTML, fromHTML, toHTML } from "../../app/utils/text.ts";,即我需要提及.ts 扩展名,否则它会失败)。

当我实际尝试导入一个在另一个文件中导入组件的 React 组件时,我收到以下错误: Module not found: Error: Cannot resolve 'file' or 'directory' ./blocks/paragraph.

目录树如下:

src/
  app/
    components/
      blocks/
        paragraph.ts
    main.ts
  tests/
    components/
       main_tests.js
    utils/

main.ts 像这样导入paragraph.ts import { ParagraphBlockComponent } from "./blocks/paragraph";

正常编译有效,但测试无效。 这是业力配置:

var path = require('path');

module.exports = function (config) {

config.set({
    basePath: 'src',
    singleRun: true,
    frameworks: ['mocha', 'chai'],
    reporters: ['dots'],
    browsers: ['Firefox'],
    files: [
        'tests/index.js'
    ],
    preprocessors: {
        'tests/index.js': ['webpack']
    },
    webpack: {
        noInfo: true,
        module: {
            loaders: [
                {
                    test: /\.ts$/,
                    loaders: ['awesome-typescript-loader']
                },
                {
                    test: /\_tests.js$/,
                    loaders: ['babel-loader']
                }
            ]
        }
    },
    webpackMiddleware: {
        noInfo: true,
        stats: {
            color: true,
            chunkModules: false,
            modules: false
        }
    }
});

};

我错过了什么吗?

【问题讨论】:

    标签: karma-runner webpack


    【解决方案1】:

    将以下内容添加到 karma webpack 配置中为我修复了它

    resolve: {
        extensions: ['', '.js', '.ts']
    },
    

    【讨论】:

      猜你喜欢
      • 2018-03-14
      • 1970-01-01
      • 2020-02-21
      • 2018-08-05
      • 1970-01-01
      • 2020-07-06
      • 2021-03-25
      • 1970-01-01
      • 2016-03-15
      相关资源
      最近更新 更多