简体   繁体   English

Browserify转换与reactify

[英]Browserify transform with reactify

I am trying to build a project using gulp. 我正在尝试使用gulp构建项目。 Unfortunately I am experiencing some trouble with it. 不幸的是我遇到了一些麻烦。

This is what my gulpfile.js looks like: 这是我的gulpfile.js的样子:

'use strict';

var watchify = require('watchify');
var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var gutil = require('gulp-util');
var sourcemaps = require('gulp-sourcemaps');
var assign = require('lodash.assign');
var reactify = require('reactify');

// add custom browserify options here
var customOpts = {
  entries: ['./js/app.js'],
  debug: true
};
var opts = assign({}, watchify.args, customOpts);
var b = watchify(browserify(opts)); 
b.transform(reactify);

// add transformations here
// i.e. b.transform(coffeeify);

gulp.task('js', bundle); // so you can run `gulp js` to build the file
b.on('update', bundle); // on any dep update, runs the bundler
b.on('log', gutil.log); // output build logs to terminal

function bundle() {

  return b.bundle()
    // log errors if they happen
    .on('error', gutil.log.bind(gutil, 'Browserify Error'))
    .pipe(source('bundle.js'))
    // optional, remove if you don't need to buffer file contents
    .pipe(buffer())
    // optional, remove if you dont want sourcemaps
    .pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file
       // Add transformation tasks to the pipeline here.
    .pipe(sourcemaps.write('./')) // writes .map file
    .pipe(gulp.dest('./js/build'));
}

The problem is that gulp is displaying an error indicating that some third party library has somewhere the illegal token '<' the file in question is written in the JSX syntax. 问题是gulp显示错误,表明某个第三方库在某个地方存在非法令牌'<',该文件使用JSX语法编写。

Yet, I don't have the problem when I run the following command: 但是,当我运行以下命令时,我没有问题:

browserify -g reactify js\app.js > js\build\bundle.js

Please, what is wrong with my gulpfile? 拜托,我的gulpfile怎么了?

Thanks in advance to anybody seeking to help! 在此先感谢任何寻求帮助的人!

Edit: I suspect that the -g option makes it transform the jsx recursively to plain javascript, I'm trying to find out an equivalent in the watchify.transform function, unsuccessfully. 编辑:我怀疑-g选项使它递归地将jsx转换为纯javascript,我试图在watchify.transform函数中找到一个等效项,但未成功。

As the docs for .transform say: 正如.transform文档所说:

If opts.global is true, the transform will operate on ALL files, despite whether they exist up a level in a node_modules/ directory 如果opts.global为true,则转换将对所有文件进行操作,尽管它们是否存在于node_modules /目录中的某个级别上

So to replicate -g reactify , you'd do 所以要复制-g reactify ,你会做

b.transform(reactify, {global: true});

Keep in mind however that I would consider the fact that you need -g to be a bug in whatever dependency node_module you are referring to. 但是请记住,我要考虑的事实是,无论您要引用的依赖项node_module如何,都需要-g作为错误。 Generally modules should include browserify config params in their own package.json file so that you don't have to configure it globally. 通常,模块应在其自己的package.json文件中包含browserify config参数,这样您就不必全局配置它。 As the next part of those docs say: 正如这些文档的下一部分所述:

Use global transforms cautiously and sparingly, since most of the time an ordinary transform will suffice. 谨慎而谨慎地使用全局转换,因为在大多数情况下,一个普通的转换就足够了。

Better to use Babel it supports JSX as well. 最好使用Babel,它也支持JSX

https://babeljs.io/docs/usage/jsx/ https://babeljs.io/docs/usage/jsx/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM