如何在 JavaScript 模块包中包含内联依赖字符串?

发布于 2025-01-09 09:37:17 字数 934 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2025-01-16 09:37:17

我最终自己找到了解决方案。我更改了构建过程以使用 Babel,让“tsc”仅生成类型声明。 (请参阅下面的 package.json 中的构建脚本。)

"build": "babel src --out-dir lib --extensions .ts && tsc",

我安装了 Babel 插件“babel-plugin-inline-import”和“babel-plugin-module-resolver”,其中前者导致结果是我想要的。为了编译,我使用了预设“@babel/preset-typescript”。 (请参阅下面的 babel.config.js。)

module.exports = {
    presets: ['@babel/preset-typescript'],
    plugins: [
        [
            'babel-plugin-inline-import',
            {
                extensions: ['.lua']
            }
        ]
    ],
    env: {
        test: {
            plugins: ['@babel/plugin-transform-modules-commonjs']
        }
    }
};

下面也是我更新的 tsconfig.json 供参考。

{
  "compilerOptions": {
    "target": "ES2015",
    "module": "commonjs",
    "declaration": true,
    "outDir": "lib",
    "strict": true,
    "removeComments": true,
    "skipLibCheck": true,
    "emitDeclarationOnly": true,
    "isolatedModules": true,
    "moduleResolution": "node",
  },
    "include": [
    "src/**/*"
  ]
}

配置完这些之后,我可以简单地在 TypeScript 模块中导入 Lua 文件并在“lua-in-js”中使用它。

import luaAdapter from './adapter.lua';

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.)

"build": "babel src --out-dir lib --extensions .ts && tsc",

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.)

module.exports = {
    presets: ['@babel/preset-typescript'],
    plugins: [
        [
            'babel-plugin-inline-import',
            {
                extensions: ['.lua']
            }
        ]
    ],
    env: {
        test: {
            plugins: ['@babel/plugin-transform-modules-commonjs']
        }
    }
};

Below is also my updated tsconfig.json for reference.

{
  "compilerOptions": {
    "target": "ES2015",
    "module": "commonjs",
    "declaration": true,
    "outDir": "lib",
    "strict": true,
    "removeComments": true,
    "skipLibCheck": true,
    "emitDeclarationOnly": true,
    "isolatedModules": true,
    "moduleResolution": "node",
  },
    "include": [
    "src/**/*"
  ]
}

After these were configured, I could simply import the Lua file in the TypeScript module and use it in 'lua-in-js'.

import luaAdapter from './adapter.lua';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文