Search code examples
webpacksass-loadermini-css-extract-plugin

webpack non-entry filenames


webpack.config.js

const webpack = require('webpack');
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const fs = require('fs');



module.exports = {
  mode: 'production',
  context: path.resolve(__dirname, 'app'),
  devServer: {
    host: "www.mydomain.local",
    compress: true,
    https: {
      key: fs.readFileSync('https.key'),
      cert: fs.readFileSync('https.crt')
    }
  },
  optimization: {
    minimizer: [new UglifyJsPlugin({})],
  },
  entry: {
    blog: path.resolve(__dirname, 'src', 'blog.js')
  },
  output: {
    path: path.resolve(__dirname, 'web', '_assets'),
    publicPath: path.resolve(__dirname, 'web', '_assets'),
    filename: "js/[name].[hash].bundle.js"
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "css/[name].[hash].bundle.css",
    })
  ],
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          "sass-loader"
        ]
      }
    ]
  }
};

As you can see I have only one entry in the webpack configuration "blog" which resolves to ./src/blog.js which has this content:

import('./css/blog.scss')

console.log(1)

When I run webpack --config webpack.config.js it builds as expected ./web/_assets/js/blog.21342i1j4h1kj.bundle.js which has the console.log(1) in it.

However the odd part is that it also builds ./web/_assets/js/1.21342i1j4h1kj.bundle.js and ./web/_assets/css/1.21342i1j4h1kj.bundle.js

the extra JS file contains

(window.webpackJsonp=window.webpackJsonp||[]).push([[1],[,function(n,w,o){}]]);

and the 1.css file contains the sass to css compiled css.

My questions are:

  • Why does the extra JS file exist?
  • Why is the css file not called blog.21342i1j4h1kj.bundle.css ?
  • How do I change the config to resolve this?

Solution

  • Turns out the 1.js is a webpack dynamic import feature that is because the css was imported inside the blog.js file. I ended up simply making this change to the webpack.config.js entry section

    blog: [
          path.resolve(__dirname, 'src_new', 'blog.js'),
          path.resolve(__dirname, 'src_new', 'blog.scss')
        ],
    

    and the plugin config takes care of the rest:

    plugins: [
        new MiniCssExtractPlugin({
          filename: "[name].[hash].bundle.css",
        })
      ],
    

    this results in blog.5903ffe3afb455f5e5d0.bundle.css and blog.5903ffe3afb455f5e5d0.bundle.js defined in output.path