From 273e787df84a2fcf2e59e6adb6059f80cf46ae03 Mon Sep 17 00:00:00 2001 From: bplok20010 Date: Sat, 17 Oct 2020 18:16:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=9B=B4=E6=8E=A5import=20cs?= =?UTF-8?q?s=E7=9A=84=E5=8E=9F=E5=A7=8B=E6=89=A9=E5=B1=95=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/loaders/css.js | 22 ++++++++++++++++++++++ package.json | 2 +- test/src/css/less/_var.less | 3 +++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/src/css/less/_var.less diff --git a/lib/loaders/css.js b/lib/loaders/css.js index bd9116f..415f3da 100644 --- a/lib/loaders/css.js +++ b/lib/loaders/css.js @@ -9,6 +9,19 @@ const cssModules = require("postcss-modules"); const fs = require("fs-extra"); const log = require("../logger"); const { handleError } = require("../utils"); +const through2 = require("through2"); + +function generateCssJS({ meta }) { + return through2.obj(function (file, _, cb) { + const fileName = path.basename(meta.outFile, path.extname(meta.outFile)) + ".css"; + + fs.writeFile(meta.outFile + ".js", `"use strict";require('./${fileName}');`) + .then(() => { + cb(null, file); + }) + .catch(e => cb(e)); + }); +} function isScssFile(file) { return file.extname === ".scss"; @@ -72,6 +85,15 @@ module.exports = function (stream, options, meta) { .on("error", function (err) { handleError.call(this, err, "PostCSS"); }) + .pipe( + gulpif( + file => isCssFile(file) && !isCssModule, + generateCssJS({ + meta, + esModule, + }) + ) + ) .pipe( gulpif( () => isCssModule, diff --git a/package.json b/package.json index 639e1fb..418a87c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "transform-es", - "version": "5.5.0", + "version": "5.6.0", "description": "", "keywords": [ "compile", diff --git a/test/src/css/less/_var.less b/test/src/css/less/_var.less new file mode 100644 index 0000000..fd2f2c1 --- /dev/null +++ b/test/src/css/less/_var.less @@ -0,0 +1,3 @@ +// Variables +@link-color: #428bca; // sea blue +@link-color-hover: darken(@link-color, 10%); \ No newline at end of file