Compare commits
No commits in common. "main" and "patch-2" have entirely different histories.
@ -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
|
# 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.
|
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.
|
||||||
|
|||||||
16
package.json
16
package.json
@ -1,11 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@frappe/esbuild-plugin-postcss2",
|
"name": "esbuild-plugin-postcss2",
|
||||||
"version": "0.1.3",
|
"version": "0.1.2",
|
||||||
"description": "Use postcss with esbuild",
|
"description": "Use postcss with esbuild",
|
||||||
"repository": {
|
"repository": "https://github.com/martonlederer/esbuild-plugin-postcss2",
|
||||||
"type": "git",
|
|
||||||
"url": "git+https://github.com/frappe/esbuild-plugin-postcss2.git"
|
|
||||||
},
|
|
||||||
"author": "Marton Lederer <marton@lederer.hu>",
|
"author": "Marton Lederer <marton@lederer.hu>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": false,
|
"private": false,
|
||||||
@ -33,6 +30,7 @@
|
|||||||
"autoprefixer": "^10.2.5",
|
"autoprefixer": "^10.2.5",
|
||||||
"fs-extra": "^9.1.0",
|
"fs-extra": "^9.1.0",
|
||||||
"less": "^4.x",
|
"less": "^4.x",
|
||||||
|
"postcss": "8.x",
|
||||||
"postcss-modules": "^4.0.0",
|
"postcss-modules": "^4.0.0",
|
||||||
"resolve-file": "^0.3.0",
|
"resolve-file": "^0.3.0",
|
||||||
"sass": "^1.x",
|
"sass": "^1.x",
|
||||||
@ -63,9 +61,5 @@
|
|||||||
"postcss": "8.x",
|
"postcss": "8.x",
|
||||||
"sass": "^1.x",
|
"sass": "^1.x",
|
||||||
"stylus": "^0.x"
|
"stylus": "^0.x"
|
||||||
},
|
}
|
||||||
"bugs": {
|
|
||||||
"url": "https://github.com/frappe/esbuild-plugin-postcss2/issues"
|
|
||||||
},
|
|
||||||
"homepage": "https://github.com/frappe/esbuild-plugin-postcss2#readme"
|
|
||||||
}
|
}
|
||||||
|
|||||||
53
src/index.ts
53
src/index.ts
@ -30,6 +30,7 @@ interface PostCSSPluginOptions {
|
|||||||
sassOptions?: SassOptions;
|
sassOptions?: SassOptions;
|
||||||
lessOptions?: Less.Options;
|
lessOptions?: Less.Options;
|
||||||
stylusOptions?: StylusRenderOptions;
|
stylusOptions?: StylusRenderOptions;
|
||||||
|
writeToFile?: boolean;
|
||||||
fileIsModule?: (filename: string) => boolean;
|
fileIsModule?: (filename: string) => boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,16 +48,18 @@ export const defaultOptions: PostCSSPluginOptions = {
|
|||||||
sassOptions: {},
|
sassOptions: {},
|
||||||
lessOptions: {},
|
lessOptions: {},
|
||||||
stylusOptions: {},
|
stylusOptions: {},
|
||||||
|
writeToFile: true,
|
||||||
fileIsModule: null
|
fileIsModule: null
|
||||||
};
|
};
|
||||||
|
|
||||||
const postCSSPlugin = ({
|
const postCSSPlugin = ({
|
||||||
plugins = [],
|
plugins,
|
||||||
modules = true,
|
modules,
|
||||||
rootDir = process.cwd(),
|
rootDir,
|
||||||
sassOptions = {},
|
sassOptions,
|
||||||
lessOptions = {},
|
lessOptions,
|
||||||
stylusOptions = {},
|
stylusOptions,
|
||||||
|
writeToFile,
|
||||||
fileIsModule
|
fileIsModule
|
||||||
}: PostCSSPluginOptions = defaultOptions): Plugin => ({
|
}: PostCSSPluginOptions = defaultOptions): Plugin => ({
|
||||||
name: "postcss2",
|
name: "postcss2",
|
||||||
@ -141,10 +144,7 @@ const postCSSPlugin = ({
|
|||||||
|
|
||||||
// parse files with preprocessors
|
// parse files with preprocessors
|
||||||
if (sourceExt === ".sass" || sourceExt === ".scss") {
|
if (sourceExt === ".sass" || sourceExt === ".scss") {
|
||||||
const sassResult = await renderSass({
|
const sassResult = await renderSass({...sassOptions, file: sourceFullPath})
|
||||||
...sassOptions,
|
|
||||||
file: sourceFullPath
|
|
||||||
});
|
|
||||||
css = sassResult.css.toString();
|
css = sassResult.css.toString();
|
||||||
watchFiles.push(...sassResult.stats.includedFiles);
|
watchFiles.push(...sassResult.stats.includedFiles);
|
||||||
}
|
}
|
||||||
@ -172,14 +172,21 @@ const postCSSPlugin = ({
|
|||||||
watchFiles.push(...getPostCssDependencies(result.messages));
|
watchFiles.push(...getPostCssDependencies(result.messages));
|
||||||
|
|
||||||
// Write result CSS
|
// Write result CSS
|
||||||
|
if (writeToFile) {
|
||||||
await writeFile(tmpFilePath, result.css);
|
await writeFile(tmpFilePath, result.css);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
namespace: isModule ? "postcss-module" : "file",
|
namespace: isModule
|
||||||
|
? "postcss-module"
|
||||||
|
: writeToFile
|
||||||
|
? "file"
|
||||||
|
: "postcss-text",
|
||||||
path: tmpFilePath,
|
path: tmpFilePath,
|
||||||
watchFiles,
|
watchFiles,
|
||||||
pluginData: {
|
pluginData: {
|
||||||
originalPath: sourceFullPath
|
originalPath: sourceFullPath,
|
||||||
|
css: result.css
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -192,16 +199,30 @@ const postCSSPlugin = ({
|
|||||||
const mod = modulesMap.find(
|
const mod = modulesMap.find(
|
||||||
({ path }) => path === args?.pluginData?.originalPath
|
({ path }) => path === args?.pluginData?.originalPath
|
||||||
),
|
),
|
||||||
resolveDir = path.dirname(args.path);
|
resolveDir = path.dirname(args.path),
|
||||||
|
css = args?.pluginData?.css || "";
|
||||||
|
|
||||||
return {
|
return {
|
||||||
resolveDir,
|
resolveDir,
|
||||||
contents: `import ${JSON.stringify(
|
contents: [
|
||||||
args.path
|
writeToFile ? `import ${JSON.stringify(args.path)};` : null,
|
||||||
)};\nexport default ${JSON.stringify(mod && mod.map ? mod.map : {})};`
|
`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)};`
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user