New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading JSON files with bundle-loader crashes #74

Open
Ephys opened this Issue Mar 14, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@Ephys

Ephys commented Mar 14, 2018

Hello,

I'm not sure what I'm doing wrong but for some reason, if I try to lazily load JSON files using this loader, webpack interprets the generated loading code as if it were JSON too and tries to parse it.

This happens using Webpack 4.1.1

I assume it's because the name of the resource bundle-loader generates ends with .json too. (eg. ./node_modules/bundle-loader?lazy&name=file-[name]!./json-files/a.json)

I reproduced the issue using only bundle-loader and a couple of JSON files here: http://github.com/Ephys/bundle-loader-bug

Here is the webpack log of the above project: https://github.com/Ephys/bundle-loader-bug/blob/master/stacktrace.log

Thank you in advance for your help! :)

@Ephys

This comment has been minimized.

Show comment
Hide comment
@Ephys

Ephys Mar 14, 2018

If anyone else is encountering the same bug, my current workaround is to go back to using to json-loader:

module: {
  rules: [{
    test: /\.json$/i,
    type: 'javascript/auto',
    loader: 'json-loader',
  }],
},

Sadly I haven't found a way to do that for bundle-loader-loaded JSON files only (resourceQuery can be used but you'd need to flag every use of bundle-loader in your codebase)

Ephys commented Mar 14, 2018

If anyone else is encountering the same bug, my current workaround is to go back to using to json-loader:

module: {
  rules: [{
    test: /\.json$/i,
    type: 'javascript/auto',
    loader: 'json-loader',
  }],
},

Sadly I haven't found a way to do that for bundle-loader-loaded JSON files only (resourceQuery can be used but you'd need to flag every use of bundle-loader in your codebase)

@rmja

This comment has been minimized.

Show comment
Hide comment
@rmja

rmja Apr 13, 2018

I think this is related to webpack/webpack#6572, as applying webpack/webpack#6572 (comment) fixes the issue.

rmja commented Apr 13, 2018

I think this is related to webpack/webpack#6572, as applying webpack/webpack#6572 (comment) fixes the issue.

@rmja

This comment has been minimized.

Show comment
Hide comment
@rmja

rmja Jul 3, 2018

Adding the following code snippet just before return result.join(""); resolves the issue but it is not nice. I don't know how to property get the generator and parser instance:

if (this._module.type !== "javascript/auto") {
		this._module.type = "javascript/auto";
		for (var i = 0, ii = this._compilation.modules.length; i < ii; i++) {
			if (this._compilation.modules[i].type === "javascript/auto") {
				this._module.generator = this._compilation.modules[i].generator;
				this._module.parser = this._compilation.modules[i].parser;
				break;
			}
		}
	}

Is there a better api to get the instance other then searching in the other modules?

rmja commented Jul 3, 2018

Adding the following code snippet just before return result.join(""); resolves the issue but it is not nice. I don't know how to property get the generator and parser instance:

if (this._module.type !== "javascript/auto") {
		this._module.type = "javascript/auto";
		for (var i = 0, ii = this._compilation.modules.length; i < ii; i++) {
			if (this._compilation.modules[i].type === "javascript/auto") {
				this._module.generator = this._compilation.modules[i].generator;
				this._module.parser = this._compilation.modules[i].parser;
				break;
			}
		}
	}

Is there a better api to get the instance other then searching in the other modules?

rmja added a commit to rmja/bundle-loader that referenced this issue Jul 4, 2018

Support json modules
This is a proposal for a fix to webpack-contrib#74. It is not elegant but it gets the job done

rmja added a commit to rmja/bundle-loader that referenced this issue Jul 4, 2018

Support json modules
This is a proposal for a fix to webpack-contrib#74. It is not elegant but it gets the job done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment