From 07545133649c7ec93e18da9960080b318ec222da Mon Sep 17 00:00:00 2001 From: Guten Ye Date: Fri, 17 Sep 2021 08:45:06 +0800 Subject: [PATCH] feat: add writeToFile option --- src/index.ts | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5b5174c..1af1693 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ interface PostCSSPluginOptions { sassOptions?: SassOptions; lessOptions?: Less.Options; stylusOptions?: StylusRenderOptions; + writeToFile?: boolean; } interface CSSModule { @@ -45,7 +46,8 @@ const postCSSPlugin = ({ rootDir = process.cwd(), sassOptions = {}, lessOptions = {}, - stylusOptions = {} + stylusOptions = {}, + writeToFile = true }: PostCSSPluginOptions): Plugin => ({ name: "postcss2", setup(build) { @@ -152,14 +154,21 @@ const postCSSPlugin = ({ }); // Write result CSS - await writeFile(tmpFilePath, 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: getFilesRecursive(sourceDir), pluginData: { - originalPath: sourceFullPath + originalPath: sourceFullPath, + css: result.css } }; } @@ -172,16 +181,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)};` + }; + }); } });