jsonix icon indicating copy to clipboard operation
jsonix copied to clipboard

define wrapper blocks webpack2

Open bartvde opened this issue 8 years ago • 13 comments
trafficstars

The current define wrapper in JSONIX blocks usage in webpack2, I wonder if it's time to get rid of this? selection_273

        /*if (typeof define !== 'function') {^M
                // Load the define function via amdefine^M
                var define = require('amdefine')(module);^M
                // If we're not in browser^M
                if (typeof window === 'undefined')^M
                {^M
                        // Require xmldom, xmlhttprequest and fs^M
                        define(["xmldom", "xmlhttprequest", "fs"], _jsonix_factory);^M
                }^M
                else^M
                {^M
                        // We're probably in browser, maybe browserify^M
                        // Do not require xmldom, xmlhttprequest as they'r provided by the browser^M
                        // Do not require fs since file system is not available anyway^M
                        define([], _jsonix_factory);^M
                }^M
        }^M

bartvde avatar May 22 '17 14:05 bartvde

I'm getting the error as well,

Uncaught Error: define cannot be used indirect
    at webpackJsonp.219.module.exports (amd-define.js:2)
    at Object.<anonymous> (jsonix.js:6103)
    at Object.118 (jsonix.js:6129)
    at __webpack_require__ (bootstrap f29fa90…:1336)
    at fn (bootstrap f29fa90…:754)
    at Object.117 (WFS110Context.js:7)
    at __webpack_require__ (bootstrap f29fa90…:1336)
    at fn (bootstrap f29fa90…:754)
    at Object.230 (SMIL_2_0.js:251)
    at __webpack_require__ (bootstrap f29fa90…:1336)

I thought webpack was supposed to play nicely with this kind of thing.

WillieMaddox avatar Jun 22 '17 05:06 WillieMaddox

@WillieMaddox I made some fixes in a fork https://github.com/boundlessgeo/jsonix/commits/master and published as @boundlessgeo/jsonix for now, until we get the mainstream version fixed

Let me know if it works for you or not.

bartvde avatar Jun 22 '17 07:06 bartvde

Could you please PR a reproducing project? Under jsonix/nodejs/tests/webpack2 analog to browserify.

highsource avatar Jun 27 '17 20:06 highsource

Sure I will do that @highsource

bartvde avatar Jun 27 '17 20:06 bartvde

Actually I wonder if we can get the error reproduced by using the command-line tool, since the error only occurs in the browser with the webpack dev server

bartvde avatar Jun 28 '17 17:06 bartvde

yeah command-line it works fine

bartvde avatar Jun 28 '17 17:06 bartvde

I can confim that problem still exists for webpack 3.10 and webpack-dev-server 2.11.1.

pmeller avatar Mar 07 '18 10:03 pmeller

Hi, I see that this was apparently fixed. I see that there has not been any build in npm. Could you publish it in npm please?

HugoJBello avatar Nov 15 '18 16:11 HugoJBello

Looks like https://github.com/boundlessgeo/jsonix/commit/3342c011779261a860488b1a692fa09910cd273e was never PRed and/or merged here. So it looks to me like the issue is actually not fixed.

@bartvde, if it works for you, then best PR your fix here?

mbaer3000 avatar Nov 26 '18 11:11 mbaer3000

Right IIRC the issue was that I could not create a test case that failed

bartvde avatar Nov 26 '18 11:11 bartvde

Hi, any news or workarounds for this? I'm using webpack 4.x and the error still occurs.

dubst3pp4 avatar Feb 14 '20 10:02 dubst3pp4

Okay, I could get a workaround for webpack as follows:

Install imports-loader and exports-loader and add the following rule to webpack.config.js:

    module : {
        rules: [
            {
                test: require.resolve("jsonix"),
                use: ['imports-loader?require=>false', 'exports-loader?Jsonix']
            },
        ]
    }

You can then import Jsonix like so:

import Jsonix from 'jsonix';

The webpack-rule does the following:

  • it triggers when jsonix is loaded
  • for the jsonix module it sets require to false (so jsonix thinks it runs in the browser)
  • it exports the global variable Jsonix, so that it can be used in ES6 import statements

@highsource the code that checks in which context Jsonix is running should also check for the ES6 import statement, which is the mostly used way to import modules in webpack. So it would happily run with webpack as well as with native ES6 imports. Thanks for that great library :-)

dubst3pp4 avatar Feb 14 '20 12:02 dubst3pp4

    module : {
        rules: [
            {
                test: require.resolve("jsonix"),
                use: ['imports-loader?require=>false', 'exports-loader?Jsonix']
            },
        ]
    }

On new versions of imports/exports-loader (I'm using v1.1.0) the equivalent syntax is:

    module : {
      rules: [
        {
          test: require.resolve("jsonix"),
          use:[
            "imports-loader?type=commonjs&additionalCode=var%20require%20=%20null",
            "exports-loader?type=commonjs&exports=single|Jsonix"
          ]
        }
      ]
    }

GFdevelop avatar Aug 18 '20 13:08 GFdevelop