Sails.js与React.js,如何正确操作

Sails.js with React.js, how to do it correctly?

本文关键字:js 何正确 操作 React Sails      更新时间:2023-09-26

我想在我的Sails.js-application.中集成React.js+browserfy

为此,我使用了一个grunt插件grunt react。

我创建了一个文件/tasks/config/browserify.js

module.exports = function(grunt) {
  grunt.config.set('browserify', {
    //dev: {
      options:      {
        transform:  [ require('grunt-react').browserify ],
        extension: 'jsx'
      },
      app:          {
        src:        'assets/jsx/app.jsx',
        dest:       '.tmp/public/js/app.js'
      }
    //}
  });
  grunt.loadNpmTasks('grunt-browserify');
};

然后我在compileAssets.jssyncAssets.js:中添加了一行

// compileAssets.js
module.exports = function (grunt) {
    grunt.registerTask('compileAssets', [
        'clean:dev',
        'stylus:dev',
        'browserify',   // <<< this added
        'copy:dev'
    ]);
};

// syncAssets.js
module.exports = function (grunt) {
    grunt.registerTask('syncAssets', [
        'stylus:dev',
        'browserify',   // <<< this added
        'sync:dev'
    ]);
};

然后我修改了copy.js中的一行。

// copy.js
module.exports = function(grunt) {
    grunt.config.set('copy', {
        dev: {
            files: [{
                expand: true,
                cwd: './assets',
                src: ['**/*.!(styl|jsx)'],   /// <<< this modified
                dest: '.tmp/public'
            }]
        },
        build: {
            files: [{
                expand: true,
                cwd: '.tmp/public',
                src: ['**/*'],
                dest: 'www'
            }]
        }
    });
    grunt.loadNpmTasks('grunt-contrib-copy');
};

它成功了

但我觉得我做得不太对。

如果我取消/tasks/config/browserify.jsdev: {}行的注释,如下所示:

module.exports = function(grunt) {
  grunt.config.set('browserify', {
    dev: {                           /// <<< this uncommented
      options:      {
        transform:  [ require('grunt-react').browserify ],
        extension: 'jsx'
      },
      app:          {
        src:        'assets/jsx/app.jsx',
        dest:       '.tmp/public/js/app.js'
      }
    }                           /// <<< this uncommented
  });
  grunt.loadNpmTasks('grunt-browserify');
};

如果我在compileAssets.jssyncAssets.js:中进行更改

// compileAssets.js
module.exports = function (grunt) {
    grunt.registerTask('compileAssets', [
        'clean:dev',
        'stylus:dev',
        'browserify:dev',   // <<< this added :dev
        'copy:dev'
    ]);
};

// syncAssets.js
module.exports = function (grunt) {
    grunt.registerTask('syncAssets', [
        'stylus:dev',
        'browserify:dev',   // <<< this added :dev
        'sync:dev'
    ]);
};

它不起作用

值得担心吗?

当我在compileAssets.jssyncAssets.js文件中添加browserify: dev时,为什么它不起作用?

我找到了正确的解决方案。

UPD:现在,我们可以使用https://github.com/erikschlegel/sails-generate-reactjs

我创建了grunt reatify,允许您为JSX文件创建一个捆绑文件,以便更容易地使用模块化React组件。您所要做的就是指定父目标文件夹和源文件:

grunt.initConfig({
  reactify: {
      'tmp': 'test/**/*.jsx'
  },
})

在Sails中,转到tasks/config文件夹,创建一个新文件"reatify.js"并插入以下代码:

module.exports = function (grunt) {
    grunt.config.set('reactify', {
        '[Destination folder]': '[folder containing React Components]/**/*.jsx'
    });
    grunt.loadNpmTasks('grunt-reactify');
};

然后转到文件tasks/register/compaileAssets.js并添加reatify:

module.exports = function (grunt) {
	grunt.registerTask('compileAssets', [
		'clean:dev',
		'jst:dev',
		'less:dev',
		'copy:dev',
		'coffee:dev',
        'reactify'
	]);
};

就这样!