将 proj4js 与 webpack 捆绑在一起会导致 "Unexpected token m in JSON at position 0"

Bundling proj4js with webpack leads to "Unexpected token m in JSON at position 0"

我尝试将 proj4js (2.5.0) 作为 ES6 模块包含在 webpack (4.27.1) 项目中。

import proj4 from 'proj4';

导致此错误:

ERROR in ./node_modules/proj4/package.json
Module parse failed: Unexpected token m in JSON at position 0 while parsing near 'module.exports = __w...'
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token m in JSON at position 0 while parsing near 'module.exports = __w...'

我该怎么做才能绕过这个错误?


查看 proj4js github 页面上的问题报告,看起来 json-loader 执行了两次,因为 proj4js 明确需要 package.json:

proj4js/lib/version.js:

export {version as default} from '../package.json';

问题是由我们的 webpack-config 中的加载器引起的:

{
     test: /\.(png|gif|jpg|jpeg|svg|xml|json)$/,
     use: ["url-loader"]
}

我从这个加载器中排除了 proj4,它可以工作。