Webpack doesn't babelize some node_modules with ES6 - javascript

I have been having issues with webpack and some node_modules libraries not being "babelized" in the outputed bundle. Most notably ES6 syntax such as 'const'.
Up until now I would solve this problem by including the specific libraries that would create that issue in the babel-loader path to force their babelization. However in this particular case, the unbabelized code seems to come from a library that isn't in my package.json, which means it must be nested in another library.
How can I track down from which library that nested library comes from ?
Or is there a simpler solution to ensure that the entirety of my modules don't remain in ES6 form ?
Below is my webpack.config.js file :
var webpack = require('webpack');
var path = require('path');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
var BUILD_DIR = path.resolve(__dirname, 'static/public/js');
var APP_DIR = path.resolve(__dirname, 'static/js');
var CSS_DIR = path.resolve(APP_DIR, 'css');
var config_function = function(env){
var config = {
entry: ['babel-polyfill', APP_DIR + '/index.js'],
output: {
path: BUILD_DIR,
filename: env && env.prod ? 'bundle.min.js' : 'bundle.js'
},
plugins: env && env.prod ? [
new UglifyJSPlugin({uglifyOptions:{
ecma: 5,
mangle: {
safari10: true
},
compress: true
}})
]: [],
module: {
loaders: [{
test: /\.jsx?/,
include: [APP_DIR, /node_modules\/snakecase-keys/, /node_modules\/redux-api-middleware/,/node_modules\/map-obj/], // Those are some of the libraries that are not babelized...
loader: 'babel-loader',
query: {
presets: ['es2015', 'react', 'stage-2'] //, 'react-hmre']
}
}
, {
test: /\.scss$/,
loaders: ['style-loader', 'css-loader', 'sass-loader']
}, {
test: /\.json$/,
loader: 'json-loader'
}, {
test: /\.less$/,
include: CSS_DIR,
loaders: ['style-loader', 'css-loader', 'less-loader']
},
{
test: /\.css$/,
loaders: ['style-loader', 'css-loader']
}
]
}
};
return config;
}
module.exports = config_function;

Related

webpack Module not found issue

I'm new in Webpack (before I used Gulp) and now I want to move the existing application from Gulp to Webpack.
But I'm getting issues like Module not found: Error: Can't resolve '/src/app/index.js' in '/var/www/project' or Module not found: Error: Can't resolve '/src/styles/main.scss' in '/var/www/project' and same for every file i'm using on my enty chain.
Here is my file structures:
package.json
.env
.env.example
webpack.conf.js
src/
app/
index.js
other js/vue related files
styles/
main.scss and all related styles
public/
index.html
favicon.ico
images/
all images
and here is mine webpack.conf.js:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const Dotenv = require('dotenv-webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
const ENV = process.env.APP_ENV;
const isDev = ENV === 'dev'
const isProd = ENV === 'prod';
function setDevTool() {
if (isDev) {
return 'inline-source-map';
} else if (isProd) {
return 'source-map';
} else {
return 'eval-source-map';
}
}
const config = {
entry: {
'bundle.min.css': [
path.resolve(__dirname, '/src/styles/main.scss'),
],
'bundle.js': [
path.resolve(__dirname, '/src/app/index.js')
]
},
output: {
filename: '[name]',
path: path.resolve(__dirname, 'dist'),
},
devtool: setDevTool(),
module: {
rules: [
// fonts loader
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/'
}
},
// babel loader
{
test: /\.js$/,
use: 'babel-loader',
exclude: [
/node_modules/
]
},
// raw html loader
{
test: /\.html/,
loader: 'raw-loader'
},
// css loader
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader'
})
},
// sass loader
{
test: /\.(sass|scss)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['style-loader', 'css-loader', 'sass-loader']
})
},
// vue loader
{
test: /\.vue$/,
loader: 'vue-loader'
},
// image url inliner
{
test: /\.(jpe?g|png|gif)$/,
loader: 'url-loader',
options: {
limit: 50 * 1024
}
},
// svg url inliner
{
test: /\.svg$/,
loader: 'svg-url-loader',
options: {
limit: 50 * 1024,
noquotes: true,
}
},
// image loader
{
test: /\.(jpg|png|gif|svg)$/,
loader: 'image-webpack-loader',
enforce: 'pre'
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: __dirname + "/src/public/index.html",
inject: 'body'
}),
new Dotenv({safe: true}),
new ExtractTextPlugin("bundle.min.css"),
new VueLoaderPlugin()
],
devServer: {
contentBase: './src',
port: 7700,
}
}
if (isProd) {
config.plugins.push(
new UglifyJSPlugin(),
new CopyWebpackPlugin([{
from: __dirname + '/src/public'
}])
);
};
module.exports = config;
I do not understand why it can't find those files if for examlpe '/var/www/project//src/styles/main.scss' it mention in error is correct path?
Try to remove the initial slashes from path.resolve(). For example:
path.resolve(__dirname, '/src/styles/main.scss')
should be:
path.resolve(__dirname, 'src/styles/main.scss')

Exclude specific file from webpack bundle

In my React app I'm using Webpack. Here is my webpack.config:
"use strict";
var path = require("path");
var WebpackNotifierPlugin = require("webpack-notifier");
var BrowserSyncPlugin = require("browser-sync-webpack-plugin");
module.exports = {
entry: "./Scripts/reactApp/index.jsx",
output: {
path: path.resolve(__dirname, "./Scripts/reactApp/build"),
filename: "react_bundle.js"
},
module: {
rules: [
{
test: /\.jsx$/,
exclude: [/node_modules/, path.resolve(__dirname, "./Scripts/reactApp/translations/he.translations.json")],
use: {
loader: "babel-loader"
}
},
{
test: /\.js$/,
exclude: [/node_modules/, path.resolve(__dirname, "./Scripts/reactApp/translations/he.translations.json")],
use: {
loader: "babel-loader"
}
}
]
},
devtool: "inline-source-map",
plugins: [new WebpackNotifierPlugin(), new BrowserSyncPlugin()]
};
I'm trying to exclude from bundle he.translations.json
But webpack included it whatever.
Can you explain me what I do wrong?
Sorry in advance, I'm new in webpack.
You can just do (from webpack docs)
...
exclude: [/node_modules/, path.resolve(__dirname, 'Scripts/reactApp/translations/he.translations.json')],
...
There's no dot in the path and, of course, assuming you have the wepback config file in the proper place.

Is there a way to exclude node_modules from webpack on a global levle?

I have simple config for demonstration that excludes node modules from js and jsx file, but can't find a solution to exclude node_modules, globally so I don't need to always specify this exclude in every loader
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry: {
app: path.resolve(__dirname, "../src/index.js")
},
output: {
path: path.resolve(__dirname, "../dist"),
filename: "[name].[chunkhash].js",
publicPath: "/",
chunkFilename: "[name].[chunkhash].js"
},
module: {
rules: [
{ test: /\.js$/, loaders: ["babel-loader"], exclude: /node_modules/ },
{ test: /\.jsx$/, loaders: ["babel-loader"], exclude: /node_modules/ },
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
}
]
},
plugins: [
new CleanWebpackPlugin([path.resolve(__dirname, "../dist")]),
new HtmlWebpackPlugin({ template: path.resolve(__dirname, "../public/index.html") }),
new ExtractTextPlugin("styles.css")
]
};
Is there a way to achieve this? I'm on webpack 3
You can use
{ test: /\.jsx?$/, loaders: ["babel-loader"], exclude: /node_modules/ },

Webpack generates big file

Webpack generates too large a file
Webpack 2.x
Webpack experts, i now want to connect css in thejs file
How i include
import 'bootstrap/dist/css/bootstrap-theme.min.css';
import 'bootstrap-select/dist/css/bootstrap-select.min.css';
import 'bootstrap-multiselect/dist/css/bootstrap-multiselect.css';
import 'font-awesome/css/font-awesome.min.css';
import 'angular-ui-notification/dist/angular-ui-notification.min.css';
import 'slick-carousel/slick/slick.css';
import 'slick-carousel/slick/slick-theme.css';
import '../css/styles.css';
import '../css/custom.css';
import '../css/max-width_767.css';
webpack config
var glob = require('glob'),
ngAnnotate = require('ng-annotate-webpack-plugin'),
ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry: __dirname + '/application/application.js',
output: {
path: __dirname + '/build',
filename: 'bundle.js'
},
plugins: [
new ngAnnotate({
add: true,
}),
new ExtractTextPlugin({
filename: '[name].css',
})
],
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['babel-preset-es2015'].map(require.resolve)
},
exclude: /(node_modules|bower_components)/
},
{
test: /\.(png|woff|woff2|eot|ttf|svg|gif|jpg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader'
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
}
]
},
node: {
fs: 'empty'
}
};
That's what i'm getting out of the way, a huge bundle.js file is probably 5 MB with all fonts, pictures, etc.
bundle.js 5.53 MB 0 [emitted] [big] main
I just want to concatenate only css and output to bundle.css
What am I doing wrong ?
You have included extract-text-plugin but you dont actually seem to be using it.
Change here:
{
test: /\.css$/,
loader: 'style-loader!css-loader'
}
To something like:
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallbackLoader: "style-loader",
loader: "css-loader"
})
}
it's answer
It was necessary to look at the limit
var glob = require('glob'),
ngAnnotate = require('ng-annotate-webpack-plugin'),
ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: __dirname + '/application/application.js',
output: {
path: __dirname + '/build',
filename: 'bundle.js'
},
plugins: [
new ngAnnotate({
add: true,
}),
new ExtractTextPlugin('bundle.css'),
],
resolve: {
alias: {
moment: __dirname + '/node_modules/moment/min/moment'
},
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['babel-preset-es2015'].map(require.resolve)
},
exclude: /(node_modules|bower_components)/
},
{
test: /\.(png|woff|woff2|eot|ttf|svg|gif|jpg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader',
options: {
limit: 1000
}
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader'
})
}
]
},
node: {
fs: 'empty'
},
devServer: {
historyApiFallback: true
}
};

Why webpack can't import extracted css file?

I want to generate an independent CSS file via extract-text-webpack-plugin, but it doesn't work, and the console has not reported any errors. But when I use loaders: ["style-loader", "css-loader"] it work.
This is my webpack.config.js file:
var webpack = require("webpack");
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: "./src/js/entry.js",
output: {
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
// loaders: ["style-loader", "css-loader"]
}
]
},
plugins: [
new ExtractTextPlugin('[name].css')
]
};
This is my entry.js file:
require('../css/base.css');
How can I solve this?

Categories

Resources