简体   繁体   English

使用 Metro 捆绑器将特定的 javascript 文件作为字符串导入 react-native 中

[英]Import specific javascript files as string in react-native with metro bundler

  • I need to import specific javascript files into a component.我需要将特定的 javascript 文件导入组件。
  • The content of the javascript is code but needs to be transformed into a string javascript的内容是代码但需要转成字符串
  • I cannot transform it in the runtime我无法在运行时转换它
  • The content of the file needs to be injected into a web view so it handles objects like window , so currently it throws an error on import when the code gets interpreted文件的内容需要注入到 web 视图中,因此它可以处理window之类的对象,因此当前在解释代码时会在导入时引发错误

Thank you for your help!谢谢您的帮助!

In my case I needed to write a custom transformer.就我而言,我需要编写一个自定义转换器。

const babelTransformer = require('metro-react-native-babel-transformer');

module.exports.transform = ({ src, filename, options }) => {
    return babelTransformer.transform({
      src: `let code = ${JSON.stringify(src)}; export default code;`,
      filename,
      options,
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM