How to skip Javascript output in Webpack 4?
Asked Answered
P

5

8

I use Webpack 4 in a project where I only need to compile and bundle styles so far. There's no Javascript.

Here's the config I have:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: {
    'css/bundle': path.resolve(__dirname, 'static/scss/index.scss'),
  },
  output: {
    path: path.resolve(__dirname, 'static'),
  },
  module: {
    rules: [
      {
        test: /\.s[ac]ss$/,
        include: path.resolve(__dirname, 'static/scss'),
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
      },
    ],
  },
  plugins: [
    new MiniCssExtractPlugin(),
  ],
};

The problem is that it outputs two files: bundle.css and bundle.js. Is there a way to configure Webpack so that it doesn't output the Javascript bundle? I tried to navigate the docs, tried a dozen different things, but it didn't really work.

One important note here is that if I remove the css-loader, bundling fails. So while css-loader is most likely responsible for outputting the bundle.js file, I'm not entirely sure how to avoid using it.

Purvey answered 20/5, 2018 at 0:2 Comment(1)
I guess this is not possible, because wp is in the first place a JavaScript bundler. I would use a plugin which cleans up the dist folder after emit..Lashelllasher
H
1

March 2021:

In Webpack 5, on-build-webpack plugin did not work for me.

I found this:

Webpack Shell Plugin Next

The project I’m working on we’re using Webpack 5 as a build tool for a CSS pattern library. Therefore, we didn’t need the main.js in our dist.

Run npm i -D webpack-shell-plugin-next

Then in webpack.config.ts (just showing the pertinent parts):

import WebpackShellPluginNext from "webpack-shell-plugin-next";

module.exports = {
    output: {
        path: path.resolve(__dirname, "static/dist")
    },
    plugins: [
        // Run commands before or after webpack 5 builds:
        new WebpackShellPluginNext({
            onBuildEnd: {
                scripts: [
                    () => {
                        fs.unlinkSync(path.join(config.output.path, "main.js"));
                    }
                ]
            }
        })
    ]
};

export default config;
Hufnagel answered 31/3, 2021 at 5:19 Comment(0)
G
5

webpack-extraneous-file-cleanup-plugin has no effect with webpack 4.12.0.

I can suggest to remove bundle.js manually with on-build-webpack plugin:

var WebpackOnBuildPlugin = require('on-build-webpack');

// ...

plugins: [
    // ...

    new WebpackOnBuildPlugin(function () {
        fs.unlinkSync(path.join('path/to/build', 'bundle.js'));
    }),
],
Gall answered 22/7, 2018 at 11:57 Comment(1)
I ran the cleanup task on npm level, but this solution looks sharp, thanks!Purvey
I
1

Unfortunately, this is just the way that webpack currently works. However, we are not alone in this problem! There's a plugin to cleanup any unwanted files:

install the plugin:

yarn add webpack-extraneous-file-cleanup-plugin -D

and then in your config:

const ExtraneousFileCleanupPlugin = require('webpack-extraneous-file-cleanup-plugin');

plugins: [
  new ExtraneousFileCleanupPlugin({
    extensions: ['.js'],
    minBytes: 1024,
    paths: ['./static']
  }),
]
Indistinct answered 21/5, 2018 at 15:5 Comment(0)
H
1

March 2021:

In Webpack 5, on-build-webpack plugin did not work for me.

I found this:

Webpack Shell Plugin Next

The project I’m working on we’re using Webpack 5 as a build tool for a CSS pattern library. Therefore, we didn’t need the main.js in our dist.

Run npm i -D webpack-shell-plugin-next

Then in webpack.config.ts (just showing the pertinent parts):

import WebpackShellPluginNext from "webpack-shell-plugin-next";

module.exports = {
    output: {
        path: path.resolve(__dirname, "static/dist")
    },
    plugins: [
        // Run commands before or after webpack 5 builds:
        new WebpackShellPluginNext({
            onBuildEnd: {
                scripts: [
                    () => {
                        fs.unlinkSync(path.join(config.output.path, "main.js"));
                    }
                ]
            }
        })
    ]
};

export default config;
Hufnagel answered 31/3, 2021 at 5:19 Comment(0)
R
1

The webpack-remove-empty-scripts plugin, compatible with webpack 5, cover the current issue. It remove unexpected empty js file.

Rebirth answered 30/9, 2022 at 15:30 Comment(0)
R
0

I simply delete the unneeded output with rm in package.json:

"scripts": {
    "build": "npm run clean && webpack -p && rm ./dist/unneeded.js"
 },
Richma answered 30/8, 2018 at 12:25 Comment(1)
That’s the problem: it’s okay to run consecutive commands once, but it won’t work the same way in watch mode. I could use concurrently and run a clean task, but it feels unnatural, as if I’d be using two wrong tools instead of the right one.Purvey

© 2022 - 2024 — McMap. All rights reserved.