我拼命想让FontAwesome在React-native-web中工作 可以在android上正常工作
// web/webpack.config.js
const path = require('path');
const webpack = require('webpack');
const appDirectory = path.resolve(__dirname, './../');
// Many OSS React Native packages are not compiled to ES5 before being
// published. If you depend on uncompiled packages they may cause webpack build
// errors. To fix this webpack can be configured to compile to the necessary
// 'node_module'.
const babelLoaderConfiguration = {
test: /\.js$/,
// Add every directory that needs to be compiled by Babel during the build.
include: [
path.resolve(appDirectory, './src/App'),
path.resolve(appDirectory, './src/assets'),
path.resolve(appDirectory, './src/Components'),
path.resolve(appDirectory, './src/Index'),
path.resolve(appDirectory, './src/Styles'),
path.resolve(appDirectory, './src/Screens'),
path.resolve(appDirectory, './node_modules/react-native-web'),
path.resolve(appDirectory, './node_modules/react-navigation'),
path.resolve(appDirectory, './node_modules/react-native-tab-view'),
path.resolve(appDirectory, './node_modules/react-native-paper'),
path.resolve(appDirectory, './node_modules/react-native-vector-icons'),
path.resolve(appDirectory, './node_modules/react-native-safe-area-view'),
path.resolve(appDirectory, './node_modules/@expo/samples'),
path.resolve(appDirectory, './node_modules/@expo/vector-icons'),
path.resolve(appDirectory, './node_modules/react-native-speedometer-chart'),
path.resolve(appDirectory, './node_modules/react-native-easy-toast'),
path.resolve(appDirectory, './node_modules/react-native-dialog'),
path.resolve(appDirectory, './node_modules/react-native-modal'),
path.resolve(appDirectory, './node_modules/react-native-animatable'),
path.resolve(appDirectory, './node_modules/react-native-platform-touchable'),
path.resolve(appDirectory, './assets/fonts'),
],
use: {
loader: 'babel-loader',
options: {
// cacheDirectory: false,
babelrc: false,
// Babel configuration (or use .babelrc)
// This aliases 'react-native' to 'react-native-web' and includes only
// the modules needed by the app.
plugins: [
'react-native-web',
'transform-decorators-legacy',
['transform-runtime', { helpers: false, polyfill: false, regenerator: true }],
],
// The 'react-native' preset is recommended to match React Native's packager
presets: ['react-native'],
},
},
};
// This is needed for loading css
const cssLoaderConfiguration = {
test: /\.css$/,
use: ['style-loader', 'css-loader'],
};
const imageLoaderConfiguration = {
test: /\.(gif|jpe?g|png|svg)$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
};
const ttfLoaderConfiguration = {
test: /\.ttf$/,
use: [
{
loader: 'file-loader',
options: {
name: './fonts/[hash].[ext]',
},
},
],
include: [
path.resolve(appDirectory, './src/assets/fonts'),
path.resolve(appDirectory, 'node_modules/react-native-vector-icons'),
path.resolve(appDirectory, './assets'),
],
};
module.exports = {
// your web-specific entry file
entry: ['babel-polyfill', path.resolve(appDirectory, './src/Index/index.web.js')],
devtool: 'eval',
// configures where the build ends up
output: {
filename: 'bundle.js',
publicPath: '/assets/',
path: path.resolve(appDirectory, './public/assets'),
},
module: {
rules: [
babelLoaderConfiguration,
cssLoaderConfiguration,
imageLoaderConfiguration,
ttfLoaderConfiguration,
],
},
plugins: [
// process.env.NODE_ENV === 'production' must be true for production
// builds to eliminate development checks and reduce build size. You may
// wish to include additional optimizations.
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
__DEV__: process.env.NODE_ENV === 'production' || true,
}),
],
resolve: {
// If you're working on a multi-platform React Native app, web-specific
// module implementations should be written in files using the extension
// '.web.js'.
symlinks: false,
extensions: ['.web.js', '.js'],
alias: {
'./assets/images/expo-icon.png': './assets/images/expo-icon@2x.png',
'./assets/images/slack-icon.png': './assets/images/slack-icon@2x.png',
'@expo/vector-icons': 'expo-web',
'react-native': 'react-native-web',
},
},
};
fontawesome.ttf位于。\ Projectname \ assets \ fonts \ fontawesome.ttf“
<Text style={{ fontFamily: 'fontawesome', fontSize: 20 }}></Text>
<Text style={{ fontFamily: 'fontawesome', fontSize: 20 }}></Text>
我没有使用第三方库。我通过rnpm(Package.json rnpm / assets)导入了字体,请参见此处font-awesome-in-react-native-project-without-using-any-third-party
谢谢您的帮助!