2017-05-04 148 views
3

我在最近两天尝试了这种方法,但无法按预期方式使用它: 我想构建自己的JavaScript库并将其注册到已有的现有的名称空间(“OCA” - 在这种特殊情况下)。正如你可能理解的,我不想被迫通过类型码或模块等类似安全的现代方法。在webpack中将外部配置库设置为不适用于UMD作为libraryTarget

因此,我使用webpack 2libraryTarget: umd在“OCA.Ocr”(我的库名为“Ocr”)下注册输出。这种方式就像预期的那样工作,但是当涉及到我想要使用的点时,例如underscorejs,因为它将在应用程序中全局可用,图书馆也应该交付给我,但我无法让它工作。 我跟着的WebPack配置文件和它说,的外部配置选项应该是要走的路:

externals: { // object 
    angular: "this angular", // this["angular"] 
    react: { // UMD 
     commonjs: "react", 
     commonjs2: "react", 
     amd: "react", 
     root: "React" 
    } 
    } 
    // Don't follow/bundle these modules, but request them at runtime from the environment 

我用它就像由导提出,但它不工作:

/* global __dirname, require, module*/ 

const webpack = require("webpack"); 
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin; 
const path = require("path"); 

module.exports = function (env) { 
    let target = env.target; 

    let libraryName = ["OCA", "Ocr"]; 

    let plugins = []; 
    let outputFile; 

    if (target === "production") { 
    plugins.push(new UglifyJsPlugin({ minimize: true })); 
    } 
    outputFile = "ocr[name].min.js"; 

    const config = { 
    entry: { 
     app: __dirname + "/src/app.ts", 
     personal: __dirname + "/src/personal.ts" 
    }, 
    output: { 
     path: __dirname + "/dist", 
     filename: outputFile, 
     library: libraryName, 
     libraryTarget: "umd", 
     umdNamedDefine: true 
    }, 
    module: { 
     rules: [ 
     { 
      test: /\.ts$/, 
      enforce: "pre", 
      loader: "tslint-loader", 
      options: { 
      tsConfigFile: "tsconfig.app.json", 
      } 
     }, 
     { 
      test: /\.ts?$/, 
      loader: "ts-loader", 
      exclude: /node_modules/, 
      options: { 
      configFileName: "tsconfig.app.json" 
      } 
     } 
     ], 
    }, 
    resolve: { 
     modules: [path.resolve("./src")], 
     extensions: [".ts"], 
    }, 
    externals: { 
     underscore: { // UMD 
     commonjs: "underscore", 
     commonjs2: "underscore", 
     amd: "underscore", 
     root: "_" 
     } 
    }, 
    plugins: plugins, 
    }; 

    return config; 

} 

app.ts文件,它使用下划线库(例如_.defer方法,当然这并不总是最好用的)看起来像这样:

import _ from 'underscore'; 

export class App { 

    constructor() { 
     _.defer(() => { 
      console.log('test'); 
     }); 
    } 
} 

export let $app: App = new App(); 

我把它的应用程序,还检查了,以前我的lib被浏览器加载的underscorejs库是越来越加载,但控制台输出仍指出:

TypeError: underscore_1.default is undefined

编译输出如下(也许这有点帮助):

(function webpackUniversalModuleDefinition(root, factory) { 
    if(typeof exports === 'object' && typeof module === 'object') 
     module.exports = factory(require("underscore")); 
    else if(typeof define === 'function' && define.amd) 
     define("Ocr", ["underscore"], factory); 
    else if(typeof exports === 'object') 
     exports["Ocr"] = factory(require("underscore")); 
    else 
    root["OCA"] = root["OCA"] || {}, root["OCA"]["Ocr"] =  factory(root["_"]); 
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) { 
return /******/ (function(modules) { // webpackBootstrap 
/******/ // The module cache 
/******/ var installedModules = {}; 
/******/ 
/******/ // The require function 
/******/ function __webpack_require__(moduleId) { 
/******/ 
/******/  // Check if module is in cache 
/******/  if(installedModules[moduleId]) { 
/******/   return installedModules[moduleId].exports; 
/******/  } 
/******/  // Create a new module (and put it into the cache) 
/******/  var module = installedModules[moduleId] = { 
/******/   i: moduleId, 
/******/   l: false, 
/******/   exports: {} 
/******/  }; 
/******/ 
/******/  // Execute the module function 
/******/  modules[moduleId].call(module.exports, module,  module.exports, __webpack_require__); 
/******/ 
/******/  // Flag the module as loaded 
/******/  module.l = true; 
/******/ 
/******/  // Return the exports of the module 
/******/  return module.exports; 
/******/ } 
/******/ 
/******/ 
/******/ // expose the modules object (__webpack_modules__) 
/******/ __webpack_require__.m = modules; 
/******/ 
/******/ // expose the module cache 
/******/ __webpack_require__.c = installedModules; 
/******/ 
/******/ // identity function for calling harmony imports with the correct  context 
/******/ __webpack_require__.i = function(value) { return value; }; 
/******/ 
/******/ // define getter function for harmony exports 
/******/ __webpack_require__.d = function(exports, name, getter) { 
/******/  if(!__webpack_require__.o(exports, name)) { 
/******/   Object.defineProperty(exports, name, { 
/******/    configurable: false, 
/******/    enumerable: true, 
/******/    get: getter 
/******/   }); 
/******/  } 
/******/ }; 
/******/ 
/******/ // getDefaultExport function for compatibility with non-harmony  modules 
/******/ __webpack_require__.n = function(module) { 
/******/  var getter = module && module.__esModule ? 
/******/   function getDefault() { return module['default']; } : 
/******/   function getModuleExports() { return module; }; 
/******/  __webpack_require__.d(getter, 'a', getter); 
/******/  return getter; 
/******/ }; 
/******/ 
/******/ // Object.prototype.hasOwnProperty.call 
/******/ __webpack_require__.o = function(object, property) { return  Object.prototype.hasOwnProperty.call(object, property); }; 
/******/ 
/******/ // __webpack_public_path__ 
/******/ __webpack_require__.p = ""; 
/******/ 
/******/ // Load entry module and return exports 
/******/ return __webpack_require__(__webpack_require__.s = 2); 
    /******/ }) 
/************************************************************************/ 
/******/ ([ 
/* 0 */, 
/* 1 */ 
/***/ (function(module, exports) { 

module.exports = __WEBPACK_EXTERNAL_MODULE_1__; 

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

"use strict"; 

Object.defineProperty(exports, "__esModule", { value: true }); 
var underscore_1 = __webpack_require__(1); 
var App = (function() { 
    function App() { 
     underscore_1.default.defer(function() { 
      console.log('test'); 
     }); 
    } 
    return App; 
}()); 
exports.App = App; 
exports.$app = new App(); 


/***/ }) 
/******/ ]); 
}); 

有没有人知道这是如何工作,我将不得不做的?我完全失去了,现在希望你的帮助。

btw:This也不适合我。

+0

我也问了这个在[Github上(https://github.com/webpack/webpack/issues/4824)和事实证明,这是一个错误。修复后我会进行更新。 – janis91

+0

有没有更新?我也面临与AMD相同的问题 –

回答

3

我和你有同样的问题,但是,如果你在libraryTarget选项中设置了属性var,那么变量会停止未定义。也许这将帮助你:

externals: { 
    "lodash": { 
     var:'_' 
    } 
} 
+1

保存我的一天,谢谢!但为什么?即使在官方文档中也没有关于这个“var”属性的信息:https://webpack.js.org/configuration/externals/ – H0WARD

+2

Hello @ H0WARD,正如您可以在Expose库部分(https:/ /webpack.js.org/guides/author-libraries/),有一个错误,但也是一个解决方法。 –

相关问题