Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

output images are in parent folder when useRelativePath option is true #149

Closed
@aamirshahx

Description

@aamirshahx

My webpack file

import CleanWebpackPlugin from 'clean-webpack-plugin';
import StyleLintPlugin from 'stylelint-webpack-plugin';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import combineLoaders from 'webpack-combine-loaders';
import {modulesRoot, root, staticRoot} from './helpers';

const projectRootPath = root('.');
const modulesPath = modulesRoot();
const buildPath = staticRoot();

export default (env) => {
	let proMode = env.toLowerCase() !== 'production';

	return {
		devtool: proMode ? 'source-map' : 'eval',

		entry: {
			'css/web/home/styles': 'css/web/home/styles.pcss'
		},

		output: {
			path: buildPath,
			publicPath: buildPath,
			filename: proMode ? '[name].[chunkhash].css' : '[name].css',
			sourceMapFilename: proMode ? '[name].[chunkhash].map' : '[name].map'
		},

		resolve: {
			modules: [projectRootPath, modulesPath, root('images')],
			extensions: ['.pcss']
		},
		module: {
			rules: [
				{
					test: /\.(jpe?g|png|webp|svg|gif|cur)$/i,
					loader: 'file-loader',
					options: {
						name: '[path][name].[hash].[ext]',
						publicPath: '../../'
					}
				},
				{
					test: /\.pcss$/,
					loader: ExtractTextPlugin.extract({
						use: combineLoaders([
							{
								loader: 'css-loader',
								options: {
									importLoaders: 1,
									sourceMap: proMode,
									url: true
								}
							},
							{
								loader: 'postcss-loader',
								options: {
									sourceMap: proMode ? undefined : 'inline'
								}
							}
						])
					})
				}
			]
		},
		plugins: [
			...(env === 'watch' ? [] : [new CleanWebpackPlugin(['build'], {root: projectRootPath, verbose: true})]),
			new StyleLintPlugin({files: 'css/**/*.pcss'}),
			new ExtractTextPlugin({filename: (proMode ? '[name].[chunkhash].css' : '[name].css'), allChunks: true})
		],
		stats: {
			assets: true,
			children: false,
			chunks: true,
			chunkModules: true,
			chunkOrigins: true,
			colors: true,
			errors: true,
			errorDetails: false,
			hash: false,
			modules: false,
			reasons: false,
			source: true,
			timings: true,
			version: true,
			warnings: true
		},
		node: {
			global: true,
			crypto: 'empty',
			process: true,
			module: false,
			clearImmediate: false,
			setImmediate: false
		}
	};
};

My folder structure

|-
|----css/
|--------web/
|-------------home/
|-------------------style.pcss
|-------------app/
|-------------------app.pcss
|------------desktop/
|-------------------desktop.pcss
|----images/
|-------------home/
|-------------------image1.jpg
|-------------app/
|-------------------image2.jpg
|------------desktop/
|-------------------image3.jpg

`

when i am building my project with above config
my build folder structure is

|-
|----build/
|---------css/
|--------------web/
|-------------------home/
|-------------------------style.css
|-------------------app/
|-------------------------app.css
|--------------desktop/
|-----------------desktop.css
|---------images/
|--------------web/
|-------------------home/
|-------------------------image1.jpg
|-------------------app/
|-------------------------image2.jpg
|--------------desktop/
|-------------------------image3.jpg

`

but since i am specifying public path ('../../')
background: url(../../image/desktop/image3.jpg) in my desktop.css are working correctly

but web/home/style.css
background: url(../../image/web/home/image1.jpg) in style.css is not correct (correct path should be ../../../image/desktop/image3.jpg)

then i tried


				{
					test: /\.(jpe?g|png|webp|svg|gif|cur)$/i,
					loader: 'file-loader',
					options: {
						name: '[name].[hash].[ext]',
						useRelativePath: true,
						publicPath: url => url
					}
				}

then the css files are creating correct paths
for desktop background: url(../../image/desktop/image3.jpg)
for web/home background: url(../../image/web/home/image1.jpg)

but images folder is not in build directory
desktop images are in the project's parent folder (../../)
and web/home images in project's parent's parent folder (../../../)

How to create images folder in build/ and have the same paths as with useRelativePath?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions