Styled components: babel-plugin-styled-components not working - reactjs

I've been trying to get the babel-plugin-styled-components plugin to work with my webpack/babelrc configuration but I have not been able to do so. I am using babel-loader and no matter what I try I can't get the plugin to be recognised.
I am using Typescript 4 and Webpack 4.
I've searched and tried a number of proposed solutions but none have worked. I even tried the typescript-styled-components-plugin solution (https://github.com/Igorbek/typescript-plugin-styled-components), but no matter what it seems that the plugin is not recognised and the settings do not work.
I can only imagine that it's an issue with my configuration. Webpack is still a bit of a mystery to me in general.
My webpack.common.js file looks like this:
module.exports = [
{
context,
cache: local,
devtool: 'inline-source-map',
mode: 'development',
resolve: {
symlinks: false,
alias: {
'app.veams$': `${context}/app.veams.tsx`,
env: envFile
},
extensions: ['.ts', '.tsx', '.js']
},
// Entry point for webpack
entry: {
app: `${context}/app.tsx`
},
// Output directory and filename
output: {
filename: 'scripts/[name].bundle.js',
chunkFilename: 'scripts/[name].chunk.js',
path: outputPath,
pathinfo: !local // Performance #2: Garbage Collection
},
// Tell webpack to run babel on every file it runs through
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
babelrc: true,
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
// Let's add babel presets ...
presets: [
'#babel/preset-react',
[
'#babel/preset-env',
{
targets: {
browsers: ['last 2 versions', 'ie >= 11']
}
}
]
],
// ... and plugins.
plugins: [
'#babel/plugin-proposal-function-bind',
// Stage 2
[
'#babel/plugin-proposal-decorators',
{
legacy: true
}
],
[
'#babel/plugin-proposal-class-properties',
{
loose: false
}
]
]
}
}
]
},
scriptRule(configContext),
styleRule(),
assetsRule()
]
},
plugins: [].concat(scriptPlugins(configContext), stylePlugins(configContext)),
// Some libraries import Node modules but don't use them in the browser.
// Tell Webpack to provide empty mocks for them so importing them works.
node: {
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty'
},
// Turn off performance hints during development because we don't do any
// splitting or minification in interest of speed. These warnings become
// cumbersome.
performance: {
hints: false
},
optimization: {
minimizer: optimizerPlugins(),
removeAvailableModules: false, // Performance #1: Do not optimize so much in incremental builds
removeEmptyChunks: false, // Performance #1: Do not optimize so much in incremental builds
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules/,
chunks: 'initial',
name: 'vendor',
priority: 10,
enforce: true
}
} // Performance #1: Do not optimize so much in incremental builds
}
},
devServer: {
contentBase: outputPath,
compress: false,
port: 3000,
proxy: {
'/': 'http://localhost:2999'
},
overlay: {
warnings: true,
errors: true
}
}
}
];
The scriptRule file looks like this:
module.exports = function() {
return {
test: /\.tsx?$/,
use: [
{
loader: 'cache-loader'
},
{
loader: 'babel-loader',
options: {
babelrc: true,
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
// Let's add babel presets ...
presets: [
'#babel/preset-react',
[
'#babel/preset-env',
{
targets: {
browsers: ['last 2 versions', 'ie >= 11']
}
}
]
],
// ... and plugins.
plugins: [
'#babel/plugin-proposal-function-bind',
// Stage 2
[
'babel-plugin-styled-components',
{
displayName: true
}
],
[
'#babel/plugin-proposal-decorators',
{
legacy: true
}
],
[
'#babel/plugin-proposal-class-properties',
{
loose: false
}
]
]
}
},
{
loader: 'thread-loader',
options: {
// there should be 1 cpu for the fork-ts-checker-webpack-plugin
workers: require('os').cpus().length - 1
}
},
{
loader: 'ts-loader',
options: {
transpileOnly: true, // Performance #3 :: Limit amount of modules to transpile at a time per iteration
experimentalWatchApi: true,
happyPackMode: true // IMPORTANT! use happyPackMode mode to speed-up compilation and reduce errors
// reported to webpack
}
}
]
};
};
.babelrc looks like this:
{
"env": {
"client": {
"presets": [
[
"#babel/preset-env",
{
"targets": {
"browsers": ["last 2 versions", "ie >= 11"]
}
}
],
"#babel-preset-react"
],
"plugins": [
"#babel/plugin-proposal-function-bind",
[
"babel-plugin-styled-components",
{
"displayName": true
}
],
[
"#babel/plugin-proposal-decorators",
{
"legacy": true
}
],
[
"#babel/plugin-proposal-class-properties",
{
"loose": false
}
]
]
},
"server": {
"presets": ["#babel/preset-env"],
"plugins": [
"#babel/plugin-proposal-function-bind",
"#babel/plugin-proposal-export-default-from",
"#babel/plugin-proposal-logical-assignment-operators",
[
"#babel/plugin-proposal-optional-chaining",
{
"loose": false
}
],
[
"#babel/plugin-proposal-pipeline-operator",
{
"proposal": "minimal"
}
],
[
"#babel/plugin-proposal-nullish-coalescing-operator",
{
"loose": false
}
],
"#babel/plugin-proposal-do-expressions",
[
"#babel/plugin-proposal-decorators",
{
"legacy": true
}
],
"#babel/plugin-proposal-function-sent",
"#babel/plugin-proposal-export-namespace-from",
"#babel/plugin-proposal-numeric-separator",
"#babel/plugin-proposal-throw-expressions",
"#babel/plugin-syntax-dynamic-import",
"#babel/plugin-syntax-import-meta",
[
"#babel/plugin-proposal-class-properties",
{
"loose": false
}
],
"#babel/plugin-proposal-json-strings",
"#babel/plugin-transform-runtime"
]
}
}
}
And finally the package.json looks like this:
"dependencies": {
"#veams/core": "^1.0.0",
"#veams/helpers": "^1.2.8",
"#veams/http-service": "^1.0.2",
"c3": "0.7.20",
"carbon-components": "^9.66.3",
"carbon-components-react": "^6.74.1",
"carbon-icons": "^7.0.7",
"connected-react-router": "6.8.0",
"cors": "2.8.5",
"downshift": "3.1.5",
"express-http-proxy": "1.6.2",
"file-saver": "2.0.2",
"history": "4.7.2",
"html-to-react": "1.4.5",
"immutable": "4.0.0-rc.12",
"json5": "2.1.3",
"query-string": "6.13.7",
"react": "17.0.1",
"react-c3js": "0.1.20",
"react-d3-cloud": "0.7.0",
"react-dom": "17.0.1",
"react-excel-workbook": "0.0.4",
"react-redux": "7.2.2",
"react-router-config": "4.4.0-beta.6",
"react-router-dom": "5.2.0",
"redux": "4.0.5",
"redux-devtools-extension": "2.13.8",
"redux-immutable": "4.0.0",
"redux-immutable-state-invariant": "2.1.0",
"redux-observable": "1.2.0",
"reselect": "4.0.0",
"rxjs": "6.6.3",
"styled-components": "5.2.1",
"tslib": "2.0.3",
"xlsx": "0.16.8"
},
"devDependencies": {
"#babel/core": "^7.0.0",
"#babel/plugin-proposal-class-properties": "^7.0.0",
"#babel/plugin-proposal-decorators": "^7.0.0",
"#babel/plugin-proposal-do-expressions": "^7.0.0",
"#babel/plugin-proposal-export-default-from": "^7.0.0",
"#babel/plugin-proposal-export-namespace-from": "^7.0.0",
"#babel/plugin-proposal-function-bind": "^7.0.0",
"#babel/plugin-proposal-function-sent": "^7.0.0",
"#babel/plugin-proposal-json-strings": "^7.0.0",
"#babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
"#babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
"#babel/plugin-proposal-numeric-separator": "^7.0.0",
"#babel/plugin-proposal-optional-chaining": "^7.0.0",
"#babel/plugin-proposal-pipeline-operator": "^7.0.0",
"#babel/plugin-proposal-throw-expressions": "^7.0.0",
"#babel/plugin-syntax-dynamic-import": "^7.0.0",
"#babel/plugin-syntax-import-meta": "^7.0.0",
"#babel/plugin-transform-runtime": "^7.0.0",
"#babel/preset-env": "^7.0.0",
"#babel/preset-react": "^7.0.0",
"#babel/register": "^7.0.0",
"#babel/runtime": "^7.0.0",
"#types/history": "^4.6.2",
"#types/jest": "^23.3.12",
"#types/node": "^10.12.18",
"#types/react": "16.9.56",
"#types/react-dom": "16.9.9",
"#types/react-redux": "7.1.11",
"#types/react-router": "4.4.3",
"#types/react-router-config": "1.0.7",
"#types/react-router-dom": "5.1.6",
"#types/redux-immutable-state-invariant": "2.1.1",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.4.2",
"babel-loader": "^8.0.5",
"babel-plugin-styled-components": "1.11.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.16.0",
"babel-preset-stage-1": "^6.24.1",
"babel-register": "^6.26.0",
"babel-runtime": "^6.26.0",
"body-parser": "^1.18.1",
"browser-sync": "^2.24.4",
"cache-loader": "^2.0.1",
"case-sensitive-paths-webpack-plugin": "^2.1.1",
"chalk": "^2.0.1",
"command-line-args": "^4.0.7",
"command-line-usage": "^4.0.2",
"compression-webpack-plugin": "^2.0.0",
"connect-browser-sync": "^2.1.0",
"copy-webpack-plugin": "^4.3.1",
"cross-env": "^5.0.5",
"css-loader": "^2.1.0",
"deep-extend": "^0.6.0",
"eslint": "^5.12.0",
"eslint-config-prettier": "^3.4.0",
"eslint-plugin-prettier": "^3.0.1",
"express": "^4.16.3",
"fork-ts-checker-webpack-plugin": "^0.5.2",
"globby": "^9.0.0",
"husky": "^1.0.0-rc.8",
"include-media": "^1.4.8",
"jshint-stylish": "^2.0.0",
"lint-staged": "^8.1.5",
"lodash": "^4.17.4",
"mini-css-extract-plugin": "^0.5.0",
"node-sass": "^4.7.2",
"node-sass-magic-importer": "^5.0.3",
"nodemon": "^1.14.0",
"npm-run-all": "^4.1.1",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"postcss": "^7.0.11",
"postcss-cssnext": "^3.1.0",
"postcss-loader": "^3.0.0",
"prettier": "^1.13.4",
"react-dev-utils": "^7.0.1",
"request": "^2.81.0",
"rimraf": "^2.6.2",
"sass-loader": "^7.0.2",
"style-loader": "^0.23.1",
"stylelint": "^9.2.0",
"stylelint-config-prettier": "^5.0.0",
"stylelint-prettier": "^1.0.6",
"thread-loader": "^2.1.1",
"ts-jest": "^23.10.5",
"ts-loader": "^5.3.3",
"tslint": "^5.12.0",
"tslint-config-airbnb": "^5.9.2",
"tslint-config-prettier": "^1.13.0",
"tslint-plugin-prettier": "^2.0.1",
"tslint-react": "^3.6.0",
"typescript": "4.0.5",
"uglifyjs-webpack-plugin": "^2.1.1",
"webpack": "^4.10.2",
"webpack-cli": "^3.0.1",
"webpack-dev-server": "^3.1.14",
"winston": "^2.3.1"
},
I didn't originally write this webpack config and have tried moving the order of the plugin etc. to no avail.
I hope someone can help...
Cheers!

It seems you may need to add "babel-plugin-macros" before it in your plugins array. I was trawling the web for ages trying to get babel-plugin-styled-components working for Twin in Create React App (which also uses Webpack). I was using CRACO to add babel plugins, but nothing I was doing was working.
I finally stumbled upon this absolutely perfect example repo twin.examples/webpack-styled-components-typescript which contained literally everything I wanted. Here is the .babelrc file it used:
{
"presets": [
[
"#babel/preset-react",
{ "runtime": "automatic" }
],
"#babel/preset-typescript"
],
"plugins": [
"babel-plugin-twin",
"babel-plugin-macros",
"babel-plugin-styled-components"
]
}
The plugins part was what I needed, so I just copied it across and everything worked perfectly.
To check it applied to situations without babel-plugin-twin, I removed it from the plugins list and tested my setup using the alternative method for using Twin (adding import "twin.macro" at the top of the file I was styling in). The setup worked, but not if I removed babel-plugins-macro, supporting my theory.
What seems odd is CRA already loads babel-plugins-macro from babel-preset-react-app. I think actually babel-plugins-macro needs to be specifically loaded just before babel-plugin-styled-components, as I tried swapping the order of just those two and it silently broke the styling.
Hope my findings help you or someone else out there!

Related

Error: Cannot find module 'webpack/lib/node/NodeOutputFileSystem' React App after upgrading Webpack 4 to 5

Got this error after upgrading webpack from 4 to 5.
I saw this error on many other questions, but nothing seems to solve my issue.
This are my dependencies:
"dependencies": {
"#babel/polyfill": "7.4.3",
"#bootstrap-styled/color": "^1.0.9",
"#bootstrap-styled/css-mixins": "^2.0.7",
"#bootstrap-styled/css-utils": "^1.3.2",
"#bootstrap-styled/navigation-bar": "^1.9.5",
"#bootstrap-styled/provider": "^1.5.1",
"#bootstrap-styled/v4": "^3.1.5",
"bootstrap-styled": "^2.5.2",
"chalk": "2.4.2",
"compression": "1.7.4",
"connected-react-router": "6.4.0",
"cross-env": "5.2.0",
"draft-js": "^0.11.7",
"express": "4.16.4",
"fontfaceobserver": "2.1.0",
"history": "4.9.0",
"hoist-non-react-statics": "3.3.0",
"immer": "9.0.6",
"intl": "1.2.5",
"invariant": "2.2.4",
"ip": "1.1.5",
"js-sha512": "^0.8.0",
"kefir": "^3.8.6",
"lodash": "^4.17.21",
"minimist": "^1.2.5",
"prop-types": "15.7.2",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-helmet": "6.0.0-beta",
"react-intl": "2.8.0",
"react-redux": "7.0.2",
"react-router-dom": "^5.2.0",
"redux": "4.0.1",
"redux-saga": "1.0.2",
"reselect": "4.0.0",
"sanitize.css": "8.0.0",
"shortid": "^2.2.15",
"showdown": "^1.9.1",
"styled-components": "4.2.0"
},
"devDependencies": {
"#babel/cli": "7.4.3",
"#babel/core": "7.4.3",
"#babel/plugin-proposal-class-properties": "7.4.0",
"#babel/plugin-syntax-dynamic-import": "7.2.0",
"#babel/plugin-transform-modules-commonjs": "7.4.3",
"#babel/plugin-transform-react-constant-elements": "7.2.0",
"#babel/plugin-transform-react-inline-elements": "7.2.0",
"#babel/preset-env": "7.4.3",
"#babel/preset-react": "7.0.0",
"#babel/register": "7.4.0",
"add-asset-html-webpack-plugin": "3.1.3",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "10.0.1",
"babel-loader": "8.0.5",
"babel-plugin-dynamic-import-node": "2.2.0",
"babel-plugin-lodash": "3.3.4",
"babel-plugin-react-intl": "3.0.1",
"babel-plugin-styled-components": "1.10.0",
"babel-plugin-transform-react-remove-prop-types": "0.4.24",
"circular-dependency-plugin": "5.0.2",
"compare-versions": "3.4.0",
"compression-webpack-plugin": "6.0.2",
"coveralls": "3.0.3",
"css-loader": "2.1.1",
"eslint": "5.16.0",
"eslint-config-airbnb": "17.1.0",
"eslint-config-airbnb-base": "13.1.0",
"eslint-config-prettier": "4.1.0",
"eslint-import-resolver-webpack": "0.11.1",
"eslint-plugin-import": "2.17.2",
"eslint-plugin-jsx-a11y": "6.2.1",
"eslint-plugin-prettier": "3.0.1",
"eslint-plugin-react": "7.12.4",
"eslint-plugin-react-hooks": "1.6.0",
"eslint-plugin-redux-saga": "1.0.0",
"file-loader": "3.0.1",
"html-loader": "0.5.5",
"html-webpack-plugin": "^5.4.0",
"image-webpack-loader": "4.6.0",
"imports-loader": "0.8.0",
"jest-cli": "26.4.2",
"jest-dom": "3.1.3",
"jest-styled-components": "^6.3.3",
"lint-staged": "^11.0.0",
"ngrok": "3.1.1",
"node-plop": "^0.26.2",
"null-loader": "0.1.1",
"plop": "^2.7.4",
"pre-commit": "1.2.2",
"prettier": "1.17.0",
"react-app-polyfill": "0.2.2",
"react-test-renderer": "16.8.6",
"react-testing-library": "6.1.2",
"rimraf": "2.6.3",
"shelljs": "0.8.3",
"style-loader": "0.23.1",
"stylelint": "13.13.1",
"stylelint-config-recommended": "2.2.0",
"stylelint-config-styled-components": "0.1.1",
"stylelint-processor-styled-components": "1.6.0",
"svg-url-loader": "2.3.2",
"terser-webpack-plugin": "4.2.2",
"url-loader": "1.1.2",
"webpack": "^5.60.0",
"webpack-cli": "^4.9.1",
"webpack-dev-middleware": "^5.2.1",
"webpack-dev-server": "^4.3.1",
"webpack-hot-middleware": "^2.25.1",
"webpack-pwa-manifest": "^4.3.0",
"whatwg-fetch": "3.0.0"
}
}
And this is my webpack config:
/**
* COMMON WEBPACK CONFIGURATION
*/
const path = require('path');
const webpack = require('webpack');
module.exports = options => ({
mode: options.mode,
entry: options.entry,
output: Object.assign(
{
// Compile into js/build.js
path: path.resolve(process.cwd(), 'build'),
publicPath: '/',
},
options.output,
), // Merge with env dependent settings
optimization: options.optimization,
module: {
rules: [
{
test: /\.jsx?$/, // Transform all .js and .jsx files required somewhere with Babel
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: options.babelQuery,
},
},
{
// Preprocess our own .css files
// This is the place to add your own loaders (e.g. sass/less etc.)
// for a list of loaders, see https://webpack.js.org/loaders/#styling
test: /\.css$/,
exclude: /node_modules/,
use: ['style-loader', 'css-loader'],
},
{
// Preprocess 3rd party .css files located in node_modules
test: /\.css$/,
include: /node_modules/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(eot|otf|ttf|woff|woff2)$/,
use: 'file-loader',
},
{
test: /\.svg$/,
use: [
{
loader: 'svg-url-loader',
options: {
// Inline files smaller than 10 kB
limit: 10 * 1024,
noquotes: true,
},
},
],
},
{
test: /\.(jpg|png|gif)$/,
use: [
{
loader: 'url-loader',
options: {
// Inline files smaller than 10 kB
limit: 10 * 1024,
},
},
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
enabled: false,
// NOTE: mozjpeg is disabled as it causes errors in some Linux environments
// Try enabling it in your environment by switching the config to:
// enabled: true,
// progressive: true,
},
gifsicle: {
interlaced: false,
},
optipng: {
optimizationLevel: 7,
},
pngquant: {
quality: '65-90',
speed: 4,
},
},
},
],
},
{
test: /\.html$/,
use: 'html-loader',
},
{
test: /\.(mp4|webm)$/,
use: {
loader: 'url-loader',
options: {
limit: 10000,
},
},
},
],
},
plugins: options.plugins.concat([
// Always expose NODE_ENV to webpack, in order to use `process.env.NODE_ENV`
// inside your code for any environment checks; Terser will automatically
// drop any unreachable code.
new webpack.EnvironmentPlugin({
NODE_ENV: 'development',
}),
]),
resolve: {
modules: ['node_modules', 'app'],
extensions: ['.js', '.jsx', '.react.js'],
mainFields: ['browser', 'jsnext:main', 'main'],
},
devtool: options.devtool,
target: 'web', // Make web variables accessible to webpack, e.g. window
performance: options.performance || {},
});
Not sure what is missing. I checked on github (https://github.com/webpack/webpack/tree/main/lib/node) And this NodeOutputFileSystem does not seems to exist.
Any ideas?
The problem was the version of webpack-dev-plugin, I had to update to 5.2.1 With that, the app is running fine again with webpack 5.
No further configuration change was needed in my case

Compiled CSS is not being attached to JSX in react build

I am currently attempting to test the first build of my react application. All my .scss looks great in dev, however, when I build for production none of my styling is added to the jsx, unless I use :local(my-css) around every item.
for the boilerplate of the application, I used create-react-app. All the dependencies are listed in the package.json below. so far, I have tried messing with my webpack.config.prod.js to change various options for all the loaders. Among this, I have tried setting sourceMap to true and false in the css-loader options, and tried minimizing the scss config to just:
test: /\.scss/,
loader: 'style-loader!css-loader!sass-loader'
I have also tried removing the postcss-loader with no success. I have searched so many different sites trying to figure out a solution, that I ran out of articles written in English and had to translate the pages from various languages.
One other relevant thing to note is that the scss is compiled into css and added to a main.css file. main.map.css also contains the correct mappings for files. There are also .css files for every component present that contain non-minified versions of the compiled css. Image to illustrate this:
Prod Webpack Config:
{
test: /\.css$/,
exclude: /node_modules/,
loader: ExtractTextPlugin.extract(
Object.assign(
{
fallback: {
loader: require.resolve('style-loader'),
options: {
hmr: false,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
},
},
use: [
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
minimize: true,
sourceMap: shouldUseSourceMap,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
],
},
extractTextPluginOptions
)
),
},
{
test: /\.scss$/,
exclude: /node_modules/,
loader: ExtractTextPlugin.extract(
Object.assign(
{
fallback: {
loader: require.resolve('style-loader'),
options: {
hmr: false,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
},
},
use: [
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
minimize: true,
sourceMap: shouldUseSourceMap,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
{
loader: 'sass-loader',
},
],
},
extractTextPluginOptions
)
),
// Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
},
package.json:
"dependencies": {
"#babel/core": "^7.2.2",
"#babel/preset-env": "^7.2.3",
"#babel/preset-react": "^7.0.0",
"#babel/runtime": "^7.2.0",
"#kennethormandy/react-fittext": "^0.5.1",
"ajv": "^6.6.2",
"autoprefixer": "7.1.6",
"axiom": "^0.1.6",
"axios": "^0.18.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^9.0.0",
"babel-jest": "^23.4.2",
"babel-loader": "^8.0.5",
"babel-plugin-react-css-modules": "^5.0.0",
"babel-preset-react-app": "^6.1.0",
"case-sensitive-paths-webpack-plugin": "2.1.1",
"chalk": "1.1.3",
"css-loader": "0.28.7",
"dotenv": "4.0.0",
"dotenv-expand": "4.2.0",
"eslint": "4.10.0",
"eslint-config-react-app": "^2.1.0",
"eslint-loader": "1.9.0",
"eslint-plugin-flowtype": "2.39.1",
"eslint-plugin-import": "2.8.0",
"eslint-plugin-jsx-a11y": "5.1.1",
"eslint-plugin-react": "7.4.0",
"file-loader": "^1.1.11",
"fs-extra": "3.0.1",
"html-webpack-plugin": "2.29.0",
"imagemin": "^5.3.1",
"img-loader": "^3.0.1",
"jest": "20.0.4",
"lodash": "^4.17.11",
"moment": "^2.23.0",
"object-assign": "4.1.1",
"postcss-flexbugs-fixes": "3.2.0",
"postcss-loader": "2.0.8",
"postcss-scss": "^2.0.0",
"promise": "8.0.1",
"prop-types": "^15.6.2",
"proptypes": "^1.1.0",
"raf": "3.4.0",
"react": "^16.7.0",
"react-big-calendar": "^0.20.2",
"react-dev-utils": "^5.0.3",
"react-dom": "^16.7.0",
"react-google-login": "^4.0.1",
"react-icons": "^2.2.7",
"react-input-mask": "^2.0.4",
"react-linkify": "^0.2.2",
"react-redux": "^5.1.1",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-textarea-autosize": "^7.1.0",
"react-transition-group": "^2.5.2",
"redux": "^4.0.1",
"redux-axios-middleware": "^4.0.0",
"redux-logger": "^3.0.6",
"redux-persist": "^5.10.0",
"redux-thunk": "^2.3.0",
"resolve": "1.6.0",
"style-loader": "0.19.0",
"sw-precache-webpack-plugin": "0.11.4",
"url-loader": "^0.6.2",
"webpack": "3.8.1",
"webpack-dev-server": "2.9.4",
"webpack-manifest-plugin": "1.3.2",
"whatwg-fetch": "2.0.3"
},
"devDependencies": {
"extract-text-webpack-plugin": "^3.0.2",
"node-sass": "^4.11.0",
"sass-loader": "^7.1.0",
"sass": "*"
}
The only thing that seems to work is if I wrap every item in every scss file in :local(). This is not a satisfactory option, as I wish to know what is wrong with my config so I can avoid it in the future. Also, I would have to go and manually change hundreds of items which does not sound like a good time. Any insight on how to tackle this issue would be greatly appreciated.
I fixed the problem. Turns out you need to enable modules in 'css-loader' like so:
{
loader: require.resolve('css-loader'),
options: {
modules: true //must add this
importLoaders: 1,
minimize: true,
sourceMap: shouldUseSourceMap,
},
},

Trace: The node type SpreadProperty has been renamed to SpreadElement at Object.isSpreadProperty

I'm launching a react app, and here's my Webpack configuration:
'use strict'
const ExtractPlugin = require('extract-text-webpack-plugin')
const HTMLPlugin = require('html-webpack-plugin')
module.exports = {
devtool: 'eval',
entry: `${__dirname}/src/main.js`,
output: {
filename: 'bundle-[hash].js',
path: `${__dirname}/build`,
publicPath: '/',
},
mode: 'development',
performance: {
hints: false
},
plugins: [
new HTMLPlugin(),
new ExtractPlugin('bundle-[hash].css'),
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_module/,
loader: 'babel-loader',
},
{
test: /\.scss$/,
loader: ExtractPlugin.extract(['css-loader', 'sass-loader']),
},
],
},
}
Then, I have a package.json file, here are the dependencies:
"devDependencies": {
"#babel/core": "^7.1.6",
"#babel/plugin-proposal-object-rest-spread": "^7.0.0",
"#babel/preset-env": "^7.1.6",
"#babel/preset-react": "^7.0.0",
"and": "0.0.3",
"babel-cli": "^6.26.0",
"babel-core": "^7.0.0-bridge.0",
"babel-loader": "^8.0.4",
"eslint": "^5.9.0",
"install": "^0.12.2",
"jest": "^23.6.0",
"npm": "^6.4.1",
"webpack-cli": "^3.1.2"
},
"dependencies": {
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"css-loader": "^1.0.1",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.11.0",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"resolve-url-loader": "^3.0.0",
"sass-loader": "^7.1.0",
"webpack": "^4.25.1",
"webpack-dev-server": "^3.1.10"
}
I have tried plenty of ways of updating babel packages up to 7th version, changing babelrc config, what ever.
The project though compiles, but in the beginning of compilation I get the following message:
Trace: The node type SpreadProperty has been renamed to SpreadElement
at Object.isSpreadProperty
And about of hundred rows like that. After all that rows being printed out, the compilation process proceeds and is completed successfully.
What's that and how can I get rid of this rows?
here is the final setting that solved problem for me.
.babelrc
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
"#babel/plugin-proposal-object-rest-spread"
]
}
For a better understanding, here is my package.json's devDependencies:
"devDependencies": {
"#babel/core": "^7.1.6",
"#babel/plugin-proposal-object-rest-spread": "^7.0.0",
"#babel/plugin-transform-object-assign": "^7.0.0",
"#babel/plugin-transform-react-jsx": "^7.1.6",
"#babel/preset-env": "^7.1.6",
"#babel/preset-react": "^7.0.0",
"babel-loader": "8.0.4",
"prop-types": "15.6.2",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"style-loader": "^0.23.1",
"utils": "^0.3.1",
"webpack": "4.26.1",
"webpack-cli": "3.1.2",
"webpack-dev-server": "^3.1.10"
}
Here is my webpack.config.js module's section:
module: {
rules: [
{
test: /\.(js|jsx)$/ ,
exclude: /node_modules/,
loaders: "babel-loader"
}
]
}
This issue is occurring due to using outdated
`"babel-plugin-transform-object-rest-spread"`
update this in package.json
`"#babel/plugin-proposal-object-rest-spread": "^7.0.0",`
and update your .babelrc.js file
in my case it looks like this
const isTest = String(process.env.NODE_ENV) === 'test'
module.exports = {
presets: [["#babel/env", { modules: isTest ? 'commonjs' : false }, "#babel/react"]],
plugins: [
'syntax-dynamic-import',
'transform-class-properties',
'#babel/plugin-proposal-object-rest-spread',
],
}
this solves my problem
Before in my .babelrc
i was using the plugin: ["transform-object-rest-spread", { "useBuiltIns": true }],
then i switch it to "#babel/plugin-proposal-object-rest-spread" and all those warnings went away, which has been very nice.
`
In my case, I was not implementing the Webpack configuration and still error was there. After so many solutions implementation, the error was same or once an error was removed another appeared. Finally, I deleted .bablerc, .babelrc and package-lock.json files and ran rm -rf node_modules && npm install and then ran react-native run-android and it worked for me. :-)
If nothing up here worked, Just remove plugins from old babel. In my case removing following spread plugin had disappeared the thing.
plugins: ["transform-object-rest-spread"]

Grunt-webpack: How to force production mode without build server access?

I do not have access to the build server, I can only modify the files. So I can't add any flags to the grunt command. The build server seems to simply run "grunt". I have tried all sorts of configurations without success, suggested here and other sources.
From my understanding webpack should by default build a production build but perhaps it was introduced in later versions of webpack only or not supported by grunt-webpack?
Gruntfile.js
const path = require('path');
const BUILD_DIR = path.resolve(__dirname, 'build/');
const APP_DIR = path.resolve(__dirname, 'src/');
module.exports = function config(grunt) {
grunt.option("p"); // attempt to force a -p flag on webpack, doesn't work
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
webpack: {
reactcatalog: {
entry: ['core-js/fn/promise', 'core-js/fn/map', 'formdata-polyfill', 'whatwg-fetch', `${APP_DIR}/index.js`],
output: {
path: BUILD_DIR,
filename: 'react-catalog.js',
},
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
loaders: [{
test: /\.jsx?$/,
include: APP_DIR,
loader: ['babel-loader', 'eslint-loader'],
},
{
test: /\.css$/,
include: APP_DIR,
loader: 'style-loader!css-loader',
},
{
test: /\.scss$/,
include: APP_DIR,
loaders: ['style-loader', 'css-loader', 'sass-loader'],
},
{
test: /\.svg$/,
loader: 'babel-loader!react-svg-loader',
}],
},
stats: {
colors: true,
},
progress: false,
failOnError: false,
},
},
});
grunt.loadNpmTasks('grunt-webpack');
grunt.registerTask('default', ['webpack']);
};
package.json:
{
"name": "react-catalog",
"version": "0.1.0",
"description": "ES based React catalog",
"private": true,
"dependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-plugin-transform-proto-to-assign": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2017-node7": "^0.5.2",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-runtime": "^6.26.0",
"breakpoint-sass": "^2.7.1",
"core-js": "^2.5.5",
"css-loader": "^1.0.0",
"formdata-polyfill": "^3.0.10",
"grunt-webpack": "^3.1.1",
"loaders.css": "^0.1.2",
"node-sass": "^4.9.3",
"npm-install": "0.0.1",
"prop-types": "^15.6.1",
"react": "^16.3.0",
"react-device-detect": "^1.3.4",
"react-dom": "^16.3.0",
"react-image-gallery": "^0.8.8",
"react-image-lightbox": "^4.6.0",
"react-infinite-scroller": "^1.1.4",
"react-intl-universal": "^1.10.1",
"react-md-spinner": "^0.2.5",
"react-move": "^2.6.0",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-scroll": "^1.7.9",
"react-skylight": "^0.5.1",
"react-spinners": "^0.2.5",
"react-svg": "^2.2.18",
"react-svg-loader": "^2.1.0",
"react-svg-spinner": "^0.2.0",
"react-tabs": "^2.2.2",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"run": "^1.4.0",
"sass-loader": "^7.0.1",
"sassimple": "^1.3.8",
"style-loader": "^0.20.3",
"susy": "^3.0.5",
"webpack": "^3.11.0",
"whatwg-fetch": "^2.0.4"
},
"devDependencies": {
"babel-eslint": "8.0.1",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-loader": "^2.0.0",
"eslint-plugin-flowtype": "^2.49.3",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.8.2",
"webpack-livereload-plugin": "^1.2.0"
},
"scripts": {
"webpack": "webpack", // does not help to add a -p flag here as it doesn't seem to use this
"webpack:watch": "webpack --watch"
}
}
I have tried all sorts of things without any success.
plugins: [
new webpack.DefinePlugin({
process: {
env: {
NODE_ENV: 'production'
}
}
})
]
becomes:
https://user-images.githubusercontent.com/893783/44646081-34498c00-a9da-11e8-8bd1-66c70b8ba9f5.png
Finally I figured out the solution myself!
const path = require('path');
++ const webpack = require('webpack');
const BUILD_DIR = path.resolve(__dirname, 'build/');
const APP_DIR = path.resolve(__dirname, 'src/');
module.exports = function config(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
webpack: {
reactcatalog: {
// ...
plugins: [
new webpack.DefinePlugin({
process: {
env: {
NODE_ENV: '"production"'
}
}
}),
new webpack.optimize.UglifyJsPlugin()
]
},
},
});
};

State of the art webpack / React configuration

I'm trying to setup a webpack 2, babel, and React configuration for achieving:
Native ES6/ES7 features
Tree shaking builds
Hot reloading
I had a demo repo but it has distinct stuff mixed, like even JSHint and ESLint at the same time.
I'd like to get my setup up and running and get suggestions for best practices
So, as first option I tried to use babel-preset-env. Then after some dependencies being installed. I ran into this issue:
ERROR in ./src/main.jsx
Module build failed: SyntaxError: 'import' and 'export' may only appear at the top level (3:0)
However, the first line in my code is import 'babel-polyfill'; then just import's.
This is how my Babel config file looks like:
{
"presets": [
[
"env",
{
"modules": false,
"targets": {
"browsers": ["last 2 versions"]
}
}
],
"react"
],
"plugins": [
"babel-plugin-transform-class-properties",
"transform-react-require"
]
}
This is what my development webpack config file looks like:
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const path = require('path');
const buildPath = path.resolve(__dirname, 'build');
const nodeModulesPath = path.resolve(__dirname, 'node_modules');
const TransferWebpackPlugin = require('transfer-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const config = {
//Entry points to the project
entry: [
'babel-polyfill',
'webpack/hot/dev-server',
'webpack/hot/only-dev-server',
path.join(__dirname, '../src/main.jsx')
],
//Config options on how to interpret requires imports
resolve: {
extensions: [".js", ".jsx"]
},
//Server Configuration options
devServer:{
contentBase: 'build', //Relative directory for base of server
devtool: 'eval',
hot: true, //Live-reload
inline: true,
port: 3000, //Port Number
host: 'localhost', //Change to '0.0.0.0' for external facing server
proxy: {
'^\/api': {
target: 'http://127.0.0.1:9090'
}
},
historyApiFallback: true
},
devtool: 'eval',
output: {
path: buildPath, //Path of output file
filename: 'app.js'
},
plugins: [
new webpack.DefinePlugin({
API_BASE: '""',
PRODUCTION: false,
'process.env.NODE_ENV': '"development"'
}),
//Enables Hot Modules Replacement
new webpack.HotModuleReplacementPlugin(),
//Allows error warnings but does not stop compiling. Will remove when eslint is added
new webpack.NoEmitOnErrorsPlugin(),
//Moves files
new TransferWebpackPlugin([
{from: 'www'}
], path.resolve(__dirname, "src")),
new ExtractTextPlugin("main.css")
],
module: {
rules: [
{
//React-hot loader and
test: /\.(js|jsx)$/, //All .js and .jsx files
loaders: [ 'babel-loader', 'react-hot-loader'],
//react-hot is like browser sync and babel loads jsx and es6-7
exclude: [nodeModulesPath]
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract({
fallback: 'style',
use: 'css'
})
},
{
test: /\.svg$/,
loader: 'svg-sprite?' + JSON.stringify({
name: '[name]_[hash]',
prefixize: true
})
}
]
}
};
module.exports = config;
And this below is package.json
{
"name": "LumaHealth",
"version": "1.0.0",
"description": "LumaHealth",
"main": "start.js",
"scripts": {
"start": "webpack --config ./webpack/webpack.config.development.js",
"build": "webpack --config ./webpack/webpack.config.production.js",
"clean": "rm build/app.js"
},
"repository": {
"type": "git",
"url": "git#github.com:lumahealthhq/web-app.git"
},
"keywords": [],
"author": "Marcelo Oliveira",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.0",
"babel-core": "^6.24.0",
"babel-eslint": "^7.2.1",
"babel-plugin-react-require": "^3.0.0",
"babel-plugin-transform-class-properties": "^6.23.0",
"babel-preset-env": "^1.2.2",
"babel-preset-react": "^6.23.0",
"css-loader": "^0.26.4",
"enzyme": "^2.0.0",
"eslint": "^3.7.1",
"eslint-config-airbnb": "^14.1.0",
"eslint-loader": "^1.7.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.4.1",
"extract-text-webpack-plugin": "^2.1.0",
"html-webpack-plugin": "^2.28.0",
"nyc": "^10.1.2",
"postcss-loader": "^1.3.3",
"postcss-nested": "^1.0.0",
"react-addons-test-utils": "^15.4.1",
"sinon": "^1.17.2",
"style-loader": "^0.13.2",
"sw-precache": "^5.0.0",
"transfer-webpack-plugin": "^0.1.4",
"webpack": "^2.3.2",
"webpack-dev-server": "^2.4.2"
},
"dependencies": {
"babel-core": "^6.5.2",
"babel-eslint": "^7.0.0",
"babel-plugin-transform-react-require": "^1.0.1",
"babel-polyfill": "^6.23.0",
"co": "^4.6.0",
"express": "^4.12.3",
"file-loader": "^0.10.1",
"humps": "^2.0.0",
"isomorphic-fetch": "^2.2.1",
"local-storage": "^1.4.2",
"lodash": "^4.16.4",
"material-ui": "^0.17.0",
"moment": "^2.15.2",
"q": "^1.4.1",
"react": "^15.4.1",
"react-dom": "^15.4.1",
"react-redux": "^5.0.3",
"react-router": "^3.0.2",
"react-router-redux": "^4.0.6",
"react-slick": "^0.14.4",
"react-tap-event-plugin": "^2.0.0",
"react-web-notification": "^0.2.3",
"redux": "^3.6.0",
"redux-form": "^6.1.1",
"redux-logger": "^2.7.0",
"redux-socket.io": "^1.3.1",
"redux-thunk": "^2.1.0",
"socket.io-client": "^1.7.2",
"url-loader": "^0.5.7",
"vanilla-masker": "^1.0.9"
}
}
I recently upgraded my boilerplate from webpack 1 to webpack 2, feel free to get any information / concept from the webpack config file there, hope it helps.
https://github.com/iroy2000/react-redux-boilerplate

Resources