Files
react-native-web/scripts/babel/createModuleMap.js
Nicolas Gallagher a53372ceb3 [fix] babel-plugin only rewrites paths for known modules
Don't rewrite import paths for non-existent modules or types. They will
attempt to be imported from the package's main export. This change
currently requires a module map to be generated for the babel-plugin to
use. The map is automatically regenerated for any commit that alters the
entry file of react-native-web.

Fix #822
2018-02-19 13:05:42 -08:00

25 lines
893 B
JavaScript

/**
* Creates a map of exported modules, allowing the RNW babel plugin to rewrite
* paths only for modules it knows are exported by RNW.
*/
const fs = require('fs');
const path = require('path');
const isDirectory = source => fs.lstatSync(source).isDirectory();
const getDirectories = source =>
fs.readdirSync(source).filter(name => isDirectory(path.join(source, name)));
const packagesDir = path.join(__dirname, '../../packages/');
const exportsDir = path.join(packagesDir, 'react-native-web/src/exports');
const moduleMapOutfile = path.join(__dirname, 'babel-plugin-react-native-web/src/moduleMap.js');
const moduleMap = getDirectories(exportsDir).reduce((acc, curr) => {
acc[curr] = true;
return acc;
}, {});
const data = `// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
module.exports = ${JSON.stringify(moduleMap, null, 2)}`;
fs.writeFileSync(moduleMapOutfile, data);