如何缩小AngularJS模块

如何缩小AngularJS模块,angularjs,Angularjs,我已经将Angularjs应用程序分解为模块,并将其组件分为各自的文件 /app /components calendar.directive.js calendar.directive.html /people people.html people-view.html people.controller.js /event event.html event-v

我已经将Angularjs应用程序分解为模块,并将其组件分为各自的文件

/app
    /components
        calendar.directive.js
        calendar.directive.html
    /people
        people.html
        people-view.html
        people.controller.js
    /event
        event.html
        event-view.html
        event.controller.js
        event.service.js
这部分我不太清楚。。在构建应用程序时,是否需要调用所有这些文件

<script src="calendar.js"></script>
<script src="people.controller.js"></script>
<script src="event.controller.js"></script>


或者,有没有一种方法可以将所有js模块合并到一个文件中,并一次调用所有js模块?

您可以将所有js模块合并到一个文件中,但您需要将视图保存在单独的文件中,因为您要按其文件名包含视图

你可以使用合并和缩小你所有的东西,这将减少你的时间

要做的一件事是,当您在另一个控制器或服务中使用inject模块时,用户阵列将使用direct函数。例如

//这将在缩小过程后中断应用程序。
角度控制器('myController',函数($scope){
})
//使用此模式,这将在缩小过程后非常有用
角度控制器('myController',['$scope',函数($scope){
}

])
您可以将所有js模块合并到一个文件中,但您需要将视图保存在单独的文件中,因为您要按视图的文件名包含视图

你可以使用合并和缩小你所有的东西,这将减少你的时间

要做的一件事是,当您在另一个控制器或服务中使用inject模块时,用户阵列将使用direct函数。例如

//这将在缩小过程后中断应用程序。
角度控制器('myController',函数($scope){
})
//使用此模式,这将在缩小过程后非常有用
角度控制器('myController',['$scope',函数($scope){
}

])
是的,AngularJS文档提供了一篇关于缩小AngularJS应用程序时需要注意的事项的长篇文章:


要注意的主要问题是,如果您正在使用,缩小可以打破“神奇的依赖注入”,但有办法管理它。

是的,AngularJS文档提供了一篇关于缩小AngularJS应用程序时要注意的事项的长篇文章:


要注意的主要问题是,如果您正在使用“神奇的依赖注入”,缩小可能会破坏“神奇的依赖注入”,但有一些方法可以管理它。

我正在使用maven构建系统来实现这一点。这篇文章假设你了解Maven。正如另一个答案中所提到的,还需要对Angularjs代码进行一些更改,以便进行缩小

下面是POM配置文件的示例

<profiles>
    <profile>
    <id>release</id>
    <build>
        <plugins>
        <plugin>
            <groupId>com.samaxes.maven</groupId>         
            <artifactId>minify-maven-plugin</artifactId>         
            <version>1.6.2</version>         
            <executions>             
                <execution>
                    <id>default-minify</id>
                    <phase>prepare-package</phase><!-- When omitted defaults to 'process-resources' -->
                    <configuration>
                        <charset>UTF-8</charset>
                        <jsSourceDir>app</jsSourceDir>
                        <jsSourceIncludes>
                            <jsSourceInclude>app.js</jsSourceInclude>
                            <jsSourceInclude>**/*.js</jsSourceInclude>
                        </jsSourceIncludes>
                        <jsTargetDir>min/js</jsTargetDir>
                        <jsFinalFile>app-min-final.js</jsFinalFile>
                        <jsEngine>CLOSURE</jsEngine>
                    </configuration>
                    <goals>
                        <goal>minify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <configuration>
                        <tasks>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        </plugins>
    </build>
    </profile>
<profiles>

我正在使用maven构建系统来完成这项工作。这篇文章假设你了解Maven。正如另一个答案中所提到的,还需要对Angularjs代码进行一些更改,以便进行缩小

下面是POM配置文件的示例

<profiles>
    <profile>
    <id>release</id>
    <build>
        <plugins>
        <plugin>
            <groupId>com.samaxes.maven</groupId>         
            <artifactId>minify-maven-plugin</artifactId>         
            <version>1.6.2</version>         
            <executions>             
                <execution>
                    <id>default-minify</id>
                    <phase>prepare-package</phase><!-- When omitted defaults to 'process-resources' -->
                    <configuration>
                        <charset>UTF-8</charset>
                        <jsSourceDir>app</jsSourceDir>
                        <jsSourceIncludes>
                            <jsSourceInclude>app.js</jsSourceInclude>
                            <jsSourceInclude>**/*.js</jsSourceInclude>
                        </jsSourceIncludes>
                        <jsTargetDir>min/js</jsTargetDir>
                        <jsFinalFile>app-min-final.js</jsFinalFile>
                        <jsEngine>CLOSURE</jsEngine>
                    </configuration>
                    <goals>
                        <goal>minify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <configuration>
                        <tasks>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        </plugins>
    </build>
    </profile>
<profiles>

你可以用grunt。Grunt有ngmin模块,负责特定于angularjs的最小化。下面是可以用于最小化和其他优化的代码段任务。更多详情请访问


grunt.registerTask('build'[
“清洁区”,
"生产",,
“指南针:服务器”,
“使用MinPrepare”,
"海螺",,
"ngmin",,
'副本:dist',
“cssmin”,
"丑",,
“rev”,
“usemin”,
“htmlmin”
]);

您可以使用grunt。Grunt有ngmin模块,负责特定于angularjs的最小化。下面是可以用于最小化和其他优化的代码段任务。更多详情请访问


grunt.registerTask('build'[
“清洁区”,
"生产",,
“指南针:服务器”,
“使用MinPrepare”,
"海螺",,
"ngmin",,
'副本:dist',
“cssmin”,
"丑",,
“rev”,
“usemin”,
“htmlmin”
]);

查看有关如何在代码的内部版本中执行此操作的提示

对于您的开发环境,您将不希望缩小并手动添加这些文件。查看(主要是“吞咽”)如何将其设置为自动注入index.html

如果您熟悉语法,下面是gulp的片段

gulp.task('wiredep', function() {
log('Wiring the bower dependencies into the html');

var wiredep = require('wiredep').stream;
var options = config.getWiredepDefaultOptions();

return gulp
    .src(config.index)
    .pipe(wiredep(options))
    .pipe($.inject(gulp.src(config.js)))
    .pipe(gulp.dest(config.client));
 });
查看有关如何在代码的内部版本中执行此操作的提示

对于您的开发环境,您将不希望缩小并手动添加这些文件。查看(主要是“吞咽”)如何将其设置为自动注入index.html

如果您熟悉语法,下面是gulp的片段

gulp.task('wiredep', function() {
log('Wiring the bower dependencies into the html');

var wiredep = require('wiredep').stream;
var options = config.getWiredepDefaultOptions();

return gulp
    .src(config.index)
    .pipe(wiredep(options))
    .pipe($.inject(gulp.src(config.js)))
    .pipe(gulp.dest(config.client));
 });

当然,只要不使用“magic DI”,就可以将AngularJS文件缩小为一个文件。缩小通常在构建过程中执行。您目前是否正在使用任何构建过程?如果不是的话,那么值得研究一些针对您的平台的问题,许多构建过程中都存在一些常见的问题。是的,你可以把它们合并成一个文件。您的应用程序的引导程序已完成。我们目前不使用生成过程。。我只是想澄清一下。。假设我每页使用一个模块。。在需要时加载脚本是否更好(例如:使用ui router resolve和oclazyload加载发票页面的发票_模块)。或者简单地将所有应用程序文件合并成一个文件,并一次性加载它们,只要您不使用“magic DI”,您就可以将AngularJS文件缩小为一个文件。缩小通常在构建过程中执行。您目前是否正在使用任何构建过程?如果不是的话,那么值得研究一些针对您的平台的问题,许多构建过程中都存在一些常见的问题。是的,你可以把它们合并成一个文件。您的应用程序的引导程序已完成。我们目前不使用生成过程。。我只是想澄清一下。。假设我每页使用一个模块。。在需要时加载脚本是否更好(例如:使用ui router resolve和oclazyload加载发票页面的发票_模块)。或者干脆把所有的应用程序文件合并成一个