【问题标题】:webpack 2.2.1 arranging dependencies in the wrong order / locationwebpack 2.2.1 以错误的顺序/位置排列依赖项
【发布时间】:2017-08-28 08:23:37
【问题描述】:

我有一个打字稿 .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,
    }

因为预先输入的定义在@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】:

    这个包是一个古怪的包。它被命名为 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;
      }
      

      不幸的是,上面的代码引用了@types/typeahead 无条件声明的全局Bloodhound。幸运的是,它在运行时不会是全局的。

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

    【讨论】:

      猜你喜欢
      • 2018-08-06
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多