Build: Require strict mode in Node.js scripts via ESLint
[jquery.git] / Gruntfile.js
blob8458a6772b03ec1fc514719a5dfa39584687d901
1 "use strict";
3 module.exports = function( grunt ) {
4         function readOptionalJSON( filepath ) {
5                 var stripJSONComments = require( "strip-json-comments" ),
6                         data = {};
7                 try {
8                         data = JSON.parse( stripJSONComments(
9                                 fs.readFileSync( filepath, { encoding: "utf8" } )
10                         ) );
11                 } catch ( e ) {}
12                 return data;
13         }
15         var fs = require( "fs" ),
16                 gzip = require( "gzip-js" ),
17                 isTravis = process.env.TRAVIS,
18                 oldNode = /^v6\./.test( process.version );
20         // Support: Node.js <8
21         // Skip running tasks that dropped support for Node.js 6
22         // in those Node versions.
23         function runIfNewNode( task ) {
24                 return oldNode ? "print_old_node_message:" + task : task;
25         }
27         if ( !grunt.option( "filename" ) ) {
28                 grunt.option( "filename", "jquery.js" );
29         }
31         grunt.initConfig( {
32                 pkg: grunt.file.readJSON( "package.json" ),
33                 dst: readOptionalJSON( "dist/.destination.json" ),
34                 "compare_size": {
35                         files: [ "dist/jquery.js", "dist/jquery.min.js" ],
36                         options: {
37                                 compress: {
38                                         gz: function( contents ) {
39                                                 return gzip.zip( contents, {} ).length;
40                                         }
41                                 },
42                                 cache: "build/.sizecache.json"
43                         }
44                 },
45                 babel: {
46                         options: {
47                                 sourceMap: "inline",
48                                 retainLines: true,
49                                 plugins: [ "@babel/transform-for-of" ]
50                         },
51                         nodeSmokeTests: {
52                                 files: {
53                                         "test/data/core/jquery-iterability-transpiled.js":
54                                                 "test/data/core/jquery-iterability-transpiled-es6.js"
55                                 }
56                         }
57                 },
58                 build: {
59                         all: {
60                                 dest: "dist/jquery.js",
61                                 minimum: [
62                                         "core",
63                                         "selector"
64                                 ],
66                                 // Exclude specified modules if the module matching the key is removed
67                                 removeWith: {
68                                         ajax: [ "manipulation/_evalUrl", "event/ajax" ],
69                                         callbacks: [ "deferred" ],
70                                         css: [ "effects", "dimensions", "offset" ],
71                                         "css/showHide": [ "effects" ],
72                                         deferred: {
73                                                 remove: [ "ajax", "effects", "queue", "core/ready" ],
74                                                 include: [ "core/ready-no-deferred" ]
75                                         },
76                                         sizzle: [ "css/hiddenVisibleSelectors", "effects/animatedSelector" ]
77                                 }
78                         }
79                 },
80                 npmcopy: {
81                         all: {
82                                 options: {
83                                         destPrefix: "external"
84                                 },
85                                 files: {
86                                         "sizzle/dist": "sizzle/dist",
87                                         "sizzle/LICENSE.txt": "sizzle/LICENSE.txt",
89                                         "npo/npo.js": "native-promise-only/lib/npo.src.js",
91                                         "qunit/qunit.js": "qunit/qunit/qunit.js",
92                                         "qunit/qunit.css": "qunit/qunit/qunit.css",
93                                         "qunit/LICENSE.txt": "qunit/LICENSE.txt",
95                                         "requirejs/require.js": "requirejs/require.js",
97                                         "sinon/sinon.js": "sinon/pkg/sinon.js",
98                                         "sinon/LICENSE.txt": "sinon/LICENSE"
99                                 }
100                         }
101                 },
102                 jsonlint: {
103                         pkg: {
104                                 src: [ "package.json" ]
105                         }
106                 },
107                 eslint: {
108                         options: {
110                                 // See https://github.com/sindresorhus/grunt-eslint/issues/119
111                                 quiet: true
112                         },
114                         // We have to explicitly declare "src" property otherwise "newer"
115                         // task wouldn't work properly :/
116                         dist: {
117                                 src: "dist/jquery.js"
118                         },
119                         dev: {
120                                 src: [ "src/**/*.js", "Gruntfile.js", "test/**/*.js", "build/**/*.js" ]
121                         }
122                 },
123                 testswarm: {
124                         tests: [
126                                 // A special module with basic tests, meant for
127                                 // not fully supported environments like Android 2.3,
128                                 // jsdom or PhantomJS. We run it everywhere, though,
129                                 // to make sure tests are not broken.
130                                 "basic",
132                                 "ajax",
133                                 "animation",
134                                 "attributes",
135                                 "callbacks",
136                                 "core",
137                                 "css",
138                                 "data",
139                                 "deferred",
140                                 "deprecated",
141                                 "dimensions",
142                                 "effects",
143                                 "event",
144                                 "manipulation",
145                                 "offset",
146                                 "queue",
147                                 "selector",
148                                 "serialize",
149                                 "support",
150                                 "traversing",
151                                 "tween"
152                         ]
153                 },
154                 karma: {
155                         options: {
156                                 customContextFile: "test/karma.context.html",
157                                 customDebugFile: "test/karma.debug.html",
158                                 customLaunchers: {
159                                         ChromeHeadlessNoSandbox: {
160                                                 base: "ChromeHeadless",
161                                                 flags: [ "--no-sandbox" ]
162                                         }
163                                 },
164                                 frameworks: [ "qunit" ],
165                                 middleware: [ "mockserver" ],
166                                 plugins: [
167                                         "karma-*",
168                                         {
169                                                 "middleware:mockserver": [
170                                                         "factory",
171                                                         require( "./test/middleware-mockserver.js" )
172                                                 ]
173                                         }
174                                 ],
175                                 files: [
176                                         "test/data/jquery-1.9.1.js",
177                                         "external/sinon/sinon.js",
178                                         "external/npo/npo.js",
179                                         "external/requirejs/require.js",
180                                         "test/data/testinit.js",
182                                         "test/jquery.js",
184                                         // Replacement for testinit.js#loadTests()
185                                         "test/data/testrunner.js",
186                                         "test/unit/basic.js",
187                                         "test/unit/core.js",
188                                         "test/unit/callbacks.js",
189                                         "test/unit/deferred.js",
190                                         "test/unit/deprecated.js",
191                                         "test/unit/support.js",
192                                         "test/unit/data.js",
193                                         "test/unit/queue.js",
194                                         "test/unit/attributes.js",
195                                         "test/unit/event.js",
196                                         "test/unit/selector.js",
197                                         "test/unit/traversing.js",
198                                         "test/unit/manipulation.js",
199                                         "test/unit/wrap.js",
200                                         "test/unit/css.js",
201                                         "test/unit/serialize.js",
202                                         "test/unit/ajax.js",
203                                         "test/unit/effects.js",
204                                         "test/unit/offset.js",
205                                         "test/unit/dimensions.js",
206                                         "test/unit/animation.js",
207                                         "test/unit/tween.js",
208                                         "test/unit/ready.js",
210                                         { pattern: "dist/jquery.*", included: false, served: true },
211                                         { pattern: "src/**", included: false, served: true },
212                                         { pattern: "external/**", included: false, served: true },
213                                         {
214                                                 pattern: "test/**/*.@(js|css|jpg|html|xml|svg)",
215                                                 included: false,
216                                                 served: true
217                                         }
218                                 ],
219                                 reporters: [ "dots" ],
220                                 autoWatch: false,
221                                 concurrency: 3,
222                                 captureTimeout: 20 * 1000,
223                                 singleRun: true
224                         },
225                         main: {
227                                 // The Chrome sandbox doesn't work on Travis.
228                                 browsers: [ isTravis ? "ChromeHeadlessNoSandbox" : "ChromeHeadless" ]
229                         },
231                         jsdom: {
232                                 options: {
233                                         files: [
234                                                 "test/data/jquery-1.9.1.js",
235                                                 "test/data/testinit-jsdom.js",
237                                                 // We don't support various loading methods like AMD,
238                                                 // choosing a version etc. for jsdom.
239                                                 "dist/jquery.js",
241                                                 // Replacement for testinit.js#loadTests()
242                                                 "test/data/testrunner.js",
244                                                 // jsdom only runs basic tests
245                                                 "test/unit/basic.js",
247                                                 { pattern: "external/**", included: false, served: true },
248                                                 {
249                                                         pattern: "test/**/*.@(js|css|jpg|html|xml|svg)",
250                                                         included: false,
251                                                         served: true
252                                                 }
253                                         ]
254                                 },
255                                 browsers: [ "jsdom" ]
256                         },
258                         // To debug tests with Karma:
259                         // 1. Run 'grunt karma:chrome-debug' or 'grunt karma:firefox-debug'
260                         //    (any karma subtask that has singleRun=false)
261                         // 2. Press "Debug" in the opened browser window to start
262                         //    the tests. Unlike the other karma tasks, the debug task will
263                         //    keep the browser window open.
264                         "chrome-debug": {
265                                 browsers: [ "Chrome" ],
266                                 singleRun: false
267                         },
268                         "firefox-debug": {
269                                 browsers: [ "Firefox" ],
270                                 singleRun: false
271                         },
272                         "ie-debug": {
273                                 browsers: [ "IE" ],
274                                 singleRun: false
275                         }
276                 },
277                 watch: {
278                         files: [ "<%= eslint.dev.src %>" ],
279                         tasks: [ "dev" ]
280                 },
281                 uglify: {
282                         all: {
283                                 files: {
284                                         "dist/<%= grunt.option('filename').replace('.js', '.min.js') %>":
285                                                 "dist/<%= grunt.option('filename') %>"
286                                 },
287                                 options: {
288                                         preserveComments: false,
289                                         sourceMap: true,
290                                         sourceMapName:
291                                                 "dist/<%= grunt.option('filename').replace('.js', '.min.map') %>",
292                                         report: "min",
293                                         output: {
294                                                 "ascii_only": true,
296                                                 // Support: Android 4.0 only
297                                                 // UglifyJS 3 breaks Android 4.0 if this option is not enabled.
298                                                 // This is in lieu of setting ie8 for all of mangle, compress, and output
299                                                 "ie8": true
300                                         },
301                                         banner: "/*! jQuery v<%= pkg.version %> | " +
302                                                 "(c) JS Foundation and other contributors | jquery.org/license */",
303                                         compress: {
304                                                 "hoist_funs": false,
305                                                 loops: false,
307                                                 // Support: IE <11
308                                                 // typeofs transformation is unsafe for IE9-10
309                                                 // See https://github.com/mishoo/UglifyJS2/issues/2198
310                                                 typeofs: false
311                                         }
312                                 }
313                         }
314                 }
315         } );
317         // Load grunt tasks from NPM packages
318         require( "load-grunt-tasks" )( grunt );
320         // Integrate jQuery specific tasks
321         grunt.loadTasks( "build/tasks" );
323         // Support: Node.js <8
324         // Print a message on Node.js <8 notifying the task is skipped there.
325         grunt.registerTask( "print_old_node_message", function() {
326                 var task = [].slice.call( arguments ).join( ":" );
327                 grunt.log.writeln( "Old Node.js detected, running the task \"" + task + "\" skipped..." );
328         } );
330         grunt.registerTask( "lint", [
331                 "jsonlint",
333                 // Running the full eslint task without breaking it down to targets
334                 // would run the dist target first which would point to errors in the built
335                 // file, making it harder to fix them. We want to check the built file only
336                 // if we already know the source files pass the linter.
337                 "eslint:dev",
338                 "eslint:dist"
339         ] );
341         grunt.registerTask( "lint:newer", [
342                 "newer:jsonlint",
344                 // Don't replace it with just the task; see the above comment.
345                 "newer:eslint:dev",
346                 "newer:eslint:dist"
347         ] );
349         grunt.registerTask( "test:fast", "node_smoke_tests" );
350         grunt.registerTask( "test:slow", [
351                 "promises_aplus_tests",
353                 // Support: Node.js <8
354                 // Karma no longer supports Node.js <8 as it relies on async-await internally.
355                 runIfNewNode( "karma:jsdom" )
356         ] );
358         grunt.registerTask( "test", [
359                 "test:fast",
360                 "test:slow"
361         ] );
363         grunt.registerTask( "dev", [
364                 "build:*:*",
365                 "newer:eslint:dev",
366                 "newer:uglify",
367                 "remove_map_comment",
368                 "dist:*",
369                 "qunit_fixture",
370                 "compare_size"
371         ] );
373         grunt.registerTask( "default", [
374                 "eslint:dev",
375                 "build:*:*",
376                 "uglify",
377                 "remove_map_comment",
378                 "dist:*",
379                 "qunit_fixture",
380                 "eslint:dist",
381                 "test:fast",
382                 "compare_size"
383         ] );