Grunt-Browserify extensions flag not working

大憨熊 提交于 2020-01-15 11:33:29

问题


I am trying to use Grunt-browserify and reactify to parse and bundle React components written in jsx. I want to use the extension flag so that I don't have have to specify the file extension name of my modules, but I have been unable to get this to work. Here is some test code:

A Gruntfile:

'use strict';
module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        browserify: {
            dev: {
                src: 'src/app.jsx',
                dest: 'dest/app.js',
                options: {
                    debug: true,
                    transform: ['reactify'],
                    extensions: ['.jsx']
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-browserify');

    grunt.registerTask('default', ['browserify:dev']);

};

The main app file app.jsx:

'use strict';
var React = require('react');
var Test = require('./components/Test');   // Here is the problem... 

React.render(
    <Test />,
    document.getElementById('test')
);

And then Test.jsx:

'use strict';
var React = require('react');

var Test = React.createClass({

    render: function() {
        return(
            <div>
            <p>Some markup</p>
            </div>
        );
    }
});

module.exports = Test;

When I try to compile this by running grunt, I get an error:

Error: Cannot find module './components/Test' from '/Users/****/Sites/grunt-test/src'

The Grunt-browserify documentation says that it has supported the extensions flag since v1.2.6 and I have seen examples of this all over the web, but I can't seem to get it to work here. If I run browserify from the command line -- like so browserify -t reactify --extension=.jsx -o dest/app.js -- it works.

Any ideas?


回答1:


Have you tried moving the properties inside browserifyOptions as follow?

grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        browserify: {
            dev: {
                src: 'src/app.jsx',
                dest: 'dest/app.js',
                options: {
                    browserifyOptions: {
                        debug: true,
                        transform: ['reactify'],
                        extensions: ['.jsx']
                    }
                }
            }
        }
    });


来源:https://stackoverflow.com/questions/28393969/grunt-browserify-extensions-flag-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!