Exporting JS from an inline loader that consumes a JSON file

Do you want to request a feature or report a bug?
Report a bug (though i might just need to set a property somewhere – couldn’t tell from the docs. I suspect this is related to natively supporting JSON modules now.)

What is the current behavior?
I have a loader which loads a JSON file and outputs JS code. When I try to use it with Webpack 4, I get a JSON parse error (webpack is trying to parse the outputted JS code as JSON)

ERROR in ./node_modules/config-loader!./src/config.json
Module parse failed: Unexpected token r in JSON at position 0
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token r in JSON at position 0
  at JSON.parse (<anonymous>:null:null)
  at JsonParser.parse (/Users/jback/dev/plugin-build/node_modules/webpack/lib/JsonParser.js:16:21)
  at doBuild.err (/Users/jback/dev/plugin-build/node_modules/webpack/lib/NormalModule.js:367:32)
  at runLoaders (/Users/jback/dev/plugin-build/node_modules/webpack/lib/NormalModule.js:264:12)
  at /Users/jback/dev/plugin-build/node_modules/loader-runner/lib/LoaderRunner.js:370:3
  at iterateNormalLoaders (/Users/jback/dev/plugin-build/node_modules/loader-runner/lib/LoaderRunner.js:211:10)
  at iterateNormalLoaders (/Users/jback/dev/plugin-build/node_modules/loader-runner/lib/LoaderRunner.js:218:10)
  at /Users/jback/dev/plugin-build/node_modules/loader-runner/lib/LoaderRunner.js:233:3
  at runSyncOrAsync (/Users/jback/dev/plugin-build/node_modules/loader-runner/lib/LoaderRunner.js:130:11)
  at iterateNormalLoaders (/Users/jback/dev/plugin-build/node_modules/loader-runner/lib/LoaderRunner.js:229:2)
  at Array.<anonymous> (/Users/jback/dev/plugin-build/node_modules/loader-runner/lib/LoaderRunner.js:202:4)
  at Storage.finished (/Users/jback/dev/plugin-build/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:43:16)
  at provider (/Users/jback/dev/plugin-build/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:79:9)
  at /Users/jback/dev/plugin-build/node_modules/graceful-fs/graceful-fs.js:78:16
  at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?
I can output JS code from my loader parsing a JSON file (maybe with an option saying I’m outputting JS instead of JSON)
If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.
OSX 10.13.3, Node 8.9.4, Webpack 4

Author: Fantashit

2 thoughts on “Exporting JS from an inline loader that consumes a JSON file

  1. {
       type: 'javascript/auto', // <= Set the module.type explicitly
       test: /\.json$/, // Maybe better to use a different extname (e.g /\.config\.json$/)
       use: [ { loader: 'json-to-js-loader' } ]

Comments are closed.