Skip to content

feat(create-rslib): setup Node.js package templates #319

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

Merged
merged 4 commits into from
Oct 22, 2024
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
1 change: 1 addition & 0 deletions packages/create-rslib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"create-rstack": "1.0.7"
},
"devDependencies": {
"@rslib/core": "workspace:*",
"@rslib/tsconfig": "workspace:*",
"@types/fs-extra": "^11.0.4",
"@types/node": "~18.19.39",
Expand Down
18 changes: 12 additions & 6 deletions packages/create-rslib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function getTemplateName({ template }: Argv) {
if (typeof template === 'string') {
const pair = template.split('-');
const language = pair[1] ?? 'js';
const type = pair[0];
return `${type}-${language}`;
const lang = pair[pair.length - 1];
if (lang && ['js', 'ts'].includes(lang)) {
return template;
}
// default to ts
return `${template}-ts`;
}

const type = checkCancel<string>(
await select({
message: 'Select template',
options: [{ value: 'example', label: 'Example' }],
options: [
{ value: 'node-dual', label: 'Node.js dual ESM/CJS package' },
{ value: 'node-esm', label: 'Node.js pure ESM package' },
],
}),
);

Expand All @@ -39,14 +45,14 @@ async function getTemplateName({ template }: Argv) {
}

function mapESLintTemplate(templateName: string) {
const language = templateName.split('-')[1];
const language = templateName.split('-').pop();
return `vanilla-${language}` as ESLintTemplateName;
}

create({
root: path.resolve(__dirname, '..'),
name: 'rslib',
templates: ['example-js', 'example-ts'],
templates: ['node-dual-js', 'node-dual-ts', 'node-esm-js', 'node-esm-ts'],
getTemplateName,
mapESLintTemplate,
});
12 changes: 0 additions & 12 deletions packages/create-rslib/template-example-js/package.json

This file was deleted.

5 changes: 0 additions & 5 deletions packages/create-rslib/template-example-js/rslib.config.mjs

This file was deleted.

13 changes: 0 additions & 13 deletions packages/create-rslib/template-example-ts/package.json

This file was deleted.

5 changes: 0 additions & 5 deletions packages/create-rslib/template-example-ts/rslib.config.ts

This file was deleted.

23 changes: 23 additions & 0 deletions packages/create-rslib/template-node-dual-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "rslib-node-dual-js",
"version": "0.0.0",
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"files": [
"dist"
],
"scripts": {
"build": "rslib build",
"dev": "rslib build --watch"
},
"devDependencies": {
"@rslib/core": "workspace:*"
}
}
15 changes: 15 additions & 0 deletions packages/create-rslib/template-node-dual-js/rslib.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
{
format: 'esm',
syntax: 'es2021',
},
{
format: 'cjs',
syntax: 'es2021',
},
],
output: { target: 'node' },
});
26 changes: 26 additions & 0 deletions packages/create-rslib/template-node-dual-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "rslib-node-dual-ts",
"version": "0.0.0",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "rslib build",
"dev": "rslib build --watch"
},
"devDependencies": {
"@rslib/core": "workspace:*",
"typescript": "^5.6.3"
}
}
16 changes: 16 additions & 0 deletions packages/create-rslib/template-node-dual-ts/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
{
format: 'esm',
syntax: 'es2021',
dts: {},
},
{
format: 'cjs',
syntax: 'es2021',
},
],
output: { target: 'node' },
});
21 changes: 21 additions & 0 deletions packages/create-rslib/template-node-esm-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "rslib-node-esm-js",
"version": "0.0.0",
"type": "module",
"exports": {
".": {
"import": "./dist/index.js"
}
},
"module": "./dist/index.js",
"files": [
"dist"
],
"scripts": {
"build": "rslib build",
"dev": "rslib build --watch"
},
"devDependencies": {
"@rslib/core": "workspace:*"
}
}
11 changes: 11 additions & 0 deletions packages/create-rslib/template-node-esm-js/rslib.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
{
format: 'esm',
syntax: 'es2021',
},
],
output: { target: 'node' },
});
3 changes: 3 additions & 0 deletions packages/create-rslib/template-node-esm-js/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function example() {
return 'hello world';
}
24 changes: 24 additions & 0 deletions packages/create-rslib/template-node-esm-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "rslib-node-esm-ts",
"version": "0.0.0",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "rslib build",
"dev": "rslib build --watch"
},
"devDependencies": {
"@rslib/core": "workspace:*",
"typescript": "^5.6.3"
}
}
12 changes: 12 additions & 0 deletions packages/create-rslib/template-node-esm-ts/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
{
format: 'esm',
syntax: 'es2021',
dts: {},
},
],
output: { target: 'node' },
});
3 changes: 3 additions & 0 deletions packages/create-rslib/template-node-esm-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function example(): string {
return 'hello world';
}
16 changes: 16 additions & 0 deletions packages/create-rslib/template-node-esm-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "ES2021",
"lib": ["DOM", "ES2021"],
"module": "ESNext",
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"isolatedModules": true,
"resolveJsonModule": true,
"moduleResolution": "bundler",
"useDefineForClassFields": true,
"allowImportingTsExtensions": true
},
"include": ["src"]
}
28 changes: 18 additions & 10 deletions packages/create-rslib/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@ import { join } from 'node:path';
import { expect, test } from 'vitest';
import { createAndValidate } from './helper';

test('should create example-js project as expected', async () => {
createAndValidate(__dirname, 'example-js');
test('should create node-dual-js project as expected', async () => {
createAndValidate(__dirname, 'node-dual-js');
});

test('should create example-ts project as expected', async () => {
createAndValidate(__dirname, 'example-ts');
test('should create node-dual-ts project as expected', async () => {
createAndValidate(__dirname, 'node-dual-ts');
});

test('should create node-esm-js project as expected', async () => {
createAndValidate(__dirname, 'node-esm-js');
});

test('should create node-esm-ts project as expected', async () => {
createAndValidate(__dirname, 'node-esm-ts');
});

test('should allow to create project in sub dir', async () => {
createAndValidate(__dirname, 'example', {
createAndValidate(__dirname, 'node-esm-js', {
name: 'test-temp-dir/rslib-project',
});
});

test('should allow to create project in relative dir', async () => {
createAndValidate(__dirname, 'example', {
createAndValidate(__dirname, 'node-esm-js', {
name: './test-temp-relative-dir',
});
});

test('should create project with eslint as expected', async () => {
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'example', {
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'node-esm-js', {
name: 'test-temp-eslint',
tools: ['eslint'],
clean: false,
Expand All @@ -35,7 +43,7 @@ test('should create project with eslint as expected', async () => {
});

test('should create project with prettier as expected', async () => {
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'example', {
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'node-esm-js', {
name: 'test-temp-prettier',
tools: ['prettier'],
clean: false,
Expand All @@ -46,7 +54,7 @@ test('should create project with prettier as expected', async () => {
});

test('should create project with eslint and prettier as expected', async () => {
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'example', {
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'node-esm-js', {
name: 'test-temp-eslint-prettier',
tools: ['eslint', 'prettier'],
clean: false,
Expand All @@ -59,7 +67,7 @@ test('should create project with eslint and prettier as expected', async () => {
});

test('should create project with biome as expected', async () => {
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'example', {
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'node-esm-js', {
name: 'test-temp-eslint',
tools: ['biome'],
clean: false,
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions website/docs/en/guide/start/quick-start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ import { PackageManagerTabs } from '@theme';

Then follow the prompts to complete the operation.

### Templates

When creating a project, you can choose from the following templates provided by `create-rslib`:

| Template | Description |
| ------------ | -------------------------------------------------- |
| node-dual-js | Node.js dual ESM/CJS package |
| node-dual-ts | Node.js dual ESM/CJS package written in TypeScript |
| node-esm-js | Node.js pure ESM package |
| node-esm-ts | Node.js pure ESM package written in TypeScript |

### Optional Tools

`create-rslib` can help you set up some commonly used tools, including [Biome](https://github.com/biomejs/biome), [ESLint](https://github.com/eslint/eslint), and [prettier](https://github.com/prettier/prettier). You can use the arrow keys and the space bar to make your selections. If you don't need these tools, you can simply press Enter to skip.
Expand Down
Loading