2

当我这样做时require("!!raw-loader!themes/builtin_themes/default/layout.json"),我收到以下错误:

Module parse failed: Unexpected token m in JSON at position 0 while parsing near 'module.exports = "{\...'
File was processed with these loaders:
* ../node_modules/raw-loader/index.js
You may need an additional loader to handle the result of these loaders.
SyntaxError: Unexpected token m in JSON at position 0 while parsing near 'module.exports = "{\...'
     at JSON.parse (<anonymous>)
     at parseJson (/Users/frederikcreemers/dev/project/node_modules/json-parse-better-errors/index.js:7:17)
     at JsonParser.parse (/Users/frederikcreemers/dev/project/node_modules/webpack/lib/JsonParser.js:16:16)
     at /Users/frederikcreemers/dev/project/node_modules/webpack/lib/NormalModule.js:482:32
     at /Users/frederikcreemers/dev/project/node_modules/webpack/lib/NormalModule.js:358:12
[w-renderer]     at /Users/frederikcreemers/dev/project/node_modules/loader-runner/lib/LoaderRunner.js:373:3
     at iterateNormalLoaders (/Users/frederikcreemers/dev/project/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
     at iterateNormalLoaders (/Users/frederikcreemers/dev/project/node_modules/loader-runner/lib/LoaderRunner.js:221:10)
     at /Users/frederikcreemers/dev/project/node_modules/loader-runner/lib/LoaderRunner.js:236:3
     at runSyncOrAsync (/Users/frederikcreemers/dev/project/node_modules/loader-runner/lib/LoaderRunner.js:130:11)
     at iterateNormalLoaders (/Users/frederikcreemers/dev/project/node_modules/loader-runner/lib/LoaderRunner.js:232:2)
     at Array.<anonymous> (/Users/frederikcreemers/dev/project/node_modules/loader-runner/lib/LoaderRunner.js:205:4)
     at Storage.finished (/Users/frederikcreemers/dev/project/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:43:16)
     at /Users/frederikcreemers/dev/project/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:79:9
     at /Users/frederikcreemers/dev/project/node_modules/graceful-fs/graceful-fs.js:90:16
     at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:61:3)

我们确实有这样配置的 json 加载器:

{ 
test: /\.json$/, 
  use: [{
    loader: "json-loader",
    options: { context: "." }
  }]
}

但我的理解是“!!” 前面将禁用任何已配置的加载程序。

如果可能的话,我想单独保留 webpack 配置,只需更改 require 语句以使其工作。

4

1 回答 1

0

像这样更改导入文件的格式

raw-loader!./my-json-file.txt

使用txt或任何其他后缀 - 对我有用

于 2021-12-08T11:29:57.363 回答