Skip to content

Commit

Permalink
new: Support solid exports automatically. (#162)
Browse files Browse the repository at this point in the history
* Update logic.

* Update tests.

* Update exports.

* Change features.

* Add flags.

* Support public.
  • Loading branch information
milesj committed Nov 15, 2022
1 parent 1eee0db commit 1407fb1
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 127 deletions.
33 changes: 9 additions & 24 deletions packages/packemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,23 @@
"./package.json": "./package.json",
"./babel": {
"node": {
"import": {
"types": "./cjs/babel.d.ts",
"default": "./cjs/babel-wrapper.mjs"
},
"require": {
"types": "./cjs/babel.d.ts",
"default": "./cjs/babel.cjs"
}
"types": "./cjs/babel.d.ts",
"import": "./cjs/babel-wrapper.mjs",
"require": "./cjs/babel.cjs"
}
},
"./bin": {
"node": {
"import": {
"types": "./cjs/bin.d.ts",
"default": "./cjs/bin-wrapper.mjs"
},
"require": {
"types": "./cjs/bin.d.ts",
"default": "./cjs/bin.cjs"
}
"types": "./cjs/bin.d.ts",
"import": "./cjs/bin-wrapper.mjs",
"require": "./cjs/bin.cjs"
}
},
".": {
"node": {
"import": {
"types": "./cjs/index.d.ts",
"default": "./cjs/index-wrapper.mjs"
},
"require": {
"types": "./cjs/index.d.ts",
"default": "./cjs/index.cjs"
}
"types": "./cjs/index.d.ts",
"import": "./cjs/index-wrapper.mjs",
"require": "./cjs/index.cjs"
}
}
},
Expand Down
87 changes: 53 additions & 34 deletions packages/packemon/src/Artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import execa from 'execa';
import fs from 'fs-extra';
import { rollup } from 'rollup';
import { applyStyle } from '@boost/cli';
import { Path, toArray, VirtualPath } from '@boost/common';
import { isObject, Path, toArray, VirtualPath } from '@boost/common';
import { createDebugger, Debugger } from '@boost/debug';
import { removeSourcePath } from './helpers/removeSourcePath';
import type { Package } from './Package';
Expand Down Expand Up @@ -73,9 +73,11 @@ export class Artifact {
/**
* Build code and types in parallel.
*/
async build(options: BuildOptions, packemonConfig: ConfigFile): Promise<void> {
const features = this.package.getFeatureFlags();

async build(
options: BuildOptions,
features: FeatureFlags,
packemonConfig: ConfigFile,
): Promise<void> {
await Promise.all([this.buildCode(features, packemonConfig), this.buildTypes(features)]);
}

Expand Down Expand Up @@ -263,18 +265,18 @@ export class Artifact {
return `${this.platform}:${this.support}:${this.builds.map((build) => build.format).join(',')}`;
}

getPackageExports(): PackageExports {
getPackageExports(features: FeatureFlags): PackageExports {
const exportMap: PackageExports = {};

if (this.api === 'private' || this.bundle) {
Object.keys(this.inputs).forEach((outputName) => {
this.mapPackageExportsFromBuilds(outputName, exportMap);
this.mapPackageExportsFromBuilds(outputName, exportMap, features);
});
} else {
// Use subpath export patterns when not bundling
// https://nodejs.org/api/packages.html#subpath-patterns
this.mapPackageExportsFromBuilds('*', exportMap);
this.mapPackageExportsFromBuilds(this.getIndexInput(), exportMap, true);
this.mapPackageExportsFromBuilds('*', exportMap, features);
this.mapPackageExportsFromBuilds(this.getIndexInput(), exportMap, features, true);
}

return exportMap;
Expand All @@ -296,6 +298,7 @@ export class Artifact {
protected mapPackageExportsFromBuilds(
outputName: string,
exportMap: PackageExports,
features: FeatureFlags,
index: boolean = false,
) {
const defaultEntry = this.findEntryPoint(['lib'], outputName);
Expand Down Expand Up @@ -327,58 +330,74 @@ export class Artifact {
const mjsEntry = this.findEntryPoint(['mjs'], outputName);
const cjsEntry = this.findEntryPoint(['cjs'], outputName);

if (mjsEntry || cjsEntry) {
if (mjsEntry && cjsEntry) {
paths = {
import: {
types: mjsEntry.declPath,
default: mjsEntry.entryPath,
},
require: {
types: cjsEntry.declPath,
default: cjsEntry.entryPath,
},
};
} else if (mjsEntry) {
paths = {
import: mjsEntry
? {
types: mjsEntry.declPath,
default: mjsEntry.entryPath,
}
: undefined,
require: cjsEntry
? {
types: cjsEntry.declPath,
default: cjsEntry.entryPath,
}
: undefined,
types: mjsEntry.declPath,
import: mjsEntry.entryPath,
};
} else if (cjsEntry) {
paths = {
types: cjsEntry.declPath,
require: cjsEntry.entryPath,
};

// Automatically apply the mjs wrapper for cjs
if (!paths.import && outputName !== '*' && cjsEntry) {
paths.import = {
types: cjsEntry.declPath,
default: cjsEntry.entryPath.replace('.cjs', '-wrapper.mjs'),
};
if (!paths.import && outputName !== '*') {
paths.import = cjsEntry.entryPath.replace('.cjs', '-wrapper.mjs');
}
}

if (defaultEntry) {
if (!paths.types && !isObject(paths.import) && !isObject(paths.require)) {
paths.types = defaultEntry.declPath;
}

if (!paths.require && defaultEntry) {
if (!paths.require) {
paths.default = defaultEntry.entryPath;
}
} else {
paths.types = defaultEntry?.declPath;
paths.default = defaultEntry?.entryPath;
}

break;
}

case 'native':
paths.types = defaultEntry?.declPath;
paths.default = defaultEntry?.entryPath;
break;

default:
break;
}

const pathsMap = {
const pathsMap: Record<string, PackageExportPaths | string> = {
[this.platform === 'native' ? 'react-native' : this.platform]: paths,
};

// Point to the source files when using solid
if (features.solid) {
if (outputName === '*') {
pathsMap.solid = `./src/*.${features.typescript ? 'tsx' : 'js'}`;
} else {
const input = this.inputs[outputName];

pathsMap.solid = input.startsWith('./') ? input : `./${input}`;
}
}

// Provide fallbacks if condition above is not
if (defaultEntry) {
Object.assign(pathsMap, {
default: defaultEntry.entryPath,
});
pathsMap.default = defaultEntry.entryPath;
}

// eslint-disable-next-line no-param-reassign
Expand Down
10 changes: 6 additions & 4 deletions packages/packemon/src/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ export class Package {
this.debug('Building artifacts');

// Build artifacts in parallel
const features = this.getFeatureFlags();

await Promise.all(
this.artifacts.map(async (artifact) => {
const start = Date.now();

try {
artifact.state = 'building';

await artifact.build(options, packemonConfig);
await artifact.build(options, features, packemonConfig);

artifact.state = 'passed';
} catch (error: unknown) {
Expand All @@ -101,7 +103,7 @@ export class Package {

// Add package `exports` based on artifacts
if (options.addExports) {
this.addExports();
this.addExports(features);
}

// Add package `files` whitelist
Expand Down Expand Up @@ -473,15 +475,15 @@ export class Package {
}
}

protected addExports() {
protected addExports(features: FeatureFlags) {
this.debug('Adding `exports` to `package.json`');

let exportMap: PackageExports = {
'./package.json': './package.json',
};

this.artifacts.forEach((artifact) => {
Object.entries(artifact.getPackageExports()).forEach(([path, conditions]) => {
Object.entries(artifact.getPackageExports(features)).forEach(([path, conditions]) => {
if (conditions) {
exportMap[path] = mergeExports(exportMap[path] ?? {}, conditions);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/packemon/src/babel/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function getBabelInputConfig(
const presets: PluginItem[] = [];
const tsOptions = {
allowDeclareFields: true,
onlyRemoveTypeImports: true,
onlyRemoveTypeImports: false,
optimizeConstEnums: true,
};

Expand Down
Loading

0 comments on commit 1407fb1

Please sign in to comment.