webpack 2.2.1以错误的顺序/位置安排依赖关系

问题描述 投票:0回答:1

我有一个打字稿.ts文件与以下内容。 我正在使用webpack版本2.2.1

import { module } from "angular";
import "typeahead";

class TypeAheadController {
    static $inject = ["$log", "$timeout", "$http", "$interpolate"];
    constructor(
        public $log: ng.ILogService,
        public $timeout: ng.ITimeoutService,
        public $http: ng.IHttpService,
        public $interpolate: ng.IInterpolateService) {

        // do stuff with Typeahead / Bloodhound.
        var bloodhoundSuggestions = new Bloodhound({
            datumTokenizer: _ => Bloodhound.tokenizers.obj.whitespace(_[this.inputValue]),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
    }

因为typeahead定义在@types/typeahead中,并且实现在typeahead.js中,所以必须在webpack.config.js中对别名进行别名

globule = require("globule");

var configuration = {
    context: __dirname,
    resolve:
    {
        alias: {
            typeahead: "typeahead.js"
        }
    },
    entry: [
        ...globule.find("client/*.ts", { srcBase: "wwwroot/js/admin" })
    ],
    output: {
        filename: "./wwwroot/js/admin/admin.js"
    },
    module: {
        rules: [
            { test: /\.ts$/, use: 'ts-loader' }
        ]
    }
};

console.log(configuration);
module.exports = configuration;

不幸的是,在生成的javascript文件中,Bloodhound未定义。

Webpack似乎包含并使用相关的require(323),但它显然不起作用,因为Bloodhound未定义。

在输出文件中,require仅在定义控制器之前出现。

Object.defineProperty(exports, "__esModule", { value: true });    
var angular_1 = __webpack_require__(16);    
__webpack_require__(323);    
/**    
 * initialise and use a type ahead to find suggestions.    
 */    
var TypeAheadController = (function () {

在文件的下方,我找到323。

/***/ }),
/* 323 */
/***/ (function(module, exports, __webpack_require__) {

/* WEBPACK VAR INJECTION */(function(setImmediate) {var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/*!
 * typeahead.js 0.11.1
 * https://github.com/twitter/typeahead.js

如何修复未定义的Bloodhound?

javascript typescript webpack webpack-2
1个回答
2
投票

这个包是一个古怪的包。它被命名为typeahead.js,但是package.json中的"main"条目实际上导出了Bloodhound函数并将typeahead函数附加到jQuery.fn。更糟糕的是,它的@types软件包名称错误(缺少.js),并使用声明格式编写,希望您从"bloodhound"导入。这很痛苦,但可以解决。

以下是您需要采取的步骤:

  1. 用npm安装typeahead.js(因为你使用的是Webpack) npm install --save typeahead.js
  2. 安装@types包(注意没有.js,这很烦人) npm install --save @types/typeahead
  3. 删除不需要的别名。具体来说,必须从webpack.config.js中删除以下行: typeahead: "typeahead.js"
  4. 创建一个声明文件ambience.d.ts(名称不重要) declare module "typeahead.js" { export = Bloodhound; } 不幸的是,上面的代码是指Bloodhound无条件宣布的全球@types/typeahead。幸运的是,它在运行时不会是全局的。
  5. 调整您的代码,使其大致如下 import angular from "angular"; import Bloodhound from "typeahead.js"; // note the `.js` class TypeAheadController { static $inject = ["$log", "$timeout", "$http", "$interpolate"]; constructor( readonly $log: angular.ILogService, readonly $timeout: angular.ITimeoutService, readonly $http: angular.IHttpService, readonly $interpolate: angular.IInterpolateService) { // do stuff with Typeahead / Bloodhound. const bloodhoundSuggestions = new Bloodhound({ datumTokenizer: _ => Bloodhound.tokenizers.obj .whitespace(_[this.inputValue]), queryTokenizer: Bloodhound.tokenizers.whitespace }); } }
© www.soinside.com 2019 - 2024. All rights reserved.