0

My folder structure is below.

I am using WordPress and my setup looks different from the usual setup. I am new and took what I was using from another developer and tweaking it. I also do not have an index.js file at all.

├── src
│   └── themes
│       └── someThemeName
│           ├── images
│           │   └── header
│           │       └── header.jpg
│           └── fonts
├── public_html
│   └── wp-content
│      └── themes
│          └── someThemeName
│             ├── images
│             │   └── header
│             │       └── header.jpg
│             └── fonts
│  
├── package-lock.json
├── package.json
└── webpack.config.js

How do I update assetModuleFilename?

webpack.config.js

const fs = require('fs');
const {
  resolve
} = require('path');
const svgToMiniDataURI = require('mini-svg-data-uri');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

let entries = {};
const theme = process.env.npm_config_theme;
const paths = {
  ['themes/' + theme + '/style']: './src/themes/' + theme + '/scss/style.scss',
  ['themes/' + theme + '/js/additions']: './src/themes/' + theme + '/js/additions.js'
};

for (var key in paths) {
  if (fs.existsSync(paths[key])) {
    entries[key] = paths[key]
  }
}

module.exports = {
  entry: entries,
  output: {
    path: resolve('./public_html/wp-content/'),
    assetModuleFilename: (pathData) => {
      const filepath = path.dirname(pathData.filename).split('/').slice(1).join('/');
      return filepath + '/[name][ext]';
    }
  },
  resolve: {
    extensions: ['.js', '.scss'],
    modules: [
      resolve('src'),
      'node_modules',
    ],
    alias: {
      'themes': resolve('./src/themes')
    }
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name].css'
    })
  ],
  module: {
    rules: [
      {
        test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|otf)$/i,
        type: "asset/resource"
      },
      {
        test: /\.svg/,
        type: 'asset/inline',
        generator: {
          dataUrl: content => {
            content = content.toString();
            return svgToMiniDataURI(content);
          }
        }
      },
      {
        test: /\.js$/,
        exclude: [/public_html/, /node_modules/],
        use: ['babel-loader']
      },
      {
        test: /\.scss$/,
        exclude: /public_html/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
      }
    ]
  }
};

I tried to use the code in the link below but I keep getting

Error: Conflict: Multiple chunks emit assets to the same filename undefined (chunks 375 and 375)

Webpack 5 Assets Module: How to keep the folder structure in the output folder?

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Amy Ling
  • 11
  • 3
  • Can't reproduce. Please create a https://stackoverflow.com/help/minimal-reproducible-example and show the full error stack – Lin Du Jul 19 '23 at 12:39
  • @LinDu I will try to do this in the next few days. I am working on a deadline at the moment. – Amy Ling Jul 19 '23 at 22:36

0 Answers0