React js babel transform and minification in asp.net core - reactjs

I have a asp.net core 2.0 application and I am using react.js also. I have some jsx files in that.
I want to minify those jsx files and then use. For js & css minification,
I defined rules in bundleconfig.json but wiring rules for jsx throws errors.
Can anyone help me how I can minify jsx in asp.net core application.
Update
Below is my project structure
below is webpack.config file
const path = require("path");
const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const WebpackShellPlugin = require('webpack-shell-plugin');
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
process.traceDeprecation = true;
module.exports = {
entry: {
home: "./wwwroot/js/v2/userdefined/home.page.js"
},
output: {
path: path.resolve(__dirname, "wwwroot/dist"),
filename: "[name].js",
publicPath: "/dist/"
},
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: false,
extractComments: 'all',
uglifyOptions: {
compress: true,
output: null
}
}),
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
safe: true,
discardComments: {
removeAll: true,
},
},
})
]
},
plugins: [
new webpack.ContextReplacementPlugin(/\.\/locale$/, 'empty-module', false, /jsx$/),
new webpack.LoaderOptionsPlugin({
options: {}
}),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
Popper: ['popper.js', 'default']
}),
new CompressionPlugin({
test: /\.(js|css)/
}),
new UglifyJsPlugin(),
new WebpackShellPlugin({
onBuildStart: ['echo "Starting postcss command"'],
onBuildEnd: ['postcss --dir wwwroot/dist wwwroot/dist/*.css']
})
],
resolve: {
modules: [
path.resolve('./node_modules')
]
},
module: {
rules: [{
test: /\.scss$/,
use: [
'style-loader',
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
minimize: true,
sourceMap: true
}
},
{
loader: "sass-loader"
}
]
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: ["babel-loader", "eslint-loader"]
},
{
test: /\.(jpe?g|png|gif)$/i,
loader: "file-loader"
},
{
test: /\.(woff|ttf|otf|eot|woff2|svg)$/i,
loader: "file-loader"
}
]
}
};
below is Gruntfile.js
/// <binding AfterBuild='cleanup' />
module.exports = function(grunt) {
require("jit-grunt")(grunt);
grunt.initConfig({
clean: ["./Modules/*"],
copy: {
main: {
expand: true,
src: [
"../Modules/**/bin/Debug/**/**/*.*",
"../Modules/**/wwwroot/**",
],
dest: "./Modules/"
},
css: {
expand: true,
cwd: "../Modules/AwesomeCMSCore.Modules.Frontend/wwwroot/dist",
src: ["cmscore.css"],
dest: "./wwwroot/dist/"
},
js: {
expand: true,
cwd: "../Modules/AwesomeCMSCore.Modules.Frontend/wwwroot/dist",
src: ["*.js"],
dest: "./wwwroot/dist/"
},
static: {
expand: true,
cwd: "../Modules/AwesomeCMSCore.Modules.Frontend/wwwroot/dist",
src: ["**"],
dest: "./wwwroot/dist/"
}
},
watch: {
css: {
files: ["../Modules/**/wwwroot/dist/*.css"],
tasks: ["copy:css"],
options: {
reload: true,
spawn: false
}
},
js: {
files: ["../Modules/**/wwwroot/dist/*.js"],
tasks: ["copy:js"],
options: {
reload: true,
spawn: false
}
}
}
});
grunt.registerTask("default", ["watch"]);
grunt.registerTask("cleanup", [
"clean",
"copy:main",
"copy:static"
]);
};
below is my package.json
{
"name": "ProjV2",
"version": "1.0.0",
"main": "index.js",
"author": "JitendraPancholi",
"license": "",
"scripts": {
"prod": "webpack -p --mode=production --config webpack.prod.js",
"start": "webpack --mode=development --config webpack.dev.js",
"lint:js": "eslint \"wwwroot/js/react/src/**/*.{js,jsx}\" --color",
"lint:style": "stylelint \"wwwroot/css/**/*.scss\" --syntax scss"
},
"devDependencies": {
"babel-cli": "6.26.0",
"babel-core": "6.26.3",
"babel-eslint": "8.2.6",
"babel-loader": "7.1.5",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-es2015-destructuring": "6.23.0",
"babel-plugin-transform-object-rest-spread": "6.26.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-react": "6.24.1",
"babel-preset-stage-0": "6.24.1",
"compression-webpack-plugin": "1.1.11",
"css-loader": "1.0.0",
"cssnano": "^4.1.7",
"empty-module": "^0.0.2",
"eslint": "^5.0.0",
"eslint-loader": "2.0.0",
"eslint-plugin-react": "7.10.0",
"file-loader": "1.1.11",
"grunt": "1.0.3",
"grunt-contrib-clean": "1.1.0",
"grunt-contrib-copy": "1.0.0",
"grunt-contrib-watch": "1.1.0",
"jit-grunt": "0.10.0",
"jshint": "2.9.5",
"jshint-loader": "0.8.4",
"mini-css-extract-plugin": "^0.4.4",
"node-sass": "^4.11.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"sass-loader": "7.0.3",
"style-loader": "0.21.0",
"stylelint": "9.3.0",
"stylelint-config-recommended": "2.1.0",
"stylelint-config-recommended-scss": "3.2.0",
"stylelint-config-standard": "18.2.0",
"stylelint-scss": "3.1.3",
"uglify-js": "^3.4.9",
"uglifyjs-webpack-plugin": "1.2.7",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-merge": "^4.2.1",
"webpack-shell-plugin": "^0.5.0"
},
"dependencies": {
"#aspnet/signalr": "^1.1.0",
"#tinymce/tinymce-react": "^2.2.5",
"axios": "^0.18.0",
"bootstrap": "^4.1.1",
"chart.js": "^2.7.2",
"font-awesome": "^4.7.0",
"jquery": "^3.3.1",
"moment": "^2.22.2",
"popper.js": "^1.14.1",
"prop-types": "^15.6.0",
"react": "^16.4.1",
"react-bootstrap-table": "^4.3.1",
"react-dom": "^16.4.1",
"react-select": "^1.2.1",
"react-values": "^0.2.4",
"reactstrap": "^6.3.0",
"toastr": "^2.1.4"
}
}
All my fils jsx files are as per below

You cant do that. React has to transform into js file in order to run in the browser
Normally you will need module bundle tool like webpack
You can view my full config here
Let me know if you need any more help
Update: Here is how I transform jsx into js
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: ["babel-loader", "eslint-loader"]
},

Related

How to stop infinite refresh in react webpack app?

When I change the TS file, Webpack doesn't stop refreshing the page.
Console says: #ebpack 5.66.0 compiled successfully
I've googled this and tried different plugins, but they didn't work.
The mild refresh plugin just stopping the refresh and excluding JS files from ts-loader doesn't work at all.
The related issue in git didn't give the result as well.
webpack.config.json:
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const HTMLInlineCSSWebpackPlugin = require("html-inline-css-webpack-plugin").default;
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
const htmlWebpackPlugin = new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html",
inlineSource: '.(js|css)$',
inject: 'body',
});
module.exports = {
entry: ['./src/index.tsx', './src/main.css'],
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
fallback: {
"path": require.resolve("path-browserify"),
"os": require.resolve("os-browserify/browser"),
"url": require.resolve("url"),
"tty": require.resolve("tty-browserify"),
"minimatch": require.resolve("minimatch"),
"process": require.resolve("process")
},
},
module: {
rules: [{
test: /\.tsx?$/,
exclude: [/node_modules/, /\.js$/, /\.d\.ts$/, /dist/],
loader: 'ts-loader',
options: {
configFile: 'tsconfig.json',
}
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
}
],
},
output: {
filename: 'bundle.js',
publicPath: '',
},
plugins: [
new webpack.ProvidePlugin({
process: 'process',
}),
new MiniCssExtractPlugin({
filename: "main.css",
chunkFilename: "mainc.css"
}),
htmlWebpackPlugin,
new HTMLInlineCSSWebpackPlugin(),
new InlineChunkHtmlPlugin(HtmlWebPackPlugin, [/bundle/]),
]
};
package.json:
{
"name": "browser",
"version": "0.0.0",
"scripts": {
"build": "webpack --mode development",
"start": "webpack serve --mode development --port 3000"
},
"devDependencies": {
"#types/chrome": "^0.0.163",
"#types/jquery": "^3.5.10",
"#types/node": "^16.11.7",
"#types/react": "^17.0.34",
"#types/react-dom": "^17.0.11",
"#types/vscode": "^1.62.0",
"#types/vscode-webview": "^1.57.0",
"css-loader": "^6.5.1",
"html-inline-css-webpack-plugin": "^1.11.1",
"html-webpack-plugin": "^5.5.0",
"mini-css-extract-plugin": "^2.4.6",
"react-dev-utils": "^12.0.0",
"ts-loader": "^9.2.6",
"typescript": "^4.5.4",
"webpack": "^5.66.0",
"webpack-cli": "^4.9.1",
"webpack-dev-server": "^4.7.3"
},
"dependencies": {
"ace-builds": "^1.4.13",
"axios": "^0.24.0",
"bootstrap": "^5.1.3",
"jquery": "^3.6.0",
"minimatch": "^5.0.1",
"mobx": "^6.3.6",
"mobx-react": "^7.2.1",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"react": "^17.0.2",
"react-ace": "^9.5.0",
"react-bootstrap": "^2.0.2",
"react-dom": "^17.0.2",
"redoc": "^2.0.0-rc.66",
"styled-components": "^5.3.5",
"tty-browserify": "0.0.1",
"url": "^0.11.0",
"uuid": "^8.3.2"
}
}
You better change InlineChunkHtmlPlugin tests from /bundle/ to /runtime/.
Replace
new InlineChunkHtmlPlugin(HtmlWebPackPlugin, [/bundle/])
With
new InlineChunkHtmlPlugin(HtmlWebPackPlugin, [/runtime~.+[.]tsx/])
OR
new InlineChunkHtmlPlugin(HtmlWebPackPlugin, [/runtime/])

./components/Avatar.tsx Error: Cannot find module '#babel/preset-stage-0'

After a few hours of research, I still haven't solved an issue I've been having with Babel and Webpack. ): I'm sure the solution is simple.
My .babelrc:
{
"presets": ["#babel/preset-env", "#babel/preset-react", "#babel/preset-stage-0"]
}
My package.json
{
"name": "sabrinasbase",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"chokidar": "^3.5.1",
"css-modules-typescript-loader": "^4.0.1",
"next": "10.0.5",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-particles-js": "^3.4.1",
"react-tsparticles": "^1.18.11",
"sass": "^1.32.5",
"three": "^0.124.0"
},
"devDependencies": {
"#babel/cli": "^7.12.10",
"#babel/core": "^7.12.10",
"#babel/preset-env": "^7.12.11",
"#babel/preset-react": "^7.12.10",
"#babel/preset-stage-0": "^7.8.3",
"#babel/register": "^7.12.10",
"#types/node": "^14.14.22",
"#types/react": "^17.0.0",
"babel-loader": "^8.2.2",
"typescript": "^4.1.3",
"url-loader": "^4.1.1",
"webpack": "^5.16.0",
"webpack-cli": "^4.4.0",
"webpack-dev-middleware": "^1.2.0",
"webpack-hot-middleware": "^2.0.0"
},
"resolutions": {
"#babel/core": "^7.12.10"
}
}
My webpack.config.js
var path = require("path");
module.exports = {
//mode: "production",
mode: "development",
devtool: "inline-source-map",
context: path.join(__dirname, "pages"),
entry: ["/_app.js" /*main*/],
output: {
filename: "./bundle.js", // in /dist
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js", ".css", ".scss"],
},
module: {
rules: [
{ test: /\.tsx?$/, loader: "ts-loader" },
{
test: /\.scss$/,
use: [
{ loader: "style-loader" }, // to inject the result into the DOM as a style block
{ loader: "css-modules-typescript-loader" }, // to generate a .d.ts module next to the .scss file (also requires a declaration.d.ts with "declare modules '*.scss';" in it to tell TypeScript that "import styles from './styles.scss';" means to load the module "./styles.scss.d.td")
{ loader: "css-loader", options: { modules: true } }, // to convert the resulting CSS to Javascript to be bundled (modules:true to rename CSS classes in output to cryptic identifiers, except if wrapped in a :global(...) pseudo class)
{ loader: "sass-loader" }, // to convert SASS to CSS
// NOTE: The first build after adding/removing/renaming CSS classes fails, since the newly generated .d.ts typescript module is picked up only later
],
},
{
test: /\.png|jpg|gif$/i,
use: [
{
loader: "url-loader",
options: {
limit: 8192,
},
},
],
},
{ test: /\.js|tsx|ts$/, loader: "babel-loader", exclude: /node_modules/ },
],
},
};
All I want to do is import a picture into a component. Originally I was getting Plugin/Preset files are not allowed to export objects, only functions error, but now I am getting vaguer errors that the module isn't working correctly. There were a few times that the Webpack would compile successfully, but the file I wanted to use wouldn't. Is there anything I am missing? I promise I am installing the preset-stage-0 package.
Error: Cannot find module '#babel/preset-stage-0'
Require stack:
- C:\Users\Sabri\Documents\GitHub\sabrinasbase\sabrinasbase\node_modules\next\dist\compiled\babel\bundle.js
- C:\Users\Sabri\Documents\GitHub\sabrinasbase\sabrinasbase\node_modules\next\dist\compiled\babel\code-frame.js
- C:\Users\Sabri\Documents\GitHub\sabrinasbase\sabrinasbase\node_modules\next\dist\lib\typescript\diagnosticFormatter.js
- C:\Users\Sabri\Documents\GitHub\sabrinasbase\sabrinasbase\node_modules\next\dist\lib\typescript\runTypeCheck.js
- C:\Users\Sabri\Documents\GitHub\sabrinasbase\sabrinasbase\node_modules\next\dist\lib\verifyTypeScriptSetup.js
- C:\Users\Sabri\Documents\GitHub\sabrinasbase\sabrinasbase\node_modules\next\dist\server\next-dev-server.js
- C:\Users\Sabri\Documents\GitHub\sabrinasbase\sabrinasbase\node_modules\next\dist\server\next.js
- C:\Users\Sabri\Documents\GitHub\sabrinasbase\sabrinasbase\node_modules\next\dist\server\lib\start-server.js
- C:\Users\Sabri\Documents\GitHub\sabrinasbase\sabrinasbase\node_modules\next\dist\cli\next-dev.js
- C:\Users\Sabri\Documents\GitHub\sabrinasbase\sabrinasbase\node_modules\next\dist\bin\next
at Array.map (<anonymous>)
at Generator.next (<anonymous>)
at loadFileChain.next (<anonymous>)
Not sure why you are getting this error
Here is my webpack which works as expected
Also, apologies in advance as I have done heck load of optimization to reduce bundle size
Webpack.config.base.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webpack = require('webpack');
const CopyPlugin = require("copy-webpack-plugin");
const ServiceWorkerWebpackPlugin = require('serviceworker-webpack-plugin');
const {WebpackManifestPlugin} = require('webpack-manifest-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const PurifyCssPlugin = require('purifycss-webpack');
const glob = require("glob");
module.exports = {
target: 'web',
stats: 'verbose',
output: {
path: __dirname + '/dist',
filename: '[name].[hash].bundle.js'
},
resolve: {
extensions: ['.js', '.jsx']
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.html$/,
use: [
{
loader: "html-loader"
}
]
},
{
test: /\.css$/,
use: [{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: './'
}
}, 'css-loader']
},
{
test: /\.scss$/,
use: [{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: './'
}
}, 'css-loader', 'sass-loader']
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true, // webpack#1.x
disable: true, // webpack#2.x and newer
mozjpeg: {
progressive: true,
},
optipng: {
enabled: false,
},
pngquant: {
quality: [0.65, 0.90],
speed: 4
},
gifsicle: {
interlaced: false,
},
},
}
]
}
]
},
plugins: [
new CopyPlugin({
patterns: [
{from: "icons", to: "icons"},
{from: "./manifest.webmanifest", to: ""},
{from: "./.htaccess", to: ""},
{from: "./robots.txt", to: ""}
],
}),
new MiniCssExtractPlugin({
filename: "[name].[hash].css"
}),
new HtmlWebpackPlugin({
template: "./src/app/index.html",
favicon: "./src/assets/image/favicon.png",
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true
}
}),
new ServiceWorkerWebpackPlugin({
entry: path.join(__dirname, './src/app/serviceWorker/serviceWorkerInit.js'),
}),
new WebpackManifestPlugin({
fileName: 'asset-manifest.json', // Not to confuse with manifest.json
}),
new ESLintPlugin({files: './src/app/app.js'}),
new PurifyCssPlugin({
paths: [
...(glob.sync(`./src/app/*.html`)),
...(glob.sync(`./src/app/**/*.jsx`)),
...(glob.sync(`./dist/*.html`))
],
styleExtensions: ['.css','.scss'],
moduleExtensions: [".html"],
verbose: true,
purifyOptions: {
info: true,
minify: true,
whitelist: ["*purify*"]
},
})
]
};
Webpack.config.prod.js
const merge = require('webpack-merge');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const webpackBaseConfiguration = require('./webpack.config.base.js');
const TerserPlugin = require('terser-webpack-plugin')
const webpack = require('webpack');
const GLOBALS = {
'process.env.NODE_ENV': JSON.stringify('production'),
__DEV__: false
};
module.exports = merge(webpackBaseConfiguration,{
mode: 'production',
entry: [
'#babel/polyfill',
'./src/app/app.jsx'
],
devServer: {
contentBase: './dist'
},
optimization: {
splitChunks: {
chunks: 'all',
minSize: 20000,
maxSize: 0,
minChunks: 2,
maxAsyncRequests: 30,
maxInitialRequests: 30,
enforceSizeThreshold: 50000,
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true
}
}
},
usedExports: true,
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
ecma: 6,
},
}),
new OptimizeCSSAssetsPlugin()
]
},
plugins: [
new webpack.DefinePlugin(GLOBALS),
new CompressionPlugin({
algorithm: 'gzip',
minRatio: Number.MAX_SAFE_INTEGER
}),
new CompressionPlugin({
filename: "[path].br",
algorithm: "brotliCompress",
test: /\.(js|css|html|svg|png|svg|jpg|gif)$/,
deleteOriginalAssets: false,
minRatio: Number.MAX_SAFE_INTEGER
})
]
});
Webpack.config.dev.js
const merge = require('webpack-merge');
const webpackBaseConfiguration = require('./webpack.config.base');
const webpack = require('webpack');
module.exports = merge(webpackBaseConfiguration,{
mode: 'development',
output: {
publicPath: '/'
},
devServer: {
contentBase: './src',
historyApiFallback: true
},
devtool: 'cheap-module-eval-source-map',
entry: [
'#babel/polyfill',
'webpack-hot-middleware/client?reload=true',
'./src/app/app.jsx'
],
plugins: [
/*For hot deployment*/
new webpack.HotModuleReplacementPlugin()
]
});
package.json
{
"name": "Sample",
"version": "1.0.0",
"description": "React app",
"scripts": {
"clean": "rimraf dist",
"deploy": "node deploy",
"build:prod:deploy": "npm run clean && webpack --mode production --config webpack.config.prod.js && node deploy",
"build:prod": "npm run clean && webpack --mode production --config webpack.config.prod.js",
"build:dev": "webpack-dev-server --mode development --config webpack.config.dev.js --open --hot",
"start": "npm run build && npm run deploy",
"build": "npm run clean && webpack --mode production --config webpack.config.prod.js && npm run deploy"
},
"engines": {
"node": "14.x",
"npm": "6.x"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/cli": "^7.12.1",
"#babel/core": "^7.2.2",
"#babel/node": "^7.2.2",
"#babel/plugin-proposal-class-properties": "^7.3.0",
"#babel/polyfill": "^7.2.5",
"#babel/preset-env": "^7.2.0",
"#babel/preset-react": "^7.0.0",
"#babel/register": "^7.0.0",
"#gfx/zopfli": "^1.0.15",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.0.4",
"brotli-webpack-plugin": "^1.1.0",
"compression-webpack-plugin": "^2.0.0",
"copy-webpack-plugin": "6.2.1",
"css-loader": "^2.1.0",
"eslint": "^7.18.0",
"eslint-webpack-plugin": "^2.4.1",
"file-loader": "^3.0.1",
"ftp-deploy": "^2.4.0",
"glob": "^7.1.6",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"image-minimizer-webpack-plugin": "^1.0.0",
"image-webpack-loader": "^7.0.1",
"imagemin-gifsicle": "^7.0.0",
"imagemin-jpegtran": "^7.0.0",
"imagemin-optipng": "^8.0.0",
"imagemin-svgo": "^8.0.0",
"mini-css-extract-plugin": "^0.5.0",
"node-sass": "^4.14.1",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"purify-css": "^1.2.5",
"purifycss-webpack": "^0.7.0",
"rimraf": "^2.6.3",
"sass-loader": "^7.1.0",
"serviceworker-webpack-plugin": "^1.0.1",
"terser-webpack-plugin": "4.2.3",
"uglifyjs-webpack-plugin": "^2.1.1",
"webpack": "^4.40.0",
"webpack-cli": "^3.1.2",
"webpack-dev-middleware": "^3.5.2",
"webpack-dev-server": "^3.11.0",
"webpack-ftp-upload-plugin": "^1.0.3",
"webpack-hot-middleware": "^2.24.3",
"webpack-manifest-plugin": "^3.0.0",
"webpack-merge": "^4.2.1",
"workbox-cacheable-response": "^6.0.2",
"workbox-expiration": "^6.0.2",
"workbox-routing": "^6.0.2",
"workbox-strategies": "^6.0.2",
"workbox-webpack-plugin": "^6.0.2",
"zlib": "^1.0.5"
},
"dependencies": {
"axios": "^0.19.0",
"bootstrap": "^4.5.0",
"react": "^16.8.1",
"react-dom": "^16.8.1",
"react-lazy-load-image-component": "^1.5.1",
"react-paginate": "^6.5.0",
"react-redux": "^7.2.2",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"redux": "^4.0.5",
"redux-immutable-state-invariant": "^2.1.0",
"redux-thunk": "^2.3.0"
}
}

Ant Design: Property 'TabPane' does not exist

Struggling to find a solution to this. I fear it's something to do with versions. However i am not excited about the prospect of downgrading, but downgrade what?
Any help would be appreciated. Please ask for any extra information.. Was not sure what else or what to show.
After running webpack;
TS2339: Property 'TabPane' does not exist on type '(...args: any[]) =>
any'.
Code main.tsx
import * as React from "react";
import { Tabs, Button, Icon } from 'antd';
const TabPane = Tabs.TabPane;
export class Main extends React.Component
{
render()
{
return (
<div className="main-container">
<div>
<Tabs>
<TabPane key="1" tab={<span><Icon type="android" />Tab1</span>}>
<div style={{ padding: '5px' }}>
Tab1
</div>
</TabPane>
<TabPane key="2" tab={<span><Icon type="android" />Tab2 </span>}>
</TabPane>
</Tabs>
</div>
</div>
);
}
}
Webpack.config.js
var createVendorChunk = require('webpack-create-vendor-chunk');
var webpack = require('webpack');
var path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const nodeExternals = require('webpack-node-externals');
module.exports = {
mode: 'development',
entry: {
ui: './src/index.tsx'
},
externals: {
"react": "React",
"react-dom": "ReactDOM",
},
devtool: 'source-map',
resolve: {
extensions: [".ts", ".tsx", ".js"]
},
output: {
path: path.resolve(__dirname, "chrome-extension/build/"),
publicPath: "build",
filename: "[name].bundle.js",
chunkFilename: '[name].bundle.js',
},
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
enforce: true,
chunks: 'all'
},
}
},
runtimeChunk: true
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
})
],
target: 'node', // in order to ignore built-in modules like path, fs, etc.
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
module: {
rules: [
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader",
},
{
test: /\.tsx?$/,
loader: "ts-loader",
exclude: /node_modules/
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"]
},
{
test: /\.less$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader"
}, {
loader: "less-loader",
options: {
javascriptEnabled: true
}
}]
}
],
}
};
package.json
{
"name": "test",
"version": "0.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"dev": "webpack --watch"
},
"author": "Elgan",
"license": "MIT",
"devDependencies": {
"#babel/core": "^7.0.1",
"#babel/preset-env": "^7.0.0",
"#babel/preset-react": "^7.0.0",
"awesome-typescript-loader": "^5.2.1",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.2",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.11.1",
"css-loader": "^1.0.0",
"file-loader": "^2.0.0",
"less": "^3.8.1",
"less-loader": "^4.1.0",
"mini-css-extract-plugin": "^0.4.2",
"node-sass": "^4.9.3",
"sass-loader": "^7.1.0",
"source-map-loader": "^0.2.4",
"style-loader": "^0.23.0",
"typescript": "^3.0.3",
"webpack": "^4.19.0",
"webpack-cli": "^3.1.0",
"webpack-create-vendor-chunk": "^0.1.1",
"webpack-node-externals": "^1.7.2"
},
"dependencies": {
"#types/react": "^16.4.14",
"#types/react-dom": "^16.0.7",
"antd": "^3.9.2",
"babel": "^6.23.0",
"babel-plugin-import": "^1.9.1",
"babel-preset-env": "^1.7.0",
"classnames": "^2.2.5",
"install": "^0.12.1",
"lodash": "^4.17.11",
"npm": "^6.4.1",
"prop-types": "^15.6.2",
"react": "^16.5.1",
"react-dom": "^16.5.1",
"ts-loader": "^5.1.1"
}
}
Changing to awesome-typescript-loader seemed to help. Though not sure why, so elaboration would be useful.
I changed the TS loader from ts-loader to awesome-typescript-loader in the webpack.config.js. Then i changed the TSConfig module to commonjs. It compiled now.
webpack.config.js
{
test: /\.tsx?$/,
loader: "awesome-typescript-loader",
exclude: /node_modules/
},
tsconfig.ts
{
"compilerOptions": {
"outDir": "./dist/", // path to output directory
"sourceMap": true, // allow sourcemap support
"strictNullChecks": true, // enable strict null checks as a best practice
"module": "commonjs", // specify module code generation
"jsx": "react", // use typescript to transpile jsx to js
"target": "es5", // specify ECMAScript target version
"allowJs": true // allow a partial TypeScript and JavaScript codebase
},
"exclude": [
"node_modules"
],
}

Require JS Compile React application

I am working on React JS application. While building the code using grunt build I am getting the below error:
Running "requirejs:compile" (requirejs) task
Error: Cannot uglify file: C:/Dev/first-research/first-research-min.js.
Skipping it. Error is:
SyntaxError: Unexpected token: name (ClassicEditor)
If the source uses ES2015 or later syntax, please pass "omptimize: 'none'" to r.js
and use an ES2015+ compatible minifier after running r.js. The included
UglifyJS only understands ES5 or earlier syntax.
Calling webpack configuration from grunt to build and transpile from es6 to es5.
Attached are the Webpack, grunt, package.json and babel.rc file for ref
Webpack file
const path = require('path'),
webpack = require('webpack'),
MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { styles } = require( '#ckeditor/ckeditor5-dev-utils' );
process.env.NODE_ENV = 'development';
module.exports = {
optimization: {
splitChunks: {
cacheGroups: {
styles: {
name: 'app',
test: /\.css$/,
chunks: 'all',
enforce: true
}
}
}
},
mode: 'development',
entry: {
'app': './src/app'
},
resolve: {
extensions: ['.js', '.jsx', '.json', '.scss', '.css'],
modules: ['./node_modules']
},
output: {
path: path.resolve(__dirname, '../dist'),
filename: '[name]-bundle.js',
libraryTarget: 'amd'
},
module: {
rules: [
{
test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,
use: [ 'raw-loader' ]
},
{ // To process CKEditor CSS files.
test: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css/,
use: [
{
loader: 'style-loader',
options: {
singleton: true
}
},
{
loader: 'postcss-loader',
options: styles.getPostCssConfig( {
themeImporter: {
themePath: require.resolve('#ckeditor/ckeditor5-theme-lark')
},
minify: true
} )
}
]
},
// ESlint loader
{
test: /\.(js|jsx)$/, // include .js and .jsx files
use: [
'eslint-loader'
],
enforce: 'pre',
exclude: /node_modules/ // exclude any and all files in the node_modules folder
},
// ES6 loader
{
test: /\.(js|jsx)$/,
exclude: /node_modules/, // exclude any and all files in the node_modules folder
use: {
loader: 'babel-loader',
options:{
presets: ['env']
}
}
},
// Style loader other than CK5 editor CSS files.
{
test: /\.css$/,
exclude: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css/,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
},
{
test: /\.scss$/,
exclude: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.scss/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
exclude: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,
use: [{
loader: 'url-loader',
options: {
limit: 10000
}
}]
},
// html loader
{
test: /\.html$/,
use: [{
loader: 'html-loader',
options: {
minimize: true,
}
}]
},
{
loader: require.resolve('file-loader'),
// Exclude `js` files to keep the "css" loader working as it injects
// its runtime that would otherwise be processed through the "file" loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpack's internal loaders.
exclude: [
/\.(js|jsx|mjs)$/,
/\.html$/,
/\.json$/,
/ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,
/ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css/,
/(node_modules)/,
/\.css$/
],
options: {
name: 'static/media/[name].[hash:8].[ext]'
}
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name]-bundle.css',
}),
new webpack.LoaderOptionsPlugin({ options: {} }),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
}
}),
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en-gb/)
// new CKEditorWebpackPlugin( {
// language: 'pl'
// })
],
externals: [
/^user-profile/, //OK
/^settings\/settings/, //OK
/^am-permissions\/permission/, //OK
/^logger\/log/, //OK
/^am-permissions/, //OK
/^am-address/, //OK
/^am-alerter/, //OK
/^amfx-blotter-grid\//, //OK
/^amfx-blotter-grid-components\//, //OK
/^amfx-blotter-filters\//, //OK
/^amfx-react-components\//, //OK
/^menu\//, //OK
'react', //OK
'react-dom', //OK
'prop-types', //OK
/^jquery$/, //OK:
/^underscore$/, //OK:
/^lodash$/,
/^q$/, //OK:
/^d3$/, //OK:
/^text/, //OK:
/^d3-utils\//, //OK:
/^bootstrap\//, //OK:
/^moment$/, //OK:
/^am-modal\//, //OK
/^xhr-client\//, //OK
/^q\//, //OK
/^whatwg-fetch\//, //OK
'immutable', //OK
/^amfx-tab\//, //OK
/^fx-blotter\//, //OK
/^zap-tab-comms\//, //OK
/^trade-operations\// //OK
],
devtool: 'source-map',
resolveLoader: {
alias: {
'css/css': 'style-loader'
}
}
};
package.json
{
"name": "first-research",
"version": "0.1.18",
"description": "first-research: Built using the Zambezi Platform",
"main": "dist/react-your-component-name.min.js",
"repository": {
"type": "git",
"url": "https://stash.dts.fm.rbsgrp.net/scm/amfirstresearch/first-research.git"
},
"scripts": {
"build": "webpack --progress --watch --config ./config/webpack.dev.js",
"test": "jest"
},
"devDependencies": {
"#ckeditor/ckeditor5-dev-utils": "^10.0.3",
"#ckeditor/ckeditor5-theme-lark": "^11.0.0",
"babel": "^6.1.18",
"babel-core": "^6.14.0",
"babel-eslint": "^6.1.0",
"babel-jest": "^15.0.0",
"babel-loader": "^7.1.2",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015": "^6.14.0",
"babel-preset-es2015-node5": "^1.2.0",
"babel-preset-react": "^6.11.1",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-0": "^6.5.0",
"babel-runtime": "^6.6.1",
"cross-env": "^1.0.8",
"css": "^1.0.7",
"css-loader": "^0.28.7",
"eslint": "^3.7.0",
"eslint-config-airbnb": "^10.0.1",
"eslint-loader": "^1.5.0",
"eslint-plugin-import": "^1.13.0",
"eslint-plugin-jsx-a11y": "^2.1.0",
"eslint-plugin-react": "^6.1.1",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^0.11.2",
"grunt": "^1.0.3",
"grunt-contrib-clean": "^1.1.0",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-requirejs": "^1.0.0",
"grunt-contrib-sass": "^1.0.0",
"grunt-contrib-watch": "^1.0.0",
"grunt-webpack": "^3.0.2",
"identity-obj-proxy": "^3.0.0",
"jest": "^15.1.1",
"jest-static-stubs": "^0.0.1",
"lodash": "^4.17.10",
"mini-css-extract-plugin": "^0.4.1",
"moment": "^2.22.2",
"postcss-loader": "^3.0.0",
"raw-loader": "^0.5.1",
"react-addons-test-utils": "^15.3.1",
"react-test-renderer": "^15.3.1",
"rimraf": "^2.5.4",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "^4.16.5",
"webpack-merge": "^4.1.0",
"zambezi-contrib-build": "^2.1.3"
},
"dependencies": {
"#ckeditor/ckeditor5-adapter-ckfinder": "^10.0.2",
"#ckeditor/ckeditor5-autoformat": "^10.0.2",
"#ckeditor/ckeditor5-basic-styles": "^10.0.2",
"#ckeditor/ckeditor5-block-quote": "^10.0.2",
"#ckeditor/ckeditor5-build-classic": "^11.0.1",
"#ckeditor/ckeditor5-dev-webpack-plugin": "^6.0.3",
"#ckeditor/ckeditor5-easy-image": "^10.0.2",
"#ckeditor/ckeditor5-editor-classic": "^11.0.0",
"#ckeditor/ckeditor5-essentials": "^10.1.1",
"#ckeditor/ckeditor5-heading": "^10.0.2",
"#ckeditor/ckeditor5-image": "^10.2.0",
"#ckeditor/ckeditor5-link": "^10.0.3",
"#ckeditor/ckeditor5-list": "^11.0.1",
"#ckeditor/ckeditor5-paragraph": "^10.0.2",
"#ckeditor/ckeditor5-react": "^1.0.0-beta.1",
"#zambezi/sdk": "^5.19.1",
"axios": "^0.18.0",
"enzyme": "^3.3.0",
"es6-promise": "^4.2.4",
"isomorphic-fetch": "^2.2.1",
"prop-types": "^15.6.2",
"react": "^15.3.1",
"react-dom": "^15.3.1",
"react-modal": "^3.5.1",
"react-router-dom": "^4.3.1",
"react-tabs": "^2.2.2"
},
"_zambezi": {
"runtimeDependencies": [
"am-address",
"am-alerter",
"am-permissions",
"react",
"react-dom",
"css",
"lodash",
"prop-types"
]
},
"jest": {
"collectCoverage": true,
"moduleNameMapper": {
"^.+\\.(png|jpg|gif)$": "jest-static-stubs/$1",
"^.+\\.(eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "identity-obj-proxy",
"^.+\\.(css|less)$": "identity-obj-proxy"
}
}
}
Grunt file
module.exports = function (grunt) {
grunt.initConfig({
build: {
options: {
optimize: 'none',
include: ['first-research'],
plugins: {
'css/css': '../node_modules/css/css',
'settings/settings': 'empty:',
"am-permissions/permission": "empty:",
"user-profile/user-profile": "empty:",
"logger/log": "empty:"
}
}
},
webpack: {
dev: require('./config/webpack.dev.js'),
prod: require('./config/webpack.prod.js')
},
watch: {
files: ['src/**/*.js'],
tasks: ['webpack:dev', 'build']
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-webpack');
grunt.loadNpmTasks('zambezi-contrib-build');
grunt.registerTask('default', ['webpack:dev', 'build']);
grunt.registerTask('ci-build', ['webpack:prod', 'build']);
};
babel.rc
{
"presets": [
"stage-0",
"react",
"es2015-node5"
],
"plugins": [
"transform-class-properties"
],
"sourceMaps": true
}

Unable to configure Jest in my React App

I'm just starting with tests right now, trying to set up the Jest with my project, but it has been painful.. I am trying to do the React App tutorial, with the Webpack Tutorial, and I'm facing if this error:
C:\Users\myuser\Desktop\myapp\__tests__\Link.test.js:2
import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at transformAndBuildScript (node_modules\jest-runtime\build\transform.js:320:12)
I'm using Node v7.5.0 and Webpack 2. I installed all the necessary dependencies and I also tried to sort with jest configuration, but it didn't sort my problem.
My package.json looks like this:
{
"name": "myapp",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"jest": {
"modulePaths": ["<rootDir>/src/"],
"moduleDirectories": ["node_modules"],
"transform": {
"^.+\\.jsx?$": "babel-jest"
},
"transformIgnorePatterns": [
"<rootDir>/(node_modules)/"
]
},
"scripts": {
"start": "webpack-dev-server --progress --watch",
"build": "webpack --progress",
"test": "jest --verbose"
},
"devDependencies": {
"babel-core": "^6.22.1",
"babel-eslint": "^7.1.1",
"babel-jest": "^18.0.0",
"babel-loader": "^6.2.10",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.0",
"babel-polyfill": "^6.22.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"babel-preset-stage-2": "^6.22.0",
"css-loader": "^0.26.1",
"eslint": "^3.15.0",
"eslint-config-airbnb": "^14.1.0",
"eslint-import-resolver-webpack": "^0.8.1",
"eslint-loader": "^1.6.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jest": "^1.0.2",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.9.0",
"extract-text-webpack-plugin": "^1.0.1",
"html-webpack-plugin": "^2.28.0",
"jest": "^18.1.0",
"jest-cli": "^19.0.2",
"node-sass": "^4.5.0",
"react-hot-loader": "3.0.0-beta.6",
"react-test-renderer": "^15.4.2",
"sass-loader": "^5.0.1",
"style-loader": "^0.13.1",
"url-loader": "^0.5.8",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0"
},
"dependencies": {
"chart.js": "^2.5.0",
"classnames": "^2.2.5",
"file-loader": "^0.11.1",
"flexboxgrid": "^6.3.1",
"google-map-react": "^0.22.3",
"intl": "^1.2.5",
"intl-locales-supported": "^1.0.0",
"lodash": "^4.17.4",
"material-ui": "^0.17.0",
"moment": "^2.17.1",
"node-schedule": "^1.2.1",
"normalize.css": "^5.0.0",
"re-base": "^2.6.0",
"react": "^15.4.2",
"react-chartjs-2": "^2.0.5",
"react-dom": "^15.4.2",
"react-flexbox-grid": "^0.10.2",
"react-router-dom": "^4.0.0",
"react-scrollbar": "^0.5.1",
"react-tap-event-plugin": "^2.0.1",
"webpack": "^2.2.1"
}
}
My .babelrc file looks like this:
{
"presets": [
["es2015", {"modules": false}],
"stage-2",
"react"
],
"plugins": [
"react-hot-loader/babel"
],
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
I also set the environment to test, but it didn't make any effect.
My Webpack file looks like this:
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const plugins = [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './src/index.html'),
inject: true,
favicon: path.resolve(__dirname, './src/images/favicon.ico'),
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('dev'),
},
}),
];
module.exports = {
devtool: 'cheap-eval-source-map',
entry: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:3000',
'webpack/hot/only-dev-server',
'./index.jsx',
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'public'),
publicPath: '/',
},
context: path.resolve(__dirname, 'src'),
resolve: {
modules: [path.resolve(__dirname, './src'), path.resolve(__dirname, './node_modules')],
extensions: ['.js', '.jsx'],
},
devServer: {
hot: true,
port: 3000,
contentBase: path.resolve(__dirname, 'public'),
historyApiFallback: true,
publicPath: '/',
},
module: {
rules: [
{
test: /(\.js|\.jsx)$/,
enforce: 'pre',
exclude: /node_modules/,
use: {
loader: 'eslint-loader',
options: {
failOnWarning: false,
failOnError: false,
},
},
},
{
test: /(\.js|\.jsx)$/,
use: ['babel-loader'],
exclude: /node_modules/,
},
{
test: /(\.scss|\.css)$/,
use: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
options: {
modules: true,
},
},
{ loader: 'sass-loader' },
],
},
{
test: /\.(png|jpg|)$/,
use: ['url-loader?limit=200000'],
},
],
},
plugins: plugins,
};
Can someone help me with this issue? Thanks!

Resources