diff --git a/index.js b/index.js index 2464300..d122ca2 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ const typescriptExtensionRegex = /\.tsx?$/ const errorTypes = { TYPESCRIPT_AND_TSIFY: 'TYPESCRIPT_AND_TSIFY', TYPESCRIPT_NONEXISTENT: 'TYPESCRIPT_NONEXISTENT', + TYPESCRIPT_NOT_CONFIGURED: 'TYPESCRIPT_NOT_CONFIGURED', TYPESCRIPT_NOT_STRING: 'TYPESCRIPT_NOT_STRING', } @@ -193,6 +194,15 @@ const preprocessor = (options = {}) => { const browserifyOptions = await getBrowserifyOptions(filePath, options.browserifyOptions, options.typescript) const watchifyOptions = Object.assign({}, defaultOptions.watchifyOptions, options.watchifyOptions) + if (!options.typescript && typescriptExtensionRegex.test(filePath)) { + throwError({ + type: errorTypes.TYPESCRIPT_NOT_CONFIGURED, + message: `You are attempting to preprocess a TypeScript file, but do not have TypeScript configured. Pass the 'typescript' option to enable TypeScript support. + +The file: ${filePath}`, + }) + } + const bundler = browserify(browserifyOptions) if (file.shouldWatch) { diff --git a/test/unit/index_spec.js b/test/unit/index_spec.js index dce6acc..0d705cf 100644 --- a/test/unit/index_spec.js +++ b/test/unit/index_spec.js @@ -486,6 +486,32 @@ describe('browserify preprocessor', function () { 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`) }) }) + + it('throws error when processing .ts file and typescript option is not set', function () { + this.options.typescript = undefined + this.file.filePath = 'path/to/file.ts' + + return this.run() + .then(shouldntResolve) + .catch((err) => { + verifyErrorIncludesPrefix(err) + expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_NOT_CONFIGURED) + 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`) + expect(err.message).to.include('path/to/file.ts') + }) + }) + + it('throws error when processing .tsx file and typescript option is not set', function () { + this.options.typescript = undefined + this.file.filePath = 'path/to/file.tsx' + + return this.run() + .then(shouldntResolve) + .catch((err) => { + verifyErrorIncludesPrefix(err) + expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_NOT_CONFIGURED) + 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`) + expect(err.message).to.include('path/to/file.tsx') }) }) })