Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip Javascript output in Webpack 4?

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.

like image 444
rishat Avatar asked May 20 '18 00:05

rishat


People also ask

Why is my webpack build so slow?

However, due to the additional packaging process, the building speed is getting slower as the project grows. As a result, each startup takes dozens of seconds (or minutes), followed by a round of build optimization.

What is libraryTarget in webpack?

This is according to their documentation: "libraryTarget: "umd" - This exposes your library under all the module definitions, allowing it to work with CommonJS, AMD and as global variable." Also, I built the exact same code with Webpack 3 and it produced a proper bundle.

What is output in webpack config?

Configuring the output configuration options tells webpack how to write the compiled files to disk. Note that, while there can be multiple entry points, only one output configuration is specified.


2 Answers

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'));
    }),
],
like image 98
Khasky Avatar answered Sep 24 '22 11:09

Khasky


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;
like image 20
mhulse Avatar answered Sep 22 '22 11:09

mhulse