Skip to content

Commit

Permalink
new: Support a bundle config option. (#38)
Browse files Browse the repository at this point in the history
* Add option.

* Update tests.

* Update docs.
  • Loading branch information
milesj authored Mar 9, 2021
1 parent 8e773d1 commit bb4844e
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 24 deletions.
4 changes: 4 additions & 0 deletions src/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export class Package {
});

toArray(config.platform).forEach((platform) => {
let bundle = true;
let formats = [...toArray(config.format)];
const isEmpty = formats.length === 0;

Expand All @@ -197,6 +198,8 @@ export class Package {
break;

case 'node':
bundle = false;

if (isEmpty) {
formats.push('lib');
} else {
Expand All @@ -220,6 +223,7 @@ export class Package {
}

this.configs.push({
bundle: cfg.bundle === undefined ? bundle : config.bundle,
formats,
inputs: config.inputs,
namespace: config.namespace,
Expand Down
1 change: 1 addition & 0 deletions src/Packemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export class Packemon {
pkg,
// Pass platform and support here for convenience
config.formats.map((format) => ({
bundle: config.bundle,
format,
platform: config.platform,
support: config.support,
Expand Down
11 changes: 5 additions & 6 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ export function getRollupOutputConfig(
features: FeatureFlags,
build: BundleBuild,
): OutputOptions {
const { format, platform, support } = build;
const { bundle = true, format, platform, support } = build;
const name = artifact.outputName;
const { ext, folder } = artifact.getOutputMetadata(format);
const isNode = platform === 'node';
const isTest = process.env.NODE_ENV === 'test';
const preserve = !bundle;

const output: OutputOptions = {
dir: artifact.package.path.append(folder).path(),
Expand All @@ -102,10 +102,9 @@ export function getRollupOutputConfig(
paths: getRollupPaths(artifact, ext),
// Use our extension for file names
assetFileNames: '../assets/[name]-[hash][extname]',
chunkFileNames: isNode && !isTest ? `[name]-[hash].${ext}` : `${name}-[hash].${ext}`,
entryFileNames: isNode && !isTest ? `[name].${ext}` : `${name}.${ext}`,
// Dont bundle to a single file when targeting node
preserveModules: isNode,
chunkFileNames: preserve && !isTest ? `[name]-[hash].${ext}` : `${name}-[hash].${ext}`,
entryFileNames: preserve && !isTest ? `[name].${ext}` : `${name}.${ext}`,
preserveModules: preserve,
// Use const when not supporting new targets
preferConst: support === 'current' || support === 'experimental',
// Output specific plugins
Expand Down
1 change: 1 addition & 0 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const support = string<Support>(DEFAULT_SUPPORT).oneOf(SUPPORTS);
// BLUEPRINTS

export const packemonBlueprint: Blueprint<PackemonPackageConfig> = {
bundle: bool(true),
format: union([array(format), format], []),
inputs: object(string(), { index: DEFAULT_INPUT }).custom((obj) => {
Object.keys(obj).forEach((key) => {
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type DeclarationType = 'api' | 'none' | 'standard';
// PACKAGES

export interface PackemonPackageConfig {
bundle?: boolean;
format?: Format | Format[];
inputs?: Record<string, string>;
namespace?: string;
Expand All @@ -70,6 +71,7 @@ export interface PackemonPackage extends PackageStructure {
}

export interface PackageConfig {
bundle: boolean;
formats: Format[];
inputs: Record<string, string>;
namespace: string;
Expand Down Expand Up @@ -116,6 +118,7 @@ export interface BuildResult {
}

export interface BundleBuild {
bundle?: boolean;
format: Format;
platform: Platform;
support: Support;
Expand Down
42 changes: 42 additions & 0 deletions tests/Package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ describe('Package', () => {

expect(pkg.configs).toEqual([
{
bundle: true,
formats: ['lib', 'esm'],
inputs: {},
platform: 'browser',
Expand All @@ -918,6 +919,7 @@ describe('Package', () => {

expect(pkg.configs).toEqual([
{
bundle: true,
formats: ['lib', 'esm', 'umd'],
inputs: {},
platform: 'browser',
Expand All @@ -940,6 +942,7 @@ describe('Package', () => {

expect(pkg.configs).toEqual([
{
bundle: true,
formats: ['lib'],
inputs: {},
platform: 'native',
Expand All @@ -962,6 +965,7 @@ describe('Package', () => {

expect(pkg.configs).toEqual([
{
bundle: false,
formats: ['lib'],
inputs: {},
platform: 'node',
Expand All @@ -984,6 +988,7 @@ describe('Package', () => {

expect(pkg.configs).toEqual([
{
bundle: false,
formats: ['cjs', 'mjs'],
inputs: {},
platform: 'node',
Expand All @@ -1003,13 +1008,15 @@ describe('Package', () => {

expect(pkg.configs).toEqual([
{
bundle: true,
formats: ['lib', 'esm'],
inputs: {},
platform: 'browser',
namespace: '',
support: 'stable',
},
{
bundle: false,
formats: ['lib'],
inputs: {},
platform: 'node',
Expand All @@ -1030,20 +1037,23 @@ describe('Package', () => {

expect(pkg.configs).toEqual([
{
bundle: true,
formats: ['lib', 'esm'],
inputs: {},
platform: 'browser',
namespace: '',
support: 'stable',
},
{
bundle: false,
formats: ['lib', 'cjs'],
inputs: {},
platform: 'node',
namespace: '',
support: 'stable',
},
{
bundle: true,
formats: ['lib'],
inputs: {},
platform: 'native',
Expand All @@ -1053,6 +1063,38 @@ describe('Package', () => {
]);
});

it('can override `bundle` defaults', () => {
pkg.setConfigs([
{
bundle: true,
platform: 'node',
},
{
bundle: false,
platform: 'browser',
},
]);

expect(pkg.configs).toEqual([
{
bundle: true,
formats: ['lib'],
inputs: { index: 'src/index.ts' },
platform: 'node',
namespace: '',
support: 'stable',
},
{
bundle: false,
formats: ['lib', 'esm'],
inputs: { index: 'src/index.ts' },
platform: 'browser',
namespace: '',
support: 'stable',
},
]);
});

it('errors if invalid format is provided for `browser` platform', () => {
expect(() => {
pkg.setConfigs([
Expand Down
24 changes: 14 additions & 10 deletions tests/Packemon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,30 +326,30 @@ describe('Packemon', () => {
expect((packages[0].artifacts[0] as BundleArtifact).outputName).toBe('index');
expect((packages[0].artifacts[0] as BundleArtifact).inputFile).toBe('src/index.ts');
expect(packages[0].artifacts[0].builds).toEqual([
{ format: 'lib', platform: 'node', support: 'stable' },
{ bundle: false, format: 'lib', platform: 'node', support: 'stable' },
]);

expect((packages[0].artifacts[1] as BundleArtifact).outputName).toBe('index');
expect((packages[0].artifacts[1] as BundleArtifact).inputFile).toBe('src/index.ts');
expect(packages[0].artifacts[1].builds).toEqual([
{ format: 'lib', platform: 'browser', support: 'current' },
{ format: 'esm', platform: 'browser', support: 'current' },
{ bundle: true, format: 'lib', platform: 'browser', support: 'current' },
{ bundle: true, format: 'esm', platform: 'browser', support: 'current' },
]);

expect(packages[1].artifacts).toHaveLength(1);
expect((packages[1].artifacts[0] as BundleArtifact).outputName).toBe('core');
expect((packages[1].artifacts[0] as BundleArtifact).inputFile).toBe('./src/core.ts');
expect(packages[1].artifacts[0].builds).toEqual([
{ format: 'lib', platform: 'node', support: 'stable' },
{ bundle: false, format: 'lib', platform: 'node', support: 'stable' },
]);

expect(packages[2].artifacts).toHaveLength(1);
expect((packages[2].artifacts[0] as BundleArtifact).outputName).toBe('index');
expect((packages[2].artifacts[0] as BundleArtifact).inputFile).toBe('src/index.ts');
expect(packages[2].artifacts[0].builds).toEqual([
{ format: 'lib', platform: 'browser', support: 'stable' },
{ format: 'esm', platform: 'browser', support: 'stable' },
{ format: 'umd', platform: 'browser', support: 'stable' },
{ bundle: true, format: 'lib', platform: 'browser', support: 'stable' },
{ bundle: true, format: 'esm', platform: 'browser', support: 'stable' },
{ bundle: true, format: 'umd', platform: 'browser', support: 'stable' },
]);
});

Expand Down Expand Up @@ -427,12 +427,12 @@ describe('Packemon', () => {
packemon.generateArtifacts(packages);

expect(packages[0].artifacts[0].builds).toEqual([
{ format: 'lib', platform: 'browser', support: 'stable' },
{ format: 'esm', platform: 'browser', support: 'stable' },
{ bundle: true, format: 'lib', platform: 'browser', support: 'stable' },
{ bundle: true, format: 'esm', platform: 'browser', support: 'stable' },
]);

expect(packages[0].artifacts[1].builds).toEqual([
{ format: 'lib', platform: 'node', support: 'stable' },
{ bundle: false, format: 'lib', platform: 'node', support: 'stable' },
]);
});
});
Expand Down Expand Up @@ -463,13 +463,15 @@ describe('Packemon', () => {
expect(one.getName()).toBe('pkg-valid-array');
expect(one.configs).toEqual([
{
bundle: false,
formats: ['lib'],
inputs: { index: 'src/index.ts' },
namespace: '',
platform: 'node',
support: 'stable',
},
{
bundle: true,
formats: ['lib', 'esm'],
inputs: { index: 'src/index.ts' },
namespace: '',
Expand All @@ -481,6 +483,7 @@ describe('Packemon', () => {
expect(two.getName()).toBe('pkg-valid-object');
expect(two.configs).toEqual([
{
bundle: false,
formats: ['lib'],
inputs: { core: './src/core.ts' },
namespace: '',
Expand All @@ -492,6 +495,7 @@ describe('Packemon', () => {
expect(three.getName()).toBe('pkg-valid-object-private');
expect(three.configs).toEqual([
{
bundle: true,
formats: ['lib', 'esm', 'umd'],
inputs: { index: 'src/index.ts' },
namespace: 'Test',
Expand Down
1 change: 1 addition & 0 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ FORMATS.forEach((format) => {
}

builds.set(key, {
bundle: platform !== 'node',
format,
platform,
support,
Expand Down
16 changes: 10 additions & 6 deletions tests/rollup/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ describe('getRollupConfig()', () => {

it('generates an output config for each build', () => {
artifact.builds.push(
{ format: 'lib', platform: 'node', support: 'stable' },
{ format: 'lib', platform: 'browser', support: 'legacy' },
{ format: 'esm', platform: 'browser', support: 'current' },
{ format: 'mjs', platform: 'node', support: 'experimental' },
{ bundle: false, format: 'lib', platform: 'node', support: 'stable' },
{ bundle: true, format: 'lib', platform: 'browser', support: 'legacy' },
{ bundle: true, format: 'esm', platform: 'browser', support: 'current' },
{ bundle: false, format: 'mjs', platform: 'node', support: 'experimental' },
);

expect(getRollupConfig(artifact, {})).toEqual({
Expand Down Expand Up @@ -188,7 +188,7 @@ describe('getRollupConfig()', () => {
artifact.outputName = 'server';
artifact.platform = 'node';
artifact.support = 'stable';
artifact.builds.push({ format: 'lib', platform: 'node', support: 'stable' });
artifact.builds.push({ bundle: false, format: 'lib', platform: 'node', support: 'stable' });

expect(getRollupConfig(artifact, {})).toEqual({
cache: undefined,
Expand Down Expand Up @@ -299,7 +299,11 @@ describe('getRollupOutputConfig()', () => {

it('generates default output config', () => {
expect(
getRollupOutputConfig(artifact, {}, { format: 'lib', platform: 'node', support: 'stable' }),
getRollupOutputConfig(
artifact,
{},
{ bundle: false, format: 'lib', platform: 'node', support: 'stable' },
),
).toEqual({
assetFileNames: '../assets/[name]-[hash][extname]',
banner: expect.any(String),
Expand Down
16 changes: 15 additions & 1 deletion website/docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ To support multiple platforms, pass an array.
}
```

> In the future, we hope to support other platforms like Electron and React Native.
> In the future, we hope to support other platforms like Electron.
## Support

Expand Down Expand Up @@ -160,6 +160,20 @@ need to configure this.
> These inputs can be automatically mapped to `package.json` `exports` using the `--addExports` CLI
> option. Do note that this feature is still experimental.
## Bundle

Whether to bundle the source code into a single file by input. If not provided, is `false` for
`node` platform, but `true` for all other platforms.

```json
{
"bundle": false
}
```

> Prefer the defaults as much as possible. The only time this setting should change is if the
> package should allow deep imports. For example, a component library.
## Namespace

For browsers only, this would be the name of the UMD global variable.
Expand Down
2 changes: 1 addition & 1 deletion website/docs/pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ aborted.
## Options

Pack supports all the same command line options as [`build`](./build.md). The validation step cannot
be customized and will vaidate _everything_ by default.
be customized and will validate _everything_ by default.

0 comments on commit bb4844e

Please sign in to comment.