Merge branch 'esbuild-1101-workaround' of https://github.com/timostamm/esbuild-plugin-postcss2-esbuild1101 into timostamm-esbuild-1101-workaround
This commit is contained in:
commit
c4546c67b1
51
src/index.ts
51
src/index.ts
@ -89,21 +89,37 @@ const postCSSPlugin = ({
|
|||||||
|
|
||||||
const sourceExt = path.extname(sourceFullPath);
|
const sourceExt = path.extname(sourceFullPath);
|
||||||
const sourceBaseName = path.basename(sourceFullPath, sourceExt);
|
const sourceBaseName = path.basename(sourceFullPath, sourceExt);
|
||||||
const sourceDir = path.dirname(sourceFullPath);
|
|
||||||
const sourceRelDir = path.relative(path.dirname(rootDir), sourceDir);
|
|
||||||
const isModule = sourceBaseName.match(/\.module$/);
|
const isModule = sourceBaseName.match(/\.module$/);
|
||||||
const tmpDir = path.resolve(tmpDirPath, sourceRelDir);
|
const sourceDir = path.dirname(sourceFullPath);
|
||||||
|
|
||||||
let tmpFilePath = path.resolve(
|
let tmpFilePath: string;
|
||||||
tmpDir,
|
if (args.kind === "entry-point") {
|
||||||
`${Date.now()}-${sourceBaseName}.css`
|
// For entry points, we use <tempdir>/<path-within-project-root>/<file-name>.css
|
||||||
);
|
const sourceRelDir = path.relative(
|
||||||
|
path.dirname(rootDir),
|
||||||
// When CSS is an entry-point we don't want to append Date.now()
|
path.dirname(sourceFullPath)
|
||||||
if (args.kind === "entry-point")
|
);
|
||||||
tmpFilePath = path.resolve(tmpDir, `${sourceBaseName}.css`);
|
tmpFilePath = path.resolve(
|
||||||
|
tmpDirPath,
|
||||||
await ensureDir(tmpDir);
|
sourceRelDir,
|
||||||
|
`${sourceBaseName}.css`
|
||||||
|
);
|
||||||
|
await ensureDir(path.dirname(tmpFilePath));
|
||||||
|
} else {
|
||||||
|
// For others, we use <tempdir>/<unique-directory-name>/<file-name>.css
|
||||||
|
//
|
||||||
|
// This is a workaround for the following esbuild issue:
|
||||||
|
// https://github.com/evanw/esbuild/issues/1101
|
||||||
|
//
|
||||||
|
// esbuild is unable to find the file, even though it does exist. This only
|
||||||
|
// happens for files in a directory with several other entries, so by
|
||||||
|
// creating a unique directory name per file on every build, we guarantee
|
||||||
|
// that there will only every be a single file present within the directory,
|
||||||
|
// circumventing the esbuild issue.
|
||||||
|
const uniqueTmpDir = path.resolve(tmpDirPath, uniqueId());
|
||||||
|
tmpFilePath = path.resolve(uniqueTmpDir, `${sourceBaseName}.css`);
|
||||||
|
}
|
||||||
|
await ensureDir(path.dirname(tmpFilePath));
|
||||||
|
|
||||||
const fileContent = await readFile(sourceFullPath);
|
const fileContent = await readFile(sourceFullPath);
|
||||||
let css = sourceExt === ".css" ? fileContent : "";
|
let css = sourceExt === ".css" ? fileContent : "";
|
||||||
@ -215,4 +231,13 @@ function getFilesRecursive(directory: string): string[] {
|
|||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let idCounter = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates an id that is guaranteed to be unique for the Node.JS instance.
|
||||||
|
*/
|
||||||
|
function uniqueId(): string {
|
||||||
|
return Date.now().toString(16) + (idCounter++).toString(16);
|
||||||
|
}
|
||||||
|
|
||||||
export default postCSSPlugin;
|
export default postCSSPlugin;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user