React Native Web error with react-native-vector-icons - react-native

I am making an app with react-native and react-native-web. I have tried to add react-native-vector-icons to the project follow this documentation and i got an error on the web build:
ERROR in ./node_modules/react-native-vector-icons/lib/create-icon-set.js 91:8
Module parse failed: Unexpected token (91:8)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| return (
> <Text selectable={false} {...props}>
| {glyph}
| {children}
# ./node_modules/react-native-vector-icons/FontAwesome.js 6:0-50 9:16-29
# ./src/App.js 1:1549-1597
# ./index.web.js 1:261-281
Here is my webpack.config.js:
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const appDirectory = path.resolve(__dirname, '../')
const babelLoaderConfiguration = {
test: /\.(js)|(jsx)$/,
include: [
path.resolve(appDirectory, 'index.web.js'),
path.resolve(appDirectory, 'src'),
path.resolve(appDirectory, 'node_modules/react-native-uncompiled'),
],
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: ['module:metro-react-native-babel-preset'],
plugins: [
'react-native-web',
[
'module-resolver',
{
alias: {
'^react-native$': 'react-native-web',
},
},
],
],
},
},
}
const HtmlWebpackPluginConfig = {
filename: 'index.html',
template: path.resolve(appDirectory, 'index.html'),
}
const copyWebpackPluginConfig = {
patterns: [
{
from: path.resolve(appDirectory, 'assets/fonts/'),
to: path.resolve(appDirectory, 'public/assets/fonts'),
noErrorOnMissing: true,
},
{
from: path.resolve(appDirectory, 'assets/images/'),
to: path.resolve(appDirectory, 'public/assets/images'),
noErrorOnMissing: true,
},
],
}
const imageLoaderConfiguration = {
test: /\.(gif|jpe?g|png|svg)$/,
use: {
loader: 'url-loader',
options: {
name: '[name].[ext]',
esModule: false,
},
},
}
const iconFontLoaderConfiguration = {
test: /\.ttf$/,
loader: 'url-loader', // or directly file-loader
include: path.resolve(appDirectory, 'node_modules/react-native-vector-icons'),
}
module.exports = {
entry: [path.resolve(appDirectory, 'index.web.js')],
output: {
filename: 'bundle.[contenthash].web.js',
path: path.resolve(appDirectory, 'public'),
},
module: {
rules: [
babelLoaderConfiguration,
imageLoaderConfiguration,
iconFontLoaderConfiguration,
],
},
devServer: {
host: '0.0.0.0',
},
plugins: [
new HtmlWebpackPlugin(HtmlWebpackPluginConfig),
new CopyWebpackPlugin(copyWebpackPluginConfig),
],
resolve: {
alias: {
'react-native$': 'react-native-web',
'#api': path.resolve(appDirectory, 'src/api/'),
'#entities': path.resolve(appDirectory, 'src/entities/'),
'#utils': path.resolve(appDirectory, 'src/utils/'),
'#components': path.resolve(appDirectory, 'src/components/'),
'#theme': path.resolve(appDirectory, 'src/theme/'),
'#constants': path.resolve(appDirectory, 'src/constants/'),
'#screens': path.resolve(appDirectory, 'src/screens'),
},
extensions: ['.web.js', '.js', '.jsx'],
},
}
I also have tried to change url-loader to file-loader and ttf-loader an i got the same error

In the webpack.config.js file paste the below code
module: {
{
test: /\.ttf$/,
loader: 'url-loader', // or directly file-loader
include: path.resolve(
__dirname,
'node_modules/react-native-vector-icons',
),
},
},

I've just faced exactly the same trouble, I've solved by adding these two rules in the webpack.config.js file
...
module: {
rules: [
...
{
test: /\.js$/,
exclude: /node_modules\/(?!(react-native-elements|react-native-vector-icons)\/).*/,
loader: 'babel-loader'
},
{
test: /\.ttf$/,
loader: 'url-loader',
include: path.resolve(__dirname, "node_modules/react-native-vector-icons")
}
]
}
...

Related

Debugging in Chrome Sources tab showing webpack folder with multiple version of vue files

Something happened recently as this was working fine. I am unable to debug vue files in the sources tab like I used to do.
Currently it shows different versions of each vue file, and all of them are compressed, and they don't show the file. Also extra folders like lang sync^\ that I did not created.
I tried to add a workspace in Filesystem Tab, it did not help.
I looked in Chrome, and Firefox and both have the same problem.
This is the webpack code.
module.exports = env => {
return {
mode: 'development',
entry: {
main: ['babel-polyfill', './src/js/main.js']
},
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.scss$/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: [
/node_modules/
]
},
{
test: /\.(png|jpg|gif)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
outputPath: (url, resourcePath, context) => {
if (url.indexOf('logo') !== -1) {
return `./brand/${env}/${url}`;
}
return `./src/assets/cards/${url}`;
}
}
},
{
test: /\.(svg)$/,
loader: 'html-loader', // since svg is an inline image
options: {
name: '[name].[ext]',
outputPath: './src/assets'
}
},
{
test: /\.ico$/,
loader: 'file-loader',
options: {
name: '[name].ico',
outputPath: '/'
}
},
{
test: /\.(woff|woff2|eot|ttf)(\?v=[0-9]\.[0-9]\.[0-9])?$/, // todo include svg
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: './fonts'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
'scss': path.resolve(__dirname, './src/scss'),
'$assets': path.resolve(__dirname, './src/assets'),
'$brand': path.resolve(__dirname, './brand')
},
modules: [
'node_modules',
path.resolve(__dirname),
path.resolve(__dirname, 'src/assets')
],
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true,
allowedHosts: Object.values(BRANDS),
port: PORT,
publicPath: '/dist/',
before: function (app) {
console.log('listening on ' + Object.values(BRANDS).join(' and ') + ' on port ' + PORT);
}
},
performance: {
hints: false
},
devtool: '#inline-source-map',
plugins: [
new VueLoaderPlugin(),
new webpack.DefinePlugin(processEnv),
new HtmlWebpackPlugin({ //Generates an HTML file for your application by injecting automatically all your generated bundles.
title: '...',
template: 'template.html',
filename: 'index.html',
hash: true,
meta: {
viewport: 'width=device-width,initial-scale=1,user-scalable=no'
}
}),
],
}
};
Try to add this to vue.config.js
module.exports = {
//...
configureWebpack: config => {
if (process.env.NODE_ENV === 'development') {
config.devtool = 'eval-source-map';
config.output.devtoolModuleFilenameTemplate = info =>
info.resourcePath.match(/\.vue$/) && !info.identifier.match(/type=script/)
? `webpack-generated:///${info.resourcePath}?${info.hash}`
: `webpack-yourCode:///${info.resourcePath}`;
config.output.devtoolFallbackModuleFilenameTemplate = 'webpack:///[resource-path]?[hash]';
}
},
// ...
}
Read more

Webpack slow build for Bootstrap 5

When i import Bootstrap scss in my project, Webpack build is very slow. It is building all Bootstrap dependencies on every code changes. So i have to wait 5-6 sec every file savings. There is any way improve this build time? Maybe build bootsrap only when bootsrap-variables.scss file changed?
Here is my webpack configuration:
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const sass = require('sass');
const path = require('path');
module.exports = (env, argv) => {
return {
entry: {
bundle: ['./src/index.js', './src/index.scss'],
admin: ['./src/admin.js', './src/admin.scss'],
print: './src/print.scss',
},
output: {
path: path.resolve(process.cwd(), 'dist'),
filename: '[name].js',
},
optimization: {
minimizer: [
new TerserJSPlugin(),
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
map: {
inline: false,
annotation: true,
},
},
}),
],
},
plugins: [
new CleanWebpackPlugin({
cleanAfterEveryBuildPatterns: ['!fonts/**/*'],
output: {
path: path.resolve(process.cwd(), 'dist'),
},
}),
],
devtool:
argv.mode === 'development'
? 'cheap-module-source-map'
: 'source-map',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
plugins: [
'#babel/plugin-proposal-class-properties',
],
presets: ['#babel/preset-env'],
},
},
},
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
sourceMap: true,
},
},
'resolve-url-loader',
{
loader: 'sass-loader',
options: {
implementation: sass,
},
},
{
loader: 'sass-resources-loader',
options: {
resources: ['./src/scss/_variables.scss'],
},
},
],
},
{
test: /\.(ttf|eot|woff|woff2|svg)$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts',
publicPath: 'fonts',
},
},
exclude: /images/,
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
publicPath: 'img',
outputPath: 'img',
},
},
],
},
],
},
externals: {
jquery: 'jQuery',
},
};
};

Dist folder is not created after building vue js project

I tried to run the npm run build command on my vue project. It is successfully running but no dist folder is created. I want to build this project and run it on the server. I am currently working on Windows OS.
I tried to compare it with other project's webpack file but found no difference.
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
],
},
{
test: /\.scss$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader'
],
},
{
test: /\.sass$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse
mode, we map
// the "scss" and "sass" values for the lang attribute to the
right configs here.
// other preprocessors should work out of the box, no loader
config like this necessary.
'scss': [
'vue-style-loader',
'css-loader',
'sass-loader'
],
'sass': [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
]
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
This is my webpack config file. Please help me with this.
Try to check is there a folder created in where the webpack config file located.If there's not , it seems something wrong with your other configs, you can try this out, it works fine for me in project:
output: {
path: path.resolve(__dirname, '../dist'),
filename: 'build.js',
publicPath: './static'
}

How to include node module for Babel using Webpack

when running
npm run build
I encounter an es6 related syntax error from uglify, so I'm guessing babel isn't handling the node module (sec-to-min) properly.
My .babelrc
{
"presets": ["es2015", "stage-0"],
"plugins": ["transform-runtime"],
"comments": false,
"env": {
"test": {
"plugins": [ "istanbul" ]
}
}
}
My Webpack config:
var path = require('path')
var config = require('../config')
var utils = require('./utils')
var projectRoot = path.resolve(__dirname, '../')
var env = process.env.NODE_ENV
// check env & config/index.js to decide whether to enable CSS source maps for the
// various preprocessor loaders added to vue-loader at the end of this file
var cssSourceMapDev = (env === 'development' && config.dev.cssSourceMap)
var cssSourceMapProd = (env === 'production' && config.build.productionSourceMap)
var useCssSourceMap = cssSourceMapDev || cssSourceMapProd
module.exports = {
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
filename: '[name].js'
},
resolve: {
extensions: ['', '.js', '.vue', '.json'],
fallback: [path.join(__dirname, '../node_modules')],
alias: {
'vue$': 'vue/dist/vue.common.js',
'src': path.resolve(__dirname, '../src'),
'assets': path.resolve(__dirname, '../src/assets'),
'components': path.resolve(__dirname, '../src/components')
}
},
resolveLoader: {
fallback: [path.join(__dirname, '../node_modules')]
},
module: {
preLoaders: [
{
test: /\.vue$/,
loader: 'eslint',
include: [
path.join(projectRoot, 'src')
],
exclude: /node_modules/
},
{
test: /\.js$/,
loader: 'eslint',
include: [
path.join(projectRoot, 'src')
],
exclude: /node_modules/
}
],
loaders: [
{
test: /\.vue$/,
loader: 'vue'
},
{
test: /\.js$/,
loader: 'babel',
include: [
path.join(projectRoot, 'src'),
'node_modules/sec-to-min'
],
exclude: /node_modules/
},
{
test: /\.json$/,
loader: 'json'
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url',
query: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url',
query: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
eslint: {
formatter: require('eslint-friendly-formatter')
},
vue: {
loaders: utils.cssLoaders({ sourceMap: useCssSourceMap }),
postcss: [
require('autoprefixer')({
browsers: ['last 2 versions']
})
]
}
}
& the ERR:
ERROR in static/js/vendor.8d64852626f0513309d9.js from UglifyJs
SyntaxError: Unexpected token: operator (>)
[./~/sec-to-min/index.js:3,0]
How can I direct babel to compile this module?
please note that on Windows the slashes in the path will be \ so the above solution would have to be changed to exclude: /node_modules\\(?!(sec-to-min)\/).*/
This was the solution that worked for me, with webpack 4.3 and babel-loader 8.0.5, and using the recommended #babel/preset-env, adapted from here https://github.com/webpack/webpack/issues/2031#issuecomment-283517150
{
test: /\.(js|jsx|mjs)$/,
include: [paths.appSrc, paths.appNodeModules],
exclude: function(modulePath) {
return (
/node_modules/.test(modulePath) &&
!/node_modules\\react-dev-utils/.test(modulePath)
);
},
loader: require.resolve('babel-loader'),
options: {
compact: true,
presets: ['#babel/preset-env']
}
},
I found it helpful to use the function for exclude as I was able to add console logs within the function to check which modules were being matched by the regex.
In babel section of webpack config change to this :
{
test: /\.js$/,
loader: 'babel',
include: [
path.join(projectRoot, 'src')
],
exclude: /node_modules\/(?!(sec-to-min)\/).*/
}
Looks like exclude has priority over include.
Install following packages
npm install babel-preset-es2015 --save-dev
npm install babel-preset-stage-0 --save-dev
Add es2015 and stage-0 in your babel presets
{
"presets": ["es2015", "stage-0"],
}
UPDATE
Try this
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: ['env', 'react', 'stage-0'],
},
},
include: [
path.join(projectRoot, 'src'),
'node_modules/sec-to-min'
],
exclude: /node_modules/
},
Work for me in Webpack v4:
rules: [{
include: [
path.resolve(__dirname, 'src'),
/node_modules\/(dom7|swiper)/
],
loader: 'babel-loader',
presets: [['#babel/preset-env', {
'modules': false
}]]
},
test: /\.(js)$/
}]

vuejs CLI erroro with awesome font

I am working vue CLI webpack setup and installed the awesome font package via npm. In the in the main.js i imported the font:
import 'vuetify/dist/vuetify.min.css'
i got the following error:
./node_modules/font-awesome/fonts/fontawesome-webfont.ttf?v=4.7.0
Module parse failed: Unexpected character '' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
# ./node_modules/css-loader!./node_modules/font-awesome/css/font-awesome.css 7:684-735
# ./node_modules/font-awesome/css/font-awesome.css
# ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue
# ./src/App.vue
# ./src/main.js
# multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js
and heres the standard webpack config file:
any ideas how to use the right loader?
regards
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: 'dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
],
}, {
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
You don't need to plug vue-style-loader for css loading. This is an internal loader used by vue-loader.
rules: [
{
test: /\.css$/,
use: [
- 'vue-style-loader',
'css-loader'
],
}, {
You also need a loader for ttf file:
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
use:[ { loader: "url-loader", options: {limit: 10000, mimetype: 'application/octet-stream'} }]
},
font-awsome will also require loader for woff file:
{
test: /\.woff(2)?(\?v=\d+\.\d+\.\d+)?$/,
use:[ { loader: "url-loader", options: {limit: 10000, mimetype: 'application/font-woff'} }]
},