highsource / jsonix

Powerful XML<->JSON JavaScript mapping library.
BSD 2-Clause "Simplified" License
361 stars 78 forks source link

define wrapper blocks webpack2 #171

Open bartvde opened 7 years ago

bartvde commented 7 years ago

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
WillieMaddox commented 7 years ago

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.

bartvde commented 7 years ago

@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.

highsource commented 7 years ago

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

bartvde commented 7 years ago

Sure I will do that @highsource

bartvde commented 7 years ago

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 commented 7 years ago

yeah command-line it works fine

pmeller commented 6 years ago

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

HugoJBello commented 6 years ago

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?

mbaer3000 commented 5 years ago

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?

bartvde commented 5 years ago

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

dubst3pp4 commented 4 years ago

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

dubst3pp4 commented 4 years ago

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:

@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 :-)

GFdevelop commented 4 years ago
    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"
          ]
        }
      ]
    }