2017-05-04 56 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/),有一個錯誤,但也是一個解決方法。 –

相關問題