Webpack configuration for React Native - react-native

I'm new to react-native. I want to implement webpack for my project. Can anyone suggest a sample webpack config file for react-native

Here's one:
global.__PLATFORM__ = process.env.RN_PLATFORM || 'ios';
module.exports = {
context: __dirname,
entry: {
index: [
'react-native-webpack/clients/polyfills.js',
`./index.${__PLATFORM__}.js`,
],
},
output: {
path: assetsPath,
filename: `[name].${__PLATFORM__}.bundle`,
chunkFilename: '[name].chunk.js',
publicPath: '/',
},
devServer: {
port: 8081,
quiet: false,
noInfo: true,
lazy: true,
filename: `[name].${__PLATFORM__}.bundle`,
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
},
publicPath: '/',
stats: { colors: true },
},
};
Note that a Webpack config is not the only thing you'll need. This guide goes deeper and gives examples and explanations for the next steps:
Babel config
Module support
Assets require support

Related

Exclude certain folders in Vite and Rollup with Vue.js

I am using Vite to bundle my Vue.js app, I have #vitejs/plugin-vue and #rollup/plugin-typescript installed.
I would like Vite to skip bundling certain folders inside my src folder:
src > assets
...
src > skins
src > skins > skin1
**src > skins > skin2**
I have tried the exclude option in the vue and typescript plugins, neither work for me. Here is my vite.config.js:
const config = {
mode: process.env.MODE,
root: PACKAGE_ROOT,
resolve: {
alias: {
'/#/': join(PACKAGE_ROOT, 'src') + '/',
},
},
plugins: [vue({
exclude: ['**skin2'],
})],
base: '',
server: {
fs: {
strict: true,
},
},
build: {
sourcemap: true,
target: `chrome${chrome}`,
outDir: 'dist',
assetsDir: '.',
rollupOptions: {
input: join(PACKAGE_ROOT, 'index.html'),
external: [
...builtinModules.flatMap(p => [p, `node:${p}`]),
],
plugins: [
typescript({
exclude: ['**/skin2'],
}),
],
},
}
};
I also tried RegExp which also didn't work, what I've missed? Thank you!

Terser Plugin doesnt run with Vue CLI 5 on vue build

I am trying to create a production build with the following configureWebpack but Terser doesnt run during build.
Code is not minimized or uglified. Ialso tried hidden-source-map
Using "terser-webpack-plugin": "^5.3.3" with #vue/cli#5.0.7
isProd is correctly set to true.
const TerserPlugin = require('terser-webpack-plugin');
const isProd = process.env.NODE_ENV === 'production';
module.exports = {
publicPath: '/',
devServer: {
host: 'staging-beta.myDomain.com',
port: 9000,
allowedHosts: 'all',
},
transpileDependencies: ['vuetify'],
chainWebpack: (config) => {
// reducted code
},
configureWebpack: {
devtool: 'source-map',
optimization: {
minimize: isProd,
minimizer: isProd
? [
new TerserPlugin({
minify: TerserPlugin.uglifyJsMinify,
terserOptions: {
compress: {
drop_console: true,
},
output: {
comments: false,
},
},
}),
]
: [],
},
},
};
The correct setup is:
module.exports = defineConfig({
terser: {
minify: 'uglifyJs',
terserOptions: {
compress: {
drop_console: true,
},
},
},
})
You also need to npm install uglify-js
comments under output is deprecated.

Vue.js historyapifallback not working, Why?

In the development environment, refreshing from /test works fine.
However, when I refresh after deployment, I get a 404 error.
Where is the problem?
vue.config.js
const path = require("path");
module.exports = {
devServer: {
overlay: false,
historyApiFallback: true,
hot: true,
},
transpileDependencies: [
'element-plus'
],
publicPath: '/',
outputDir: path.resolve(__dirname, "./dist"),
}

How configure webpack optimization options in VUE

I have developed a VUE SPA that employs Vuetify as UI.
The starting project was scaffolded using vue-cli and selecting all standard options.
My current vue.config.js file is as follow:
const path = require("path");
const webpack = require("webpack");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
.BundleAnalyzerPlugin;
module.exports = {
publicPath: process.env.NODE_ENV == "production" ? "/" : "/",
css: {
sourceMap: true
},
devServer: {
headers: {
"Access-Control-Allow-Origin": "*",
https: true
}
},
productionSourceMap: false,
transpileDependencies: [
"vuetify",
"vue2-dropzone",
"vue-echarts",
"resize-detector"
],
configureWebpack: {
devtool: "source-map",
resolve: {
alias: {
//Api: path.resolve(__dirname, "src/api/"),
Components: path.resolve(__dirname, "src/components/"),
Constants: path.resolve(__dirname, "src/constants/"),
Container: path.resolve(__dirname, "src/container/"),
Views: path.resolve(__dirname, "src/views/"),
Helpers: path.resolve(__dirname, "src/helpers/"),
Themes: path.resolve(__dirname, "src/themes/")
//moment: "moment/src/moment"
},
extensions: ["*", ".js", ".vue", ".json"]
},
plugins: [
//jquery plugin
new webpack.ProvidePlugin({
$: "jquery",
jquery: "jquery",
"window.jQuery": "jquery",
jQuery: "jquery"
}),
new BundleAnalyzerPlugin(),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
],
optimization: {
splitChunks: {
chunks: "all"
// minSize: 10000,
// maxSize: 200000
}
}
}
};
I would like to add webpack optimization for minifying css/sass, images, js code, woff2 font files (served locally and not from CDN), I suppose that this is not done automatically, am I wrong?
Could you share a basic configuration to achieve what described above (being able to debug code in development)?

Is it possible to create bundle.js for ReactNative components using web pack.?

Is it possible to configure webpack for React Native components.?
And Import that component in React Native Project from the webpack bundle
You can use it through the React-native-webpack module. This is an example of configuration.
install
npm install webpack json-loader clean-webpack-plugin react-native-webpack --save
npm install webpack-dev-server --save-dev
Exmaple configure
global.__PLATFORM__ = process.env.RN_PLATFORM || 'ios';
module.exports = {
context: __dirname,
entry: {
index: [
'react-native-webpack/clients/polyfills.js',
`./index.${__PLATFORM__}.js`,
],
},
output: {
path: assetsPath,
filename: `[name].${__PLATFORM__}.bundle`,
chunkFilename: '[name].chunk.js',
publicPath: '/',
},
devServer: {
port: 8081,
quiet: false,
noInfo: true,
lazy: true,
filename: `[name].${__PLATFORM__}.bundle`,
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
},
publicPath: '/',
stats: { colors: true },
},
};