暫無描述

Gruntfile.js 8.7KB

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