首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack和现代化导致TypeError:文档未定义错误

Webpack和现代化导致TypeError:文档未定义错误
EN

Stack Overflow用户
提问于 2014-10-07 05:49:37
回答 4查看 7.8K关注 0票数 4

我用webpack来打包我的javascript文件。

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

代码语言:javascript
复制
    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是一个简单的要求:

代码语言:javascript
复制
require('modernizr/modernizr.js');

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

问题是,当我将文件包含在页面中并对其进行测试时,现代化程序出错:

代码语言:javascript
复制
TypeError: document is undefined

docElement = document.documentElement,

webpack的捆绑文件如下所示:

代码语言:javascript
复制
/******/ (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);


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

是什么导致了这个问题?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-11-13 07:45:27

这个问题是由现代派把this.document传递给它创建的闭包引起的。不幸的是,webpack把所有这些都封装在了另一个封闭的地方,导致this.document返回null

当需要时,可以通过使用this设置进口装载机来解决问题:

代码语言:javascript
复制
require('imports?this=>window!modernizr/modernizr.js');
票数 1
EN

Stack Overflow用户

发布于 2017-03-21 16:52:37

在Webpack 2上,我想我是用{test: /modernizr/, loader: 'imports-loader?this=>window!exports-loader?window.Modernizr'}在我的module.rules里工作的。

查看这个Webpack错误文件在这里:https://github.com/webpack/webpack/issues/512#issuecomment-288143187

票数 3
EN

Stack Overflow用户

发布于 2015-09-24 19:59:04

我也有同样的问题,但并不是直接要求现代化。我需要一个有现代化包装的图书馆。早期的解决方案在这种情况下不起作用。我最终使用了脚本加载器

代码语言:javascript
复制
require('script!package/vendor-bundle.js);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26229323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档