简体   繁体   English

在html文件中的一行中引用多个js文件

[英]reference multiple js files in a single line in a html file

Is there an easy way to reference all js files in an HTML file rather than referencing it one by one? 有没有一种简单的方法来引用HTML文件中的所有js文件,而不是逐个引用它?

Instead of this - 而不是这个 -

<script type="text/javascript" src="js/controllers/mainCtrl.js"></script>
<script type="text/javascript" src="js/controllers/browseCtrl.js"></script>
...

I'm looking for something like this - 我正在寻找这样的东西 -

<script type="text/javascript" src="js/controllers/*.js"></script>

Or is there a tool out there that copies the contents of these files into one file and reference that one file instead? 或者是否有工具将这些文件的内容复制到一个文件中并引用该文件? This will be minimize the HTTP calls. 这将最小化HTTP调用。

Is there an easy way to reference all js files in an HTML file rather than referencing it one by one? 有没有一种简单的方法来引用HTML文件中的所有js文件,而不是逐个引用它?

For some value of "easy". 对于某些“轻松”的价值。 There is nothing built in to browsers that will do it though. 虽然浏览器没有内置任何功能。

Or is there a tool out there that copies the contents of these files into one file and reference that one file instead? 或者是否有工具将这些文件的内容复制到一个文件中并引用该文件?

There are many. 有许多。 cat is the simplest one. cat是最简单的。

Call it from your usual build tool. 从您通常的构建工具中调用它。

You can use something like require.js to combine them at runtime during development, and call r.js via Node from your build tool for packaging for your staging and live environments. 您可以使用require.js之类的东西在开发期间在运行时将它们组合在一起,并通过构建工具中的Node调用r.js ,以便为您的暂存和实时环境打包。

You can give Require.js a go. 你可以给Require.js一个去。 Require.js is the only JavaScript-file that is loaded through the script-tag. Require.js是唯一通过script-tag加载的JavaScript文件。 When you go out of development you can use Require.js's r.js to minify and concat everything into one file. 当您退出开发时,您可以使用Require.js的r.js将所有内容缩小并连接到一个文件中。

I use this tool all the time to minify my JS files: 我一直使用这个工具来缩小我的JS文件:

Online Javascript Compression Tool 在线Javascript压缩工具

You can upload multiple files and it will concatenate them into one for you. 您可以上传多个文件,并将它们连接成一个文件。 It also produces smaller filesizes than YUI compressor, and Google's JS compiler most of the time too. 它还生成比YUI压缩器更小的文件大小,以及Google的JS编译器。

I am not sure why this hasn't been mentioned yet, but I do suppose this thread is a bit dated. 我不确定为什么还没有提到这个,但我认为这个帖子有点过时了。 Since I stumbled on this during my search to solve this very problem, I thought I would put a quick write-up about GruntJS here for other newbie JS guys to find. 由于我在寻找解决这个问题的过程中偶然发现了这个问题,我想我会在这里快速写一篇关于GruntJS的文章 ,供其他新手JS人找到。

Essentially, a properly configured Gruntfile.js will be able to perform a variety of tasks around JS including, but not limited to: concatenating files, minifying files, code linting, and much much more. 本质上,正确配置的Gruntfile.js将能够执行围绕JS的各种任务,包括但不限于:连接文件,缩小文件,代码linting等等。

You can install grunt on Ubuntu with: 您可以在Ubuntu上安装grunt

$ sudo apt-get install nodejs
$ sudo npm -g install grunt-cli
$ cd /path/to/my/project
--- Assumming you have a package.json file already in place ---
$ npm install grunt --save-dev
--- Install grunt plugins you wish to use ---
$ npm install grunt-contrib-concat --save-dev
$ npm install grunt-contrib-uglify --save-dev
$ npm install grunt-contrib-jshint --save-dev
$ npm install grunt-contrib-watch --save-dev

On the GruntJS site , there is a pretty good write-up for how to use GruntJS, but here is an example Gruntfile.js that will: GruntJS网站上 ,有一篇关于如何使用GruntJS的相当不错的文章,但这里有一个例子Gruntfile.js:

  1. Lint all the JS files ( app.js in the current directory and all .js files in the ngmodules directory). Lint所有JS文件(当前目录中的app.jsngmodules目录中的所有.js文件)。
  2. Concatenate and save the files to dist/package-name.js . 连接并将文件保存到dist/package-name.js
  3. Minify the concatenated file and save it to dist/package-name.min.js . 缩小连接文件并将其保存到dist/package-name.min.js

Gruntfile.js : Gruntfile.js

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concat: {
      options: {
        separator: ';'
      },
      dist: {
        src: ['app.js', 'ngmodules/**/*.js'],
        dest: 'dist/<%= pkg.name %>.js'
      }
    },
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
      },
      dist: {
        files: {
          'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
        }
      }
    },
    jshint: {
      files: ['Gruntfile.js', 'app.js', 'ngmodules/**/*.js'],
      options: {
        // options here to override JSHint defaults
        globals: {
          jQuery: true,
          console: true,
          module: true,
          document: true
        }
      }
    },
    watch: {
      files: ['<%= jshint.files %>'],
      tasks: ['jshint']
    }
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-concat');

  grunt.registerTask('default', ['jshint', 'concat', 'uglify']);

};

There is also Gulp ( http://gulpjs.com/ ), which can be used with various plugins. 还有Gulp( http://gulpjs.com/ ),可以与各种插件一起使用。 Here's an example that concatenates *.js files in one single file (main.js), then renames the resulting file and finally minifies it: 这是一个将* .js文件连接在一个文件(main.js)中的示例,然后重命名生成的文件并最终缩小它:

var gulp = require('gulp'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat');

gulp.task('scripts', function(){
return gulp.src('./src/js/*.js')
.pipe(concat('main.js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('./src/js/*.js'));

You can try and combine your javascript files or plugins into one: 您可以尝试将javascript文件或插件合并为一个:

<script type="text/javascript" src="js/controllers/plugins.js"></script>

You'll have to do it manually though. 你必须手动完成它。 Another option would be to write a server-side script to combine and minify all your javascript files. 另一种选择是编写服务器端脚本来组合和缩小所有javascript文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM