本文介绍了我们可以在 React Native 应用程序中使用 nodejs 代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 React Native 项目中使用 node js 作为后端.

I want to use node js as a backend in react native project .

推荐答案

是的,您可以使用 ReactNativify 为 Node 编写的包作为 Big Rich 正确地声明.一些需要考虑的事情:

Yes you can use packages written for Node by using ReactNativify as Big Rich rightfully states.Some things to consider though:

1) 我按照问题列表中的建议将 transformer.js 分成两部分:

1) I followed the advice I found in the issue list and split up transformer.js in 2 parts:

transformers.js(在 /config 中并从 rn-cli.config.js 调用):

transformers.js (in /config and invoked from rn-cli.config.js):

const babelTransformer = require('./babel-transformer');

module.exports.transform = function(src, filename, options) {

    const extension = String(filename.slice(filename.lastIndexOf('.')));
    let result;

    try {

    result = babelTransformer(src, filename);

    } catch (e) {

    throw new Error(e);
    return;
    }

    return {
    ast: result.ast,
    code: result.code,
    map: result.map,
    filename
    };
};

babel-transformer.js(也在 /config 中):

'use strict'

const babel = require('babel-core');

/**
 * This is your `.babelrc` equivalent.
 */
const babelRC = {
    presets: ['react-native'],
    plugins: [

    // The following plugin will rewrite imports. Reimplementations of node
    // libraries such as `assert`, `buffer`, etc. will be picked up
    // automatically by the React Native packager.  All other built-in node
    // libraries get rewritten to their browserify counterpart.

    [require('babel-plugin-rewrite-require'), {
        aliases: {
            constants: 'constants-browserify',
            crypto: 'react-native-crypto',
            dns: 'mock/dns',
            domain: 'domain-browser',
            fs: 'mock/empty',
            http: 'stream-http',
            https: 'https-browserify',
            net: 'mock/net',
            os: 'os-browserify/browser',
            path: 'path-browserify',
            pbkdf2: 'react-native-pbkdf2-shim',
            process: 'process/browser',
            querystring: 'querystring-es3',
            stream: 'stream-browserify',
            _stream_duplex: 'readable-stream/duplex',
            _stream_passthrough: 'readable-stream/passthrough',
            _stream_readable: 'readable-stream/readable',
            _stream_transform: 'readable-stream/transform',
            _stream_writable: 'readable-stream/writable',
            sys: 'util',
            timers: 'timers-browserify',
            tls: 'mock/tls',
            tty: 'tty-browserify',
            vm: 'vm-browserify',
            zlib: 'browserify-zlib'
        },
        throwForNonStringLiteral: true
    }],

    // Instead of the above you could also do the rewriting like this:

    ["module-resolver", {
      "alias": {
        "mock": "./config/mock",
        "sodium-universal": "libsodium"
      }
    }]
    ]
};

module.exports = (src, filename) => {

    const babelConfig = Object.assign({}, babelRC, {
    filename,
    sourceFileName: filename
    });

    const result = babel.transform(src, babelConfig);
    return {
    ast: result.ast,
    code: result.code,
    map: result.map,
    filename
    };
}

2) 正如你在上面的代码中看到的,我还使用 babel-plugin-module-resolver 进行了演示.

2) As you can see in the code above, I also demonstrated using the babel-plugin-module-resolver.

注意,我将使用这个插件而不是 ReactNative 使用的插件.它允许您引用本地文件,并用适当的引号写入允许不符合 JS 的名称,例如sodium-universal"

Note, I will use this plugin instead of the one ReactNative uses. It allows you to reference local files, and written with proper quotes allows non-JS-compliant names like 'sodium-universal'

Note2 或者按照此评论中的概述使用 .babelrc 解决方案(可能是最干净的):https://github.com/philikon/ReactNativify/issues/4#issuecomment-312136794

Note2 Or go for .babelrc solution (maybe cleanest) as outlined in this comment: https://github.com/philikon/ReactNativify/issues/4#issuecomment-312136794

3) 我发现我的项目根目录中仍然需要一个 .babelrc 来让我的 Jest 测试工作.有关详细信息,请参阅此问题:https://github.com/philikon/ReactNativify/issues/8

3) I found that I still needed a .babelrc in the root of my project to make my Jest tests work. See this issue for details: https://github.com/philikon/ReactNativify/issues/8

这篇关于我们可以在 React Native 应用程序中使用 nodejs 代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 07:03
查看更多