question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

define wrapper blocks webpack2

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
dubst3pp4commented, Feb 14, 2020

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 😃

1reaction
GFdevelopcommented, Aug 18, 2020
    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"
          ]
        }
      ]
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Configure webpack using functional feature blocks. - GitHub
defineConstants () is a small convenience wrapper around webpack's DefinePlugin. It is composable and automatically encodes the values. Use it to replace ...
Read more >
@webpack-blocks/webpack2 NPM | npm.io
This is the webpack2 block providing webpack 2 core functionality. Also provides all @webpack-blocks/core exports for convenience.
Read more >
Use webpack or node.js compiler to wrap all functions and ...
It will manipulate your code before it is bundled by webpack. It may be easier just to inject whatever you want your 'wrapper'...
Read more >
@webpack-blocks/webpack | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
Dependency Management - webpack
A context module is generated. It contains references to all modules in that directory that can be required with a request matching the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found