Skip to content

Commit

Permalink
Merge pull request #320 from johnnyreilly/master
Browse files Browse the repository at this point in the history
speculative allowJs support
  • Loading branch information
johnnyreilly authored Oct 16, 2016
2 parents 11641b6 + 2aac318 commit 6e298e4
Show file tree
Hide file tree
Showing 24 changed files with 124 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ install:
env:
- [email protected]
- [email protected]
- [email protected].0
- [email protected].2
- [email protected]
- TYPESCRIPT=typescript@next
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.9.3

- [Added support for allowJs](https://github.com/TypeStrong/ts-loader/pull/320) (#316)

## v0.9.2

- [Added support for @types](https://github.com/TypeStrong/ts-loader/pull/318) (#247)
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ environment:
matrix:
- TYPESCRIPT: [email protected]
- TYPESCRIPT: [email protected]
- TYPESCRIPT: [email protected].0
- TYPESCRIPT: [email protected].2
- TYPESCRIPT: [email protected]
- TYPESCRIPT: typescript@next
install:
Expand Down
11 changes: 9 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ interface TSCompatibleCompiler {

var instances = <TSInstances>{};
var webpackInstances = [];
const scriptRegex = /\.tsx?$/i;
let scriptRegex = /\.tsx?$/i;

// Take TypeScript errors, parse them and format to webpack errors
// Optionally adds a file name
Expand Down Expand Up @@ -199,6 +199,8 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
};

var compilerOptions: typescript.CompilerOptions = {
skipDefaultLibCheck: true,
suppressOutputPathCheck: true // This is why: https://github.com/Microsoft/TypeScript/issues/7363
};

// Load any available tsconfig.json file
Expand Down Expand Up @@ -244,6 +246,11 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
configFile.config.compilerOptions.isolatedModules = true;
}

// if allowJs is set then we should accept js(x) files
if (configFile.config.compilerOptions.allowJs) {
scriptRegex = /\.tsx?$|\.jsx?$/i;
}

var configParseResult;
if (typeof (<any>compiler).parseJsonConfigFileContent === 'function') {
// parseConfigFile was renamed between 1.6.2 and 1.7
Expand Down Expand Up @@ -390,7 +397,7 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
try {
resolvedFileName = resolver.resolveSync(path.normalize(path.dirname(containingFile)), moduleName)

if (!resolvedFileName.match(/\.tsx?$/)) resolvedFileName = null;
if (!resolvedFileName.match(scriptRegex)) resolvedFileName = null;
else resolutionResult = { resolvedFileName };
}
catch (e) { resolvedFileName = null }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-loader",
"version": "0.9.2",
"version": "0.9.3",
"description": "TypeScript loader for webpack",
"main": "index.js",
"scripts": {
Expand Down
55 changes: 55 additions & 0 deletions test/execution-tests/1.8.2_allowJs/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* eslint-disable no-var, strict */
'use strict';
var path = require('path');
var webpack = require('webpack');
var webpackConfig = require('./webpack.config.js');

module.exports = function(config) {
config.set({
browsers: [ 'PhantomJS' ],

files: [
// This loads all the tests
'main.js'
],

port: 9876,

frameworks: [ 'jasmine' ],

logLevel: config.LOG_INFO, //config.LOG_DEBUG

preprocessors: {
'main.js': [ 'webpack', 'sourcemap' ]
},

webpack: {
devtool: 'inline-source-map',
debug: true,
module: {
loaders: webpackConfig.module.loaders
},
resolve: webpackConfig.resolve,

// for test harness purposes only, you would not need this in a normal project
resolveLoader: webpackConfig.resolveLoader
},

webpackMiddleware: {
quiet: true,
stats: {
colors: true
}
},

// reporter options
mochaReporter: {
colors: {
success: 'bgGreen',
info: 'cyan',
warning: 'bgBlue',
error: 'bgRed'
}
}
});
};
2 changes: 2 additions & 0 deletions test/execution-tests/1.8.2_allowJs/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const testsContext = require.context('./', true, /\.tests\.ts(x?)$/);
testsContext.keys().forEach(testsContext);
3 changes: 3 additions & 0 deletions test/execution-tests/1.8.2_allowJs/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var dep = require('./dep');

dep('');
5 changes: 5 additions & 0 deletions test/execution-tests/1.8.2_allowJs/src/deeperDep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function doSomething(input) {
return "doSomething with " + input
}

module.exports = doSomething;
3 changes: 3 additions & 0 deletions test/execution-tests/1.8.2_allowJs/src/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var dep = require('./deeperDep');

module.exports = dep;
9 changes: 9 additions & 0 deletions test/execution-tests/1.8.2_allowJs/test/app.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare function require(path:string): any;
var dep = require('../src/dep');

describe("app", function() {
it("dep can be called", function() {
var result = dep("nothing");
expect(result).toBe("doSomething with nothing");
});
});
7 changes: 7 additions & 0 deletions test/execution-tests/1.8.2_allowJs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"allowJs": true,
"moduleResolution": "node",
"noEmitOnError": true
}
}
17 changes: 17 additions & 0 deletions test/execution-tests/1.8.2_allowJs/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
entry: './src/app.ts',
output: {
filename: 'bundle.js'
},
resolve: {
extensions: ['', '.ts', '.js']
},
module: {
loaders: [
{ test: /\.ts$/, loader: 'ts-loader' }
]
}
}

// for test harness purposes only, you would not need this in a normal project
module.exports.resolveLoader = { alias: { 'ts-loader': require('path').join(__dirname, "../../../index.js") } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "simple",
"version": false,
"globalDependencies": {
"jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#fe563dff3428bac1260d1794e2c2ecf8f097535a"
}
}

0 comments on commit 6e298e4

Please sign in to comment.