jquery/build/tasks/dist.js

32 lines
900 B
JavaScript
Raw Normal View History

"use strict";
// Process files for distribution.
Core: Move the factory to separate exports Since versions 1.11.0/2.1.0, jQuery has used a module wrapper with one strange addition - in CommonJS environments, if a global `window` with a `document` was not present, jQuery exported a factory accepting a `window` implementation and returning jQuery. This approach created a number of problems: 1. Properly typing jQuery would be a nightmare as the exported value depends on the environment. In practice, typing definitions ignored the factory case. 2. Since we now use named exports for the jQuery module version, it felt weird to have `jQuery` and `$` pointing to the factory instead of real jQuery. Instead, for jQuery 4.0 we leverage the just added `exports` field in `package.json` to expose completely separate factory entry points: one for the full build, one for the slim one. Exports definitions for `./factory` & `./factory-slim` are simpler than for `.` and `./slim` - this is because it's a new entry point, we only expose a named export and so there's no issue with just pointing Node.js to the CommonJS version (we cannot use the module version for `import` from Node.js to avoid double package hazard). The factory entry points are also not meant for the Web browser which always has a proper `window` - and they'd be unfit for an inclusion in a regular script tag anyway. Because of that, we also don't generate minified versions of these entry points. The factory files are not pushed to the CDN since they are mostly aimed at Node.js. Closes gh-5293
2023-09-19 16:58:24 +00:00
module.exports = function processForDist( text, filename ) {
if ( !text ) {
throw new Error( "text required for processForDist" );
}
if ( !filename ) {
throw new Error( "filename required for processForDist" );
}
// Ensure files use only \n for line endings, not \r\n
if ( /\x0d\x0a/.test( text ) ) {
throw new Error( filename + ": Incorrect line endings (\\r\\n)" );
}
// Ensure only ASCII chars so script tags don't need a charset attribute
if ( text.length !== Buffer.byteLength( text, "utf8" ) ) {
let message = filename + ": Non-ASCII characters detected:\n";
for ( let i = 0; i < text.length; i++ ) {
const c = text.charCodeAt( i );
if ( c > 127 ) {
message += "- position " + i + ": " + c + "\n";
message += "==> " + text.substring( i - 20, i + 20 );
break;
}
}
throw new Error( message );
}
};