Webpack 2: How to exclude all node_modules except for - javascript

I need to have babel run on /node_modules/identicons/ However I still want to exclude all other packages.
Reason is the identicons package is using template strings and breaks when I run
"webpack -p"
String in question (node_modules/identicons/index.js):
str += `<rect x="${x}" y="${y}" width="${xside}" height="${xside}" style="fill:${color}" />`
Webpack.config.babel
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
//include: /node_modules/identicons/,
use: ["babel-loader"]
},
How would that pattern be written?

I think you can use regex, something like
exclude: [
/node_modules\/(?!identicons).*/
]

Another way:
exclude: [
{
test: [
path.resolve(__dirname, './node_modules'),
],
exclude: [
path.resolve(__dirname, './node_modules/MODULE_TO_INCLUDE'),
path.resolve(__dirname, './node_modules/ANOTHER_MODULE_TO_INCLUDE'),
]
}
]
It worked for me.

You could exclude everything from node_modules that is not identicons:
exclude: /node_modules\/(?!identicons$)/

Exclude whole node_modules folder, except required module:
{
test: /\.js$/,
exclude: /node_modules\/(?!identicons\/).*/,
}
https://github.com/webpack/webpack/issues/2031#issuecomment-219040479

Related

Webpack - Include file multiple times

I want to include a file twice through two different loaders. The reasoning is I want to display code snippets in ES6 while allowing them to be run in browsers not supporting the syntax.
Effectively what I would like to achieve is the below but with both loaders results being included in the output -
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.(js|jsx)$/,
include: /app\/examples/,
use: [
{
loader: "file-loader",
options: {
regExp: /app\/examples\/([^\/]+)\/([^\.]+)+\.jsx?$/,
name: 'examples/[1]/[2].example',
}
}
]
}
With the above in my webpack config
import example from '../../examples/simple/ex1'
results in
Module {default: "examples/simple/ex1.example", __esModule: true, Symbol(Symbol.toStringTag): "Module"}
Rather than the code run through babel as I would have hoped for.
const multi = require('multi-loader');
const combineLoaders = require('webpack-combine-loaders');
module: {
loaders: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
include: /app\/examples/,
loader: multi(combineLoaders([
{ loader: 'file-loader' },
{ loader: 'babel-loader' },
]))
},
]
}
This should do the trick. you have to also use combineLoaders as you have to use options object. inside combine loaders array you can pass loader configuration also.
I couldn't manage to handle this with loaders in the end - although with further reading I don't believe this was the correct approach anyway. Instead i'm now using copy-webpack-plugin to copy the files -
plugins: [
new CopyWebpackPlugin([ {
from: path.join(rootDir, 'app', 'examples'),
to: path.join(outputDir, 'examples')
}])
],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: "babel-loader"
}
]
}

Custom less-loader in Laravel Mix

I am trying to import LESS file in my JS code.
But I get error like - 'You may need an appropriate loader to handle this file type.'.
Version - "laravel-mix": "^0.11.4"
In my webpack.mix.js I tried this code
mix.webpackConfig({
module: {
rules: [{
test: /\.less$/,
loader: ['style-loader','css-loader','less-loader']
}]
}
})........
But I still getting the same error.
const path = require('path');
mix.webpackConfig({
module: {
rules: [
{
test: /\.less$/,
loader: "style-loader!css-loader!less-loader",
exclude: [
path.resolve(__dirname, "node-modules"),
path.resolve(__dirname, "resources/assets/less"),
],
},
]}
})
Works for my issue

Using loaders in webpack got error

What's wrong with my code below? I got error bundling less files, for example
ERROR in ./~/less-loader!./resources/assets/js/bundle/components/widget/clock.less
Below is my webpack.config.js
module: {
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loaders: ["react-hot", "babel-loader","babel?presets[]=react,presets[]=es2015,presets[]=stage-0"],
},
{
test: /\.less$/,
loaders: ['style-loader','less-loader']
}
]
}
I'm trying to implement hot reloading.
If you're using webpack v1:
module: {
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loaders: ["babel-loader","babel?presets[]=react,presets[]=es2015,presets[]=stage-0"],
},
{
test: /\.less$/,
loaders: ['style-loader','css-loader', 'less-loader']
}
]
}
or if you're using webpack v2:
module: {
rules: [
{
test: /\.js?$/,
exclude: /node_modules/,
use: ["babel-loader","babel?presets[]=react,presets[]=es2015,presets[]=stage-0"],
},
{
test: /\.less$/,
use: ['style-loader','css-loader', 'less-loader']
}
]
}
As your webpack is reaching the less-loader, I don't see any issue in configuration. Otherwise it prompts issue with webpack configuration.
Now you need to check your file here resources/assets/js/bundle/components/widget/clock.less
It is the place which is having issues.
If not please share complete error, this is just the first line of error.

Webpack autoprefix not working with suggested configuration

I'm trying to get sass compilation to work with webpack using sass-loader and autoprefixer and postcssloader. The sass compilation is working successfully. However following along with the documentation for autoprefixing isn't working:
Add PostCSS Loader to webpack.config.js. Put it before css-loader and style-loader. But after sass-loader, if you use it.
So with just sass working, I had
loaders: [
{
test: /\.scss$/,
loaders: ["style", "css", "sass"]
}
],
sassLoader: {
includePaths: [path.resolve(__dirname, "./build")]
}
But reading the above, it seems like what I need is something like this.
'loaders': [
{
test: /\.scss$/,
loaders: ["sass"]
},
{
test: /\.css$/,
loaders: [
'style-loader',
'css-loader?modules&importLoaders=1',
'postcss-loader'
]
},
{
test: /\.scss$/,
loaders: ["style", "css"]
}
],
sassLoader: {
includePaths: [path.resolve(__dirname, "./build")]
}
Also regarding:
Then create postcss.config.js:
I have made that file and put this inside
module.exports = {
plugins: [
require('precss'),
require('autoprefixer')
]
}
However, I get the following error
Invalid CSS after "...load the styles": expected 1 selector or at-rule, was "var content = requi" in /path/to/scss/index.scss
and everything breaks. What's wrong with this configuration?
For my project, I arranged loaders in the following order to make it work with scss files.
loaders: [
'css-loader?modules&importLoaders=1',
'postcss-loader',
'sass-loader'
]
I found success with doing something like this:
module.exports = {
module: {
loaders: [
{
test: /\.jsx$/,
exclude: /node_modules/,
loader: "babel-loader",
query: { presets: ["es2015", "stage-0", "react", "react-hmre" ]}
},
{
test: /\.scss$/,
loaders: ["style-loader", "css-loader", "postcss-loader", "sass-loader"]
}
]
},
postcss: function() {
return [
require('precss'),
require('autoprefixer')
];
}
};

How to define precendence of loaders in webpack?

I use webpack to build a server build, the config looks like:
loaders: [
{
test: /\.js$/,
exclude: [/node_modules/],
loader: "babel-loader",
query: {
presets: ["es2015", "react"],
plugins: ["transform-object-rest-spread"]
}
}, {
test: new RegExp(`/
stardust
|redux-form
|axios
|shuffle
`),
loader: "null-loader"
}
]
Idea is pretty simple: if something matches regexp in the second loader, it shouldn't be loaded. It works well until I'm loading this:
import shuffle from "shufflejs/dist/shuffle.min.js"
It matches to both loaders and loaded to the bundle. How can I make null-loader precedence higher than the babel?
You can use pre-loaders as described in webpack documentation basically you will use your regex with null-loader as a pre-loader this way it will be resolved before your loaders. So, if regex matches null-loader will be used and it won't load the module.
Here's the modified code:
preLoaders: [
{
test: new RegExp(`/
stardust
|redux-form
|axios
|shuffle
`),
loader: "null-loader"
}
],
loaders: [
{
test: /\.js$/,
exclude: [/node_modules/],
loader: "babel-loader",
query: {
presets: ["es2015", "react"],
plugins: ["transform-object-rest-spread"]
}
}
]
The only other solution I can think of is to rely on regular expressions to exclude files containing these word. I'm not an expert in Regex but I'll try to suggest an expression:
loaders: [
{
test: /^(?!stardust|redux-form|axios|shuffle)([a-zA-Z]+)\.js$/,
exclude: [/node_modules/],
loader: "babel-loader",
query: {
presets: ["es2015", "react"],
plugins: ["transform-object-rest-spread"]
}
}
]

Categories

Resources