Skip to content
This repository has been archived by the owner on Dec 1, 2019. It is now read-only.

Commit

Permalink
feat(*): allowJs works (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-panferov committed Mar 4, 2016
1 parent 7b86044 commit d7a8a64
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ tscommand
npm-debug.log
.awcache
test/output
src/test/output
33 changes: 28 additions & 5 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,38 @@ export interface IExternals {
[key: string]: string;
}

export function createResolver(externals: IExternals, webpackResolver: any): IResolver {
export type Exclude = string[];

export function createResolver(
externals: IExternals,
exclude: Exclude,
webpackResolver: any
): IResolver {
let resolver: IResolver = promisify(webpackResolver) as any;

function resolve(base: string, dep: string): Promise<string> {
if (externals && externals.hasOwnProperty(dep)) {
let inWebpackExternals = externals && externals.hasOwnProperty(dep);
let inTypeScriptExclude = false;

if ((inWebpackExternals || inTypeScriptExclude)) {
return Promise.resolve<string>('%%ignore');
} else {
return resolver(base, dep);
return resolver(base, dep).then(resultPath => {
// ignore excluded javascript
if (!resultPath.match(/.tsx?$/)) {
let matchedExcludes = exclude.filter((excl) => {
return resultPath.indexOf(excl) !== -1;
});

if (matchedExcludes.length > 0) {
return '%%ignore';
} else {
return resultPath;
}
} else {
return resultPath;
}
});
}
}

Expand Down Expand Up @@ -100,7 +124,7 @@ export class FileAnalyzer {
this.dependencies.addTypeDeclaration(importPath);
tasks.push(this.checkDependencies(resolver, importPath));
}
} else if (isRequiredJs) {
} else if (isRequiredJs && !this.state.options.allowJs) {
continue;
} else {
this.dependencies.addDependency(fileName, importPath);
Expand Down Expand Up @@ -139,7 +163,6 @@ export class FileAnalyzer {
});

let resolvedImports = await Promise.all(task);

return resolvedImports.filter(Boolean);
}

Expand Down
11 changes: 11 additions & 0 deletions src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface ICompilerOptions extends ts.CompilerOptions {
reEmitDependentFiles?: boolean;
tsconfig?: string;
useWebpackText?: boolean;
exclude?: string[];
externals?: any;
doTypeCheck?: boolean;
forkChecker?: boolean;
Expand Down Expand Up @@ -99,6 +100,16 @@ export class Host implements ts.LanguageServiceHost {
let file = this.state.getFile(fileName);
if (!file) {
try {
// ignore excluded javascript
if (!fileName.match(/\.tsx?$|package[.]json?$/)) {
let matchedExcludes = this.state.options.exclude.filter((excl) => {
return fileName.indexOf(excl) !== -1;
});
if (matchedExcludes.length > 0) {
return;
}
}

let text = this.state.readFileSync(fileName);
file = {
version: 0,
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ async function compiler(webpack: IWebPack, text: string): Promise<void> {
let callback = webpack.async();
let fileName = state.normalizePath(webpack.resourcePath);

let resolver = createResolver(webpack._compiler.options.externals, webpack.resolve);
let resolver = createResolver(
webpack._compiler.options.externals,
state.options.exclude || [],
webpack.resolve
);

let depsInjector = {
add: (depFileName) => webpack.addDependency(depFileName),
clear: webpack.clearDependencies.bind(webpack)
Expand Down
12 changes: 9 additions & 3 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export function ensureInstance(webpack: IWebPack, options: ICompilerOptions, ins
if (configFile.compilerOptions) {
_.extend(options, configFile.compilerOptions);
_.extend(options, (configFile as any).awesomeTypescriptLoaderOptions);
options.exclude = configFile.exclude || [];
tsFiles = configFile.files;
}
}
Expand All @@ -179,10 +180,11 @@ export function ensureInstance(webpack: IWebPack, options: ICompilerOptions, ins
sourceMap: true,
verbose: false,
noLib: false,
suppressOutputPathCheck: true,
sourceRoot: process.cwd()
});

options = _.omit(options, 'outDir', 'files', 'exclude') as any;
options = _.omit(options, 'outDir', 'files') as any;
options.externals.push.apply(options.externals, tsFiles);

let babelImpl: any;
Expand Down Expand Up @@ -263,9 +265,13 @@ export function ensureInstance(webpack: IWebPack, options: ICompilerOptions, ins
function setupWatchRun(compiler, instanceName: string) {
compiler.plugin('watch-run', async function (watching, callback) {
let compiler: ICompiler = watching.compiler;
let resolver = createResolver(compiler.options.externals, watching.compiler.resolvers.normal.resolve);
let instance = resolveInstance(watching.compiler, instanceName);
let state = instance.tsState;
let resolver = createResolver(
compiler.options.externals,
state.options.exclude || [],
watching.compiler.resolvers.normal.resolve
);
let mtimes = watching.compiler.watchFileSystem.watcher.mtimes;
let changedFiles = Object.keys(mtimes);

Expand All @@ -275,7 +281,7 @@ function setupWatchRun(compiler, instanceName: string) {

try {
let tasks = changedFiles.map(async function(changedFile) {
if (/\.ts$|\.d\.ts|\.tsx$/.test(changedFile)) {
if (/\.ts$|\.d\.ts$|\.tsx$|\.js$|\.jsx$/.test(changedFile)) {
await state.readFileAndUpdate(changedFile);
await state.fileAnalyzer.checkDependencies(resolver, changedFile);
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/fixtures/salsa/exclude/ignored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @param {number} a - a
* @param {number} b - b
* @return {number}
*/
module.exports = function mul(a, b) {
return a * b;
}
17 changes: 0 additions & 17 deletions src/test/fixtures/salsa/index.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/test/fixtures/salsa/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { sum } from './lib';
import { mul } from './exclude/ignored';

sum('asdf', /asdf/);

// should be any
mul('asdf', /asdf/);
8 changes: 8 additions & 0 deletions src/test/fixtures/salsa/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @param {number} a - a
* @param {number} b - b
* @return {number}
*/
export function sum(a, b) {
return a + b;
}
7 changes: 2 additions & 5 deletions src/test/fixtures/salsa/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
"compilerOptions": {
"target": "es5",
"declaration": false,
"noImplicitAny": false,
"noEmitOnError": false,
"allowNonTsExtensions": true,
"allowJs": true,
"noLib": true
"allowJs": true
},
"exclude": [
"exclude",
"node_modules",
"bower_components"
]
Expand Down
55 changes: 0 additions & 55 deletions src/test/output/main.js

This file was deleted.

15 changes: 9 additions & 6 deletions src/test/salsa.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import {
cleanAndCompile, expect, readOutputFile,
fixturePath, readFixture, expectSource, createConfig
cleanAndCompile, expect,
fixturePath, createConfig
} from './utils';

describe('salsa test', function() {
xit('should compile js file', async function() {
it('should compile js file', async function() {
let config = {
entry: fixturePath(['salsa', 'index.js'])
entry: fixturePath(['salsa', 'index.ts'])
};

let tsconfig = fixturePath(['salsa', 'tsconfig.json']);
let loaderParams = `&tsconfig=${tsconfig}`;
let exclude = [ /exclude/ ];

let stats = await cleanAndCompile(createConfig(config, { loaderParams }));
expect(stats.compilation.errors.length).eq(1);
let stats = await cleanAndCompile(createConfig(config, { loaderParams, exclude }));
expect(stats.compilation.errors.length).eq(2);
expect(stats.compilation.errors[0].toString()).include('Cannot find module');
expect(stats.compilation.errors[1].toString()).include(`Argument of type 'string'`);
});
});
17 changes: 15 additions & 2 deletions src/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ export interface ConfigOptions {
loaderParams?: string;
watch?: boolean;
forkChecker?: boolean;
include?: (string | RegExp)[];
exclude?: (string | RegExp)[];
}

let defaultOptions: ConfigOptions = {
loaderParams: '',
watch: false,
forkChecker: false
forkChecker: false,
};

export function createConfig(conf, _options: ConfigOptions = defaultOptions) {
Expand All @@ -41,17 +43,28 @@ export function createConfig(conf, _options: ConfigOptions = defaultOptions) {
path: defaultOutputDir,
filename: '[name].js'
},
resolve: {
extensions: ['', '.ts', '.tsx', '.js', '.jsx'],
},
module: {
loaders: [
{
test: /\.(tsx?|jsx?)/,
loader: loaderDir + '?doTypeCheck&target=es6' + options.loaderParams,
loader: loaderDir + '?target=es5' + options.loaderParams,
},
],
},
plugins: []
};

if (options.include) {
(defaultConfig.module.loaders[0] as any).include = options.include;
}

if (options.exclude) {
(defaultConfig.module.loaders[0] as any).exclude = options.exclude;
}

if (options.watch) {
defaultConfig.watch = true;
}
Expand Down

0 comments on commit d7a8a64

Please sign in to comment.