[英]After modularizing an AngularJS app, how do you include the entire project in the index.html without having to type a ton of <script> tags
I've been learning Angular for awhile now and am looking into the best ways to modularize the application. 我已经学习Angular已有一段时间了,并且正在研究模块化应用程序的最佳方法。 I think my understanding of that is going pretty well, but I've looked around for awhile and can't seem to get a good answer as to how all of these .js files get included inside of the index.html without just manually typing out a bunch of tags.
我认为我对此的理解很好,但是我已经浏览了一段时间,而且似乎无法获得关于所有这些.js文件如何包含在index.html中的好的答案,而不仅仅是手动输入一堆标签。 I've been looking into Grunt/Gulp and get how those are combining the entire app into one .js file, but for development I'm guessing you don't want to have to re-run grunt or gulp every time you want to update the app.
我一直在研究Grunt / Gulp,并了解它们是如何将整个应用程序组合到一个.js文件中的,但是为了进行开发,我想您不想每次都重新运行grunt或gulp。更新应用程序。
There are many different options: gulp, grunt, or webpack seem to be the most popular. 有许多不同的选项: gulp ,grunt或webpack似乎是最受欢迎的。 I tend to use webpack the most these days.
这些天我倾向于使用webpack。
A good setup will typically run a local node server, which will refresh the browser automatically every time you make a change to a file. 一个好的设置通常会运行一个本地节点服务器,该服务器将在您每次更改文件时自动刷新浏览器。
There are many yeoman generators that will set this all up for you, or you can find simple examples on github. 有许多yeoman生成器将为您完成所有这些工作,或者您可以在github上找到简单的示例。
The basic idea is to 基本思想是
Now if you are using gulp here is how you would typically handle above steps 现在,如果您正在使用gulp,则通常按照以下步骤操作
gulp.task('process-js', function () {
return gulp.src(jsFiles, {base: './'})
.pipe(gulpif(isProd, concat('all.min.js'), concat('all.js')))
.pipe(gulpif(isProd, uglify({mangle: true, preserveComments: 'some'})))
.pipe(gulp.dest(deployFolder + '/static/js'))
});
where jsFiles is an array of files/folders that contain your angular app js files 其中jsFiles是包含您的角度应用js文件的文件/文件夹的数组
var jsFiles = [
'!static/js/**/*.js',
'!static/js/plugin/**/*.*',
'app/index/**/*module.js',
'app/index/**/*config.js',
'app/index/**/*ctrl.js'
];
Note: use !
注意:使用
!
prefix to exclude certain files/folders from processing. 前缀以排除某些文件/文件夹。
isProd
is just a flag that indicates whether you are preparing for production or development. isProd
只是一个标志,指示您是准备进行生产还是开发。
During development I also use BrowserSync to watch any changes to my js files and re-run the gulp task to prepare all.js
. 在开发期间,我还使用BrowserSync监视对js文件的任何更改,然后重新运行gulp任务以准备
all.js
It also refreshes the page in browser automatically. 它还会自动刷新浏览器中的页面。
gulp.task('run', function () {
browserSync({
server: {
baseDir: deployFolder
},
open: true,
browser: ['google chrome'],
files: deployFolder + '/**/*',
watchOptions: {
debounceDelay: 2000
}
});
gulp.watch(jsFiles, ['process-js']);
});
gulp.task('default', function () {
runSequence(
'clean',
'run'
);
});
Gulp/Grunt
to concat
all your angular files
. Gulp/Grunt
可以concat
所有的angular files
。
Create 2 tasks : 创建2个任务:
dev build task
dev build task
don't uglify
. don't uglify
。 distribution/production task
which is the same as dev one but this one uglify
the concatenated file
. distribution/production task
,但这使concatenated file
uglify
了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.