babel-plugin-transform-remove-console in dev mode? - react-native

I needed to run babel-plugin-transform-remove-console for my dev build. What I did was:
npm i babel-plugin-transform-remove-console --save-dev
Then in .babelrc I changed it to this:
{
"presets": ["react-native"],
"plugins": ["transform-remove-console"]
}
I also tried:
{
"presets": ["react-native"],
"env": {
"development,": {
"plugins": ["transform-remove-console"]
}
}
}
However console logging is still happening in my dev build. I am on Android.
Does anyone know how to get this to work in dev mode?

Maybe you are using new babel version 7, you need to change this file babel.config.js instead of .babelrc as the following:
module.exports = function override(api) {
var env = api.cache(() => process.env.NODE_ENV);
var isProd = api.cache(() => process.env.NODE_ENV === "production");
if (!isProd) {
config = {
plugins: [
["transform-remove-console"]
],
presets: ["#babel/preset-flow", "module:metro-react-native-babel-preset"]
};
}
return config;
};
this should remove the console in the new babel versions

If you are using the version 0.62.x or newer, you can update your babel.config.js as follows:
module.exports = function(api) {
if (api.env("production")) {
return {
"presets": ["module:metro-react-native-babel-preset"],
"plugins": [
"transform-flow-strip-types",
"transform-remove-console"
]
}
}
return {
"presets": ["module:metro-react-native-babel-preset"],
"plugins": [
"transform-flow-strip-types",
]
}
};
PS: make sure to restart bundler with cache reset like this npm start --reset-cache and note that this will not prevent logs from redux logger (tested that).

You can try :
{
"presets": ["react-native"],
"plugins": ["dev-expression", "babel-plugin-dev-expression"]
}
Hope this help.

module.exports = {
presets: ["#vue/cli-plugin-babel/preset"],
env: {
development: {},
production: {
plugins: ["transform-remove-console"]
}
}
}

Related

Vue.Js: Alias configurations

I am trying to make aliases for the components and sub-directories using jsconfig.json in my Vue.js project. But I am getting this error:
jsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"#components/*": [
"./src/components/*"
],
}
},
"exclude": [
"node_modules"
]
}
SomeFile.vue
import GuestLayout from '#components/Layouts/GuestLayout';
Error:
ERROR Failed to compile with 1 error
...
To install it, you can run: npm install --save #components/Layouts/GuestLayout
I tried goggling for the issue but nothing seems to be working. This is the simplest one which found https://www.youtube.com/watch?v=U73TDohXmhQ
What am I doing wrong here..?
✔ Problem solved:
The thing that did the trick for me, was setting up the configurations in vue.config.js instead of using the jsconfig.json or tsconfig.json.
vue.config.js
const path = require('path');
module.exports = {
configureWebpack: {
resolve: {
alias: {
'#Layouts': path.resolve(__dirname, 'src/components/Layouts/'),
'#Inputs': path.resolve(__dirname, 'src/components/Input/'),
'#': path.resolve(__dirname, 'src/components/'),
}
}
}
}
SomeFile.vue
import GuestLayout from '#Layouts/GuestLayout';
import FormGroup from '#Inputs/FormGroup';
...

Webpack can't resolve vue in node_modules/vue-template-compiler

I have a simple Typescript project with this code:
import {
parseComponent,
compile as compileTemplate,
ASTElement,
} from "vue-template-compiler";
...
I compile it using tsc with:
"target": "es2020",
"module": "commonjs",
And it gives code like this:
const vue_template_compiler_1 = require("vue-template-compiler");
In my package.json I have this:
"dependencies": {
"vue-template-compiler": "^2.6.12"
But I don't have "vue", because I don't need all of Vue - just the template compiler.
This all works fine, but I'm trying to use Webpack to bundle everyone into a single file. However, when I run webpack I get this error:
ERROR in ./node_modules/vue-template-compiler/index.js 2:19-41
Module not found: Error: Can't resolve 'vue' in '/path/to/myproject/node_modules/vue-template-compiler'
# ./build/analysis.js 8:32-64
# ./build/index.js 8:19-40
This corresponds to the require("vue-template-checker") line. Why do I get this error?
Here's my webpack.config.js:
const path = require("path");
const webpack = require("webpack");
module.exports = {
target: "node",
entry: "./build/index.js",
mode: "production",
output: {
filename: "index.js",
path: path.resolve(__dirname, "dist"),
},
plugins: [
new webpack.BannerPlugin({ banner: "#!/usr/bin/env node", raw: true }),
],
};
That module makes Vue version check during import. I guess, you want to skip that check. I would try aliasing. Something like:
module.exports = {
// ...
resolve: {
alias: {
'vue-template-compiler$': 'vue-template-compiler/build.js'
}
}
}

How to add option for babel loader in customize-cra?

I'm hitting Support for the experimental syntax 'classProperties' isn't currently enabled
I need to add something like the following to my webpack config.
https://github.com/babel/babel/issues/8655
{
loader: 'babel-loader',
options: {
"presets": [
"#babel/preset-env",
"#babel/preset-react",
{
"plugins": [
"#babel/plugin-proposal-class-properties"
]
}
],
},
}
Although I'm using customize-cra and need to add it through config-overrrides.js and I tried
module.exports = override(
...addBabelPresets("#babel/preset-env", "#babel/preset-react"),
...addBabelPlugins('#babel/plugin-proposal-class-properties'),
# other lines omitted for brevity
)
Try defining the plugins and presets in a .babelrc file, use the useBabelRc loader in your config:
//config-override.js
const { useBabelRc, override } = require("customize-cra");
module.exports = override(
useBabelRc()
);
and then in the .babelrc
//.babelrc
const presets = ["#babel/preset-env","#babel/preset-react"]
const plugins = [
["#babel/plugin-proposal-class-properties"]
];
module.exports = {
presets,
plugins,
};
//config-override.js
const { useBabelRc, override } = require("customize-cra");
module.exports = override(
useBabelRc()
);
//.babelrc
{
"presets": [
["#babel/preset-react"],
["#babel/preset-env"]
],
"plugins": ["#babel/plugin-proposal-class-properties"]
}

Critical dependency: the request of a dependency is an expression, vue.js

My testing-app is compiling fine, except that I get this warning:
" Critical dependency: the request of a dependency is an expression"
(base) marco#pc01:~/webMatters/vueMatters/PeerJS-VueJS-Test$ npm run serve
> testproject#0.1.0 serve /home/marco/webMatters/vueMatters/PeerJS-VueJS-Test
> vue-cli-service serve
INFO Starting development server...
98% after emitting CopyPlugin
WARNING Compiled with 1 warnings
7:22:25 PM
warning in ./node_modules/peerjs/dist/peerjs.min.js
Critical dependency: the request of a dependency is an expression
App running at:
- Local: http://localhost:8080
- Network: http://ggc.world/
Note that the development build is not optimized.
To create a production build, run npm run build.
I read around that it might depend of webpack, but didn't find how to put it right.
This is webpack.config.js :
{
"mode": "development",
"output": {
"path": __dirname+'/static',
"filename": "[name].[chunkhash:8].js"
},
"module": {
"rules": [
{
"test": /\.vue$/,
"exclude": /node_modules/,
"use": "vue-loader"
},
{
"test": /\.pem$/,
"use": "file-loader"
}
]
},
node: {
__dirname: false,
__filename: false
},
resolve: {
extension: ['*', '.pem'],
},
devServer: {
watchOptions: {
aggregateTimeout: 300,
poll: 1000
},
https: true,
compress: true,
public: 'ggc.world:8080'
}
}
Any ideas about how to solve it?
The following code works for me. Edit vue.config.js and add webpack config:
configureWebpack: {
module: {
exprContextCritical: false
}
}
const webpack = require('webpack');
module.exports = {
// ... your webpack configuration ...
plugins: [
new webpack.ContextReplacementPlugin(
/\/package-name\//,
(data) => {
delete data.dependencies[0].critical;
return data;
},
),
]
}
try this one
For people coming here using CRA and having trouble with PeerJS, install react-app-rewired and use the following override config and it should work.
/* config-overrides.js */
const webpack = require('./node_modules/webpack')
module.exports = function override (config, env) {
if (!config.plugins) {
config.plugins = []
}
config.plugins.push(
new webpack.ContextReplacementPlugin(
/\/peerjs\//,
(data) => {
delete data.dependencies[0].critical
return data
}
)
)
return config
}
It seems it is an error between the library bundler (parcel) and CRA bundler (webpack), and I couldn't find any official fix on the way.

How to do module name alias for third party package

I have a app that created by react-native init command.
My app import websocket package which in turn require http package and cause error said "Unable to resolve module http".
i.e: myApp -> 3rd-module -> ws -> http
I try to work-around by install "#tradle/react-native-http", and added follow lines to my app's package json file:
"browser": { "http": "#tradle/react-native-http" },
"react-native": { "http": "#tradle/react-native-http" },
but it doesn't work.
I also try using babel-plugin-module-resolver but unluck either. Here is my .babelrc :
{
"presets": ["module:metro-react-native-babel-preset"],
"plugins": [
[
"#babel/plugin-proposal-decorators",
{
"legacy": true
}
],
["module-resolver", {
"alias": {
"#tradle/react-native-http": "http"
}
}]
]
}
How to do alias for my case? I research to fixing this problem by using webpack configuration, but don't know where is the configure file. After google, i think project created by react-native init use metro config instead of webpack.
try
["module-resolver", {
"alias": {
"http":"#tradle/react-native-http"
}
}]