Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit 36d77a8

Browse files
committed
error if attempting to preprocess typescript file but typescript option is not set
1 parent 772548d commit 36d77a8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const typescriptExtensionRegex = /\.tsx?$/
1414
const errorTypes = {
1515
TYPESCRIPT_AND_TSIFY: 'TYPESCRIPT_AND_TSIFY',
1616
TYPESCRIPT_NONEXISTENT: 'TYPESCRIPT_NONEXISTENT',
17+
TYPESCRIPT_NOT_CONFIGURED: 'TYPESCRIPT_NOT_CONFIGURED',
1718
TYPESCRIPT_NOT_STRING: 'TYPESCRIPT_NOT_STRING',
1819
}
1920

@@ -193,6 +194,15 @@ const preprocessor = (options = {}) => {
193194
const browserifyOptions = await getBrowserifyOptions(filePath, options.browserifyOptions, options.typescript)
194195
const watchifyOptions = Object.assign({}, defaultOptions.watchifyOptions, options.watchifyOptions)
195196

197+
if (!options.typescript && typescriptExtensionRegex.test(filePath)) {
198+
throwError({
199+
type: errorTypes.TYPESCRIPT_NOT_CONFIGURED,
200+
message: `You are attempting to preprocess a TypeScript file, but do not have TypeScript configured. Pass the 'typescript' option to enable TypeScript support.
201+
202+
The file: ${filePath}`,
203+
})
204+
}
205+
196206
const bundler = browserify(browserifyOptions)
197207

198208
if (file.shouldWatch) {

test/unit/index_spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,32 @@ describe('browserify preprocessor', function () {
486486
expect(err.message).to.include(`It looks like you passed the 'typescript' option and also specified a browserify transform for TypeScript. This may cause conflicts`)
487487
})
488488
})
489+
490+
it('throws error when processing .ts file and typescript option is not set', function () {
491+
this.options.typescript = undefined
492+
this.file.filePath = 'path/to/file.ts'
493+
494+
return this.run()
495+
.then(shouldntResolve)
496+
.catch((err) => {
497+
verifyErrorIncludesPrefix(err)
498+
expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_NOT_CONFIGURED)
499+
expect(err.message).to.include(`You are attempting to preprocess a TypeScript file, but do not have TypeScript configured. Pass the 'typescript' option to enable TypeScript support`)
500+
expect(err.message).to.include('path/to/file.ts')
501+
})
502+
})
503+
504+
it('throws error when processing .tsx file and typescript option is not set', function () {
505+
this.options.typescript = undefined
506+
this.file.filePath = 'path/to/file.tsx'
507+
508+
return this.run()
509+
.then(shouldntResolve)
510+
.catch((err) => {
511+
verifyErrorIncludesPrefix(err)
512+
expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_NOT_CONFIGURED)
513+
expect(err.message).to.include(`You are attempting to preprocess a TypeScript file, but do not have TypeScript configured. Pass the 'typescript' option to enable TypeScript support`)
514+
expect(err.message).to.include('path/to/file.tsx')
489515
})
490516
})
491517
})

0 commit comments

Comments
 (0)