modernizr-loader icon indicating copy to clipboard operation
modernizr-loader copied to clipboard

Still getting "ReferenceError: document [/window] is not defined"

Open sengeezer opened this issue 8 years ago • 11 comments

I'd love to use this module, but it fails on build (es6/babel/webpack/yarn):

Error: ./.modernizrrc
Module build failed: ReferenceError: document is not defined
    at /Users/fabioa/Development/projects/mfsjs/.modernizrrc:4:1876
    at Object.<anonymous> (/Users/fabioa/Development/projects/mfsjs/.modernizrrc:6:3)
    at Module._compile (module.js:571:32)
    at Object.exec (/Users/fabioa/Development/projects/mfsjs/node_modules/webpack-core/lib/NormalModuleMixin.js:88:7)
    at Object.module.exports (/Users/fabioa/Development/projects/mfsjs/node_modules/modernizr-loader/index.js:20:26)

I took a look at merge request #25 but even after implementing it I still got the same error.

Any ideas on how to fix this? Should I be including jsdom somewhere?

If I include #25 and

modulesDirectories: [
      'node_modules',
      'bower_components',
    ],

I get

Error: ./.modernizrrc
Module build failed: ReferenceError: window is not defined
    at Object.<anonymous> (/Users/fabioa/Development/projects/mfsjs/.modernizrrc:10:4)
    at Module._compile (module.js:571:32)
    at Object.exec (/Users/fabioa/Development/projects/mfsjs/node_modules/webpack-core/lib/NormalModuleMixin.js:88:7)
    at Object.module.exports (/Users/fabioa/Development/projects/mfsjs/node_modules/modernizr-loader/index.js:24:26)

sengeezer avatar Dec 30 '16 11:12 sengeezer

Hey, thanks for reporting this issue. Could you please provide us with your webpack configuration?

flootr avatar Jan 03 '17 07:01 flootr

Sure. I've removed the bower line as I'm not using bower at this time.

import path from 'path';

export default {
  output: {
    filename: 'client-bundle.js',
  },
  devtool: 'source-map',
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: [/node_modules/],
      },
      { test: /\.json$/, loader: 'json' },
      { test: /\.modernizrrc$/, loader: 'modernizr' },
      {
        test: /\.modernizrrc(\.json)?$/,
        loader: 'modernizr!json',
      },
    ],
  },
  resolve: {
    extensions: ['', '.js', '.jsx'],
    modulesDirectories: [
      'node_modules',
    ],
    alias: {
      modernizr$: path.resolve(__dirname, './.modernizrrc'),
    },
  },
};

sengeezer avatar Jan 03 '17 10:01 sengeezer

I am having a similar issue with window not being defined. I didn't use to have this problem.

mjamesderocher avatar Jan 10 '17 21:01 mjamesderocher

Ok, I've played around some more and have changed my webpack config to this:

import path from 'path';

export default {
  output: {
    library: 'MF',
    libraryTarget: 'umd',
    filename: 'client-bundle.js',
  },
  devtool: 'source-map',
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: [/node_modules/],
      },
      { test: /modernizr-config$/, loader: 'modernizr' },
      {
        test: /modernizr-config(\.json)?$/,
        loader: 'modernizr!json',
      },
    ],
  },
  resolve: {
    extensions: ['', '.js', '.jsx', '.json'],
    modulesDirectories: [
      'node_modules',
    ],
    alias: {
      modernizr$: path.resolve(__dirname, './vendor/modernizr-config'),
    },
  },
};

I can now have if(Modernizr.input.placeholder) evaluate to true, for example. Does that mean it's working? This may seem like a silly question, but I may have gone codeblind on this topic.

sengeezer avatar Jan 11 '17 19:01 sengeezer

Does that mean it's working?

Yes and no. The Modernizr test for input.placeholder does not rely on the global window object itself. I'm working on a fix!

flootr avatar Jan 12 '17 07:01 flootr

Could you please test if 1.0.1 is working for you @sengeezer?

flootr avatar Jan 12 '17 08:01 flootr

Ok, I updated to the new version and added test/elem/picture to the rc file. Upon running, I get the css classes in the body tag and the test for Modernizr.picture passes.

As far as I can tell, picture does use window. If this does mean that it's working now, I'd like to point out that formatting of the rc file, which in my case is an output from modernizr's json generator, appears to be critical.

sengeezer avatar Jan 12 '17 17:01 sengeezer

Hello,

I confirm that window is not accessible within the tests, making stuff like touchevents test broken.

E.g.

define(['Modernizr', 'prefixes', 'testStyles'], function(Modernizr, prefixes, testStyles) {
  // Chrome (desktop) used to lie about its support on this, but that has since been rectified: http://crbug.com/36415
  Modernizr.addTest('touchevents', function() {

    console.warn('DEBUG');
    console.log(window);
    console.log(document);

    var bool;
    if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
      bool = true;
    } else {
      // include the 'heartz' as a way to have a non matching MQ to help terminate the join
      // https://git.io/vznFH
      var query = ['@media (', prefixes.join('touch-enabled),('), 'heartz', ')', '{#modernizr{top:9px;position:absolute}}'].join('');
      testStyles(query, function(node) {
        bool = node.offsetTop === 9;
      });
    }
    return bool;
  });
});

window return an object with Modernizr inside only.

ghost avatar Jan 20 '17 10:01 ghost

We ran into this issue as well. v1.0.0 was giving us an empty object instead of window. Upgrading to v1.0.1 of modernizr-loader fixes the issue. However, the repo is still showing v1.0.0 as being the latest release. Can you update it to show v1.0.1 as latest release, @flootr ?

Thanks!

cthorner avatar Jun 01 '17 00:06 cthorner

@cthorner Can you tell me where you see/don't see this? 1.0.1 should be the latest release both on GitHub and npm.

flootr avatar Jun 01 '17 06:06 flootr

When I go to releases in github, it shows "Latest Release" button next to the 1.0.0 release. I also see 1.0.1, but it is not marked as "Latest Release". Does that make sense? Another way to get there is to go to the latest url: https://github.com/peerigon/modernizr-loader/releases/latest

Right now it is redirecting to 1.0.0.

cthorner avatar Jun 30 '17 20:06 cthorner