Skip to content

Commit

Permalink
feat(template): configure automatic React transform for TypeScript
Browse files Browse the repository at this point in the history
React no longer has to be imported. Also, adapt exports and make paths relative. Add react-typescript template. release-npm

BREAKING CHANGE: React now uses automatic transform when using TypeScript. "exports" now prefixed with package namespace.
  • Loading branch information
tobua committed Feb 18, 2024
1 parent 5adcc1b commit 7eb0210
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Alternatively, you can start with various templates:
npm init now padua ./my-plugin [template]
```

Replace `[template]` with one of the following available templates: default, node, react, typescript, test or cli.
Replace `[template]` with one of the following available templates: default, node, react, typescript, react-typescript, test or cli.

## Usage

Expand Down
28 changes: 21 additions & 7 deletions configuration/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,23 @@ const switchable = (pkg) => {
}

if (options.typescript && !pkg.types) {
pkg.types = `${options.output}/index.d.ts`
pkg.types = `./${options.output}/index.d.ts`
} else if (options.source && existsSync(join(getProjectBasePath(), 'index.d.ts'))) {
pkg.types = 'index.d.ts'
pkg.types = './index.d.ts'
} else if (options.source && pkg.types && !existsSync(join(getProjectBasePath(), pkg.types))) {
delete pkg.types
}

if (options.source) {
if (pkg.main && !existsSync(join(getProjectBasePath(), pkg.main))) {
pkg.main = `${options.entry[0]}`
pkg.main = `./${options.entry[0]}`
}

if (!pkg.exports && !pkg.bin) {
pkg.exports = {
default: `./${pkg.main}`,
'.': {
default: `./index.js`,
},
}
}

Expand All @@ -115,11 +117,23 @@ const switchable = (pkg) => {
pkg.scripts.build = 'padua build'
}
if (!pkg.main) {
pkg.main = `${options.output}/index.js`
pkg.main = `./${options.output}/index.js`
}
if (!pkg.exports) {
pkg.exports = {
default: `./${pkg.main}`,
if (options.typescript) {
pkg.exports = {
'.': {
types: `./${options.output}/index.d.ts`,
// "default" condition must be last with most bundlers.
default: `./${options.output}/${pkg.main}`,
},
}
} else {
pkg.exports = {
'.': {
default: `./${options.output}/${pkg.main}`,
},
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion configuration/tsconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const tsconfig = (tsconfigUserOverrides = {}) => {
}

if (options.react) {
packageTSConfig.compilerOptions.jsx = 'react'
packageTSConfig.compilerOptions.jsx = 'react-jsx'
}

userTSConfig = merge(userTSConfig, tsconfigUserOverrides)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"source": "index.js",
"devDependencies": {
"jest-fixture": "^4.1.0",
"vitest": "^1.2.2"
"vitest": "^1.3.0"
},
"files": [
"configuration",
Expand Down
3 changes: 3 additions & 0 deletions template/react-typescript/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function App() {
return <p>Hello World</p>
}
14 changes: 14 additions & 0 deletions template/react-typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "<%= name %>",
"description": "<%= description %>",
"version": "0.0.0",
"devDependencies": {
"@types/react": "^18.2.56",
"@types/react-dom": "^18.2.19",
"padua": "latest"
},
"peerDependencies": {
"react": ">= 18",
"react-dom": ">= 18"
}
}
10 changes: 10 additions & 0 deletions template/react-typescript/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"prompts": [
{
"name": "name"
},
{
"name": "description"
}
]
}
6 changes: 3 additions & 3 deletions template/react/index.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import React from 'react'

export default () => <p>Hello World</p>
export default function App() {
return <p>Hello World</p>
}
8 changes: 4 additions & 4 deletions template/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "<%= name %>",
"description": "<%= description %>",
"version": "0.0.0",
"peerDependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"padua": "latest"
},
"peerDependencies": {
"react": ">= 18",
"react-dom": ">= 18"
}
}
41 changes: 29 additions & 12 deletions test/configuration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('Generates gitignore with default entries.', () => {
const contents = readFile('.gitignore')

expect(contents).toEqual(
['node_modules', 'package-lock.json', 'jsconfig.json', 'dist', ''].join('\r\n')
['node_modules', 'package-lock.json', 'jsconfig.json', 'dist', ''].join('\r\n'),
)
})

Expand All @@ -45,7 +45,7 @@ test('Generates proper gitignore for typescript.', () => {
const contents = readFile('.gitignore')

expect(contents).toEqual(
['node_modules', 'package-lock.json', 'tsconfig.json', 'dist', ''].join('\r\n')
['node_modules', 'package-lock.json', 'tsconfig.json', 'dist', ''].join('\r\n'),
)
})

Expand Down Expand Up @@ -181,6 +181,8 @@ test('Does not override configuration changes made by user after initial install
expect(pkg.sideEffects).toBe(true)
expect(pkg.scripts.start).toBe('my-own-script')
expect(pkg.type).toBe(undefined)
expect(pkg.main).toBe('./dist/index.js')
expect(pkg.exports['.'].types).not.toBeDefined()
})

test('eslintConfig extended when switching to source mode.', async () => {
Expand Down Expand Up @@ -232,7 +234,7 @@ test('Type definitions will be added in source mode as soon as available.', asyn

pkg = readFile('package.json')

expect(pkg.types).toBe('index.d.ts')
expect(pkg.types).toBe('./index.d.ts')

unlinkSync(join(process.cwd(), 'index.d.ts'))

Expand Down Expand Up @@ -270,6 +272,21 @@ test('Source entry will not be added again if removed by user.', async () => {
expect(pkg.source).not.toBeDefined()
})

test('Types will be added.', async () => {
prepare([packageJson('typescript'), file('index.tsx', '')])

await writePackageJson()

const pkg = readFile('package.json')

expect(pkg.jest).not.toBeDefined()
// "default" must be last.
expect(Object.keys(pkg.exports['.'])[0]).toBe('types')
expect(pkg.main).toBe('./dist/index.js')
expect(pkg.types).toBe('./dist/index.d.ts')
expect(pkg.files).toEqual(['dist'])
})

test('Files array is only changed initially.', async () => {
prepare([
packageJson('source', {
Expand Down Expand Up @@ -431,15 +448,15 @@ test('Ignores are written to all configuration files.', async () => {
file('test/basic.test.js', ''),
file(
'node_modules/padua/configuration/.prettierignore',
readFile('../../../configuration/.prettierignore')
readFile('../../../configuration/.prettierignore'),
),
file(
'node_modules/padua/configuration/eslint.cjs',
readFile('../../../configuration/eslint.cjs')
readFile('../../../configuration/eslint.cjs'),
),
file(
'node_modules/padua/configuration/stylelint.cjs',
readFile('../../../configuration/stylelint.cjs')
readFile('../../../configuration/stylelint.cjs'),
),
])

Expand Down Expand Up @@ -533,15 +550,15 @@ test('Proper ignores added when values are empty.', async () => {
file('test/basic.test.js', ''),
file(
'node_modules/padua/configuration/.prettierignore',
readFile('../../../configuration/.prettierignore')
readFile('../../../configuration/.prettierignore'),
),
file(
'node_modules/padua/configuration/eslint.cjs',
readFile('../../../configuration/eslint.cjs')
readFile('../../../configuration/eslint.cjs'),
),
file(
'node_modules/padua/configuration/stylelint.cjs',
readFile('../../../configuration/stylelint.cjs')
readFile('../../../configuration/stylelint.cjs'),
),
])

Expand Down Expand Up @@ -587,15 +604,15 @@ test('Ignores work with all possible configurations.', async () => {
file('test/basic.test.js', ''),
file(
'node_modules/padua/configuration/.prettierignore',
readFile('../../../configuration/.prettierignore')
readFile('../../../configuration/.prettierignore'),
),
file(
'node_modules/padua/configuration/eslint.cjs',
readFile('../../../configuration/eslint.cjs')
readFile('../../../configuration/eslint.cjs'),
),
file(
'node_modules/padua/configuration/stylelint.cjs',
readFile('../../../configuration/stylelint.cjs')
readFile('../../../configuration/stylelint.cjs'),
),
])

Expand Down

0 comments on commit 7eb0210

Please sign in to comment.