简体   繁体   English

如何防止Webpack生成.js文件?

[英]How to prevent Webpack from generating .js files?

I am trying to use Webpack to iterate through a few scss files and create css files from them. 我正在尝试使用Webpack迭代几个scss文件并scss创建css文件。 The program is working and generates the files but it throws an Multiple assets emit to the same filename name.css error. 该程序正在运行并生成文件,但是它Multiple assets emit to the same filename name.css错误抛出Multiple assets emit to the same filename name.css

I believe this is because the MiniCssExtractPlugin is clashing with Webpack's output . 我相信这是因为MiniCssExtractPlugin与Webpack的output冲突。 Any thoughts how can I disable this? 有什么想法该禁用它吗? Or at least keep js files from being generated? 或者至少阻止js文件的生成?

Current webpack.config.js: 当前的webpack.config.js:

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

const setEntryPoints = (themes = {}) => {
  fs.readdirSync(path.join(__dirname, 'src')).forEach(file => {
    themes[file.slice(0, -5)] = path.join(__dirname, 'src', file)
  })
  return themes
}

const themes = setEntryPoints();

module.exports = {
  mode: "development",
  entry: themes,
  output: {
    filename: '[name].css',
    path: path.join(__dirname, 'dist')
  },
  module: {
  // Add loader
  rules: [{
      test: /\.(scss)$/,
      use: [
        MiniCssExtractPlugin.loader,
        'css-loader', 'sass-loader'
      ]
    }]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "[name].css",
    })
  ],
};

Do it like this. 像这样做。 Might work properly 可能正常工作

entry: {
  app: [
    path.resolve(__dirname, 'src/pug/app.pug'),
    path.resolve(__dirname, 'src/js/app.js'),
    path.resolve(__dirname, 'src/scss/app.scss')
  ]
},
output: {
  path: path.resolve(__dirname, 'dist'),
  filename: 'js/[name].js'
},

And you do the same thing as with output.filename for the filename in the extract text plugins: 您可以对提取文本插件中的文件名执行与output.filename相同的操作:

const extractHtml = new ExtractTextPlugin({
  filename: 'html/[name].html'
});

const extractScss = new ExtractTextPlugin({
  filename: 'css/[name].[contenthash].css',
  allChunks: true
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM