Skip to content

define wrapper blocks webpack2 #171

Open

Description

@bartvde

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

Activity

  1. WillieMaddox commented on Jun 22, 2017

    @WillieMaddox

    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.

  2. bartvde commented on Jun 22, 2017

    @bartvde
    ContributorAuthor

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

  3. highsource commented on Jun 27, 2017

    @highsource
    Owner

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

  4. bartvde commented on Jun 27, 2017

    @bartvde
    ContributorAuthor

    Sure I will do that @highsource

  5. bartvde commented on Jun 28, 2017

    @bartvde
    ContributorAuthor

    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

  6. bartvde commented on Jun 28, 2017

    @bartvde
    ContributorAuthor

    yeah command-line it works fine

  7. pmeller commented on Mar 7, 2018

    @pmeller

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

  8. HugoJBello commented on Nov 15, 2018

    @HugoJBello

    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?

  9. mbaer3000 commented on Nov 26, 2018

    @mbaer3000

    Looks like planetfederal@3342c01 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?

  10. bartvde commented on Nov 26, 2018

    @bartvde
    ContributorAuthor

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

  11. dubst3pp4 commented on Feb 14, 2020

    @dubst3pp4

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

  12. dubst3pp4 commented on Feb 14, 2020

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

  13. GFdevelop commented on Aug 18, 2020

    @GFdevelop
        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"
              ]
            }
          ]
        }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions