|
| 1 | +/** |
| 2 | + * A build task that compiles jQuery Migrate JS modules into one bundle. |
| 3 | + */ |
| 4 | + |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +module.exports = function( grunt ) { |
| 8 | + const path = require( "path" ); |
| 9 | + const rollup = require( "rollup" ); |
| 10 | + const rootFolder = path.resolve( `${ __dirname }/../..` ); |
| 11 | + const srcFolder = path.resolve( `${ rootFolder }/src` ); |
| 12 | + const read = function( fileName ) { |
| 13 | + return grunt.file.read( `${ srcFolder }/${ fileName }` ); |
| 14 | + }; |
| 15 | + |
| 16 | + // Catch `// @CODE` and subsequent comment lines event if they don't start |
| 17 | + // in the first column. |
| 18 | + const wrapper = read( "wrapper.js" ) |
| 19 | + .split( /[\x20\t]*\/\/ @CODE\n(?:[\x20\t]*\/\/[^\n]+\n)*/ ); |
| 20 | + |
| 21 | + const inputRollupOptions = {}; |
| 22 | + const outputRollupOptions = { |
| 23 | + |
| 24 | + // The ESM format is not actually used as we strip it during |
| 25 | + // the build; it's just that it doesn't generate any extra |
| 26 | + // wrappers so there's nothing for us to remove. |
| 27 | + format: "esm", |
| 28 | + |
| 29 | + intro: wrapper[ 0 ] |
| 30 | + .replace( /\n*$/, "" ), |
| 31 | + outro: wrapper[ 1 ] |
| 32 | + .replace( /^\n*/, "" ) |
| 33 | + }; |
| 34 | + |
| 35 | + grunt.registerMultiTask( |
| 36 | + "build", |
| 37 | + "Build jQuery Migrate ECMAScript modules, embed date/version", |
| 38 | + async function() { |
| 39 | + const done = this.async(); |
| 40 | + |
| 41 | + try { |
| 42 | + const version = grunt.config( "pkg.version" ); |
| 43 | + const dest = this.files[ 0 ].dest; |
| 44 | + const src = this.files[ 0 ].src[ 0 ]; |
| 45 | + |
| 46 | + inputRollupOptions.input = path.resolve( `${ rootFolder }/${ src }` ); |
| 47 | + |
| 48 | + const bundle = await rollup.rollup( inputRollupOptions ); |
| 49 | + |
| 50 | + const { output: [ { code } ] } = await bundle.generate( outputRollupOptions ); |
| 51 | + |
| 52 | + const compiledContents = code |
| 53 | + |
| 54 | + // Embed Version |
| 55 | + .replace( /@VERSION/g, version ) |
| 56 | + |
| 57 | + // Embed Date |
| 58 | + // yyyy-mm-ddThh:mmZ |
| 59 | + .replace( |
| 60 | + /@DATE/g, |
| 61 | + ( new Date() ).toISOString() |
| 62 | + .replace( /:\d+\.\d+Z$/, "Z" ) |
| 63 | + ); |
| 64 | + |
| 65 | + grunt.file.write( `${ rootFolder }/${ dest }`, compiledContents ); |
| 66 | + grunt.log.ok( `File '${ dest }' created.` ); |
| 67 | + done(); |
| 68 | + } catch ( err ) { |
| 69 | + done( err ); |
| 70 | + } |
| 71 | + } ); |
| 72 | +}; |
0 commit comments