Webpack5 not resolving asset import: import PngFile from "./src/assets/[filename].png"

20 Views Asked by At

The rule for asset management is contained within my webpack.common.js file. Asset file refuses to resolve on both npm 'start' and 'build'. :/ In .src/index.js, the following line imports from ./src/assets/restaurantLogo.png (currently working through Odin Project Restaurant project):

import Logo from "./src/assets/restaurantLogo.png";

Here is my config code, from webpack.common.js:

const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
    entry: './src/index.js',
    plugins: [new HtmlWebpackPlugin({
        template: "./src/template.html"
    })],
    module: {
        rules: [
            {
                test: /\.(png|svg|jpg|jpeg|gif)$/i,
                type: 'asset/resource',
            },
        ],
    },
};

Just in case, here is .dev.js, .prod.js, & package.json:

.dev.js:

const path = require('path');
const common = require('./webpack.common.js');
const { merge } = require('webpack-merge');

module.exports = merge(common, {
    mode: "development",
    output: {
        filename: '[name].[contentHash].bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    module: {
        rules: [
            {
                test: /\.css$/i,
                use: ["style-loader", "css-loader"],
            },
            {
                test: /\.{png|jpg|jpeg|svg|gif}$/i,
                type: 'asset/resource',
            },
        ],
    },
});

.prod.js:

const path = require('path');
const common = require('./webpack.common.js');
const { merge } = require('webpack-merge');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");

module.exports = merge(common, {
    mode: "production",
    output: {
        filename: '[name].[contentHash].bundle.js',
        path: path.resolve(__dirname, 'dist'),
        clean: true, //cleans output directory w/ each new compile
    },
    plugins: [new MiniCssExtractPlugin()],
    module: {
        rules: [
            {
                test: /\.html$/i,
                loader: "html-loader",
            },
            {
                test: /\.css$/i,
                use: [MiniCssExtractPlugin.loader, "css-loader"],
            },
        ],
    },
    optimization: {
        minimizer: [
            `...`, //keeps terser for JS minimization
            new CssMinimizerPlugin(),
        ],
    },
});

package.json:

{
  "name": "restaurant-odin",
  "version": "1.0.0",
  "description": "ODIN project restaurant page project",
  "main": "index.js",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --config webpack.dev.js --open",
    "build": "webpack --config webpack.prod.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "clean-webpack-plugin": "^4.0.0",
    "css-loader": "^6.10.0",
    "css-minimizer-webpack-plugin": "^6.0.0",
    "html-loader": "^5.0.0",
    "html-webpack-plugin": "^5.6.0",
    "mini-css-extract-plugin": "^2.8.0",
    "style-loader": "^3.3.4",
    "webpack": "^5.90.1",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^5.0.1",
    "webpack-merge": "^5.10.0"
  }
}

I've been scouring Google for a solution, and I'm not finding one. I'm using the WP5 built-in asset module, and nothing seems to differ from documentation. Am I a blind noob with an obvious answer staring me in the face? Someone help.

0

There are 0 best solutions below