I want to handle my jsx code, so I write webpakc.config.js like this:
{
test: /\.js$/,
loaders: ['react-hot', 'babel-loader?presets[]=es2015'],
exclude: /node_modules/
}
But it didn't handle my jsx code and throw an error like this:
The error threw in terminal
By Google I find I need to add presets['react'] to my config file. So I update config like this:
{
test: /\.js$/,
loaders: ['react-hot', 'babel'],
query: {
presets: ['react', 'es2015']
},
exclude: /node_modules/
}
But it threw another error:
A new error threw after update config file
I am a fresher in webpack, What should I do?
The first error seems to be a syntax error in your JSX. Difficult to tell what it is from the comment. Try posting the JSX file contents.
About the second error:
Query params for a specific loader needn't necessarily be specified as a JSON object. You can specify them as a query string adjoining the loader name as well. Eg. the same config can be expressed with this line:
loaders: ['react-hot', 'babel?presets[]=react,presets[]=es2015']
Of course, you'll need to remove the query JSON once you use the above.
More info here: https://webpack.github.io/docs/using-loaders.html#query-parameters
Related
We're having a problem with our Vue.js application on Windows 10 / IE 11.
The application was giving SCRIPT1003: Expected ':' until we updated out babel.config to the following:
module.exports = {
presets: [
[
'#vue/cli-plugin-babel/preset',
{
targets: {
'ie': '11'
}
}
]
]
}
At which point the error is now SCRIPT1002: Synxax Error chunk-vendors.js (11365, 9311) which appears to relate to the vuelidate node module.
It appears that I need to exclude the above package, but I dont understand where the syntax should go.
It's also likely that there will be multiple packages that I need to exclude.
The base project was built using vue-cli 4.4.1 and the config files haven't moved far from the stock install
Do you want to transpile the modules? You could use exclude property in webpack.config.js or babel.config.js to transpile modules.
You could change this line:
...
exclude: /node_modules/,
...
into this:
...
exclude: /node_modules\/(?!name-of-untranspiled-module)/,
...
If you need to exclude more than one module you can extend the exception list like so:
exclude: /node_modules\/(?![module1|module2])/
For more information, you could refer to this link.
I am trying to render a calendar from https://github.com/wix/react-native-calendars
I am getting a failed to compile error:
./node_modules/react-native-calendars/src/expandableCalendar/asCalendarConsumer.js
Module parse failed: Unexpected token (11:8)
You may need an appropriate loader to handle this file type.
| render() {
| return (
| <CalendarContext.Consumer>
| {(context) => (
| <WrappedComponent
I think is based on my webpack setup:
// webpack.config.js
module.exports = {
plugins: ["#babel/plugin-syntax-dynamic-import"],
resolve: {
alias: {
'react-native$': 'react-native-web'
},
},
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: ['babel-preset-env', 'babel-preset-stage-0'],
plugins: ["#babel/plugin-syntax-dynamic-import"],
}
},
{
test: /\.js$/,
loader: 'babel-loader',
options: {
presets: ['babel-preset-env', 'babel-preset-stage-0'],
}
},
{
test: /\.ttf$/,
loader: "url-loader", // or directly file-loader
include: path.resolve(__dirname, "node_modules/react-native-vector-icons"),
},
]
}
But I am not really an expert on webpack.
The library seems to work with React Native Web - so what am I doing wrong that is causing me to not have it work with my setup?
Found the solution. Make sure to INCLUDE it in babel-loader. To fix it do the following:
1- Make sure to include the package folder in the babelLoader configuration in webpack.config.js as
include: [path.resolve(appDirectory, 'node_modules/react-native-calendars'), ...]
NOTE: Don't forget to also include all the directories that require babel-loader.
2- (Might need it)- You might need to go to node_modules/react-native-calendars/src/calendar/index.js and to node_modules/react-native-calendars/src/agenda/index.js and REMOVE the propTypes declarations and the ViewPropTypes import from react-native on lines 2, 20, and 32 on both files. Here's how */agenda/index.js is supposed to look like. Until the devs fix this, avoid updating the package or you will lose the changes. (I am including this because it happened to me right after step 1 so just in case)
https://gist.github.com/iosvanyd1/abd18bd35ce3fdcb635100ce5d5b0beb
I've tried to add this to my nuxt.config file:
build: {
extractCSS: true,
extend(config, ctx) {
config.module.rules.push({
test: /\.mp3$/,
include: '~/assets/sounds',
loader: 'file-loader',
});
}
}
but no use, it always gives me the error:
Module parse failed: Unexpected character '' (1:3) friendly-errors 13:48:16
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
Any idea?
I use some npm packages in my project. Two of them have the wrong main-field. Is it possible to override them?
I use webpack. I found a solution here.
This works for the main field but I also need a css-file from the same package. I refer it with ~package/css/style.css in my index.scss file. With the solution above it resolves the path with path/to/main.js/css/style.css (with main.js) instead of path/to/css/style.css (without main.js).
I could refer it directly ../node_modules/path/to/css/style.css but I think thats ugly.
So is there an other solution with webpack or npm to override this main field?
-- EDIT --
I use bootstrap-treeview as package. I refer it in index.scss like so
#import '~bootstrap-treeview/src/css/bootstrap-treeview.css';. This works.
When I add 'bootstrap-treeview': path.join(_path, 'node_modules', 'bootstrap-treeview', 'src', 'js', 'bootstrap-treeview.js') as alias in webpack import 'bootstrap-treeview'; works but the css not (as describes above).
-- EDIT 2 --
webpack.conf.js:
resolve: {
extensions: ['', '.js'],
modulesDirectories: ['node_modules'],
alias: {
// bootstrap-treeview alias
'bootstrap-treeview': path.join(_path, 'node_modules', 'bootstrap-treeview', 'src', 'js', 'bootstrap-treeview.js')
}
},
module: {
loaders: [
{
test: /\.css$/,
loaders: [
'style-loader',
'css-loader?sourceMap',
'postcss-loader'
]
},
{
test: /\.(scss|sass)$/,
loader: 'style-loader!css-loader?sourceMap!postcss-loader!sass-loader?outputStyle=expanded&sourceMap=true&sourceMapContents=true
}
]
}
index.scss see above.
Error with bootstrap-treeview alias:
Module not found: Error: Cannot resolve 'file' or 'directory' /home/ekf/develop/generator-angular-webpack/node_modules/bootstrap-treeview/src/js/bootstrap-treeview.js/src/css/bootstrap-treeview.css in ...
Error without alias:
Module not found: Error: Cannot resolve module 'bootstrap-treeview' in ...
just in case
webpack scss loader config
module: {
loaders: [
{
test: /\.css$/,
exclude: /node_modules/,
loader: "style-loader!css-loader"
},
{
test: /\.scss$/,
exclude: /node_modules/,
loader: "style-loader!css-loader!sass-loader"
}
]
}
The problem is that your alias points directly to the JS file, instead of pointing to the common ancestor of both the JS and the CSS. It's nice and convenient to be able to import Treeview from "bootstrap-treeview" but it leads to the problem you're describing.
Instead, you could specify a higher level alias:
resolve: {
alias: {
// bootstrap-treeview alias
'bootstrap-treeview': path.join(_path, 'node_modules', 'bootstrap-treeview', 'src')
}
},
and get the JS as import Treeview from "boostrap-treeview/js/bootstrap-treeview.js". This allows you to get the CSS as require("bootstrap-treeview/css/bootstrap-treeview.css").
You might be able to get clever about it and tell Webpack to look for CSS files in ~/css/ and JS files in ~/js/ but that would be adding more magic for (IMHO) little gain.
Is there a way to try to load xx.less for every xx.js being loaded?
For now, we add require 'xx.less' at the top of every relevant js file and it a little ugly.
What I ended doing was improving the imports loader to add an option to import a less file for every jsx file with the same name if it exists.
My improved import loader: https://github.com/welldone-software/imports-loader
The pull request: https://github.com/webpack/imports-loader/pull/12
For example dropping mainview.less in the same directory as mainview.jsx, would add a require("mainview.less") import to the top of the jsx file:
loaders: [
{ test: /\.jsx?$/, loaders: ['imports?null=[./{name}.less]', 'react-hot', 'babel'] },
{ test: /\.less$/, loader: 'style!css!less' }
]