Skip to content

Commit 23b59f6

Browse files
committed
Fix dependency resolution and asset bundling
1 parent f4a9534 commit 23b59f6

12 files changed

Lines changed: 226 additions & 39 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ example/ios/Pods
4848
# Ruby
4949
example/vendor/
5050

51+
# Example Assets - copied by postinstall script
52+
example/ios/assets
53+
example/android/app/src/main/assets
54+
5155
# node.js
5256
#
5357
node_modules/

D3Chart.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Pod::Spec.new do |s|
1515
s.source = { :git => "https://github.com/mitigate-dev/react-native-d3-chart.git", :tag => "#{s.version}" }
1616

1717
s.source_files = "ios/**/*.{h,m,mm,swift}"
18+
s.resources = ["ios/assets/*"]
1819

1920
# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
2021
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.

android/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ android {
4747
sourceSets {
4848
main {
4949
manifest.srcFile "src/main/AndroidManifestNew.xml"
50+
assets.srcDirs = ["src/main/assets"]
51+
}
52+
}
53+
} else {
54+
sourceSets {
55+
main {
56+
assets.srcDirs = ["src/main/assets"]
5057
}
5158
}
5259
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dev.mitigate.d3chart;
2+
3+
import com.facebook.react.ReactPackage;
4+
import com.facebook.react.bridge.NativeModule;
5+
import com.facebook.react.bridge.ReactApplicationContext;
6+
import com.facebook.react.uimanager.ViewManager;
7+
8+
import java.util.Collections;
9+
import java.util.List;
10+
11+
public class D3ChartPackage implements ReactPackage {
12+
@Override
13+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
14+
return Collections.emptyList();
15+
}
16+
17+
@Override
18+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
19+
return Collections.emptyList();
20+
}
21+
}

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
"dependencies": {
1313
"react": "19.0.0",
14-
"react-native": "0.78.0"
14+
"react-native": "0.78.0",
15+
"react-native-webview": "^13.16.0"
1516
},
1617
"devDependencies": {
1718
"@babel/core": "^7.25.2",

example/react-native.config.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

example/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useMemo, useState } from 'react';
22
import { View, StyleSheet, TouchableOpacity, Text } from 'react-native';
3-
import Chart, { ChartProps, Dataset } from '../../src/index';
3+
import Chart, { ChartProps, Dataset } from 'react-native-d3-chart';
44

55
type TimeDomainType = 'hour' | 'day' | 'week' | 'month';
66

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"lib",
2323
"android",
2424
"ios",
25+
"scripts",
2526
"cpp",
2627
"*.podspec",
2728
"react-native.config.js",
@@ -37,12 +38,15 @@
3738
"!**/.*"
3839
],
3940
"scripts": {
41+
"android": "FORCE_COPY=1 yarn copy-assets && cd example && yarn android",
4042
"example": "yarn workspace react-native-d3-chart-example",
4143
"test": "jest",
4244
"typecheck": "tsc",
4345
"lint": "eslint \"**/*.{js,ts,tsx}\"",
4446
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
4547
"prepare": "bob build",
48+
"copy-assets": "node scripts/copy-assets.js",
49+
"postinstall": "yarn copy-assets",
4650
"release": "release-it"
4751
},
4852
"keywords": [

react-native.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
dependency: {
3+
platforms: {
4+
android: {
5+
sourceDir: '../android',
6+
packageImportPath: 'dev.mitigate.d3chart.D3ChartPackage',
7+
},
8+
ios: {
9+
podspecPath: '../D3Chart.podspec',
10+
},
11+
},
12+
assets: ['android/src/main/assets', 'ios/assets'], // Note: relative to library root
13+
},
14+
};

scripts/copy-assets.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
// This script copies assets from react-native-d3-chart to the React Native app
7+
// It runs automatically when the library is installed via npm/yarn
8+
9+
function copyAssets() {
10+
try {
11+
// Check if we're in node_modules (installed as dependency)
12+
const isInNodeModules = __dirname.includes('node_modules');
13+
14+
if (!isInNodeModules && !process.env.FORCE_COPY) {
15+
// We're in development mode, skip copying unless forced
16+
console.log(
17+
'react-native-d3-chart: Skipping asset copy in development mode (set FORCE_COPY=1 to override)'
18+
);
19+
return;
20+
}
21+
22+
// Find the React Native app root
23+
let appRoot;
24+
if (isInNodeModules) {
25+
// In production: should be 2 levels up from node_modules/react-native-d3-chart
26+
appRoot = path.resolve(__dirname, '../../..');
27+
} else {
28+
// In development: use the example directory as app root
29+
appRoot = path.resolve(__dirname, '../example');
30+
}
31+
32+
// Asset source locations in the library
33+
const androidAssetsSource = path.join(
34+
__dirname,
35+
'../android/src/main/assets'
36+
);
37+
const iosAssetsSource = path.join(__dirname, '../ios/assets');
38+
39+
// Asset destination locations in the consuming app
40+
const androidAssetsTarget = path.join(
41+
appRoot,
42+
'android/app/src/main/assets/react-native-d3-chart'
43+
);
44+
45+
// Copy Android assets
46+
if (fs.existsSync(androidAssetsSource)) {
47+
try {
48+
// Ensure target directory exists
49+
fs.mkdirSync(androidAssetsTarget, { recursive: true });
50+
51+
// Copy each file
52+
const files = fs.readdirSync(androidAssetsSource);
53+
files.forEach((file) => {
54+
const sourcePath = path.join(androidAssetsSource, file);
55+
const targetPath = path.join(androidAssetsTarget, file);
56+
57+
// Only copy files (not directories)
58+
if (fs.lstatSync(sourcePath).isFile()) {
59+
fs.copyFileSync(sourcePath, targetPath);
60+
console.log(
61+
`react-native-d3-chart: Copied ${file} to Android assets`
62+
);
63+
}
64+
});
65+
} catch (error) {
66+
console.warn(
67+
'react-native-d3-chart: Failed to copy Android assets:',
68+
error.message
69+
);
70+
}
71+
} else {
72+
console.warn(
73+
'react-native-d3-chart: Android assets not found at:',
74+
androidAssetsSource
75+
);
76+
}
77+
78+
// Copy iOS assets
79+
if (fs.existsSync(iosAssetsSource)) {
80+
try {
81+
const iosDir = path.join(appRoot, 'ios');
82+
if (fs.existsSync(iosDir)) {
83+
// iOS assets should go to ios/assets directory
84+
const iosAssetsTargetDir = path.join(iosDir, 'assets');
85+
86+
try {
87+
// Ensure target directory exists
88+
fs.mkdirSync(iosAssetsTargetDir, { recursive: true });
89+
90+
// Copy each file
91+
const files = fs.readdirSync(iosAssetsSource);
92+
files.forEach((file) => {
93+
const sourcePath = path.join(iosAssetsSource, file);
94+
const targetPath = path.join(iosAssetsTargetDir, file);
95+
96+
// Only copy files (not directories or .DS_Store)
97+
if (
98+
fs.lstatSync(sourcePath).isFile() &&
99+
!file.startsWith('.DS_Store')
100+
) {
101+
fs.copyFileSync(sourcePath, targetPath);
102+
console.log(
103+
`react-native-d3-chart: Copied ${file} to iOS assets`
104+
);
105+
}
106+
});
107+
} catch (error) {
108+
console.warn(
109+
'react-native-d3-chart: Failed to copy iOS assets:',
110+
error.message
111+
);
112+
}
113+
} else {
114+
console.log(
115+
'react-native-d3-chart: iOS directory not found, skipping iOS assets'
116+
);
117+
}
118+
} catch (error) {
119+
console.warn(
120+
'react-native-d3-chart: Failed to access iOS directory:',
121+
error.message
122+
);
123+
}
124+
} else {
125+
console.warn(
126+
'react-native-d3-chart: iOS assets not found at:',
127+
iosAssetsSource
128+
);
129+
}
130+
131+
console.log('react-native-d3-chart: Assets copied successfully');
132+
} catch (error) {
133+
console.warn(
134+
'react-native-d3-chart: Failed to copy assets:',
135+
error.message
136+
);
137+
}
138+
}
139+
140+
copyAssets();

0 commit comments

Comments
 (0)