Skip to content

Commit

Permalink
1. 新增 raw-loader加载器及raw配置参数
Browse files Browse the repository at this point in the history
2. 新增cssModule控制参数
  • Loading branch information
bplok20010 committed Oct 16, 2020
1 parent b782309 commit 805bc33
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 55 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ transformEs( src, dest, options );
filter: null,// file => boolean
babel: {
decoratorsBeforeExport: true,
strictMode: true,
useFlow: true,
loose: true,
runtimeOptions: {},
Expand All @@ -73,6 +72,17 @@ transformEs( src, dest, options );
},
eslint: {},
postcss: {},
// https://www.npmjs.com/package/gulp-dart-scss
scss: {},
// https://www.npmjs.com/package/gulp-less
less: {},
cssModule: {
test: /\.module.(less|scss|css)$/,
esModule: true,
},
raw: {
test: /\.svg$/,
},
}
```

Expand Down
2 changes: 2 additions & 0 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { isDir } = require("./utils");
const babelLoader = require("./loaders/babel");
const eslintLoader = require("./loaders/eslint");
const cssLoader = require("./loaders/css");
const rawLoader = require("./loaders/raw");

module.exports = async function compile(file, options) {
const ignore =
Expand Down Expand Up @@ -38,6 +39,7 @@ module.exports = async function compile(file, options) {
if (!ignore(file)) {
stream = eslintLoader(stream, options, meta);
stream = cssLoader(stream, options, meta);
stream = rawLoader(stream, options, meta);
stream = babelLoader(stream, options, meta);
}
stream = stream.pipe(gulp.dest(outDir));
Expand Down
7 changes: 7 additions & 0 deletions lib/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ module.exports = function (options) {
eslint: {},
scss: {},
less: {},
cssModule: {
test: /\.module.(less|scss|css)$/,
esModule: true,
},
raw: {
test: /\.svg$/,
},
};

return defaultsDeep({}, options, defaults);
Expand Down
29 changes: 20 additions & 9 deletions lib/loaders/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ function isCssFile(file) {
return file.extname === ".css" || isScssFile(file) || isLessFile(file);
}

function isModuleCssFile(file) {
return /module\.(:?css|less|scss)$/.test(file.path);
}

function handleSassError(error) {
const message = new PluginError("sass", error.messageFormatted).toString();
log.save(`Scss:\n${message}\n`);
Expand All @@ -34,26 +30,41 @@ module.exports = function (stream, options, meta) {
const postcssConfig = require("../config/postcss.config");
const config = postcssConfig(options.postcss);

if (isModuleCssFile({ path: meta.filePath })) {
let isCssModule = false;
let esModule = true;

if (options.cssModule && "esModule" in options.cssModule) {
esModule = options.cssModule.esModule;
}

if (options.cssModule && options.cssModule.test && options.cssModule.test.test(meta.filePath)) {
isCssModule = true;
}

if (isCssModule) {
config.plugins.push(
cssModules({
getJSON: function (_, json) {
const fileName = path.basename(meta.outFile) + ".css";
fs.writeFile(
meta.outFile + ".js",
`require('./${fileName}');module.exports = ${JSON.stringify(json)};`
`"use strict";require('./${fileName}');${
esModule
? 'exports.__esModule = true;exports["default"]='
: "module.exports="
}${JSON.stringify(json)};`
).catch(console.log);
},
})
);
}

return stream
.pipe(gulpif(isScssFile, scss({}).on("error", handleSassError)))
.pipe(gulpif(isScssFile, scss(options.scss || {}).on("error", handleSassError)))
.on("error", function () {
this.emit("end");
})
.pipe(gulpif(isLessFile, less()))
.pipe(gulpif(isLessFile, less(options.less || {})))
.on("error", function (err) {
handleError.call(this, err, "Less");
})
Expand All @@ -63,7 +74,7 @@ module.exports = function (stream, options, meta) {
})
.pipe(
gulpif(
isModuleCssFile,
() => isCssModule,
rename({
extname: path.extname(meta.outFile) + ".css",
})
Expand Down
51 changes: 51 additions & 0 deletions lib/loaders/raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const gulpif = require("gulp-if");
const path = require("path");
const through2 = require("through2");
const rename = require("gulp-rename");
const { handleError } = require("../utils");

function raw(opts) {
return through2.obj(function (file, _, cb) {
if (file.isBuffer()) {
let content = file.contents.toString();
content = `export default ${JSON.stringify(content)};`;
file.contents = Buffer.from(content);
}

cb(null, file);
});
}

module.exports = function (stream, options, meta) {
// let esModule = true;
let useRaw = false;

// if (options.raw && "esModule" in options.raw) {
// esModule = options.raw.esModule;
// }

if (options.raw && options.raw.test && options.raw.test.test(meta.filePath)) {
useRaw = true;
}

return stream
.pipe(
gulpif(
() => useRaw,
raw({
// esModule,
})
)
)
.on("error", function (err) {
handleError.call(this, err, "RAW");
})
.pipe(
gulpif(
() => useRaw,
rename({
extname: path.extname(meta.outFile) + ".js",
})
)
);
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "transform-es",
"version": "5.3.0",
"version": "5.4.0",
"description": "",
"keywords": [
"compile",
Expand Down
1 change: 1 addition & 0 deletions test/src/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
88 changes: 45 additions & 43 deletions test/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ const path = require("path");
const fs = require("fs-extra");
const glob = require("fast-glob");

import a from "./icon.svg";

var t = require("./cjst");

import z from "./cjst";

// @ta
class MyApp {
static a = 3;
title = "test";
constructor() {}
say() {}
static a = 3;
title = "test";
constructor() {}
say() {}

a = () => {};
a = () => {};
}

//cst a = 4;

async function test() {
const a = await "23";
const a = await "23";
}

test();
Expand All @@ -28,41 +30,41 @@ new Promise(r => r(1));

const x = new Set();

module.exports = function(appSrc = "src", appDist = "dest", options = {}) {
const defaults = {
cwd: process.cwd(),
cleanDist: true,
globOptions: {}
};

appSrc = appSrc || ".";

options = Object.assign({}, defaults, options);

if (options.cleanDist) {
fs.emptyDirSync(path.resolve(options.cwd, appDist));
}

glob(
["**/?(*).*", "**/*"],
Object.assign(
{
onlyFiles: false
//absolute: true,
},
options.globOptions,
{
cwd: path.resolve(options.cwd, appSrc)
}
)
)
.then(files => {
files.forEach(file => {
const absSrcFile = path.resolve(options.cwd, appSrc, file);
const absDestFile = path.resolve(options.cwd, appDist, file);

fs.copySync(absSrcFile, absDestFile);
});
})
.catch(console.error);
module.exports = function (appSrc = "src", appDist = "dest", options = {}) {
const defaults = {
cwd: process.cwd(),
cleanDist: true,
globOptions: {},
};

appSrc = appSrc || ".";

options = Object.assign({}, defaults, options);

if (options.cleanDist) {
fs.emptyDirSync(path.resolve(options.cwd, appDist));
}

glob(
["**/?(*).*", "**/*"],
Object.assign(
{
onlyFiles: false,
//absolute: true,
},
options.globOptions,
{
cwd: path.resolve(options.cwd, appSrc),
}
)
)
.then(files => {
files.forEach(file => {
const absSrcFile = path.resolve(options.cwd, appSrc, file);
const absDestFile = path.resolve(options.cwd, appDist, file);

fs.copySync(absSrcFile, absDestFile);
});
})
.catch(console.error);
};

0 comments on commit 805bc33

Please sign in to comment.