Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Convert Packemon to a .cjs library. #121

Merged
merged 8 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ jobs:
- run: yarn run type
- run: yarn run coverage
- run: yarn run lint
compat:
name: Compatibility
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
cache: yarn
- run: yarn install --immutable
- run: yarn run setup
- run: yarn run build
- run: node ./scripts/testPackemonImports.cjs
- run: node ./scripts/testPackemonImports.mjs
# Requires TS 4.7+
# - run: ts-node ./scripts/testPackemonImports.ts
pack:
name: Pack
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"docs": "cd website && yarn run start",
"prepare": "beemo create-config",
"setup": "yarn run prepare && node ./scripts/setup.mjs",
"setup": "yarn dlx --package packemon@2.0.0 --package typescript --quiet packemon build",
"build": "yarn run packemon build",
"validate": "yarn run packemon validate",
"check": "yarn run type && yarn run test && yarn run lint",
Expand All @@ -20,7 +20,7 @@
"release": "yarn prerelease && beemo run-script lerna-release",
"version": "yarn install && git add yarn.lock",
"pack": "NODE_ENV=production yarn run packemon build --addEngines --addExports --declaration",
"packemon": "node ./packages/packemon/lib/bin.js"
"packemon": "node ./packages/packemon/cjs/bin.cjs"
},
"workspaces": [
"packages/*",
Expand Down
1 change: 0 additions & 1 deletion packages/packemon/babel.cjs

This file was deleted.

35 changes: 24 additions & 11 deletions packages/packemon/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "packemon",
"version": "2.0.0",
"description": "Build and prepare packages for NPM distribution using standardized configurations and practices. Gotta pack 'em all!",
"type": "commonjs",
"description": "Build and prepare packages for npm distribution using standardized configurations and practices. Gotta pack 'em all!",
"keywords": [
"build",
"prepare",
Expand All @@ -15,39 +16,51 @@
},
"author": "Miles Johnson",
"license": "MIT",
"main": "./lib/index.js",
"main": "./cjs/index.cjs",
"types": "./dts/index.d.ts",
"bin": "./lib/bin.js",
"bin": "./cjs/bin.cjs",
"files": [
"babel.js",
"cjs/**/*.{cjs,mjs,map}",
"dts/**/*.d.ts",
"lib/**/*.{js,map}",
"src/**/*.{ts,tsx,json}",
"templates/**/*"
],
"exports": {
"./package.json": "./package.json",
"./babel": "./babel.cjs",
"./*": {
"types": "./dts/*.d.ts",
"node": "./lib/*.js"
"./babel": {
"types": "./dts/babel.d.ts",
"node": {
"import": "./cjs/babel-wrapper.mjs",
"require": "./cjs/babel.cjs"
}
},
"./bin": {
"types": "./dts/bin.d.ts",
"node": {
"import": "./cjs/bin-wrapper.mjs",
"require": "./cjs/bin.cjs"
}
},
".": {
"types": "./dts/index.d.ts",
"node": "./lib/index.js"
"node": {
"import": "./cjs/index-wrapper.mjs",
"require": "./cjs/index.cjs"
}
}
},
"engines": {
"node": ">=14.15.0",
"npm": ">=6.14.0"
},
"packemon": {
"api": "private",
"inputs": {
"index": "src/index.ts",
"babel": "src/babel.ts",
"bin": "src/bin.ts"
},
"format": "lib",
"format": "cjs",
"platform": "node"
},
"dependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/packemon/src/CodeArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ export class CodeArtifact extends Artifact<CodeBuild> {
}

const ext = format === 'cjs' || format === 'mjs' ? format : 'js';
const extGroup = format === 'cjs' ? 'cjs,mjs,map' : `${ext},map`;
const folder = format === 'lib' && this.sharedLib ? `lib/${this.platform}` : format;
const file = `${name}.${ext}`;

return {
ext,
extGroup,
file,
folder,
path: `./${new VirtualPath(folder, file)}`,
Expand Down Expand Up @@ -216,7 +218,7 @@ export class CodeArtifact extends Artifact<CodeBuild> {

case 'cjs':
// Automatically apply the wrapper
if (!paths.import) {
if (!paths.import && outputName !== '*') {
paths.import = entry.replace('.cjs', '-wrapper.mjs');
}

Expand Down
2 changes: 1 addition & 1 deletion packages/packemon/src/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ export class Package {
// Build files
if (artifact instanceof CodeArtifact) {
artifact.builds.forEach(({ format }) => {
files.add(`${format}/**/*.{${artifact.getBuildOutput(format).ext},map}`);
files.add(`${format}/**/*.{${artifact.getBuildOutput(format).extGroup}}`);
});

files.add(`src/**/*.{${this.getSourceFileExts(artifact.inputs)}}`);
Expand Down
8 changes: 8 additions & 0 deletions packages/packemon/tests/CodeArtifact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe('CodeArtifact', () => {
it('returns metadata for `lib` format', () => {
expect(artifact.getBuildOutput('lib', 'index')).toEqual({
ext: 'js',
extGroup: 'js,map',
file: 'index.js',
folder: 'lib',
path: './lib/index.js',
Expand All @@ -131,6 +132,7 @@ describe('CodeArtifact', () => {
it('returns metadata for `esm` format', () => {
expect(artifact.getBuildOutput('esm', 'index')).toEqual({
ext: 'js',
extGroup: 'js,map',
file: 'index.js',
folder: 'esm',
path: './esm/index.js',
Expand All @@ -140,6 +142,7 @@ describe('CodeArtifact', () => {
it('returns metadata for `umd` format', () => {
expect(artifact.getBuildOutput('umd', 'index')).toEqual({
ext: 'js',
extGroup: 'js,map',
file: 'index.js',
folder: 'umd',
path: './umd/index.js',
Expand All @@ -149,6 +152,7 @@ describe('CodeArtifact', () => {
it('returns metadata for `cjs` format', () => {
expect(artifact.getBuildOutput('cjs', 'index')).toEqual({
ext: 'cjs',
extGroup: 'cjs,mjs,map',
file: 'index.cjs',
folder: 'cjs',
path: './cjs/index.cjs',
Expand All @@ -158,6 +162,7 @@ describe('CodeArtifact', () => {
it('returns metadata for `mjs` format', () => {
expect(artifact.getBuildOutput('mjs', 'index')).toEqual({
ext: 'mjs',
extGroup: 'mjs,map',
file: 'index.mjs',
folder: 'mjs',
path: './mjs/index.mjs',
Expand All @@ -170,6 +175,7 @@ describe('CodeArtifact', () => {

expect(artifact.getBuildOutput('lib', 'index')).toEqual({
ext: 'js',
extGroup: 'js,map',
file: 'index.js',
folder: 'lib/node',
path: './lib/node/index.js',
Expand All @@ -181,6 +187,7 @@ describe('CodeArtifact', () => {

expect(artifact.getBuildOutput('esm', 'index')).toEqual({
ext: 'js',
extGroup: 'js,map',
file: 'index.js',
folder: 'esm',
path: './esm/index.js',
Expand All @@ -194,6 +201,7 @@ describe('CodeArtifact', () => {

expect(artifact.getBuildOutput('mjs', 'index')).toEqual({
ext: 'mjs',
extGroup: 'mjs,map',
file: 'some/other/file.mjs',
folder: 'mjs',
path: './mjs/some/other/file.mjs',
Expand Down
2 changes: 1 addition & 1 deletion packages/packemon/tests/Package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ describe('Package', () => {
expect(pkg.packageJson).toEqual(
expect.objectContaining({
files: [
'cjs/**/*.{cjs,map}',
'cjs/**/*.{cjs,mjs,map}',
'lib/**/*.{js,map}',
'src/**/*.{ts,tsx,json}',
'umd/**/*.{js,map}',
Expand Down
31 changes: 0 additions & 31 deletions scripts/setup.mjs

This file was deleted.

15 changes: 15 additions & 0 deletions scripts/testPackemonImports.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const assert = require('assert');
const packemon = require('packemon');
const { Packemon } = require('packemon');
const { createRootConfig } = require('packemon/babel');

console.log(Packemon, createRootConfig);

import('packemon').then((result) => {
assert(Packemon === result.Packemon);
assert.deepStrictEqual({ ...packemon }, { ...result });
});

import('packemon/babel').then((result) => {
assert(createRootConfig === result.createRootConfig);
});
15 changes: 15 additions & 0 deletions scripts/testPackemonImports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from 'assert';
import * as packemon from 'packemon';
import { Packemon } from 'packemon';
import { createRootConfig } from 'packemon/babel';

console.log(Packemon, createRootConfig);

import('packemon').then((result) => {
assert(Packemon === result.Packemon);
assert.deepStrictEqual({ ...packemon }, { ...result });
});

import('packemon/babel').then((result) => {
assert(createRootConfig === result.createRootConfig);
});
15 changes: 15 additions & 0 deletions scripts/testPackemonImports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from 'assert';
import * as packemon from 'packemon';
import { Packemon } from 'packemon';
import { createRootConfig } from 'packemon/babel';

console.log(Packemon, createRootConfig);

import('packemon').then((result) => {
assert(Packemon === result.Packemon);
assert.deepStrictEqual({ ...packemon }, { ...result });
});

import('packemon/babel').then((result) => {
assert(createRootConfig === result.createRootConfig);
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16189,7 +16189,7 @@ __metadata:
typescript:
optional: true
bin:
packemon: ./lib/bin.js
packemon: ./cjs/bin.cjs
languageName: unknown
linkType: soft

Expand Down