next.js - Nextjs 中的@Babylonjs (ES6) 因意外令牌“导出”而失败
问题描述
我正在使用 Nextjs 构建我的网站,并且导入 Ballyonjs 引发了以下错误。
syntaxError: Unexpected token 'export'
module.exports = require("@babylonjs/core")
我正在使用带有 tsconfig.json 的标准 nextjs 设置我指的是这个巴比伦文档并逐字使用示例。
解决方案
经过不那么微不足道的时间搜索后,我终于学到了以下内容。
@babylon (es6) 没有编译成 javascript,而是很好定义的 (es6) typescript 友好的源代码库。(在想要摇树时有帮助)
开箱即用的 Nextjs 未配置为编译 node_modules 中的任何内容。它期望预编译的 javascript 准备好使用。
第 2 点是我收到错误消息的原因,nextjs 期待已编译的 js,但它正在获取未编译的源代码。
要解决此问题,您需要添加 anext.config.js
并使用next-transpile-modules
and对其进行配置next-compose-plugins
。
yarn add next-transpile-modules
yarn add next-compose-plugins
next.config.js
//const withTM = require('next-transpile-modules')(['@babylonjs']);
const withTM = require('next-transpile-modules')(['@babylonjs/core']); // As per comment.
const withPlugins = require('next-compose-plugins');
const nextConfig = {
target: 'serverless',
webpack: function (config) {
/// below is not required for the problem described. Just for reference.(es6)
config.module.rules.push({test: /\.yml$/, use: 'raw-loader'})
return config
}
}
module.exports = withPlugins([withTM], nextConfig);
在此之后它编译没有错误。
参考 方便的链接我遇到了解决这个问题。
- https://github.com/vercel/next.js/issues/706
- https://www.npmjs.com/package/next-transpile-modules
- https://www.npmjs.com/package/next-compose-plugins
- https://www.typescriptlang.org/tsconfig
- https://doc.babylonjs.com/divingDeeper/developWithBjs/treeShaking
- https://doc.babylonjs.com/extensions/Babylon.js+ExternalLibraries/BabylonJS_and_ReactJS
帮助一些人理解问题的链接。
推荐阅读
- django - 按天过滤日期字段给出错误“用户定义的函数引发异常”
- c# - 具有 2 阶段提交的分布式事务 - 由于提交阶段不同步导致的原子性问题
- scala - 使用 cURL 命令在 scala 中发送 POST 请求以松弛
- excel - 复制多个范围并将其粘贴到不同的工作表中,在同一行和最后一个空白行中
- android - 仅在 MarshMallow (6.0 & 6.0.1) 上使用 MapActivity 的 MapView 崩溃应用程序
- php - 创建插件时,如何正确地将数据提交到我的自定义数据库?
- django - Django rest 框架处理 many=True 序列化器数据以列出
- android - 设置新的 libGDX 项目时出错(gradle 问题)
- c# - 使用 C# 和 Json.NET 根据条件序列化 JSON 对象
- service-worker - 安装 ServiceWorker 之后,SW 会不会永久存在?