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

feat(dts): support bundle dts #573

Merged
merged 2 commits into from
Aug 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
5 changes: 4 additions & 1 deletion src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function parseCliArgs(argv: string[]) {
'--no-clean': Boolean,
'--prepare': Boolean,
'--tsconfig': String,
'--dts-bundle': Boolean,

'-h': '--help',
'-v': '--version',
Expand All @@ -90,6 +91,7 @@ function parseCliArgs(argv: string[]) {
sourcemap: !!args['--sourcemap'],
cwd: args['--cwd'],
dts: args['--no-dts'] ? false : undefined,
dtsBundle: args['--dts-bundle'],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of introducing a new option, we can use --no-external to achieve it.
If we bundle all the dependencies, then we bundle all the types to align as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per rollup-plugin-dts, enabling bundle dts is not recommended. So IMHO we should split this into a separate option.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea but I still believe users want to have types as bundled instead of relying on separate package when they specify --no-external.

I want to support types bundling for that case too. if types not working they can have a way to opt-out types bundling. I'll leave these items for later. Current approach looks good! Thanks

help: args['--help'],
version: args['--version'],
runtime: args['--runtime'],
Expand Down Expand Up @@ -118,14 +120,15 @@ async function run(args: CliArgs) {
target,
runtime,
dts,
dtsBundle,
env,
clean,
tsconfig,
} = args
const cwd = args.cwd || process.cwd()
const file = args.file ? path.resolve(cwd, args.file) : undefined
const bundleConfig: BundleConfig = {
dts,
dts: dts !== false && { respectExternal: dtsBundle ? true : undefined },
file,
format,
cwd,
Expand Down
3 changes: 3 additions & 0 deletions src/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const swcMinifyOptions = {
async function createDtsPlugin(
tsCompilerOptions: BuildContext['tsOptions']['tsCompilerOptions'],
tsConfigPath: string | undefined,
respectExternal: boolean | undefined,
cwd: string,
) {
const enableIncrementalWithoutBuildInfo =
Expand Down Expand Up @@ -114,6 +115,7 @@ async function createDtsPlugin(
).default({
tsconfig: tsConfigPath,
compilerOptions: overrideResolvedTsOptions,
respectExternal,
})

return dtsPlugin
Expand Down Expand Up @@ -262,6 +264,7 @@ async function buildInputConfig(
const dtsPlugin = await memoizeDtsPluginByKey(uniqueProcessId)(
tsCompilerOptions,
tsConfigPath,
bundleConfig.dts && bundleConfig.dts.respectExternal,
cwd,
)
typesPlugins.push(dtsPlugin)
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type BundleConfig = {
sourcemap?: boolean
external?: string[] | null
env?: string[]
dts?: boolean
dts?: { respectExternal?: boolean } | false
runtime?: string
pkg?: PackageMetadata
clean?: boolean
Expand Down Expand Up @@ -93,6 +93,7 @@ type CliArgs = {
env?: string
external?: string
dts?: false
dtsBundle?: boolean
runtime?: string
prepare?: boolean
clean?: boolean
Expand Down
5 changes: 5 additions & 0 deletions test/cli/dts-bundle/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ParserConfig } from '@swc/types'

export const createParser = (options: ParserConfig) => {
// noop
}
71 changes: 71 additions & 0 deletions test/cli/dts-bundle/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import fs from 'fs'
import fsp from 'fs/promises'
import { join } from 'path'
import { createCliTest } from '../utils'
import { deleteFile } from '../../testing-utils'

describe('cli', () => {
const dir = __dirname
beforeAll(async () => {
await fsp.writeFile(
join(dir, './package.json'),
'{ "name": "prepare-ts-with-pkg-json", "devDependencies": { "@swc/types": "*" } }',
)
})
afterAll(async () => {
await deleteFile(join(dir, './package.json'))
})

it(`cli dts-bundle option should work properly`, async () => {
await createCliTest(
{
directory: __dirname,
args: ['./base.ts', '-o', 'dist/base.js', '--dts-bundle'],
},
({ code, distFile }) => {
const typeFile = distFile.replace('.js', '.d.ts')
expect(fs.existsSync(typeFile)).toBe(true)
expect(fs.readFileSync(typeFile, 'utf-8')).toContain(
'type ParserConfig =',
)
expect(code).toBe(0)
},
)
})
it(`cli dts-bundle option should not bundle dts without dts-bundle`, async () => {
await createCliTest(
{
directory: __dirname,
args: ['./base.ts', '-o', 'dist/base.js'],
},
({ code, distFile }) => {
const typeFile = distFile.replace('.js', '.d.ts')
expect(fs.existsSync(typeFile)).toBe(true)
expect(fs.readFileSync(typeFile, 'utf-8')).toContain(
"from '@swc/types';",
)
expect(code).toBe(0)
},
)
})
it(`cli dts-bundle option should not bundle dts from dependencies`, async () => {
await fsp.writeFile(
join(dir, './package.json'),
'{ "name": "prepare-ts-with-pkg-json", "dependencies": { "@swc/types": "*" } }',
)
await createCliTest(
{
directory: __dirname,
args: ['./base.ts', '-o', 'dist/base.js'],
},
({ code, distFile }) => {
const typeFile = distFile.replace('.js', '.d.ts')
expect(fs.existsSync(typeFile)).toBe(true)
expect(fs.readFileSync(typeFile, 'utf-8')).toContain(
"from '@swc/types';",
)
expect(code).toBe(0)
},
)
})
})
Loading