Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
add support to display svg for react-native
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Jul 1, 2021
1 parent bddcd09 commit 6ada11c
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 25 deletions.
14 changes: 14 additions & 0 deletions packages/react-native/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"description": "Add react-native-codegen to dev dependencies",
"factory": "./src/migrations/update-12-4-0/add-react-native-codegen-dev-dependencies"
},
"add-react-native-svg": {
"version": "12.4.0-beta.0",
"cli": "nx",
"description": "Add support for svg",
"factory": "./src/migrations/update-12-4-0/add-react-native-svg"
},
"update-jest-for-react-native": {
"version": "12.4.0-beta.0",
"cli": "nx",
Expand Down Expand Up @@ -155,6 +161,14 @@
"@testing-library/react-native": {
"version": "8.0.0-rc.0",
"alwaysAddToPackageJson": false
},
"react-native-svg-transformer": {
"version": "0.14.3",
"alwaysAddToPackageJson": false
},
"react-native-svg": {
"version": "12.1.1",
"alwaysAddToPackageJson": false
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
const { withNxMetro } = require('@nrwl/react-native');
const { getDefaultConfig } = require('metro-config');

module.exports = withNxMetro(
{
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
module.exports = (async () => {
const {
resolver: { sourceExts, assetExts },
} = await getDefaultConfig();
return withNxMetro(
{
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
babelTransformerPath: require.resolve('react-native-svg-transformer'),
},
resolver: {
assetExts: assetExts.filter((ext) => ext !== 'svg'),
sourceExts: [...sourceExts, 'svg'],
},
},
},
{
// Change this to true to see debugging info.
// Useful if you have issues resolving modules
debug: false,
// all the file extensions used for imports other than 'ts', 'tsx', 'js', 'jsx'
extensions: [],
}
);
{
// Change this to true to see debugging info.
// Useful if you have issues resolving modules
debug: false,
// all the file extensions used for imports other than 'ts', 'tsx', 'js', 'jsx'
extensions: [],
}
);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"version": "0.0.1",
"private": true,
"dependencies": {
"react-native": "*",
"react": "*",
"@testing-library/jest-native": "*",
"@testing-library/react-native": "*",
"@testing-library/jest-native": "*"
"react": "*",
"react-native": "*",
"react-native-svg": "*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from 'react-native/Libraries/NewAppScreen';
// @ts-ignore
import openURLInBrowser from 'react-native/Libraries/Core/Devtools/openURLInBrowser';
import Star from './star.svg';

const App = () => {
return (
Expand Down Expand Up @@ -64,6 +65,23 @@ const App = () => {
about Nx.
</Text>
</TouchableOpacity>

<Text style={styles.sectionDescription}>
Thank you for using and showing some ♥ for Nx. If you like Nx
React Native, please give it a star:
</Text>

<View style={styles.githubStarContainer}>
<TouchableOpacity
style={styles.githubStarBadge}
onPress={() =>
openURLInBrowser('https://github.com/nrwl/nx-react-native')
}
>
<Star width={24} height={24} fill={Colors.dark} />
<Text> Star</Text>
</TouchableOpacity>
</View>
</View>
</View>
</ScrollView>
Expand Down Expand Up @@ -125,6 +143,23 @@ const styles = StyleSheet.create({
link: {
color: '#45bc98',
},
githubStarContainer: {
display: 'flex',
flexDirection: 'row',
justifyContent: 'center',
padding: 10,
},
githubStarBadge: {
borderWidth: 1,
borderColor: Colors.dark,
borderRadius: 3,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 10,
paddingVertical: 3,
fontWeight: '600',
},
});

export default App;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"skipLibCheck": true,
"resolveJsonModule": true
},
"files": [],
"files": [
"<%= offsetFromRoot %>node_modules/@nrwl/react-native/typings/image.d.ts"
],
"include": [],
"references": [
{
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
reactNativeCommunityCli,
reactNativeCommunityCliAndroid,
reactNativeCommunityCliIos,
reactNativeSvgTransformerVersion,
reactNativeSvgVersion,
reactNativeVersion,
reactTestRendererVersion,
reactVersion,
Expand Down Expand Up @@ -74,6 +76,8 @@ export function updateDependencies(host: Tree) {
'metro-resolver': metroVersion,
'react-native-codegen': reactNativeCodegenVersion,
'react-test-renderer': reactTestRendererVersion,
'react-native-svg-transformer': reactNativeSvgTransformerVersion,
'react-native-svg': reactNativeSvgVersion,
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ describe('Add react-native-codegen to dev dependencies 12.4.0', () => {
tree = createTreeWithEmptyWorkspace();
});

it(`should update libs`, async () => {
it(`should update libs if dependencies contain react-native`, async () => {
tree.write(
'package.json',
JSON.stringify({
dependencies: {},
dependencies: { 'react-native': '*' },
devDependencies: {},
})
);
Expand All @@ -25,4 +25,20 @@ describe('Add react-native-codegen to dev dependencies 12.4.0', () => {
readJson(tree, 'package.json').devDependencies['react-native-codegen']
).toEqual(reactNativeCodegenVersion);
});

it(`should not update libs if dependencies do not contain react-native`, async () => {
tree.write(
'package.json',
JSON.stringify({
dependencies: {},
devDependencies: {},
})
);

await update(tree);

expect(
readJson(tree, 'package.json').devDependencies['react-native-codegen']
).toEqual(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { addDependenciesToPackageJson, Tree, formatFiles } from '@nrwl/devkit';
import {
addDependenciesToPackageJson,
Tree,
formatFiles,
readJson,
} from '@nrwl/devkit';
import { reactNativeCodegenVersion } from '../../utils/versions';

/**
* Add react-native-codegen due to react-native upgrade.
* https://react-native-community.github.io/upgrade-helper/?from=0.64.1&to=0.65.0-rc.2
*/
export default async function update(tree: Tree) {
const packagesJson = readJson(tree, 'package.json');
const packages = Object.keys({
...packagesJson.dependencies,
...packagesJson.devDependencies,
});

if (!packages.includes('react-native')) {
return;
}

const installTask = addDependenciesToPackageJson(
tree,
{},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { readJson, Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Linter } from '@nrwl/linter';
import { addProject } from '../../generators/application/lib/add-project';
import { normalizeOptions } from '../../generators/application/lib/normalize-options';
import {
reactNativeSvgTransformerVersion,
reactNativeSvgVersion,
} from '../../utils/versions';
import update from './add-react-native-svg';

describe('Add react-native-svg to dev dependencies 12.4.0', () => {
let tree: Tree;

beforeEach(async () => {
tree = createTreeWithEmptyWorkspace();
});

it(`should update libs if dependencies contain react-native`, async () => {
tree.write(
'package.json',
JSON.stringify({
dependencies: { 'react-native': '*' },
devDependencies: {},
})
);

await update(tree);
const devDependencies = readJson(tree, 'package.json').devDependencies;

expect(devDependencies['react-native-svg']).toEqual(reactNativeSvgVersion);
expect(devDependencies['react-native-svg-transformer']).toEqual(
reactNativeSvgTransformerVersion
);
});

it(`should not update libs if dependencies do not contain react-native`, async () => {
tree.write(
'package.json',
JSON.stringify({
dependencies: {},
devDependencies: {},
})
);

await update(tree);
const devDependencies = readJson(tree, 'package.json').devDependencies;

expect(devDependencies['react-native-svg']).toEqual(undefined);
expect(devDependencies['react-native-svg-transformer']).toEqual(undefined);
});

it(`should add react-native-svg app tsconfig.json and package.json`, async () => {
tree.write(
'package.json',
JSON.stringify({
dependencies: { 'react-native': '*' },
devDependencies: {},
})
);
addProject(
tree,
normalizeOptions(tree, {
name: 'myApp',
displayName: 'myApp',
linter: Linter.EsLint,
e2eTestRunner: 'none',
})
);
tree.write('apps/my-app/tsconfig.json', '{}');
tree.write(
'apps/my-app/package.json',
JSON.stringify({
dependencies: {},
})
);

await update(tree);

const tsconfig = readJson(tree, 'apps/my-app/tsconfig.json');
expect(tsconfig.files).toEqual([
'../../node_modules/@nrwl/react-native/typings/image.d.ts',
]);

const packageJson = readJson(tree, 'apps/my-app/package.json');
expect(packageJson.dependencies).toEqual({ 'react-native-svg': '*' });
});

it(`should not add react-native-svg app tsconfig.json and package.json if files do not exist`, async () => {
tree.write(
'package.json',
JSON.stringify({
dependencies: { 'react-native': '*' },
devDependencies: {},
})
);
addProject(
tree,
normalizeOptions(tree, {
name: 'myApp',
displayName: 'myApp',
linter: Linter.EsLint,
e2eTestRunner: 'none',
})
);

await update(tree);

expect(() => readJson(tree, 'apps/my-app/tsconfig.json')).toThrow();
expect(() => readJson(tree, 'apps/my-app/package.json')).toThrow();
});
});
Loading

0 comments on commit 6ada11c

Please sign in to comment.