Build: Update ESLint & eslint-plugin-import, fixing the build
[jquery.git] / Gruntfile.js
blobb10ebb2dcc1c6ac5b6c4f58242b53d805951aae2
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         // Support: Node.js <12
16         // Skip running tasks that dropped support for Node.js 10
17         // in this Node version.
18         function runIfNewNode( task ) {
19                 return oldNode ? "print_old_node_message:" + task : task;
20         }
22         var fs = require( "fs" ),
23                 gzip = require( "gzip-js" ),
24                 oldNode = /^v10\./.test( process.version ),
25                 isTravis = process.env.TRAVIS,
26                 travisBrowsers = process.env.BROWSERS && process.env.BROWSERS.split( "," );
28         if ( !grunt.option( "filename" ) ) {
29                 grunt.option( "filename", "jquery.js" );
30         }
32         grunt.initConfig( {
33                 pkg: grunt.file.readJSON( "package.json" ),
34                 dst: readOptionalJSON( "dist/.destination.json" ),
35                 compare_size: {
36                         files: [ "dist/jquery.js", "dist/jquery.min.js" ],
37                         options: {
38                                 compress: {
39                                         gz: function( contents ) {
40                                                 return gzip.zip( contents, {} ).length;
41                                         }
42                                 },
43                                 cache: "build/.sizecache.json"
44                         }
45                 },
46                 babel: {
47                         options: {
48                                 sourceMap: "inline",
49                                 retainLines: true,
50                                 plugins: [ "@babel/transform-for-of" ]
51                         },
52                         tests: {
53                                 files: {
54                                         "test/data/core/jquery-iterability-transpiled.js":
55                                                 "test/data/core/jquery-iterability-transpiled-es6.js"
56                                 }
57                         }
58                 },
59                 build: {
60                         all: {
61                                 dest: "dist/jquery.js",
62                                 minimum: [
63                                         "core",
64                                         "selector"
65                                 ],
67                                 // Exclude specified modules if the module matching the key is removed
68                                 removeWith: {
69                                         ajax: [ "manipulation/_evalUrl", "deprecated/ajax-event-alias" ],
70                                         callbacks: [ "deferred" ],
71                                         css: [ "effects", "dimensions", "offset" ],
72                                         "css/showHide": [ "effects" ],
73                                         deferred: {
74                                                 remove: [ "ajax", "effects", "queue", "core/ready" ],
75                                                 include: [ "core/ready-no-deferred" ]
76                                         },
77                                         event: [ "deprecated/ajax-event-alias", "deprecated/event" ]
78                                 }
79                         }
80                 },
81                 npmcopy: {
82                         all: {
83                                 options: {
84                                         destPrefix: "external"
85                                 },
86                                 files: {
87                                         "core-js-bundle/core-js-bundle.js": "core-js-bundle/minified.js",
88                                         "core-js-bundle/LICENSE": "core-js-bundle/LICENSE",
90                                         "npo/npo.js": "native-promise-only/lib/npo.src.js",
92                                         "qunit/qunit.js": "qunit/qunit/qunit.js",
93                                         "qunit/qunit.css": "qunit/qunit/qunit.css",
94                                         "qunit/LICENSE.txt": "qunit/LICENSE.txt",
96                                         "requirejs/require.js": "requirejs/require.js",
98                                         "sinon/sinon.js": "sinon/pkg/sinon.js",
99                                         "sinon/LICENSE.txt": "sinon/LICENSE"
100                                 }
101                         }
102                 },
103                 jsonlint: {
104                         pkg: {
105                                 src: [ "package.json" ]
106                         }
107                 },
108                 eslint: {
109                         options: {
110                                 maxWarnings: 0
111                         },
113                         // We have to explicitly declare "src" property otherwise "newer"
114                         // task wouldn't work properly :/
115                         dist: {
116                                 src: [ "dist/jquery.js", "dist/jquery.min.js" ]
117                         },
118                         dev: {
119                                 src: [
120                                         "src/**/*.js",
121                                         "Gruntfile.js",
122                                         "test/**/*.js",
123                                         "build/**/*.js",
125                                         // Ignore files from .eslintignore
126                                         // See https://github.com/sindresorhus/grunt-eslint/issues/119
127                                         ...fs
128                                                 .readFileSync( `${ __dirname }/.eslintignore`, "utf-8" )
129                                                 .split( "\n" )
130                                                 .filter( filePath => filePath )
131                                                 .map( filePath => filePath[ 0 ] === "!" ?
132                                                         filePath.slice( 1 ) :
133                                                         `!${ filePath }`
134                                                 )
135                                 ]
136                         }
137                 },
138                 testswarm: {
139                         tests: [
141                                 // A special module with basic tests, meant for not fully
142                                 // supported environments like jsdom. We run it everywhere,
143                                 // though, to make sure tests are not broken.
144                                 "basic",
146                                 "ajax",
147                                 "animation",
148                                 "attributes",
149                                 "callbacks",
150                                 "core",
151                                 "css",
152                                 "data",
153                                 "deferred",
154                                 "deprecated",
155                                 "dimensions",
156                                 "effects",
157                                 "event",
158                                 "manipulation",
159                                 "offset",
160                                 "queue",
161                                 "selector",
162                                 "serialize",
163                                 "support",
164                                 "traversing",
165                                 "tween"
166                         ]
167                 },
168                 karma: {
169                         options: {
170                                 customContextFile: "test/karma.context.html",
171                                 customDebugFile: "test/karma.debug.html",
172                                 customLaunchers: {
173                                         ChromeHeadlessNoSandbox: {
174                                                 base: "ChromeHeadless",
175                                                 flags: [ "--no-sandbox" ]
176                                         }
177                                 },
178                                 frameworks: [ "qunit" ],
179                                 middleware: [ "mockserver" ],
180                                 plugins: [
181                                         "karma-*",
182                                         {
183                                                 "middleware:mockserver": [
184                                                         "factory",
185                                                         require( "./test/middleware-mockserver.js" )
186                                                 ]
187                                         }
188                                 ],
189                                 client: {
190                                         qunit: {
192                                                 // We're running `QUnit.start()` ourselves via `loadTests()`
193                                                 // in test/jquery.js
194                                                 autostart: false
195                                         }
196                                 },
197                                 files: [
198                                         "test/data/jquery-1.9.1.js",
199                                         "external/sinon/sinon.js",
200                                         "external/npo/npo.js",
201                                         "external/requirejs/require.js",
202                                         "test/data/testinit.js",
204                                         "test/jquery.js",
206                                         {
207                                                 pattern: "dist/jquery.*",
208                                                 included: false,
209                                                 served: true,
210                                                 nocache: true
211                                         },
212                                         {
213                                                 pattern: "src/**",
214                                                 type: "module",
215                                                 included: false,
216                                                 served: true,
217                                                 nocache: true
218                                         },
219                                         {
220                                                 pattern: "amd/**",
221                                                 included: false,
222                                                 served: true,
223                                                 nocache: true
224                                         },
225                                         { pattern: "external/**", included: false, served: true },
226                                         {
227                                                 pattern: "test/**/*.@(js|css|jpg|html|xml|svg)",
228                                                 included: false,
229                                                 served: true,
230                                                 nocache: true
231                                         }
232                                 ],
233                                 reporters: [ "dots" ],
234                                 autoWatch: false,
235                                 concurrency: 3,
236                                 captureTimeout: 20 * 1000,
237                                 singleRun: true
238                         },
239                         main: {
240                                 browsers: isTravis && travisBrowsers || [ "ChromeHeadless", "FirefoxHeadless" ]
241                         },
242                         esmodules: {
243                                 browsers: isTravis && travisBrowsers || [ "ChromeHeadless" ],
244                                 options: {
245                                         client: {
246                                                 qunit: {
248                                                         // We're running `QUnit.start()` ourselves via `loadTests()`
249                                                         // in test/jquery.js
250                                                         autostart: false,
252                                                         esmodules: true
253                                                 }
254                                         }
255                                 }
256                         },
257                         amd: {
258                                 browsers: isTravis && travisBrowsers || [ "ChromeHeadless" ],
259                                 options: {
260                                         client: {
261                                                 qunit: {
263                                                         // We're running `QUnit.start()` ourselves via `loadTests()`
264                                                         // in test/jquery.js
265                                                         autostart: false,
267                                                         amd: true
268                                                 }
269                                         }
270                                 }
271                         },
273                         jsdom: {
274                                 options: {
275                                         files: [
276                                                 "test/data/jquery-1.9.1.js",
277                                                 "test/data/testinit-jsdom.js",
279                                                 // We don't support various loading methods like esmodules,
280                                                 // choosing a version etc. for jsdom.
281                                                 "dist/jquery.js",
283                                                 // A partial replacement for testinit.js#loadTests()
284                                                 "test/data/testrunner.js",
286                                                 // jsdom only runs basic tests
287                                                 "test/unit/basic.js",
289                                                 {
290                                                         pattern: "test/**/*.@(js|css|jpg|html|xml|svg)",
291                                                         included: false,
292                                                         served: true
293                                                 }
294                                         ]
295                                 },
296                                 browsers: [ "jsdom" ]
297                         },
299                         // To debug tests with Karma:
300                         // 1. Run 'grunt karma:chrome-debug' or 'grunt karma:firefox-debug'
301                         //    (any karma subtask that has singleRun=false)
302                         // 2. Press "Debug" in the opened browser window to start
303                         //    the tests. Unlike the other karma tasks, the debug task will
304                         //    keep the browser window open.
305                         "chrome-debug": {
306                                 browsers: [ "Chrome" ],
307                                 singleRun: false
308                         },
309                         "firefox-debug": {
310                                 browsers: [ "Firefox" ],
311                                 singleRun: false
312                         },
313                         "ie-debug": {
314                                 browsers: [ "IE" ],
315                                 singleRun: false
316                         }
317                 },
318                 watch: {
319                         files: [ "<%= eslint.dev.src %>" ],
320                         tasks: [ "dev" ]
321                 },
322                 uglify: {
323                         all: {
324                                 files: {
325                                         "dist/<%= grunt.option('filename').replace('.js', '.min.js') %>":
326                                                 "dist/<%= grunt.option('filename') %>"
327                                 },
328                                 options: {
329                                         preserveComments: false,
330                                         sourceMap: true,
331                                         sourceMapName:
332                                                 "dist/<%= grunt.option('filename').replace('.js', '.min.map') %>",
333                                         report: "min",
334                                         output: {
335                                                 "ascii_only": true
336                                         },
337                                         banner: "/*! jQuery v<%= pkg.version %> | " +
338                                                 "(c) OpenJS Foundation and other contributors | jquery.org/license */",
339                                         compress: {
340                                                 "hoist_funs": false,
341                                                 loops: false
342                                         }
343                                 }
344                         }
345                 }
346         } );
348         // Load grunt tasks from NPM packages
349         require( "load-grunt-tasks" )( grunt, {
350                 pattern: oldNode ? [ "grunt-*", "!grunt-eslint" ] : [ "grunt-*" ]
351         } );
353         // Integrate jQuery specific tasks
354         grunt.loadTasks( "build/tasks" );
356         grunt.registerTask( "print_old_node_message", ( ...args ) => {
357                 var task = args.join( ":" );
358                 grunt.log.writeln( "Old Node.js detected, running the task \"" + task + "\" skipped..." );
359         } );
361         grunt.registerTask( "lint", [
362                 "jsonlint",
364                 // Running the full eslint task without breaking it down to targets
365                 // would run the dist target first which would point to errors in the built
366                 // file, making it harder to fix them. We want to check the built file only
367                 // if we already know the source files pass the linter.
368                 runIfNewNode( "eslint:dev" ),
369                 runIfNewNode( "eslint:dist" )
370         ] );
372         grunt.registerTask( "lint:newer", [
373                 "newer:jsonlint",
375                 // Don't replace it with just the task; see the above comment.
376                 runIfNewNode( "newer:eslint:dev" ),
377                 runIfNewNode( "newer:eslint:dist" )
378         ] );
380         grunt.registerTask( "test:fast", "node_smoke_tests" );
381         grunt.registerTask( "test:slow", [
382                 "promises_aplus_tests",
383                 "karma:jsdom"
384         ] );
386         grunt.registerTask( "test:prepare", [
387                 "npmcopy",
388                 "qunit_fixture",
389                 "babel:tests"
390         ] );
392         grunt.registerTask( "test", [
393                 "test:prepare",
394                 "test:fast",
395                 "test:slow"
396         ] );
398         grunt.registerTask( "dev", [
399                 "build:*:*",
400                 runIfNewNode( "newer:eslint:dev" ),
401                 "newer:uglify",
402                 "remove_map_comment",
403                 "dist:*",
404                 "qunit_fixture",
405                 "compare_size"
406         ] );
408         grunt.registerTask( "default", [
409                 runIfNewNode( "eslint:dev" ),
410                 "build:*:*",
411                 "amd",
412                 "uglify",
413                 "remove_map_comment",
414                 "dist:*",
415                 "test:prepare",
416                 runIfNewNode( "eslint:dist" ),
417                 "test:fast",
418                 "compare_size"
419         ] );