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"
}
]
}
Related
I'm using the new version of Ecma Script preset in Webpack 4 but It still throws the error.
(currently, my bundle.js file size is about 5MB and it's so huge. In addition to UglifyJs, please let me know any other advice to decrease bundle size in Webpack 4 and ReactJs if you know)
Here is the error:
ERROR in bundle.js from UglifyJs
Unexpected token: keyword (const) [bundle.js:4981,0]
and here is my Webpack config:
{
...
...
module: {
rules: [
{
query: {
presets: ['react', 'env', 'stage-3'],
compact: false
},
test: /\.js$/,
loader: 'babel-loader',
exclude: /(node_modules|bower_components)/
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader', 'sass-loader']
})
},
{
test: /\.(eot|otf|ttf|woff|woff2)$/,
loader: 'file-loader?name=./fonts/[name].[ext]'
},
{
test: /\.(jpg|jpeg|png|gif|svg)$/,
loader: 'file-loader?name=./images/[name].[ext]'
},
{
test: /\.css$/,
exclude: /node_modules/,
include: [
resolve(__dirname, "not_exist_path")
],
loader: 'style-loader!css-loader'
},
]
},
...
...
optimization: {
minimizer: [new UglifyJsPlugin()]
},
}
What am I doing wrong? where is the problem?
I've stumbled upon this repo vue-jekyll which is so close to my needs..
It's missing one thing though, the ability to add <style lang="scss" scoped>...</style> to the single file components, example here.
It seems that the original dev is MIA on this project - so I'm reaching out to anyone that might know how to add this functionality to Webpack. The current config is as follows:
module.exports = {
entry: {
// 🎌 OUR SOURCE FILE 🎌
jsSource: './js/source.js'
},
output: {
// 🎌 OUR DESTINATION 🎌
filename: './js/bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
},
{
test: /\.json$/,
use: 'json-loader'
},
{
test: /\.vue$/,
use: 'vue-loader'
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js' // 'vue/dist/vue.common.js' for webpack 1
}
}
}
Any ideas? :/
I have this code In my webpack.config.prod.js and I was wondering how do I exclude all json except one in a specific path like src/configs/configs
exclude: [
/\.html$/,
/\.(js|jsx)$/,
/\.css$/,
/\.json$/,
/\.bmp$/,
/\.gif$/,
/\.jpe?g$/,
/\.png$/,
],
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
}
...
According to the Webpack documentation, you can do something like this.
exclude: {
test: [
/\.html$/,
/\.(js|jsx)$/,
/\.css$/,
/\.json$/,
/\.bmp$/,
/\.gif$/,
/\.jpe?g$/,
/\.png$/,
],
exclude: [
'src/configs/configs/your.json'
]
}
To make exclude work I had to escape the dot in the specific file I wanted to exclude. Here's an example of excluding favicon.ico from a general rule and adding a special rule for it:
{
test: /\.(ico|jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
exclude: /favicon\.ico$/,
loader: 'file-loader',
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
// A special rule for favicon.ico to place it into build root directory.
{
test: /favicon\.ico$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash:8]',
},
},
For Webpack 5:
{
test: /\.(png|svg|jpg|jpeg|gif)$/,
type: 'asset/resource',
},
{
test: /favicon\.ico$/,
type: 'asset/resource',
generator: {
filename: '[name][ext]',
},
},
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.
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"]
}
}
]