如何在 JavaScript 模块包中包含内联依赖字符串?
我有一个 TypeScript 模块,它使用外部 .lua 文件中的 Lua 代码来执行一些适配器魔法。我将此 Lua 代码字符串化到 JavaScript 中,然后使用“lua-in-js”包来执行它。我使用“fs”模块通过 fs.readFileSync
读取文件,然后像这样对结果进行字符串化。 (请参阅下面的 module.ts。)
const luaCode = fs.readFileSync('./adapter.lua').toString()
然而,挑战在于这个 TypeScript 模块在 React Native 中使用,其中不支持“fs”包。我使用“tsc”来编译模块包,但在编译的代码中,包含“fs”模块并将其用作依赖项。 (请参阅下面的bundle.js。)
const fs_1 = require("fs");
const luaCode = (0, fs_1.readFileSync)('./adapter.lua').toString();
当尝试运行代码时,它会崩溃,因为 RN 不支持“fs”。因此 Lua 代码必须完整地包含在捆绑文件中的字符串变量 luaCode
内。我怎样才能实现这个目标?下面也是我的 tsconfig.json 供参考。
{
"compilerOptions": {
"target": "ES2015",
"module": "commonjs",
"declaration": true,
"outDir": "lib",
"strict": true,
"removeComments": true,
"skipLibCheck": true,
"isolatedModules": true
},
"include": [
"src/**/*"
]
}
I have a TypeScript module that uses Lua code from an external .lua file to do some adapter magic. I include this Lua code stringified in the JavaScript and then use 'lua-in-js' package to execute it. I use the 'fs' module to read the file with fs.readFileSync
and then I stringify the result like this. (See module.ts below.)
const luaCode = fs.readFileSync('./adapter.lua').toString()
However, the challenge is that this TypeScript module is used in React Native, where the 'fs' package is not supported. I use 'tsc' to compile the module bundle, but in the compiled code, the 'fs' module is included and used as a dependency. (See bundle.js below.)
const fs_1 = require("fs");
const luaCode = (0, fs_1.readFileSync)('./adapter.lua').toString();
When trying to run the code it crashes because 'fs' is not supported in RN. So the Lua code would have to be included in the bundle file in its entirety inside the string variable luaCode
. How can I achieve this? Below is also my tsconfig.json
for reference.
{
"compilerOptions": {
"target": "ES2015",
"module": "commonjs",
"declaration": true,
"outDir": "lib",
"strict": true,
"removeComments": true,
"skipLibCheck": true,
"isolatedModules": true
},
"include": [
"src/**/*"
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终自己找到了解决方案。我更改了构建过程以使用 Babel,让“tsc”仅生成类型声明。 (请参阅下面的
package.json
中的构建脚本。)我安装了 Babel 插件“babel-plugin-inline-import”和“babel-plugin-module-resolver”,其中前者导致结果是我想要的。为了编译,我使用了预设“@babel/preset-typescript”。 (请参阅下面的 babel.config.js。)
下面也是我更新的 tsconfig.json 供参考。
配置完这些之后,我可以简单地在 TypeScript 模块中导入 Lua 文件并在“lua-in-js”中使用它。
I found the solution eventually by myself. I changed the build process to use Babel, leaving 'tsc' to only generate type declarations. (See build script in
package.json
below.)I installed the Babel plugin 'babel-plugin-inline-import' and 'babel-plugin-module-resolver', of which the former lead to the result what I wanted. For compiling I used the preset '@babel/preset-typescript'. (See
babel.config.js
below.)Below is also my updated
tsconfig.json
for reference.After these were configured, I could simply import the Lua file in the TypeScript module and use it in 'lua-in-js'.