Skip to content

Commit

Permalink
fixed resolve dir for css chunks #146
Browse files Browse the repository at this point in the history
  • Loading branch information
glromeo committed Sep 1, 2023
1 parent d1a9dcb commit f30cd94
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esbuild-sass-plugin",
"version": "2.12.0",
"version": "2.13.0",
"description": "esbuild plugin for sass/scss files supporting both css loader and css result import (lit-element)",
"main": "lib/index.js",
"keywords": [
Expand Down
8 changes: 5 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ export function sassPlugin(options: SassPluginOptions = {}): Plugin {
if (transform) {
const namespace = 'esbuild-sass-plugin'

onResolve({filter: /^css-chunk:/}, ({path}) => ({
onResolve({filter: /^css-chunk:/}, ({path, resolveDir}) => ({
path,
namespace
namespace,
pluginData: {resolveDir}
}))

onLoad({filter: /./, namespace}, ({path}) => ({
onLoad({filter: /./, namespace}, ({path, pluginData: {resolveDir}}) => ({
contents: cssChunks[path],
resolveDir,
loader: 'css'
}))
}
Expand Down
28 changes: 28 additions & 0 deletions test/issues/146/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as esbuild from 'esbuild';
import { sassPlugin, postcssModules } from '../../../lib/index.js';
import fs from 'node:fs/promises'
import path from 'node:path';

await fs.rm('dist', { recursive: true, force: true });

await esbuild.build({
entryPoints: ['./src/index.js'],
format: 'esm',
target: ['esnext'],
outdir: 'dist',
sourcemap: true,
bundle: true,
loader: {
'.svg': 'dataurl',
},
plugins: [
sassPlugin({
filter: /\.module\.scss$/,
transform: postcssModules({}),
precompile(source, pathname) {
const basedir = path.dirname(pathname);
return source.replace(/(url\(['"]?)(\.\.?\/)([^'")]+['"]?\))/g, `$1${basedir}/$2$3`);
},
}),
],
});
13 changes: 13 additions & 0 deletions test/issues/146/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"private": true,
"type": "module",
"scripts": {
"build": "node build.js"
},
"dependencies": {
"esbuild": "^0.19.2",
"esbuild-sass-plugin": "^2.13.0",
"postcss": "^8.4.29",
"postcss-modules": "^6.0.0"
}
}
8 changes: 8 additions & 0 deletions test/issues/146/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Repro: resolving `url()` with esbuild-sass-plugin

To reproduce the issue, run:

```
npm install
npm run build
```
4 changes: 4 additions & 0 deletions test/issues/146/src/close.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/issues/146/src/icons.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.close {
background-image: url('./close.svg');
background-repeat: no-repeat;
}
3 changes: 3 additions & 0 deletions test/issues/146/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import styles from './icons.module.scss'

console.log(styles)

0 comments on commit f30cd94

Please sign in to comment.