【问题标题】:Webpack and modernizr causes TypeError: document is undefined errorWebpack 和 modernizr 导致 TypeError: document is undefined 错误
【发布时间】:2014-10-07 05:49:37
【问题描述】:

我正在使用 webpack 来打包我的 javascript 文件。

我的 Webpack 配置(使用 gulp 传递给 webpack)如下所示:

    var webpackConfig = {
        context: __dirname,
        entry: {
            "app": "./js/app.js"
        },
        output: {
            path: path.join(__dirname, ".."),
            filename: "/js/[name].js",
            chunkFilename: "/js/[id].js"
        },
        plugins: [
            new webpack.ResolverPlugin(
                new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
            )
        ],
        resolve: {
            modulesDirectories: ['js', 'bower_components', 'node_modules']
        }
    };

我的app.js 是一个简单的要求:

require('modernizr/modernizr.js');

Webpack 构建文件没有任何问题,生成的文件包含modernizr。

问题是当我在页面上包含文件并对其进行测试时,modernizr 出错:

TypeError: document is undefined

docElement = document.documentElement,

来自 webpack 的捆绑文件如下所示:

/******/ (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] = {
/******/            exports: {},
/******/            id: moduleId,
/******/            loaded: false
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.loaded = 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;
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

    __webpack_require__(1);

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

    /*!
     * Modernizr v2.8.3
     * www.modernizr.com
     *
     * Copyright (c) Faruk Ates, Paul Irish, Alex Sexton
     * Available under the BSD and MIT licenses: www.modernizr.com/license/
     */

    /*
     * Modernizr tests which native CSS3 and HTML5 features are available in
     * the current UA and makes the results available to you in two ways:
     * as properties on a global Modernizr object, and as classes on the
     * <html> element. This information allows you to progressively enhance
     * your pages with a granular level of control over the experience.
     *
     * Modernizr has an optional (not included) conditional resource loader
     * called Modernizr.load(), based on Yepnope.js (yepnopejs.com).
     * To get a build that includes Modernizr.load(), as well as choosing
     * which tests to include, go to www.modernizr.com/download/
     *
     * Authors        Faruk Ates, Paul Irish, Alex Sexton
     * Contributors   Ryan Seddon, Ben Alman
     */

    window.Modernizr = (function( window, document, undefined ) {

        var version = '2.8.3',

        Modernizr = {},

        /*>>cssclasses*/
        // option for enabling the HTML classes to be added
        enableClasses = true,
        /*>>cssclasses*/

        docElement = document.documentElement,

        /** rest of modernizr code here **/

        return Modernizr;

    })(this, this.document);


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

是什么导致了这个问题?

【问题讨论】:

  • browserify + debowerify 也有同样的问题。你解决过这个问题吗?
  • @Ilkka 查看我发布的答案。

标签: modernizr webpack


【解决方案1】:

在 Webpack 2 上,我想我是通过在我的 module.rules 中使用 {test: /modernizr/, loader: 'imports-loader?this=&gt;window!exports-loader?window.Modernizr'} 让它工作的。

在此处查看此 Webpack 错误:https://github.com/webpack/webpack/issues/512#issuecomment-288143187

【讨论】:

    【解决方案2】:

    这个问题是由 Modernizr 将 this.document 传递给它创建的闭包引起的。不幸的是,webpack 将所有这些包装在它自己的另一个闭包中,导致this.document 返回null

    问题可以通过在需要时使用imports loader 设置this 来解决:

    require('imports?this=>window!modernizr/modernizr.js');
    

    【讨论】:

    • 在我的项目中,modernizr 需要很多,所以不想到处改变。我可以在加载器中添加一些东西,以便每个需要的文件默认都有this=window吗?
    【解决方案3】:

    我有同样的问题,但不直接需要 Modernizr。我需要一个包含 Modernizr 的库。较早的解决方案在这种情况下不起作用。我最终最终使用了script-loader

    require('script!package/vendor-bundle.js);
    

    【讨论】:

      【解决方案4】:

      在 Webpack 配置文件中添加了这个:

      module: {
          ...
          loaders: [
              ...
              { test: /[\\\/]modernizr dependecie path[\\\/]modernizr\.js$/,
                  loader: "imports?this=>window!exports?window.Modernizr" }
              ...
          ]
      }
      

      为此,您需要安装 imports-loaderexports-loader

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-20
        • 1970-01-01
        • 2016-04-25
        • 2012-06-05
        • 1970-01-01
        • 1970-01-01
        • 2015-10-11
        相关资源
        最近更新 更多