admin管理员组文章数量:1349071
I have some React files (.jsx) in my projet, and I need to have each one of them converted into different .js file (following the same directory structure) using a Gulp task.
So, just to clarify, I don't want to use something like Browserify.boudle() since it would leave me with a single converted .js file.
I have some React files (.jsx) in my projet, and I need to have each one of them converted into different .js file (following the same directory structure) using a Gulp task.
So, just to clarify, I don't want to use something like Browserify.boudle() since it would leave me with a single converted .js file.
Share Improve this question edited Mar 26, 2017 at 3:58 Cœur 38.8k25 gold badges205 silver badges277 bronze badges asked Jan 9, 2015 at 18:31 ArrudaArruda 8802 gold badges12 silver badges25 bronze badges 1-
What have you tried? It seems like
gulp-jsx
andgulp-rename
will get you what you want, if you just want to output a jsx transformed file with a.js
extension. – Nick Tomlin Commented Jan 9, 2015 at 18:50
2 Answers
Reset to default 9If you use gulp-react, it will rename them to .js
automatically as well as transpiling:
var gulp = require('gulp')
var react = require('gulp-react')
gulp.task('transpile-js', function() {
return gulp.src('./src/**/*.jsx')
.pipe(react({harmony: true}))
.pipe(gulp.dest('./lib'))
})
Here's my Gulpfile:
It uses gulp-babel
, which can be a replacement for gulp-react
.
gulpfile.js
/// <binding BeforeBuild='minify' Clean='clean' />
"use strict";
var gulp = require("gulp"),
del = require("del"),
cssmin = require("gulp-cssmin"),
uglify = require("gulp-uglify"),
babel = require("gulp-babel"),
debug = require("gulp-debug"),
rename = require("gulp-rename");
var paths = {
wwwroot: "./wwwroot/"
};
paths.css = paths.wwwroot + "css/site/**/*.css";
paths.minCss = paths.wwwroot + "css/site/**/*.min.css";
paths.js = paths.wwwroot + "js/site/**/*.js";
paths.minJs = paths.wwwroot + "js/site/**/*.min.js";
paths.babelJsx = paths.wwwroot + "jsx/**/*.jsx";
paths.babelJs = paths.wwwroot + "jsx/**/*.js";
paths.babelMinJs = paths.wwwroot + "jsx/**/*.min.js";
gulp.task("clean-js", function (cb) {
return del([
paths.minJs
]);
});
gulp.task("clean-jsx-js", function (cb) {
return del([
paths.babelJs
]);
});
gulp.task("clean-jsx-minjs", function (cb) {
return del([
paths.babelMinJs
]);
});
gulp.task("clean-jsx", ["clean-jsx-js", "clean-jsx-minjs"]);
gulp.task("clean-css", function (cb) {
return del([
paths.minCss
]);
});
gulp.task("clean", ["clean-js", "clean-jsx", "clean-css"]);
gulp.task("transpile", function () {
return gulp.src([paths.babelJsx], { base: "." })
.pipe(debug({ title: 'babel:' }))
.pipe(babel({
presets: ["es2015", "react"]
}))
.pipe(gulp.dest("."))
.pipe(debug({ title: 'babel:' }));
});
gulp.task("minify-js", function () {
return gulp.src([paths.js, "!" + paths.minJs], { base: "." })
.pipe(debug({ title: 'minify-js:' }))
.pipe(uglify())
.pipe(rename({
suffix: ".min"
}))
.pipe(gulp.dest("."))
.pipe(debug({ title: 'minify-js:' }));
});
gulp.task("minify-jsx", ["transpile"], function () {
return gulp.src([paths.babelJs, "!" + paths.babelMinJs], { base: "." })
.pipe(debug({ title: 'minify-jsx-' }))
.pipe(uglify())
.pipe(rename({
suffix: ".min"
}))
.pipe(gulp.dest("."))
.pipe(debug({ title: 'minify-jsx-' }));
});
gulp.task("minify-css", function () {
return gulp.src([paths.css, "!" + paths.minCss], { base: "." })
.pipe(debug({ title: 'minify-css:' }))
.pipe(cssmin())
.pipe(rename({
suffix: ".min"
}))
.pipe(gulp.dest("."))
.pipe(debug({ title: 'minify-css:' }));
});
gulp.task("minify", ["transpile", "minify-js", "minify-jsx", "minify-css"]);
本文标签:
版权声明:本文标题:javascript - How to make a Gulp task to transform each jsx file of a project into different .js file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743857711a2551160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论