andris9/encoding

Warnings with webpack.

hh9527 opened this issue ยท 17 comments

WARNING in d:/dws/projects/febs/febs/~/encoding/lib/encoding.js
Critical dependencies:
9:12-34 the request of a dependency is an expression
 @ d:/dws/projects/febs/febs/~/encoding/lib/encoding.js 9:12-34

Related code: https://github.com/andris9/encoding/blob/master/lib/encoding.js#L9

Please dont try to fool webpack, users can use webpack.IgnorePlugin to prevent loading of 'iconv'.

This module is meant for Node only, do not use it with webpack if it doesn't work

I use this module in node env. With webpack, the server side application can be packaged in one js file with only required source files included.

This behavior with require as an expression is intentional to be able to load iconv in environments where it is supported and fall back to iconv-lite where it is not. Webpack support is not a priority.

Currently, webpack is ok except the above warning.

But change these 2 lines

    var iconv_package = 'iconv';
    Iconv = require(iconv_package).Iconv;

to

    Iconv = require('iconv').Iconv;

will allow user to avoid the warning.

This change would break browserify which is more used than webpack. I'm willing to change the code to support webpack as well but only if it doesn't break anything existing.

OK, thank you.

There is a way to do that:

Iconv = require('./iconv-loader');

and

// file: iconv-loader.js
var pkg = 'iconv';
module.exports = require(pkg);

and then, user can use webpack.IgnorePlugin to ignore /\/iconv-loader$/

Updated code to load Iconv as you suggested. Released as v0.1.12

Thank you!

Won't this cause an error as soon as it's run with Node? I'm getting this:

Error: Cannot find module "./iconv-loader"

I think it's meant only for requires inside a try...catch block?

This works:

  module: {
    exprContextRegExp: /$^/,
    exprContextCritical: false,
    loaders: [/*...*/]
  }

It invalidates all require(expr) cases, i.e. this one, just as the Universe intended. Stolen from webpack/webpack#198 (comment).

IgnorePlugin didn't work for me, when I used it, webpack put in some code that throws an error. I did this instead in plugins:

new webpack.NormalModuleReplacementPlugin(//iconv-loader$/, 'node-noop')

I needed to install the node-noop npm module.

@jimpick that's much cleaner, thanks ๐Ÿ‘

Use the 'whatwg-fetch' instead of 'node-fetch'

whatwg-fetch does not work in Node, which is why isomorphic-fetch exists. This issue is probably most common when you're trying to compile your tests with webpack before running them, so you have good control over how some special stuff like CSS imports will be handled.

Eventually I realized that doing that is an overkill and I can just use require.extensions to nullify those imports.

@silvenon

Eventually I realized that doing that is an overkill and I can just use require.extensions to nullify those imports.

How?

@rublev sorry for a much too late reply, but if you're using Jest, you can configure moduleNameMapper. Otherwise you can use ignore-styles or something like that.

new webpack.NormalModuleReplacementPlugin(//iconv-loader$/, 'node-noop')

Is the double slash // before iconv-loader intended?