简体   繁体   English

如何将Webpack与未导出的js一起使用

[英]How to use webpack with js that isn't exported

I'm trying to figure out how to include this javascript with webpack but I'm struggling. 我试图弄清楚如何在webpack中包含这个javascript,但我很挣扎。 I'm installing an angular module called angular-colorthief via npm. 我正在通过npm安装一个名为angular-colorthief的角度模块。 I've installed it and included the module in my webpack config file: 我已经安装了它,并将模块包含在我的webpack配置文件中:

if(TEST) {
        config.entry = {};
    } else {
        config.entry = {
            app: './client/app/app.js',
            polyfills: './client/polyfills.js',
            vendor: [
                'angular',
                'angular-colorthief',
                'angular-animate',
                'angular-aria',
                'angular-cookies',
                'angular-resource',
                'angular-route',
                'angular-sanitize',
                'angular-socket-io',
                'angular-material',
                'lodash'

            ]
        };
    }

The problem is that inside of the angular-colorthief directory is another js which the angular module depends on which is called color-thief.js. 问题在于,angular-colorthief目录内部还有另一个js,Angular模块依赖于此js,称为color-thief.js。 This file does not export anything. 此文件不导出任何内容。 The angular-colorthief module requires it this way: angular-colorthief模块需要这种方式:

use strict';

require("./color-thief");

angular.module('ngColorThief', [])
  .provider('$colorThief', [function () {

When I run my app I keep getting an error that ColorThief is undefined because although this script is being included in my vendor bundle, it's not available when the angular module runs. 当我运行我的应用程序时,我总是收到错误消息,即ColorThief未定义,因为尽管此脚本已包含在我的供应商捆绑包中,但当angular模块运行时它不可用。 Can anyone help me to fix this? 谁能帮我解决这个问题? I've tried installing the exports-loader module but I'm unsure how to use it to solve this problem. 我试过安装exports-loader模块,但是不确定如何使用它来解决此问题。 This is the loader I tried to add but this didn't work. 这是我尝试添加的装载程序,但是没有用。

 {
          include: require.resolve("./color-thief"),
          loader: "exports?ColorThief!node_modules/angular-colorthief/color-thief.js"
        }

So I'm not certain if this is the correct solution but it solved my problem. 所以我不确定这是否是正确的解决方案,但它解决了我的问题。 If someone could tell me a better way I would definitely appreciate that. 如果有人可以告诉我更好的方法,那我一定会很感激的。 For anyone else, here is what I ended up adding to my loaders. 对于其他人,这就是我最终添加到装载机中的内容。

{
          include: require.resolve(path.resolve(__dirname, 'node_modules/angular-colorthief/color-thief.js')),
          loader: "exports?ColorThief"
        }, {
          include: require.resolve(path.resolve(__dirname, 'node_modules/angular-colorthief/angular-colorthief.js')),
          loader: "imports?ColorThief=./color-thief.js"
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM