UNPKG

@nx/react-native

Version:

The Nx Plugin for React Native contains generators for managing React Native applications and libraries within an Nx workspace. It provides: -Integration with libraries such as Jest, Detox, and Storybook. -Scaffolding for creating buildable libraries th

94 lines (93 loc) 4.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.webConfigurationGenerator = webConfigurationGenerator; const devkit_1 = require("@nx/devkit"); const has_webpack_plugin_1 = require("@nx/react/src/utils/has-webpack-plugin"); const versions_1 = require("../../utils/versions"); const normalize_schema_1 = require("./lib/normalize-schema"); const webpack_targets_1 = require("./lib/webpack-targets"); /** * This function sets web configuration for react native apps with react-native-web. * 1. install react-native-web * 2. apply webpack or vite init generator * 3. create files for webpack or vite config, index.html, assets folder, babel.config.js * @param tree * @param options */ async function webConfigurationGenerator(tree, options) { const normalizedSchema = (0, normalize_schema_1.normalizeSchema)(tree, options); const tasks = []; // install react-native-web if (!options.skipPackageJson) { const installTask = (0, devkit_1.addDependenciesToPackageJson)(tree, {}, { 'react-native-web': versions_1.reactNativeWebVersion, 'react-native-svg-web': versions_1.reacttNativeSvgWebVersion, }); tasks.push(installTask); } // apply webpack or vite init generator const bundlerTask = await addBundlerConfiguration(tree, normalizedSchema); tasks.push(bundlerTask); // create files for webpack and vite config, index.html if (normalizedSchema.bundler === 'vite') { (0, devkit_1.generateFiles)(tree, (0, devkit_1.joinPathFragments)(__dirname, './files/base-vite'), normalizedSchema.projectRoot, { ...normalizedSchema, tmpl: '' }); } else { (0, devkit_1.generateFiles)(tree, (0, devkit_1.joinPathFragments)(__dirname, './files/base-webpack'), normalizedSchema.projectRoot, { ...normalizedSchema, tmpl: '', webpackPluginOptions: (0, has_webpack_plugin_1.hasWebpackPlugin)(tree) ? (0, webpack_targets_1.createNxWebpackPluginOptions)(normalizedSchema) : null, }); } if (!options.skipPackageJson) { tasks.push((0, devkit_1.addDependenciesToPackageJson)(tree, {}, { '@types/react-dom': versions_1.typesReactDomVersion, })); } if (!options.skipFormat) { await (0, devkit_1.formatFiles)(tree); } return (0, devkit_1.runTasksInSerial)(...tasks); } /** * Add bundler configuration * - for vite, viteConfigurationGenerator contains logics to add build and serve target * - for webpack, need to explict add the build and serve target */ async function addBundlerConfiguration(tree, normalizedSchema) { if (normalizedSchema.bundler === 'vite') { const { viteConfigurationGenerator } = (0, devkit_1.ensurePackage)('@nx/vite', versions_1.nxVersion); const viteTask = await viteConfigurationGenerator(tree, { ...normalizedSchema, uiFramework: 'react', project: normalizedSchema.project, newProject: true, includeVitest: false, projectType: 'application', compiler: 'babel', skipFormat: true, }); return viteTask; } else { const { webpackInitGenerator } = (0, devkit_1.ensurePackage)('@nx/webpack', versions_1.nxVersion); const webpackInitTask = await webpackInitGenerator(tree, { ...normalizedSchema, skipFormat: true, skipPackageJson: normalizedSchema.skipPackageJson, }); if (!(0, has_webpack_plugin_1.hasWebpackPlugin)(tree)) { const projectConfiguration = (0, devkit_1.readProjectConfiguration)(tree, normalizedSchema.project); projectConfiguration.targets = { ...projectConfiguration.targets, build: (0, webpack_targets_1.createBuildTarget)(normalizedSchema), serve: (0, webpack_targets_1.createServeTarget)(normalizedSchema), }; (0, devkit_1.updateProjectConfiguration)(tree, normalizedSchema.project, projectConfiguration); } return webpackInitTask; } } exports.default = webConfigurationGenerator;