Skip to content

Commit 1b33393

Browse files
authored
Added speculative test of allowJsImportTypes scenario (should initially fail until fix is applied) (#590)
* Added speculative test of allowJsImportTypes scenario * correct test to reference entry * added noEmitOnError to trigger error * no longer depends on @types/jasmine so can be made to run back with 1.8.2 * go for a 1.8.2 friendly tsconfig.json * Resolved filenames that are definition file from node_modules are treated as isExternalLibraryImport Suggested by @bsouthga - let's see if this fixes our broken test. * match backslashes or forward slashes * Just run test against 2.1+
1 parent beaa2b2 commit 1b33393

File tree

10 files changed

+158
-1
lines changed

10 files changed

+158
-1
lines changed

src/servicesHost.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ function resolveModuleName(
152152
isExternalLibraryImport: tsResolution.resolvedModule.isExternalLibraryImport
153153
};
154154
if (resolutionResult!) {
155-
if (resolutionResult!.resolvedFileName === tsResolutionResult.resolvedFileName) {
155+
if (resolutionResult!.resolvedFileName === tsResolutionResult.resolvedFileName ||
156+
/node_modules(\\|\/).*\.d\.ts$/.test(tsResolutionResult.resolvedFileName)) {
156157
resolutionResult!.isExternalLibraryImport = tsResolutionResult.isExternalLibraryImport;
157158
}
158159
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This test confirms that you can import typings from an npm module when `allowJs` is true. See details [#586](https://github.com/TypeStrong/ts-loader/issues/586).
2+
3+
This test is roughly based on https://github.com/bsouthga/ts-loader-types-error-example
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint-disable no-var, strict */
2+
'use strict';
3+
var webpackConfig = require('./webpack.config.js');
4+
5+
module.exports = function(config) {
6+
// Documentation: https://karma-runner.github.io/0.13/config/configuration-file.html
7+
config.set({
8+
browsers: [ 'PhantomJS' ],
9+
10+
files: [
11+
// This ensures we have the es6 shims in place and then loads all the tests
12+
'main.js'
13+
],
14+
15+
port: 9876,
16+
17+
frameworks: [ 'jasmine' ],
18+
19+
logLevel: config.LOG_INFO, //config.LOG_DEBUG
20+
21+
preprocessors: {
22+
'main.js': [ 'webpack', 'sourcemap' ]
23+
},
24+
25+
webpack: {
26+
devtool: 'inline-source-map',
27+
module: webpackConfig.module,
28+
resolve: webpackConfig.resolve,
29+
30+
// for test harness purposes only, you would not need this in a normal project
31+
resolveLoader: webpackConfig.resolveLoader
32+
},
33+
34+
webpackMiddleware: {
35+
quiet: true,
36+
stats: {
37+
colors: true
38+
}
39+
},
40+
41+
// reporter options
42+
mochaReporter: {
43+
colors: {
44+
success: 'bgGreen',
45+
info: 'cyan',
46+
warning: 'bgBlue',
47+
error: 'bgRed'
48+
}
49+
}
50+
});
51+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import 'babel-polyfill';
2+
3+
const testsContext = require.context('./', true, /\.tests\.js$/);
4+
testsContext.keys().forEach(testsContext);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "allow-js-import-types",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"devDependencies": {
6+
"babel": "^6.0.0",
7+
"babel-core": "^6.0.0",
8+
"babel-loader": "^7.0.0",
9+
"babel-preset-es2015": "^6.0.0",
10+
"jasmine-core": "^2.3.4"
11+
},
12+
"dependencies": {
13+
"babel-polyfill": "^6.0.0",
14+
"event-rank": "^0.0.10"
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import EventRank from './imported';
2+
3+
export function getEventRank() {
4+
return EventRank;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { EventRank } from 'event-rank';
2+
3+
export default EventRank;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var entry = require('../src/entry');
2+
3+
describe("entry", function() {
4+
it("getEventRank produces something", function() {
5+
var EventRank = entry.getEventRank();
6+
expect(EventRank).not.toBeUndefined();
7+
});
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"module": "es2015",
5+
"moduleResolution": "node",
6+
"noEmitOnError": true,
7+
"noImplicitAny": false,
8+
"sourceMap": true,
9+
"target": "es2015"
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* eslint-disable no-var, strict, prefer-arrow-callback */
2+
'use strict';
3+
4+
var path = require('path');
5+
var webpack = require('webpack');
6+
7+
var babelOptions = {
8+
"presets": [
9+
[
10+
"es2015",
11+
{
12+
"modules": false
13+
}
14+
]
15+
]
16+
};
17+
18+
module.exports = {
19+
entry: './src/entry.js',
20+
output: {
21+
filename: 'bundle.js'
22+
},
23+
module: {
24+
rules: [{
25+
test: /\.ts(x?)$/,
26+
exclude: /node_modules/,
27+
use: [
28+
{
29+
loader: 'babel-loader',
30+
options: babelOptions
31+
},
32+
{
33+
loader: 'ts-loader',
34+
options: { entryFileIsJs: true }
35+
}
36+
]
37+
}, {
38+
test: /\.js$/,
39+
exclude: /node_modules/,
40+
use: [
41+
{
42+
loader: 'babel-loader',
43+
options: babelOptions
44+
}
45+
]
46+
}]
47+
},
48+
resolve: {
49+
// Add `.ts` and `.tsx` as a resolvable extension.
50+
extensions: ['.ts', '.tsx', '.js']
51+
},
52+
};
53+
54+
// for test harness purposes only, you would not need this in a normal project
55+
module.exports.resolveLoader = { alias: { 'ts-loader': path.join(__dirname, "../../../index.js") } }

0 commit comments

Comments
 (0)