1

我正在使用新的 Yeoman 生成器phpwebapp。我已经调整了我的Gruntfileindex一点,以便我可以将它与 CMS 一起使用。

索引中应该丑化(连接和缩小)脚本的块会导致grunt build失败。

uglify 任务grunt.initConfig成功,只有块失败。

错误

Running "uglify:dist" (uglify) task
File "dist/_themes/cb/js/cb.js" created.
Uncompressed size: 48 bytes.
Compressed size: 31 bytes gzipped (16 bytes minified).

Running "uglify:dist/scripts/plugins.js" (uglify) task
Warning: Uglification failed. Use --force to continue.

导致构建失败的块示例。

<!-- build:js scripts/plugins.js -->
<script src="components/sass-bootstrap/js/bootstrap-affix.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-alert.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-dropdown.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-tooltip.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-modal.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-transition.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-button.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-popover.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-typeahead.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-carousel.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-scrollspy.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-collapse.js"></script>
<script src="components/sass-bootstrap/js/bootstrap-tab.js"></script>
<!-- endbuild -->

我的 Gruntfile

'use strict';
var lrSnippet = require('grunt-contrib-livereload/lib/utils').livereloadSnippet;
var mountFolder = function (connect, dir) {
    return connect.static(require('path').resolve(dir));
};

module.exports = function (grunt) {
    // load all grunt tasks
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

    // configurable paths
    var yeomanConfig = {
        app: 'app',
        dist: 'dist'
    };

    // Statamic Vars
    var theme_name = 'cb',
        theme_path = '_themes/' + theme_name; // cssmin and uglify settings don't accept vars

    grunt.initConfig({
        yeoman: yeomanConfig,
        watch: {
            coffee: {
                files: ['<%= yeoman.app %>/' + theme_path + '/coffee/*.coffee'],
                tasks: ['coffee:dist']
            },
            coffeeTest: {
                files: ['test/spec/*.coffee'],
                tasks: ['coffee:test']
            },
            compass: {
                files: ['<%= yeoman.app %>/' + theme_path + '/scss/*.{scss,sass}'],
                tasks: ['compass']
            },
            livereload: {
                files: [
                    '<%= yeoman.app %>/' + theme_path + '/{layouts,partials,templates}/*.html',
                    '<%= yeoman.app %>/*.php',
                    '{.tmp,<%= yeoman.app %>}/' + theme_path + '/css/*.css',
                    '{.tmp,<%= yeoman.app %>}/' + theme_path + '/js/*.js',
                    '<%= yeoman.app %>/' + theme_path + '/img/*.{png,jpg,jpeg,webp}'
                ],
                tasks: ['livereload']
            }
        },
        connect: {
            options: {
                port: 9000,
                // change this to '0.0.0.0' to access the server from outside
                hostname: 'localhost'
            },
            livereload: {
                options: {
                    middleware: function (connect) {
                        return [
                            lrSnippet,
                            mountFolder(connect, '.tmp'),
                            mountFolder(connect, 'app')
                        ];
                    }
                }
            },
            test: {
                options: {
                    middleware: function (connect) {
                        return [
                            mountFolder(connect, '.tmp'),
                            mountFolder(connect, 'test')
                        ];
                    }
                }
            },
            dist: {
                options: {
                    middleware: function (connect) {
                        return [
                            mountFolder(connect, 'dist')
                        ];
                    }
                }
            }
        },
        open: {
            server: {
                url: 'http://localhost.curtisblackwell.com'
            }
        },
        clean: {
            dist: ['.tmp', '<%= yeoman.dist %>/*'],
            server: '.tmp',
            build: '<%= yeoman.dist %>/scripts/remove-me-on-build.js'
        },
        jshint: {
            options: {
                jshintrc: '.jshintrc'
            },
            all: [
                'Gruntfile.js',
                '<%= yeoman.app %>/' + theme_path + '/js/*.js',
                'test/spec/*.js'
            ]
        },
        mocha: {
            all: {
                options: {
                    run: true,
                    urls: ['http://localhost:<%= connect.options.port %>/index.html']
                }
            }
        },
        coffee: {
            dist: {
                files: [{
                    expand: true,
                    cwd: '<%= yeoman.app %>/' + theme_path + '/js',
                    src: '*.coffee',
                    dest: '.tmp/' + theme_path + '/js',
                    ext: '.js'
                }]
            },
            test: {
                files: [{
                    expand: true,
                    cwd: '.tmp/spec',
                    src: '*.coffee',
                    dest: 'test/spec'
                }]
            }
        },
        compass: {
            options: {
                sassDir: '<%= yeoman.app %>/' + theme_path + '/scss',
                cssDir: '.tmp/' + theme_path + '/css',
                imagesDir: '<%= yeoman.app %>/' + theme_path + '/img',
                javascriptsDir: '<%= yeoman.app %>/' + theme_path + '/js',
                fontsDir: '<%= yeoman.app %>/' + theme_path + '/css/fonts',
                importPath: 'app/components',
                relativeAssets: true
            },
            dist: {},
            server: {
                options: {
                    debugInfo: true
                }
            }
        },
        // not used since Uglify task does concat,
        // but still available if needed
        /*concat: {
            dist: {}
        },*/

        uglify: {
            dist: {
                files: {
                    '<%= yeoman.dist %>/_themes/cb/js/cb.js': [
                        '<%= yeoman.app %>/' + theme_path + '/js/*.js'
                    ],
                }
            }
        },
        useminPrepare: {
            html: '<%= yeoman.app %>/' + theme_path + '/{layouts,partials,templates}/**',
            options: {
                dest: '<%= yeoman.dist %>'
            }
        },
        usemin: {
            html: ['<%= yeoman.dist %>/' + theme_path + '/{layouts,partials,templates}/**'],
            css: ['<%= yeoman.dist %>/' + theme_path + '/css/*.css'],
            options: {
                dirs: ['<%= yeoman.dist %>']
            }
        },
        imagemin: {
            dist: {
                files: [{
                    expand: true,
                    cwd: '<%= yeoman.app %>',
                    src: '{assets/img/**,' + theme_path + '/img/*.{png,jpg,jpeg}}',
                    dest: '<%= yeoman.dist %>'
                }]
            }
        },
        cssmin: {
            dist: {
                files: {
                    '<%= yeoman.dist %>/_themes/cb/css/cb.css': [
                        '.tmp/' + theme_path + '/css/*.css',
                        '<%= yeoman.app %>/' + theme_path + '/css/*.css'
                    ]
                }
            }
        },
        htmlmin: {
            dist: {
                options: {
                    /*removeCommentsFromCDATA: true,
                    // https://github.com/yeoman/grunt-usemin/issues/44
                    //collapseWhitespace: true,
                    collapseBooleanAttributes: true,
                    removeAttributeQuotes: true,
                    removeRedundantAttributes: true,
                    useShortDoctype: true,
                    removeEmptyAttributes: true,
                    removeOptionalTags: true*/
                },
                files: [{
                    expand: true,
                    cwd: '<%= yeoman.app %>',
                    src: '/' + theme_path + '/templates/' + '*.html',
                    dest: '<%= yeoman.dist %>'
                }]
            }
        },
        copy: {
            dist: {
                files: [{
                    expand: true,
                    dot: true,
                    cwd: '<%= yeoman.app %>',
                    dest: '<%= yeoman.dist %>',
                    src: [
                        '*.{ico,txt,php}',
                        '.htaccess',
                        '_{add-ons,app,cache,config,content,cp,logs}/**'
                    ]
                }]
            }
        },
        bower: {
            all: {
                rjsConfig: '<%= yeoman.app %>/' + theme_path + '/js/' + theme_name + '.js'
            }
        }
    });

    grunt.renameTask('regarde', 'watch');

    grunt.registerTask('server', function (target) {
        if (target === 'dist') {
            return grunt.task.run(['build', 'open', 'connect:dist:keepalive']);
        }

        grunt.task.run([
            'clean:server',
            'coffee:dist',
            'compass:server',
            'livereload-start',
            'connect:livereload',
            'open',
            'watch'
        ]);
    });

    grunt.registerTask('test', [
        'clean:server',
        'coffee',
        'compass',
        'connect:test',
        'mocha'
    ]);

    grunt.registerTask('build', [
        'clean:dist',
        'coffee',
        'compass:dist',
        'useminPrepare',
        'imagemin',
        'cssmin',
        'htmlmin',
        // 'concat',
        'uglify',
        'copy',
        'usemin',
        'clean:build'
    ]);

    grunt.registerTask('default', [
        'jshint',
        'test',
        'build'
    ]);
};
4

1 回答 1

0

正如我已经评论过的,我想这是因为没有文件可以被丑化。在我的项目中,我通过将 uglify-config 更改为此来解决它:

    uglify: {
        dist: {
            files: [{
                expand: true,
                cwd: '<%= yeoman.dist %>/scripts',
                src: ['**/*.js'],
                dest: '<%= yeoman.dist %>/scripts'
            }]
        }
    },

这只是循环遍历 dist/scripts 文件夹中的所有可用文件,并在那里将它们丑化。可能有更复杂的解决方案,但在我的情况下它有效。

于 2013-05-17T14:21:40.627 回答