Skip to content

Commit

Permalink
feat(core): expose inline and both config for sourcemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Apr 28, 2021
1 parent b05de60 commit 780f2bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import {
transformSync as swcTransformSync,
Options as SwcOptions,
ReactConfig,
Config,
} from '@swc/core'

export interface Options {
target?: 'es3' | 'es5' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019'
module?: 'commonjs' | 'umd' | 'amd' | 'es6'
sourcemap?: boolean | 'inline'
sourcemap?: Config['sourceMaps']
jsx?: boolean
experimentalDecorators?: boolean
emitDecoratorMetadata?: boolean
Expand Down
20 changes: 20 additions & 0 deletions packages/register/__test__/create-sourcemap-option.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Config } from '@swc/core'
import test from 'ava'
import { CompilerOptions } from 'typescript'

import { createSourcemapOption } from '../register'

const FIXTURES: [CompilerOptions, Config['sourceMaps']][] = [
[{ sourceMap: true, inlineSourceMap: true }, 'both'],
[{ sourceMap: false, inlineSourceMap: true }, 'inline'],
[{ sourceMap: true, inlineSourceMap: false }, true],
[{ sourceMap: false, inlineSourceMap: false }, false],
[{ inlineSourceMap: true }, 'both'],
[{ inlineSourceMap: false }, true],
]

for (const [config, expect] of FIXTURES) {
test(`should create ${expect} from ${JSON.stringify(config)}`, (t) => {
t.is(createSourcemapOption(config), expect)
})
}
13 changes: 12 additions & 1 deletion packages/register/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function compile(
const { code, map } = transformSync(sourcecode, filename, {
target: toTsTarget(options.target ?? ts.ScriptTarget.ES2018),
module: toModule(options.module ?? ts.ModuleKind.ES2015),
sourcemap: options.sourceMap !== false,
sourcemap: createSourcemapOption(options),
jsx: filename.endsWith('.tsx') || filename.endsWith('.jsx') || Boolean(options.jsx),
react:
options.jsxFactory || options.jsxFragmentFactory
Expand Down Expand Up @@ -115,3 +115,14 @@ export function register(options = readDefaultTsConfig()) {
exts: DEFAULT_EXTENSIONS,
})
}

// @internal
export function createSourcemapOption(options: ts.CompilerOptions) {
return options.sourceMap !== false
? options.inlineSourceMap
? 'both'
: true
: options.inlineSourceMap
? 'inline'
: false
}

0 comments on commit 780f2bb

Please sign in to comment.