错误意外的标识符“从'反应'导入React;”反应性地

时间:2019-06-07 13:53:37

标签: react-native

我试图在react native中实现预加载 模块概念。我正在关注本机文件。 Configure Preloading and Inline Requires 。我正在运行此命令“ repository.OrderBy(x=> x!= repository.First()).ThenByDescending(x=>x); ”并出现错误。

错误::

react-native start --verbose

metro.config.js

error Unexpected identifier
debug /home/iron/Documents/React-native/16-05-2019/LazyLoading/index.js:1
import React from 'react';
       ^^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:721:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/iron/Documents/React-native/16-05-2019/LazyLoading/packager/modulePaths.js:2:20)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)

index.js ::

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */

const modulePaths = require('./packager/modulePaths');
const resolve = require('path').resolve;
const fs = require('fs');

// Update the following line if the root folder of your app is somewhere else.
const ROOT_FOLDER = path.resolve(__dirname, '..');

const config = {
  transformer: {
    getTransformOptions: () => {
      const moduleMap = {};
      modulePaths.forEach(path => {
        if (fs.existsSync(path)) {
          moduleMap[resolve(path)] = true;
        }
      });
      return {
        preloadedModules: moduleMap,
        transform: { inlineRequires: { blacklist: moduleMap } },
      };
    },
  },
};
module.exports = config;

packager / modulePaths.js ::

import React from 'react';
const modules = require.getModules();
const moduleIds = Object.keys(modules);
const loadedModuleNames = moduleIds
   .filter(moduleId => modules[moduleId].isInitialized)
   .map(moduleId => modules[moduleId].verboseName);

const waitingModuleNames = moduleIds
   .filter(moduleId => !modules[moduleId].isInitialized)
   .map(moduleId => modules[moduleId].verboseName);

console.log(
   'loaded:',
   loadedModuleNames.length,
   'waiting:',
   waitingModuleNames.length
);

// grab this text blob, and put it in a file named packager/modulePaths.js
console.log(`module.exports = ${JSON.stringify(loadedModuleNames.sort())};`);
import App from './src/App';
const app = new App();

Package.json

const loadedName = require('../index');
module.exports = loadedName.loadedModuleNames;

我在上面提供了代码,请告诉我如何解决此问题。以及如何在本机中实现错误加载模块的概念。我从来没有找到任何有关此的文章。

谢谢!

0 个答案:

没有答案