I'm developing a core library, where different components can be cloned and attach themselves.Each component directory has a package.json file. my core is using webpack and when I run the build command it throw error like this
ERROR in ./src/clients/products/src/Products.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /Users/mughees/Desktop/midgard-react/src/clients/products/src/Products.js: Unexpected token (46:17)
If I remove package.json from subrepo/module it compiles fine.
This is my webpack right now.
const path = require("path");
const webpack = require("webpack");
module.exports = {
entry: ["babel-polyfill", "./src/index.js"] ,
mode: "development",
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
loader: "babel-loader",
options: { presets: ["@babel/env"] }
},
{
test: /\.(css|scss)$/,
use: [
"style-loader",
"css-loader",
"sass-loader"
]
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
"file-loader",
{
loader: "image-webpack-loader",
options: {
bypassOnDebug: true,
disable: true,
},
},
],
}
]
},
resolve: {
extensions: ["*", ".js", ".jsx"],
modules: [path.resolve(__dirname, './src'), 'node_modules'],
alias: {
store: path.resolve(__dirname, './src/store'),
}
},
output: {
path: path.resolve(__dirname, "dist/"),
publicPath: "/dist/",
filename: "bundle.js"
},
devServer: {
contentBase: path.join(__dirname, "public/"),
port: 3000,
publicPath: "http://localhost:3000/dist/",
historyApiFallback: true,
hotOnly: true
},
plugins: [new webpack.HotModuleReplacementPlugin()]
};
The problem is related to babel and not webpack. I had it before.
Here is a test of having sub-directory with package.json
file which after running webpack it throws an error like:
ERROR in ./home/three-JSX-main-patterns.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /var/www/html/ir/jsfun/home/three-JSX-main-patterns.js: Unexpected token (5:27)
and complains about JSX in my case
What's your use case?
You want to compile node_modules?
babel.config.js is for you!
You have a static configuration that only applies to your simple single package?
.babelrc is for you!
In my case I had .babelrc
like this:
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
}
and I replaced it with babel.config.js
like so:
module.exports = function (api) {
api.cache(true);
const presets = ["@babel/preset-env", "@babel/preset-react"];
return {
presets
};
}
And it works fine.