]> source.dussan.org Git - jquery.git/commitdiff
Build: Fix the Windows build
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>
Mon, 2 Dec 2019 18:55:19 +0000 (19:55 +0100)
committerGitHub <noreply@github.com>
Mon, 2 Dec 2019 18:55:19 +0000 (19:55 +0100)
This commit gets rid of rollup-plugin-hypothetical in favor of a simpler
inline Rollup plugin that fits our need and is compatible with Windows.

Fixes gh-4548
Closes gh-4549

build/tasks/build.js
build/tasks/lib/rollup-plugin-file-overrides.js [new file with mode: 0644]
package.json

index 188e1e4524d5e90f491cf7f43f27e944f048c05a..4abc70868184f35c448c9995b8df490f996a1823 100644 (file)
@@ -10,7 +10,7 @@ module.exports = function( grunt ) {
        const fs = require( "fs" );
        const path = require( "path" );
        const rollup = require( "rollup" );
-       const rollupHypothetical = require( "rollup-plugin-hypothetical" );
+       const rollupFileOverrides = require( "./lib/rollup-plugin-file-overrides" );
        const Insight = require( "insight" );
        const pkg = require( "../../package.json" );
        const srcFolder = path.resolve( `${ __dirname }/../../src` );
@@ -39,10 +39,18 @@ module.exports = function( grunt ) {
                outro: wrapper[ 1 ]
                        .replace( /^\n*/, "" )
        };
-       const rollupHypotheticalOptions = {
-               allowFallthrough: true,
-               files: {}
-       };
+       const fileOverrides = new Map();
+
+       function getOverride( filePath ) {
+               return fileOverrides.get( path.resolve( filePath ) );
+       }
+
+       function setOverride( filePath, source ) {
+
+               // We want normalized paths in overrides as they will be matched
+               // against normalized paths in the file overrides Rollup plugin.
+               fileOverrides.set( path.resolve( filePath ), source );
+       }
 
        grunt.registerMultiTask(
                "build",
@@ -186,14 +194,14 @@ module.exports = function( grunt ) {
 
                        // Remove the jQuery export from the entry file, we'll use our own
                        // custom wrapper.
-                       rollupHypotheticalOptions.files[ inputRollupOptions.input ] = read( inputFileName )
-                               .replace( /\n*export default jQuery;\n*/, "\n" );
+                       setOverride( inputRollupOptions.input,
+                               read( inputFileName ).replace( /\n*export default jQuery;\n*/, "\n" ) );
 
                        // Replace exports/global with a noop noConflict
                        if ( ( index = excluded.indexOf( "exports/global" ) ) > -1 ) {
-                               rollupHypotheticalOptions.files[ `${ srcFolder }/exports/global.js` ] =
+                               setOverride( `${ srcFolder }/exports/global.js`,
                                        "import jQuery from \"../core.js\";\n\n" +
-                                       "jQuery.noConflict = function() {};";
+                                               "jQuery.noConflict = function() {};" );
                                excluded.splice( index, 1 );
                        }
 
@@ -207,9 +215,10 @@ module.exports = function( grunt ) {
                                }
 
                                // Remove the comma for anonymous defines
-                               rollupHypotheticalOptions.files[ `${ srcFolder }/exports/amd.js` ] =
+                               setOverride( `${ srcFolder }/exports/amd.js`,
                                        read( "exports/amd.js" )
-                                               .replace( /(\s*)"jquery"(\,\s*)/, amdName ? "$1\"" + amdName + "\"$2" : "" );
+                                               .replace( /(\s*)"jquery"(\,\s*)/,
+                                                       amdName ? "$1\"" + amdName + "\"$2" : "" ) );
                        }
 
                        grunt.verbose.writeflags( excluded, "Excluded" );
@@ -225,7 +234,7 @@ module.exports = function( grunt ) {
 
                                // Replace excluded modules with empty sources.
                                for ( const module of excluded ) {
-                                       rollupHypotheticalOptions.files[ `${ srcFolder }/${ module }.js` ] = "";
+                                       setOverride( `${ srcFolder }/${ module }.js`, "" );
                                }
                        }
 
@@ -234,20 +243,21 @@ module.exports = function( grunt ) {
 
                                // Remove the default inclusions, they will be overwritten with the explicitly
                                // included ones.
-                               rollupHypotheticalOptions.files[ inputRollupOptions.input ] = "";
+                               setOverride( inputRollupOptions.input, "" );
 
                        }
 
                        // Import the explicitly included modules.
                        if ( included.length ) {
-                               rollupHypotheticalOptions.files[ inputRollupOptions.input ] += included
-                                       .map( module => `import "./${module}.js";` )
-                                       .join( "\n" );
+                               setOverride( inputRollupOptions.input,
+                                       getOverride( inputRollupOptions.input ) + included
+                                               .map( module => `import "./${module}.js";` )
+                                               .join( "\n" ) );
                        }
 
                        const bundle = await rollup.rollup( {
                                ...inputRollupOptions,
-                               plugins: [ rollupHypothetical( rollupHypotheticalOptions ) ]
+                               plugins: [ rollupFileOverrides( fileOverrides ) ]
                        } );
 
                        const { output: [ { code } ] } = await bundle.generate( outputRollupOptions );
diff --git a/build/tasks/lib/rollup-plugin-file-overrides.js b/build/tasks/lib/rollup-plugin-file-overrides.js
new file mode 100644 (file)
index 0000000..c494e4e
--- /dev/null
@@ -0,0 +1,24 @@
+"use strict";
+
+/**
+ * A Rollup plugin accepting a file overrides map and changing
+ * module sources to the overridden ones where provided. Files
+ * without overrides are loaded from disk.
+ *
+ * @param {Map<string, string>} fileOverrides
+ */
+module.exports = ( fileOverrides ) => {
+       return {
+               name: "jquery-file-overrides",
+               load( id ) {
+                       if ( fileOverrides.has( id ) ) {
+
+                               // Replace the module by a fake source.
+                               return fileOverrides.get( id );
+                       }
+
+                       // Handle this module via the file system.
+                       return null;
+               }
+       };
+};
index 1bb21b7c9fbbbc3788fdad17c419353dd9213ed4..754ac08b59e120310e1519ab5a256a79db1a198c 100644 (file)
@@ -61,7 +61,6 @@
     "raw-body": "2.3.3",
     "requirejs": "2.3.6",
     "rollup": "1.25.2",
-    "rollup-plugin-hypothetical": "2.1.0",
     "sinon": "7.3.1",
     "strip-json-comments": "2.0.1",
     "testswarm": "1.1.0",