index.js
function walkDependencies(file, rules) {

    var directory = path.dirname(file.path);
    var content = file.contents.toString();

    var list = [ ];

    // 这里不用判断是否已添加
    // 比如同一个文件出现下面两句
    //
    // require(
    //     [ 'common/a' ]
    // );
    //
    // require(
    //     [ 'common/a', 'common/b' ]
    // );
    //
    // common/a 无需判断是否存在，因为它出现在不同的匹配块中
    //
    var addDependency = function (dependency) {
        list.push(dependency);
    };

    rules.forEach(function (parser, index) {

        var results = content.match(parser.pattern);

        if (results) {
            results.forEach(function (result) {

                var dependencies = parser.match(result, file);
                if (!dependencies) {
                    return;
                }

                if (!Array.isArray(dependencies)) {
                    dependencies = [ dependencies ];
                }

                dependencies.forEach(function (dependency) {

                    // 支持返回对象，必须包含 raw 属性
                    if (typeof dependency === 'string') {
                        dependency = {
                            raw: dependency
                        };
                    }

                    var raw = dependency.raw;
                    var absolute = dependency.absolute;

                    if (!absolute) {
                        absolute = /^(?:\w|\.(?:\.)?)/.test(raw)
                                 ? path.join(directory, raw)
                                 : raw;
                    }

                    var extname = dependency.extname;
                    if (extname && extname.length > 1) {

                        var terms = absolute.split('.');
                        terms.pop();
                        terms.push(
                            extname.substr(1)
                        );

                        absolute = terms.join('.');

                    }

                    dependency.raw = cleanQuery(raw);
                    dependency.absolute = cleanQuery(absolute);

                    // 便于替换回去
                    dependency.match = result;

                    addDependency(dependency);

                });

            });

        }

    });

    return list;

}
function walkDependencies(file, rules) {

    var directory = path.dirname(file.path);
    var content = file.contents.toString();

    var list = [ ];

    // 这里不用判断是否已添加
    // 比如同一个文件出现下面两句
    //
    // require(
    //     [ 'common/a' ]
    // );
    //
    // require(
    //     [ 'common/a', 'common/b' ]
    // );
    //
    // common/a 无需判断是否存在，因为它出现在不同的匹配块中
    //
    var addDependency = function (dependency) {
        list.push(dependency);
    };

    rules.forEach(function (parser, index) {

        var results = content.match(parser.pattern);

        if (results) {
            results.forEach(function (result) {

                var dependencies = parser.match(result, file);
                if (!dependencies) {
                    return;
                }

                if (!Array.isArray(dependencies)) {
                    dependencies = [ dependencies ];
                }

                dependencies.forEach(function (dependency) {

                    // 支持返回对象，必须包含 raw 属性
                    if (typeof dependency === 'string') {
                        dependency = {
                            raw: dependency
                        };
                    }

                    var raw = dependency.raw;
                    var absolute = dependency.absolute;

                    if (!absolute) {
                        absolute = /^(?:\w|\.(?:\.)?)/.test(raw)
                                 ? path.join(directory, raw)
                                 : raw;
                    }

                    var extname = dependency.extname;
                    if (extname && extname.length > 1) {

                        var terms = absolute.split('.');
                        terms.pop();
                        terms.push(
                            extname.substr(1)
                        );

                        absolute = terms.join('.');

                    }

                    dependency.raw = cleanQuery(raw);
                    dependency.absolute = cleanQuery(absolute);

                    // 便于替换回去
                    dependency.match = result;

                    addDependency(dependency);

                });

            });

        }

    });

    return list;

}
正则全局提取依赖

@inner
@param {Object} file
@param {Array} rules
@return {Array}