The following points need to be made clear:
1. The local front-end debugging code must call the original path and code, but the online operation must be through another packaged path. Here is the generated dist folder.
2. With the introduction of requirejs, how to control the online and offline paths? This is how we control it. The code is as follows:
<script></script>
This${resource} is controlled by the server and passed to the page. Debugging this locally The value of ${resource} is /resource/v1/; Then the value online is /dist/v1/. So the cooperation between online and offline js is completed. Local debugging calls resources under /resource/v1/, and online resources under /dist/v1/. Of course, v1 is actually redundant. At that time, it was mainly a version number added for version release.
#Now we will explain step by step how to package all the entry files under resource/v1/js/.
First take a look at where all my entry files are, as shown in the picture:
#These js are under resource/v1/js/.
The entrance now has 11 js files, and all imported modules need to be packaged into their respective entrance js.
The first step is to copy the fonts, images, css in the original resources and the js under the base in js. The js files under the base are mainly basic libraries, including requirejs library, etc. Copy to the dist folder.
The purpose of copying is that I also need the fonts, images, and css under dist online.
copy: {/* main: { expand: true, cwd: 'src', src: '**', dest: 'dist/', }, */ main:{ files:[ {expand: true,cwd: 'resources/v1/css/',src: '**',dest:'dist/v1/css/'}, {expand: true,cwd: 'resources/v1/fonts/',src: '**',dest:'dist/v1/fonts/'}, {expand: true,cwd: 'resources/v1/images/',src: '**',dest:'dist/v1/images/'}, {expand: true,cwd: 'resources/v1/js/base/',src: '**',dest:'dist/v1/js/base/'} ] } }
The second step is to package the entry file through grunt-contrib-requirejs
. The configuration file is as follows:
// r.js 打包准备var files = grunt.file.expand('resources/v1/js/*.js');var requirejsOptions = {}; //用来存储 打包配置的对象//遍历文件files.forEach(function(file,index,array) {var name = file.substring(file.lastIndexOf('/') + 1);var reqname = name.replace(/\.js$/,''); console.log(name);var filename = 'requirejs' + index; requirejsOptions[filename] = { options: { baseUrl: "resources/v1/js", paths:{"jquery":'base/jquery-1.11.3.min','vue':'base/vue.min',"vuedraggable":'base/vuedraggable','bootstrap':'base/bootstrap.min',"sortablejs":'base/Sortable',"basicLib":'widgets/basicLib','msg':'widgets/msg','baseUrl':'widgets/baseUrl','common':'widgets/common',"ajaxfileupload":'widgets/ajaxfileupload','document':'widgets/document',"comp":'widgets/comp','header':'module/header','accountCenter':'view/accountCenter','docking':'view/docking','templateUploadCtr':'view/templateUploadCtr' }, shim:{'vue':{ exports:'vue' },'basicLib':['jquery'],'bootstrap':['jquery'],'ajaxfileupload':['jquery'],'sortablejs':['vue'] }, optimizeAllPluginResources: true, name: reqname, out: 'dist/v1/js/' + name } }; });
Then initialize the configuration and load the registration task
grunt.initConfig({ requirejs: requirejsOptions }) grunt.loadNpmTasks('grunt-contrib-requirejs'); grunt.registerTask('default', ['requirejs']);
Since my code has es6 syntax, so after merging The es6 syntax is converted to es5; then comments and other comments are removed during compression.
The total configuration code is as follows:
module.exports = function(grunt) { // r.js 打包准备var files = grunt.file.expand('resources/v1/js/*.js');var requirejsOptions = {}; //用来存储 打包配置的对象//遍历文件files.forEach(function(file,index,array) {var name = file.substring(file.lastIndexOf('/') + 1);var reqname = name.replace(/\.js$/,''); console.log(name);var filename = 'requirejs' + index; requirejsOptions[filename] = { options: { baseUrl: "resources/v1/js", paths:{"jquery":'base/jquery-1.11.3.min','vue':'base/vue.min',"vuedraggable":'base/vuedraggable','bootstrap':'base/bootstrap.min',"sortablejs":'base/Sortable',"basicLib":'widgets/basicLib','msg':'widgets/msg','baseUrl':'widgets/baseUrl','common':'widgets/common',"ajaxfileupload":'widgets/ajaxfileupload','document':'widgets/document',"comp":'widgets/comp','header':'module/header','accountCenter':'view/accountCenter','docking':'view/docking','templateUploadCtr':'view/templateUploadCtr' }, shim:{'vue':{ exports:'vue' },'basicLib':['jquery'],'bootstrap':['jquery'],'ajaxfileupload':['jquery'],'sortablejs':['vue'] }, optimizeAllPluginResources: true, name: reqname, out: 'dist/v1/js/' + name } }; }); //配置参数 grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), requirejs:requirejsOptions, watch: { js: { files:['resources/**/*.js'], tasks:['default'], options: {livereload:false} }, babel:{ files:'resources/**/*.js', tasks:['babel'] } }, jshint:{ build:['resources/**/*.js'], options:{ jshintrc:'.jshintrc' //检测JS代码错误 } }, copy: {/* main: { expand: true, cwd: 'src', src: '**', dest: 'dist/', }, */ main:{ files:[ {expand: true,cwd: 'resources/v1/css/',src: '**',dest:'dist/v1/css/'}, {expand: true,cwd: 'resources/v1/fonts/',src: '**',dest:'dist/v1/fonts/'}, {expand: true,cwd: 'resources/v1/images/',src: '**',dest:'dist/v1/images/'}, {expand: true,cwd: 'resources/v1/js/base/',src: '**',dest:'dist/v1/js/base/'} ] } }, babel: { options: { sourceMap: false, presets: ['babel-preset-es2015'] }, dist: { files: [{ expand:true, cwd:'dist/v1/js/', //js目录下 src:['*.js'], //所有js文件 dest:'dist/v1/js/' //输出到此目录下 }] } }, uglify: { options: { mangle: true, //混淆变量名comments: 'false' //false(删除全部注释),some(保留@preserve @license @cc_on等注释) }, my_target: { files: [{ expand:true, cwd:'dist/v1/js/', //js目录下 src:['*.js'], //所有js文件 dest:'dist/v1/js/' //输出到此目录下 }] } } }); //载入uglify插件,压缩js grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-babel'); //grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-requirejs'); grunt.loadNpmTasks('grunt-contrib-watch'); //注册任务 grunt.registerTask('default', ['copy','requirejs','babel','uglify']); grunt.registerTask('watcher',['watch']); }
Reference address:
Use grunt to complete the merge and compression of requirejs and the version control of js files
requirejs Multiple pages, multiple js packaging codes, requirejs many-to-many packaging [Collection]
The above is the detailed content of Detailed explanation of examples of multi-entry js file packaging. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
