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