Skip to content

Commit

Permalink
Merge pull request #246 from Comandeer/t/228
Browse files Browse the repository at this point in the history
Closes #228
  • Loading branch information
Comandeer committed Feb 5, 2023
2 parents c9348cf + eb33ab7 commit ff7e09a
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 260 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

---

## [0.18.0]
## [0.18.0] – 2023-02-05
### Changed
* [#209]: **BREAKING CHANGE**: parsing the project's `package.json` is no longer blocking as all file operations were rewritten to be asynchronous.

### Removed
* [#228]: **BREAKING CHANGE**: support for non-`exports` entrypoints.

### Fixed
* [#240]: bundling types incorrectly removes bundled types definitions right after creating them.
* [#242]: bundling of types creates incorrect file structure in `dist/` if source files are inside subdirectories.
Expand Down Expand Up @@ -234,6 +237,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#220]: https://github.com/Comandeer/rollup-lib-bundler/issues/220
[#222]: https://github.com/Comandeer/rollup-lib-bundler/issues/222
[#225]: https://github.com/Comandeer/rollup-lib-bundler/issues/225
[#228]: https://github.com/Comandeer/rollup-lib-bundler/issues/228
[#240]: https://github.com/Comandeer/rollup-lib-bundler/issues/240
[#242]: https://github.com/Comandeer/rollup-lib-bundler/issues/242

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ No configuration. Consider it a feature.
It gets `package.json` from the current working directory, parses it and get neeeded info:

* `name`, `author`, `version` and `license` to create beautiful banner comment,
* `exports.import`, `module` or `jsnext:main` for saving ESM bundle,
* `exports.require` or `main` to get path for saving CJS bundle (optional).
* `exports.import` for saving ESM bundle,
* `exports.require` to get path for saving CJS bundle (optional).

Then the bundling happens. The default entry point for Rollup is `src/index.js`. Please note that **`dist/` directory is purged before bundling**! So if anything should be there alongside the bundle, it should be added there _after_ the bundling.

Expand Down
105 changes: 15 additions & 90 deletions src/packageParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,82 +38,29 @@ async function loadAndParsePackageJSONFile( dirPath ) {
}

function lintObject( obj ) {
checkProperty( 'name' );
checkProperty( 'version' );
checkProperties( 'exports/./import', 'exports/import', 'module', 'jsnext:main' );
checkProperty( 'author' );
checkProperty( 'license' );

function checkProperty( name ) {
if ( typeof obj[ name ] === 'undefined' ) {
throw new ReferenceError( `Package metadata must contain "${ name }" property.` );
}
if ( typeof obj.name === 'undefined' ) {
throw new ReferenceError( 'Package metadata must contain "name" property.' );
}

function checkProperties( ...properties ) {
const isAtLeastOnePresent = properties.some( ( property ) => {
const propertyPath = property.split( '/' );

return checkPropertyExistence( obj, propertyPath );
} );

if ( !isAtLeastOnePresent ) {
throw new ReferenceError( `Package metadata must contain one of ${ prepareNamesForError( properties ) } properties or all of them.` );
}
if ( typeof obj.version === 'undefined' ) {
throw new ReferenceError( 'Package metadata must contain "version" property.' );
}

function checkPropertyExistence( obj, propertyPath ) {
const currentProperty = propertyPath.shift();

if ( typeof obj[ currentProperty ] === 'undefined' ) {
return false;
}

if ( propertyPath.length === 0 ) {
return true;
}
const isESMEntryPointPresent = typeof obj.exports?.import !== 'undefined' ||
typeof obj.exports?.[ '.' ]?.import !== 'undefined';

return checkPropertyExistence( obj[ currentProperty ], propertyPath );
if ( !isESMEntryPointPresent ) {
throw new ReferenceError(
'Package metadata must contain one of "exports[ \'.\' ].import" or "exports.import" properties or all of them.'
);
}

function prepareNamesForError( names ) {
return names.map( ( name, i ) => {
const formattedName = formatName( name );
const quotedName = `"${ formattedName }"`;

if ( i === 0 ) {
return quotedName;
}

const conjuction = ( i === names.length - 1 ) ? ' or ' : ', ';

return `${ conjuction}${ quotedName }`;
} ).join( '' );

function formatName( name ) {
// Thanks to using capturing groups, the separator will be preserved
// in the splitted string.
const separatorRegex = /(\/)/g;
const nameParts = name.split( separatorRegex );

return nameParts.reduce( ( parts, part ) => {
const lastPart = parts[ parts.length - 1 ];

if ( part.startsWith( '.' ) && lastPart === '.' ) {
return [ ...parts.slice( 0, -1 ), '[ \'', part ];
}

if ( part === '/' && lastPart.startsWith( '.' ) ) {
return [ ...parts, '\' ].' ];
}

if ( part === '/' ) {
return [ ...parts, '.' ];
}
if ( typeof obj.author === 'undefined' ) {
throw new ReferenceError( 'Package metadata must contain "author" property.' );
}

return [ ...parts, part ];
}, [] ).join( '' );
}
if ( typeof obj.license === 'undefined' ) {
throw new ReferenceError( 'Package metadata must contain "license" property.' );
}
}

Expand Down Expand Up @@ -150,12 +97,6 @@ async function prepareDistMetadata( packageDir, metadata ) {
function getSubPaths( metadata ) {
const exports = metadata.exports;

if ( !exports ) {
return [
'.'
];
}

const subpaths = Object.keys( exports ).filter( ( subpath ) => {
return subpath.startsWith( '.' );
} );
Expand Down Expand Up @@ -246,38 +187,22 @@ function getEntryPointType( srcPath ) {
function getESMTarget( metadata, subPath ) {
const exportsTarget = getExportsTarget( metadata, subPath, 'import' );

if ( subPath === '.' ) {
return exportsTarget || metadata.module || metadata[ 'jsnext:main' ];
}

return exportsTarget;
}

function getCJSTarget( metadata, subPath ) {
const exportsTarget = getExportsTarget( metadata, subPath, 'require' );

if ( subPath === '.' ) {
return exportsTarget || metadata.main;
}

return exportsTarget;
}

function getTypesTarget( metadata, subPath ) {
const exportsTarget = getExportsTarget( metadata, subPath, 'types' );

if ( subPath === '.' ) {
return exportsTarget || metadata.types;
}

return exportsTarget;
}

function getExportsTarget( metadata, subPath, type ) {
if ( !metadata.exports ) {
return null;
}

const exports = metadata.exports;

if ( exports[ subPath ] ) {
Expand Down
8 changes: 6 additions & 2 deletions tests/__fixtures__/dynamicExternalImport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
"private": true,
"version": "1.0.0",
"description": "Test package",
"main": "dist/es5.js",
"module": "dist/es2015.js",
"exports": {
".": {
"import": "./dist/es2015.js",
"require": "./dist/es5.js"
}
},
"author": "Comandeer",
"license": "ISC"
}
8 changes: 6 additions & 2 deletions tests/__fixtures__/errorPackage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
"private": true,
"version": "1.0.0",
"description": "Test package",
"main": "dist/es5.js",
"module": "dist/es2015.js",
"exports": {
".": {
"import": "./dist/es2015.js",
"require": "./dist/es5.js"
}
},
"author": "Comandeer",
"license": "ISC"
}
8 changes: 6 additions & 2 deletions tests/__fixtures__/externalDepPackage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
"private": true,
"version": "1.0.0",
"description": "Test package",
"main": "dist/es5.js",
"module": "dist/es2015.js",
"exports": {
".": {
"import": "./dist/es2015.js",
"require": "./dist/es5.js"
}
},
"author": "Comandeer",
"license": "ISC"
}
8 changes: 6 additions & 2 deletions tests/__fixtures__/jsonPackage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
"private": true,
"version": "1.0.0",
"description": "Test package",
"main": "dist/es5.js",
"module": "dist/es2015.js",
"exports": {
".": {
"import": "./dist/es2015.js",
"require": "./dist/es5.js"
}
},
"author": {
"name": "Piotr Kowalski",
"email": "piecioshka@gmail.com",
Expand Down
8 changes: 6 additions & 2 deletions tests/__fixtures__/testPackage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
"private": true,
"version": "1.0.0",
"description": "Test package",
"main": "dist/es5.js",
"module": "dist/es2015.js",
"exports": {
".": {
"import": "./dist/es2015.js",
"require": "./dist/es5.js"
}
},
"author": "Comandeer",
"license": "ISC"
}
Loading

0 comments on commit ff7e09a

Please sign in to comment.