Skip to content

Commit 2a8c35a

Browse files
committed
Add support for manually adding additional files to the TypeScript compiler. (#6)
1 parent 9d5a3d0 commit 2a8c35a

File tree

7 files changed

+85
-11
lines changed

7 files changed

+85
-11
lines changed

index.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ interface Options {
2828
noImplicitAny: boolean;
2929
target: string;
3030
module: string;
31+
additionalFiles: string[];
3132
}
3233

3334
interface TSFile {
@@ -55,9 +56,23 @@ var instances = <TSInstances>{};
5556
function ensureTypeScriptInstance(options: Options): TSInstance {
5657

5758
var compiler = require(options.compiler);
58-
59-
if (Object.prototype.hasOwnProperty.call(instances, options.instance))
60-
return instances[options.instance]
59+
var files = <TSFiles>{};
60+
61+
if (Object.prototype.hasOwnProperty.call(instances, options.instance)) {
62+
var instance = instances[options.instance];
63+
files = instance.files;
64+
65+
options.additionalFiles.forEach(filePath => {
66+
if (!Object.prototype.hasOwnProperty.call(options.additionalFiles, filePath)) {
67+
files[filePath] = {
68+
text: fs.readFileSync(filePath, 'utf-8'),
69+
version: 0
70+
}
71+
}
72+
});
73+
74+
return instance;
75+
}
6176

6277
var target: typescript.ScriptTarget;
6378
switch (options.target) {
@@ -72,14 +87,15 @@ function ensureTypeScriptInstance(options: Options): TSInstance {
7287
sourceMap: !!options.sourceMap,
7388
noImplicitAny: !!options.noImplicitAny
7489
}
75-
76-
var files = <TSFiles>{};
7790

78-
var libPath = path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts');
79-
files[libPath] = {
80-
text: fs.readFileSync(libPath, 'utf-8'),
81-
version: 0
82-
}
91+
options.additionalFiles.push(path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts'));
92+
93+
options.additionalFiles.forEach(filePath => {
94+
files[filePath] = {
95+
text: fs.readFileSync(filePath, 'utf-8'),
96+
version: 0
97+
}
98+
});
8399

84100
var servicesHost = {
85101
getScriptFileNames: () => Object.keys(files),
@@ -122,9 +138,12 @@ function loader(contents) {
122138
options = objectAssign<Options>({}, {
123139
instance: 'default',
124140
compiler: 'typescript',
125-
sourceMap: false
141+
sourceMap: false,
142+
additionalFiles: []
126143
}, options);
127144

145+
options.additionalFiles = options.additionalFiles.map(filePath => path.resolve(this.context, filePath));
146+
128147
var instance = ensureTypeScriptInstance(options);
129148

130149
if (!Object.prototype.hasOwnProperty.call(instance.files, filePath)) {

test/additionalFiles/app.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import submodule = require('./submodule/submodule');
2+
3+
console.log(submodule);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module externalLib {
2+
export function doSomething(arg: any): void;
3+
}
4+
5+
declare module 'externalLib' {
6+
export = externalLib
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
doSomething: function() { }
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import externalLib = require('externalLib');
2+
var message = "Hello from submodule"
3+
4+
externalLib.doSomething(message);
5+
6+
export = message
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var path = require('path')
2+
3+
var tsLoaderOptions = {
4+
instance: 'additionalFiles',
5+
additionalFiles: [
6+
path.join(__dirname, "./lib/externalLib.d.ts")
7+
]
8+
}
9+
10+
module.exports = {
11+
context: __dirname,
12+
entry: './app.ts',
13+
output: {
14+
path: __dirname,
15+
filename: 'bundle.js'
16+
},
17+
resolve: {
18+
alias: { externalLib: path.join(__dirname, "./lib/externalLib.js") },
19+
extensions: ['', '.js', '.ts']
20+
},
21+
module: {
22+
loaders: [
23+
{ test: /\.ts$/, loader: "../../index.js?"+JSON.stringify(tsLoaderOptions) }
24+
]
25+
}
26+
}

test/run.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ function assertModuleCount(stats, expectedNumberOfModules) {
3030
assert.equal(statsJson.modules.length, expectedNumberOfModules, 'wrong number of modules found')
3131
}
3232

33+
describe('additionalFiles', function() {
34+
it('should not error', function(done) {
35+
webpack(require('./additionalFiles/webpack.config')).run(function(err, stats) {
36+
if (!handleErrors(err, stats, done)) {
37+
done()
38+
}
39+
})
40+
})
41+
})
42+
3343
describe('basic', function() {
3444

3545
it('should have the correct output', function(done) {

0 commit comments

Comments
 (0)