Revert "feat: add writeToFile option"

This reverts commit 07545133649c7ec93e18da9960080b318ec222da.

This feature used convert css files to js (mostly because it esbuild internally was using js loader because of export statement 🤷‍♂️)
This commit is contained in:
Suraj Shetty 2022-03-15 08:51:00 +05:30
parent 07aa0b6d3a
commit 0a417a0368
3 changed files with 29 additions and 43 deletions

View File

@ -1,4 +1,5 @@
# Looking for a maintainer! # 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. 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

View File

@ -1,8 +1,11 @@
{ {
"name": "esbuild-plugin-postcss2", "name": "@frappe/esbuild-plugin-postcss2",
"version": "0.1.2", "version": "0.1.3",
"description": "Use postcss with esbuild", "description": "Use postcss with esbuild",
"repository": "https://github.com/martonlederer/esbuild-plugin-postcss2", "repository": {
"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,
@ -30,7 +33,6 @@
"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",
@ -61,5 +63,9 @@
"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"
} }

View File

@ -30,7 +30,6 @@ 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;
} }
@ -48,18 +47,16 @@ export const defaultOptions: PostCSSPluginOptions = {
sassOptions: {}, sassOptions: {},
lessOptions: {}, lessOptions: {},
stylusOptions: {}, stylusOptions: {},
writeToFile: true,
fileIsModule: null fileIsModule: null
}; };
const postCSSPlugin = ({ const postCSSPlugin = ({
plugins, plugins = [],
modules, modules = true,
rootDir, rootDir = process.cwd(),
sassOptions, sassOptions = {},
lessOptions, lessOptions = {},
stylusOptions, stylusOptions = {},
writeToFile,
fileIsModule fileIsModule
}: PostCSSPluginOptions = defaultOptions): Plugin => ({ }: PostCSSPluginOptions = defaultOptions): Plugin => ({
name: "postcss2", name: "postcss2",
@ -144,7 +141,10 @@ const postCSSPlugin = ({
// parse files with preprocessors // parse files with preprocessors
if (sourceExt === ".sass" || sourceExt === ".scss") { if (sourceExt === ".sass" || sourceExt === ".scss") {
const sassResult = await renderSass({...sassOptions, file: sourceFullPath}) const sassResult = await renderSass({
...sassOptions,
file: sourceFullPath
});
css = sassResult.css.toString(); css = sassResult.css.toString();
watchFiles.push(...sassResult.stats.includedFiles); watchFiles.push(...sassResult.stats.includedFiles);
} }
@ -172,21 +172,14 @@ 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 namespace: isModule ? "postcss-module" : "file",
? "postcss-module"
: writeToFile
? "file"
: "postcss-text",
path: tmpFilePath, path: tmpFilePath,
watchFiles, watchFiles,
pluginData: { pluginData: {
originalPath: sourceFullPath, originalPath: sourceFullPath
css: result.css
} }
}; };
} }
@ -199,30 +192,16 @@ 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: [ contents: `import ${JSON.stringify(
writeToFile ? `import ${JSON.stringify(args.path)};` : null, args.path
`export default ${JSON.stringify(mod && mod.map ? mod.map : {})};`, )};\nexport 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)};`
};
});
} }
}); });