我想在我的 Sails.js 应用程序中集成 React.js + browserify。
为此,我使用了一个 grunt-plugin 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.js
和syncAssets.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.js
中取消注释行 dev: {
和 }
,如下所示:
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.js
和 syncAssets.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.js
和 syncAssets.js
文件中添加 browserify: dev
时,为什么它不起作用?
最佳答案
我找到了正确的解决方案。
UPD:现在,我们可以使用 https://github.com/erikschlegel/sails-generate-reactjs
关于javascript - Sails.js 与 React.js,如何正确做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28331032/