From b92e8deb806d0dd4c3890052191993f0c0d269e6 Mon Sep 17 00:00:00 2001 From: g45t345rt Date: Wed, 24 Mar 2021 13:01:48 -0400 Subject: [PATCH] compiled code --- .gitignore | 1 - dist/index.esm.js | 119 ++++++++++++++++++++++++++++++++++++++ dist/index.js | 142 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 dist/index.esm.js create mode 100644 dist/index.js diff --git a/.gitignore b/.gitignore index 0c63dcb..7bd55b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ node_modules -dist build yarn-error.log test/dist diff --git a/dist/index.esm.js b/dist/index.esm.js new file mode 100644 index 0000000..224e51a --- /dev/null +++ b/dist/index.esm.js @@ -0,0 +1,119 @@ +import {ensureDir, readFile, writeFile} from "fs-extra"; +import {TextDecoder} from "util"; +import path from "path"; +import tmp from "tmp"; +import postcss from "postcss"; +import postcssModules from "postcss-modules"; +import less from "less"; +import stylus from "stylus"; +import resolveFile from "resolve-file"; +const postCSSPlugin = ({ + plugins = [], + modules = true, + rootDir = process.cwd() +}) => ({ + name: "postcss2", + setup(build) { + const tmpDirPath = tmp.dirSync().name, modulesMap = []; + const modulesPlugin = postcssModules({ + generateScopedName: "[name]__[local]___[hash:base64:5]", + ...typeof modules !== "boolean" ? modules : {}, + getJSON(filepath, json, outpath) { + modulesMap.push({ + path: filepath, + map: json + }); + if (typeof modules !== "boolean" && typeof modules.getJSON === "function") + return modules.getJSON(filepath, json, outpath); + } + }); + build.onResolve({filter: /.\.(css|sass|scss|less|styl)$/}, async (args) => { + if (args.namespace !== "file" && args.namespace !== "") + return; + let sourceFullPath = resolveFile(args.path); + if (!sourceFullPath) + sourceFullPath = path.resolve(args.resolveDir, args.path); + const sourceExt = path.extname(sourceFullPath); + 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 tmpDir = path.resolve(tmpDirPath, sourceRelDir); + let tmpFilePath = path.resolve(tmpDir, `${Date.now()}-${sourceBaseName}.css`); + if (args.kind === "entry-point") + tmpFilePath = path.resolve(tmpDir, `${sourceBaseName}.css`); + await ensureDir(tmpDir); + const fileContent = await readFile(sourceFullPath); + let css = sourceExt === ".css" ? fileContent : ""; + if (sourceExt === ".sass" || sourceExt === ".scss") + css = (await renderSass({file: sourceFullPath})).css.toString(); + if (sourceExt === ".styl") + css = await renderStylus(new TextDecoder().decode(fileContent), { + filename: sourceFullPath + }); + if (sourceExt === ".less") + css = (await less.render(new TextDecoder().decode(fileContent), { + filename: sourceFullPath, + rootpath: path.dirname(args.path) + })).css; + const result = await postcss(isModule ? [modulesPlugin, ...plugins] : plugins).process(css, { + from: sourceFullPath, + to: tmpFilePath + }); + await writeFile(tmpFilePath, result.css); + return { + namespace: isModule ? "postcss-module" : "file", + path: tmpFilePath, + pluginData: { + originalPath: sourceFullPath + } + }; + }); + build.onLoad({filter: /.*/, namespace: "postcss-module"}, async (args) => { + const mod = modulesMap.find(({path: path2}) => path2 === args?.pluginData?.originalPath), resolveDir = path.dirname(args.path); + return { + resolveDir, + contents: `import ${JSON.stringify(args.path)}; +export default ${JSON.stringify(mod && mod.map ? mod.map : {})};` + }; + }); + } +}); +function renderSass(options) { + return new Promise((resolve, reject) => { + getSassImpl().render(options, (e, res) => { + if (e) + reject(e); + else + resolve(res); + }); + }); +} +function renderStylus(str, options) { + return new Promise((resolve, reject) => { + stylus.render(str, options, (e, res) => { + if (e) + reject(e); + else + resolve(res); + }); + }); +} +function getSassImpl() { + let impl = "sass"; + try { + require.resolve("sass"); + } catch { + try { + require.resolve("node-sass"); + impl = "node-sass"; + } catch { + throw new Error('Please install "sass" or "node-sass" package'); + } + } + return require(impl); +} +var src_default = postCSSPlugin; +export { + src_default as default +}; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..d2883f5 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,142 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __markAsModule = (target) => __defProp(target, "__esModule", {value: true}); +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, {get: all[name], enumerable: true}); +}; +var __exportStar = (target, module2, desc) => { + if (module2 && typeof module2 === "object" || typeof module2 === "function") { + for (let key of __getOwnPropNames(module2)) + if (!__hasOwnProp.call(target, key) && key !== "default") + __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable}); + } + return target; +}; +var __toModule = (module2) => { + return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2); +}; +__markAsModule(exports); +__export(exports, { + default: () => src_default +}); +var import_fs_extra = __toModule(require("fs-extra")); +var import_util = __toModule(require("util")); +var import_path = __toModule(require("path")); +var import_tmp = __toModule(require("tmp")); +var import_postcss2 = __toModule(require("postcss")); +var import_postcss_modules = __toModule(require("postcss-modules")); +var import_less = __toModule(require("less")); +var import_stylus = __toModule(require("stylus")); +var import_resolve_file = __toModule(require("resolve-file")); +const postCSSPlugin = ({ + plugins = [], + modules = true, + rootDir = process.cwd() +}) => ({ + name: "postcss2", + setup(build) { + const tmpDirPath = import_tmp.default.dirSync().name, modulesMap = []; + const modulesPlugin = (0, import_postcss_modules.default)({ + generateScopedName: "[name]__[local]___[hash:base64:5]", + ...typeof modules !== "boolean" ? modules : {}, + getJSON(filepath, json, outpath) { + modulesMap.push({ + path: filepath, + map: json + }); + if (typeof modules !== "boolean" && typeof modules.getJSON === "function") + return modules.getJSON(filepath, json, outpath); + } + }); + build.onResolve({filter: /.\.(css|sass|scss|less|styl)$/}, async (args) => { + if (args.namespace !== "file" && args.namespace !== "") + return; + let sourceFullPath = (0, import_resolve_file.default)(args.path); + if (!sourceFullPath) + sourceFullPath = import_path.default.resolve(args.resolveDir, args.path); + const sourceExt = import_path.default.extname(sourceFullPath); + const sourceBaseName = import_path.default.basename(sourceFullPath, sourceExt); + const sourceDir = import_path.default.dirname(sourceFullPath); + const sourceRelDir = import_path.default.relative(import_path.default.dirname(rootDir), sourceDir); + const isModule = sourceBaseName.match(/\.module$/); + const tmpDir = import_path.default.resolve(tmpDirPath, sourceRelDir); + let tmpFilePath = import_path.default.resolve(tmpDir, `${Date.now()}-${sourceBaseName}.css`); + if (args.kind === "entry-point") + tmpFilePath = import_path.default.resolve(tmpDir, `${sourceBaseName}.css`); + await (0, import_fs_extra.ensureDir)(tmpDir); + const fileContent = await (0, import_fs_extra.readFile)(sourceFullPath); + let css = sourceExt === ".css" ? fileContent : ""; + if (sourceExt === ".sass" || sourceExt === ".scss") + css = (await renderSass({file: sourceFullPath})).css.toString(); + if (sourceExt === ".styl") + css = await renderStylus(new import_util.TextDecoder().decode(fileContent), { + filename: sourceFullPath + }); + if (sourceExt === ".less") + css = (await import_less.default.render(new import_util.TextDecoder().decode(fileContent), { + filename: sourceFullPath, + rootpath: import_path.default.dirname(args.path) + })).css; + const result = await (0, import_postcss2.default)(isModule ? [modulesPlugin, ...plugins] : plugins).process(css, { + from: sourceFullPath, + to: tmpFilePath + }); + await (0, import_fs_extra.writeFile)(tmpFilePath, result.css); + return { + namespace: isModule ? "postcss-module" : "file", + path: tmpFilePath, + pluginData: { + originalPath: sourceFullPath + } + }; + }); + build.onLoad({filter: /.*/, namespace: "postcss-module"}, async (args) => { + const mod = modulesMap.find(({path: path2}) => path2 === args?.pluginData?.originalPath), resolveDir = import_path.default.dirname(args.path); + return { + resolveDir, + contents: `import ${JSON.stringify(args.path)}; +export default ${JSON.stringify(mod && mod.map ? mod.map : {})};` + }; + }); + } +}); +function renderSass(options) { + return new Promise((resolve, reject) => { + getSassImpl().render(options, (e, res) => { + if (e) + reject(e); + else + resolve(res); + }); + }); +} +function renderStylus(str, options) { + return new Promise((resolve, reject) => { + import_stylus.default.render(str, options, (e, res) => { + if (e) + reject(e); + else + resolve(res); + }); + }); +} +function getSassImpl() { + let impl = "sass"; + try { + require.resolve("sass"); + } catch { + try { + require.resolve("node-sass"); + impl = "node-sass"; + } catch { + throw new Error('Please install "sass" or "node-sass" package'); + } + } + return require(impl); +} +var src_default = postCSSPlugin;