-
-
Notifications
You must be signed in to change notification settings - Fork 433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implements a custom resolveModuleName option #862
Changes from 3 commits
6367073
46d721a
f2f6026
289d904
9a0540a
0bc78bd
8b694a8
afa570d
ad4ed7b
d8f3735
bee1c37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {HelloWorld} from 'foo'; | ||
|
||
export type HelloBuilder = (hello: number, world: number) => HelloWorld; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "bar", | ||
"version": "1.0.0", | ||
"typings": "./index.d.ts" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type HelloWorld = { | ||
hello: number, | ||
world: number, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "foo", | ||
"version": "1.0.0", | ||
"typings": "./index.d.ts" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint-disable no-var, strict */ | ||
'use strict'; | ||
var path = require('path'); | ||
var webpack = require('webpack'); | ||
var webpackConfig = require('./webpack.config.js'); | ||
var reporterOptions = require('../../reporterOptions'); | ||
|
||
module.exports = function(config) { | ||
config.set({ | ||
browsers: [ 'ChromeHeadless' ], | ||
|
||
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', | ||
mode: webpackConfig.mode, | ||
module: webpackConfig.module, | ||
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: reporterOptions | ||
}); | ||
}; |
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); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "basic", | ||
"license": "MIT", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"devDependencies": { | ||
"@types/jasmine": "^2.5.35", | ||
"jasmine-core": "^2.3.4" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {HelloBuilder} from 'bar'; | ||
|
||
export const makeHello: HelloBuilder = (hello: number, world: number) => ({ | ||
hello, | ||
world, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as app from '../src/app'; | ||
|
||
// We don't actually care about the result of the following operations; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not true actually; we do care about the result of the following operations! Can we amend the comment to make test intent clearer please? We care both about type resolution and that the code executes... |
||
// what we care about is typescript resolving json as modules | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you amend the comment to reflect what the intention of the test is please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left the comment because it's actually the same intent - we don't care about what is being built, just about TS being able to make the resolution There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see a "json" is in the comment - will change it 👍 |
||
// allowing this code to compile without errors, hence this: | ||
// "noEmitOnError": true | ||
// in tsconfig.json | ||
describe("app", () => { | ||
it("makeHello to exist", () => { | ||
expect(!!app.makeHello).toBe(true); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"noEmitOnError": true, | ||
"resolveJsonModule": true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure 👍 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var path = require('path') | ||
|
||
module.exports = { | ||
mode: 'development', | ||
entry: './app.ts', | ||
output: { | ||
filename: 'bundle.js' | ||
}, | ||
resolve: { | ||
extensions: ['.ts', '.js'] | ||
}, | ||
module: { | ||
rules: [ | ||
{ test: /\.ts$/, loader: 'ts-loader', options: { | ||
resolveModuleName: (moduleName, containingFile, compilerOptions, compilerHost, parentResolver) => { | ||
switch (moduleName) { | ||
case 'foo': return parentResolver(path.join(__dirname, 'foo-pkg'), containingFile, compilerOptions, compilerHost); | ||
case 'bar': return parentResolver(path.join(__dirname, 'bar-pkg'), containingFile, compilerOptions, compilerHost); | ||
default: return parentResolver(moduleName, containingFile, compilerOptions, compilerHost); | ||
} | ||
}, | ||
} } | ||
] | ||
} | ||
} | ||
|
||
// for test harness purposes only, you would not need this in a normal project | ||
module.exports.resolveLoader = { alias: { 'ts-loader': path.join(__dirname, "../../../index.js") } } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
"@types/jasmine@^2.5.35": | ||
version "2.8.5" | ||
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.8.5.tgz#96e58872583fa80c7ea0dd29024b180d5e133678" | ||
|
||
jasmine-core@^2.3.4: | ||
version "2.9.1" | ||
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-2.9.1.tgz#b6bbc1d8e65250d56f5888461705ebeeeb88f22f" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hadn't expected the
.bind(null, compiler)
would be necessary... I'm guessing it is? What happens without it? Presumably some kind ofthis
issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we simply need the
compiler
variable to be able to callcompiler.resolveModuleName
(andcompiler
is obtained frominstance
, which isn't a global)