Compare commits

..

No commits in common. "main" and "patch-2" have entirely different histories.

3 changed files with 46 additions and 28 deletions

View File

@ -1,3 +1,6 @@
# Looking for a maintainer!
Unfortunately I don't have time for this plugin. Please contact me if you'd like to take on this project.
# esbuild-plugin-postcss2
This plugin is an optimized, type-friendly version of [esbuild-plugin-postcss](https://github.com/deanc/esbuild-plugin-postcss). It supports CSS preprocessors and CSS modules.

View File

@ -1,11 +1,8 @@
{
"name": "@frappe/esbuild-plugin-postcss2",
"version": "0.1.3",
"name": "esbuild-plugin-postcss2",
"version": "0.1.2",
"description": "Use postcss with esbuild",
"repository": {
"type": "git",
"url": "git+https://github.com/frappe/esbuild-plugin-postcss2.git"
},
"repository": "https://github.com/martonlederer/esbuild-plugin-postcss2",
"author": "Marton Lederer <marton@lederer.hu>",
"license": "MIT",
"private": false,
@ -33,6 +30,7 @@
"autoprefixer": "^10.2.5",
"fs-extra": "^9.1.0",
"less": "^4.x",
"postcss": "8.x",
"postcss-modules": "^4.0.0",
"resolve-file": "^0.3.0",
"sass": "^1.x",
@ -63,9 +61,5 @@
"postcss": "8.x",
"sass": "^1.x",
"stylus": "^0.x"
},
"bugs": {
"url": "https://github.com/frappe/esbuild-plugin-postcss2/issues"
},
"homepage": "https://github.com/frappe/esbuild-plugin-postcss2#readme"
}
}

View File

@ -30,6 +30,7 @@ interface PostCSSPluginOptions {
sassOptions?: SassOptions;
lessOptions?: Less.Options;
stylusOptions?: StylusRenderOptions;
writeToFile?: boolean;
fileIsModule?: (filename: string) => boolean;
}
@ -47,16 +48,18 @@ export const defaultOptions: PostCSSPluginOptions = {
sassOptions: {},
lessOptions: {},
stylusOptions: {},
writeToFile: true,
fileIsModule: null
};
const postCSSPlugin = ({
plugins = [],
modules = true,
rootDir = process.cwd(),
sassOptions = {},
lessOptions = {},
stylusOptions = {},
plugins,
modules,
rootDir,
sassOptions,
lessOptions,
stylusOptions,
writeToFile,
fileIsModule
}: PostCSSPluginOptions = defaultOptions): Plugin => ({
name: "postcss2",
@ -141,10 +144,7 @@ const postCSSPlugin = ({
// parse files with preprocessors
if (sourceExt === ".sass" || sourceExt === ".scss") {
const sassResult = await renderSass({
...sassOptions,
file: sourceFullPath
});
const sassResult = await renderSass({...sassOptions, file: sourceFullPath})
css = sassResult.css.toString();
watchFiles.push(...sassResult.stats.includedFiles);
}
@ -172,14 +172,21 @@ const postCSSPlugin = ({
watchFiles.push(...getPostCssDependencies(result.messages));
// Write result CSS
if (writeToFile) {
await writeFile(tmpFilePath, result.css);
}
return {
namespace: isModule ? "postcss-module" : "file",
namespace: isModule
? "postcss-module"
: writeToFile
? "file"
: "postcss-text",
path: tmpFilePath,
watchFiles,
pluginData: {
originalPath: sourceFullPath
originalPath: sourceFullPath,
css: result.css
}
};
}
@ -192,16 +199,30 @@ const postCSSPlugin = ({
const mod = modulesMap.find(
({ path }) => path === args?.pluginData?.originalPath
),
resolveDir = path.dirname(args.path);
resolveDir = path.dirname(args.path),
css = args?.pluginData?.css || "";
return {
resolveDir,
contents: `import ${JSON.stringify(
args.path
)};\nexport default ${JSON.stringify(mod && mod.map ? mod.map : {})};`
contents: [
writeToFile ? `import ${JSON.stringify(args.path)};` : null,
`export default ${JSON.stringify(mod && mod.map ? mod.map : {})};`,
writeToFile
? null
: `export const stylesheet=${JSON.stringify(css)};`
]
.filter(Boolean)
.join("\n")
};
}
);
build.onLoad({ filter: /.*/, namespace: "postcss-text" }, async (args) => {
const css = args?.pluginData?.css || "";
return {
contents: `export default ${JSON.stringify(css)};`
};
});
}
});