Checking for Files without Compiling: A guide to using Webpack 2

I have a scenario where I am importing several .css files from npm modules in my main.js file, which serves as the entry point for my webpack configuration. Here is an example of how it's set up:

"use strict";
const ExtractTextPlugin = require("extract-text-webpack-plugin");

var webpack = require("webpack");

module.exports = {
    entry: {
        dashboard: './js/main.js',
        vendor: ["fixed-data-table","react","react-dom","jquery", "bootstrap"],
    },
    output: { path: "../resources/public", filename: 'bundle.js' },

    plugins: [
        new webpack.optimize.CommonsChunkPlugin({name: "vendor", filename: "static/vendor.bundle.js"}),
        new ExtractTextPlugin("[name].css"),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        }),
    ],

    module: {
        loaders: [
            {
                test: /.js?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react', 'stage-0']
                }
            },
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader'}),
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/i,
                loaders: [
                    'file-loader',
                ],
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                loader: 'file-loader'
            }
        ]
    },
};

To avoid errors during the build process due to missing images and fonts needed by certain modules like bootstrap.css, I included loaders for testing images and fonts, which has resolved the issue. Everything now builds successfully!

However, the solution also results in unwanted .png, .woff2, .eot, .ttf files being output to the same folder.

An alternative approach would be to create a script that removes these specific files after running webpack in the terminal. But ideally, I'd prefer not to take that route.

Is there a way to adjust my webpack configuration to test for images and fonts without processing and outputting them to the output folder?

Answer №1

To dispose of unwanted files, one method is to send them directly to the trash with a somewhat unconventional approach:

    {
        test: /\.(jpe?g|png|gif|svg)$/i,
        loaders: [
            'file-loader?hash=sha512&digest=hex&name=~/.local/share/Trash/[hash].[ext]',
            'image-webpack-loader?bypassOnDebug&optimizationLevel=7&interlaced=false'
        ],
    },
    {
        test: /\.(eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader?name=~/.local/share/Trash/[name].[ext]'
    }

While this solution may work, it's not necessarily the recommended way to handle the situation :)

Answer №2


    { extension: /\.jpg$/, method: "file-loader?name=assets/images/[hash].[name].[ext]"},
    { extension: /\.png$/, method: "file-loader?name=assets/images/[hash].[name].[ext]&mimetype=image/png"},
    {
        extension: /\.(html|hbs)$/,
        tools: ['handlebars-template-loader']
    },
    // Integrating Bootstrap
    // { extension: /bootstrap\/dist\/js\/umd\//, method: 'imports?jQuery=jquery' },
    // { extension: /bootstrap-sass\/assets\/javascripts\//, method: 'imports?jQuery=jquery' },
    {
        extension: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        method: 'url?name=assets/fonts/[hash].[name].[ext]&limit=10000',
    },
    { extension: /\.[ot]tf$/, method: 'url?limit=65000&mimetype=application/octet-stream&name=assets/fonts/[name].[ext]' },
    {
        extension: /\.(eot|svg)(\?[\s\S]+)?$/,
        method: 'file?name=assets/fonts/[hash].[name].[ext]',
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What CSS property can be used to make short words wrap to the next line?

Is it possible to automatically identify if a line of text ends with a short word and then move it to the next line, so that the text flows smoothly? This would ensure a more organized layout. For instance, I envision the following text: Cascading Style ...

Is it feasible to alter cookie data within a jQuery ajax call?

I'm currently developing a Chrome extension that enables users to capture all HTTP requests for a specific website, edit components of the request, and then send it again. My plan is to utilize jQuery's ajax function to design and dispatch the a ...

Saving multiple nested objects within a mongoose schema

Trying my hand at creating my first REST API using Mongoose. I've been working on this for days but can't seem to make it work. I want to save a survey with an array of controls, and each control should have an array of control properties. In s ...

What is the best way to integrate Solr with Node.js?

After checking out https://github.com/gsf/node-solr and installing node-solr using the command: npm install solr I'm now wondering how to establish a connection with Solr on port 8080. Can anyone help me with this? ...

Issue with installing vscode-ripgrep during VSCode build/run process

After attempting to build and run VSCode on my Ubuntu 17.10 by following the instructions from this guide: https://github.com/Microsoft/vscode/wiki/How-to-Contribute#build-and-run, I encountered an issue when installing dependencies using yarn. The error m ...

Loop through items in a list using Angular.js and display each item within an <

I am facing an issue where the model returned from the server contains html tags instead of plain text, such as b tag or i tag. When I use ng-repeat to create a list based on this model, the html is displayed as pure text. Is there a filter or directive av ...

initiate changes to the application's style from a component nested within the app

I'm looking to update the background color of the entire page, including the app-nav-bar, when I'm inside a child component. When I navigate away from that component, I want the default style to be restored. Any suggestions on how to achieve this ...

Tips on moving information from one form to another after referencing the original page using Javascript and HTML

Imagine this scenario: A page with three text fields, each with default values. There is also a hyperlink that performs a database lookup and returns parameters to the same page where the hyperlink was clicked. The goal is for the text boxes to be automa ...

Issue with Sequelize failing to update a row within a database table

This is my first experience using sequelize. Within my database, I have a table named feed containing columns such as ID, caption, and url. In my controller, there is a straightforward function aimed at updating a row within this table: router.patch(' ...

What steps should I take to resolve this animation issue?

My objective with this card animation is for the cards to expand in size when hovered over. Unfortunately, the current issue is that when one card is hovered over, all the other cards also expand, which is not the intended behavior. I am seeking a solution ...

Mastering the art of seamless navigation within a single page through the magic of

I have two HTML pages: "Login.html" and "index.html". Instead of a normal href linking, I want the user to navigate to "index.html" when they click on the login button. Furthermore, I want the index page to be displayed within the codes of login.html, cre ...

Tips for querying orchestrate.io

Recently, I found myself in need of a user-friendly database for a small highscore system in my game development projects using JavaScript. Through the Github student developer pack, I came across Orchestrate.io. After discovering a suitable driver module ...

What is the best way to align sections side by side using Bootstrap?

Hello, I'm currently trying to customize the layout of this Bootstrap webpage to resemble the design shown in this image https://i.sstatic.net/w7i6Z.png. Specifically, I want to display the blog section and the team section side by side. As a beginne ...

Experiencing difficulties capturing a webpage image using node-webshot

I am working on a web application built with node.js using the express framework and Twitter Bootstrap 3 for the front end. I have integrated the module node-webshot to capture and save a screenshot of the page when a user logs out, which is later displaye ...

Update the data table after a new file has been uploaded

As a novice web developer, I embarked on my first project using Vue. In this project, I created a form for file uploads in Vue 2 and Laravel. If you want to take a look at the full code: View: https://pastebin.com/QFrBfF74 Data table user file: https:/ ...

Trimming a Three.js Sprite to conform with the boundaries of its parent Object

Imagine a scenario where I have a Sprite, which was created and added as a child of an Object with a transparent material, like so: let mySprite = new THREE.Sprite(new SpriteMaterial({ map: myTexture })); mySprite.scale.set(2, 2, 1.0); mySprite.posit ...

Error: Attempting to access the property 'highest' of an undefined value resulted in a TypeError - Mute Command

Greetings to the Stackoverflow community! I find myself in a state of confusion as to why this particular function stopped working suddenly, resulting in the error highlighted in the title. Previously, the function would verify whether the user attempting ...

Looking to merge two components into one single form using Angular?

I am currently developing an Angular application with a dynamic form feature. The data for the dynamic form is loaded through JSON, which is divided into two parts: part 1 and part 2. // JSON Data Part 1 jsonDataPart1: any = [ { "e ...

Downloading a file through a JavaScript link in HTMLUnit: A step-by-step guide

Looking to download a file with HTMLUnit from a javascript link is proving to be quite challenging. The journey begins at this page. When clicking on the "Authenticate with Java Web Start (new method)" link, a .jnlp file is downloaded, initiating a Java pr ...

Error Occurred: ngRepeat directive encountered an invalid expression while attempting to render the

HTML <tr ng-repeat="sale as examples"> <td class="text-right"> @{{sale.sales_person}}</td> <td class="text-right"> @{{sale.sales_total}}</td> <td class="text-right"> @{{sale.sales_target_amount}}</td> ...