From 636707331be675d51202df87f9425ba2323276a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Fri, 26 Oct 2018 19:33:38 +0100 Subject: [PATCH 01/11] Implements a custom resolveModuleName option --- src/index.ts | 3 ++- src/interfaces.ts | 16 ++++++++++++++++ src/servicesHost.ts | 36 +++++++++++++++++++++++++----------- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index 80272b0ac..2fe667793 100644 --- a/src/index.ts +++ b/src/index.ts @@ -254,7 +254,8 @@ const validLoaderOptions: ValidLoaderOptions[] = [ 'experimentalWatchApi', 'allowTsInNodeModules', 'experimentalFileCaching', - 'projectReferences' + 'projectReferences', + 'resolveModuleName' ]; /** diff --git a/src/interfaces.ts b/src/interfaces.ts index 49fdf34bc..787e91a55 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -295,6 +295,21 @@ export interface ReverseDependencyGraph { export type LogLevel = 'INFO' | 'WARN' | 'ERROR'; +export type ResolveModuleName = ( + moduleName: string, + containingFile: string, + compilerOptions: typescript.CompilerOptions, + moduleResolutionHost: typescript.ModuleResolutionHost, +) => typescript.ResolvedModuleWithFailedLookupLocations; + +export type CustomResolveModuleName = ( + moduleName: string, + containingFile: string, + options: typescript.CompilerOptions, + moduleResolutionHost: typescript.ModuleResolutionHost, + parentResolver: ResolveModuleName +) => typescript.ResolvedModuleWithFailedLookupLocations; + export interface LoaderOptions { silent: boolean; logLevel: LogLevel; @@ -320,6 +335,7 @@ export interface LoaderOptions { allowTsInNodeModules: boolean; experimentalFileCaching: boolean; projectReferences: boolean; + resolveModuleName?: CustomResolveModuleName; } export interface TSFile { diff --git a/src/servicesHost.ts b/src/servicesHost.ts index 2b1cfda7c..5e4b20368 100644 --- a/src/servicesHost.ts +++ b/src/servicesHost.ts @@ -4,6 +4,7 @@ import * as typescript from 'typescript'; import * as constants from './constants'; import { ModuleResolutionHost, + CustomResolveModuleName, ResolvedModule, ResolveSync, TSInstance, @@ -36,7 +37,11 @@ export function makeServicesHost( compiler, compilerOptions, files, - loaderOptions: { appendTsSuffixTo, appendTsxSuffixTo } + loaderOptions: { + appendTsSuffixTo, + appendTsxSuffixTo, + resolveModuleName: customResolveModuleName + } } = instance; const newLine = @@ -148,7 +153,8 @@ export function makeServicesHost( instance, moduleNames, containingFile, - getResolutionStrategy + getResolutionStrategy, + customResolveModuleName ), getCustomTransformers: () => instance.transformers @@ -426,7 +432,8 @@ function resolveModuleNames( instance: TSInstance, moduleNames: string[], containingFile: string, - resolutionStrategy: ResolutionStrategy + resolutionStrategy: ResolutionStrategy, + customResolveModuleName?: CustomResolveModuleName ) { const resolvedModules = moduleNames.map(moduleName => resolveModuleName( @@ -438,7 +445,8 @@ function resolveModuleNames( instance, moduleName, containingFile, - resolutionStrategy + resolutionStrategy, + customResolveModuleName ) ); @@ -466,7 +474,8 @@ function resolveModuleName( instance: TSInstance, moduleName: string, containingFile: string, - resolutionStrategy: ResolutionStrategy + resolutionStrategy: ResolutionStrategy, + customResolveModuleName?: CustomResolveModuleName ) { const { compiler, compilerOptions } = instance; @@ -496,12 +505,17 @@ function resolveModuleName( // tslint:disable-next-line:no-empty } catch (e) {} - const tsResolution = compiler.resolveModuleName( - moduleName, - containingFile, - compilerOptions, - moduleResolutionHost - ); + const tsResolver = (moduleName: string, containingFile: string, compilerOptions: typescript.CompilerOptions, moduleResolutionHost: typescript.ModuleResolutionHost) => + compiler.resolveModuleName( + moduleName, + containingFile, + compilerOptions, + moduleResolutionHost + ); + + const tsResolution = customResolveModuleName + ? customResolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost, tsResolver) + : tsResolver(moduleName, containingFile, compilerOptions, moduleResolutionHost); if (tsResolution.resolvedModule !== undefined) { const resolvedFileName = path.normalize( From 46d721aae2ff7a68ef555c150fec8111dd2b3cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 27 Oct 2018 12:34:00 +0100 Subject: [PATCH 02/11] Addresses feedback --- src/interfaces.ts | 2 +- src/servicesHost.ts | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index 787e91a55..738d523a7 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -305,7 +305,7 @@ export type ResolveModuleName = ( export type CustomResolveModuleName = ( moduleName: string, containingFile: string, - options: typescript.CompilerOptions, + compilerOptions: typescript.CompilerOptions, moduleResolutionHost: typescript.ModuleResolutionHost, parentResolver: ResolveModuleName ) => typescript.ResolvedModuleWithFailedLookupLocations; diff --git a/src/servicesHost.ts b/src/servicesHost.ts index 5e4b20368..a3d5e17ef 100644 --- a/src/servicesHost.ts +++ b/src/servicesHost.ts @@ -3,8 +3,8 @@ import * as typescript from 'typescript'; import * as constants from './constants'; import { - ModuleResolutionHost, CustomResolveModuleName, + ModuleResolutionHost, ResolvedModule, ResolveSync, TSInstance, @@ -465,6 +465,15 @@ function isJsImplementationOfTypings( ); } +function applyTsResolver(compiler: typeof typescript, moduleName: string, containingFile: string, compilerOptions: typescript.CompilerOptions, moduleResolutionHost: typescript.ModuleResolutionHost) { + return compiler.resolveModuleName( + moduleName, + containingFile, + compilerOptions, + moduleResolutionHost + ); +} + function resolveModuleName( resolveSync: ResolveSync, moduleResolutionHost: ModuleResolutionHost, @@ -505,17 +514,9 @@ function resolveModuleName( // tslint:disable-next-line:no-empty } catch (e) {} - const tsResolver = (moduleName: string, containingFile: string, compilerOptions: typescript.CompilerOptions, moduleResolutionHost: typescript.ModuleResolutionHost) => - compiler.resolveModuleName( - moduleName, - containingFile, - compilerOptions, - moduleResolutionHost - ); - const tsResolution = customResolveModuleName - ? customResolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost, tsResolver) - : tsResolver(moduleName, containingFile, compilerOptions, moduleResolutionHost); + ? customResolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost, applyTsResolver.bind(null, compiler)) + : applyTsResolver(compiler, moduleName, containingFile, compilerOptions, moduleResolutionHost); if (tsResolution.resolvedModule !== undefined) { const resolvedFileName = path.normalize( From f2f6026f4eb51d5db35726836f77012731d73ad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 27 Oct 2018 14:22:31 +0100 Subject: [PATCH 03/11] Adds a test --- .../bar-pkg/index.d.ts | 3 ++ .../bar-pkg/package.json | 5 ++ .../foo-pkg/index.d.ts | 4 ++ .../foo-pkg/package.json | 5 ++ .../3.0.1_resolveModuleName/karma.conf.js | 47 +++++++++++++++++++ .../3.0.1_resolveModuleName/main.js | 2 + .../3.0.1_resolveModuleName/package.json | 10 ++++ .../3.0.1_resolveModuleName/src/app.ts | 6 +++ .../3.0.1_resolveModuleName/test/app.tests.ts | 12 +++++ .../3.0.1_resolveModuleName/tsconfig.json | 6 +++ .../3.0.1_resolveModuleName/webpack.config.js | 28 +++++++++++ .../3.0.1_resolveModuleName/yarn.lock | 11 +++++ 12 files changed, 139 insertions(+) create mode 100644 test/execution-tests/3.0.1_resolveModuleName/bar-pkg/index.d.ts create mode 100644 test/execution-tests/3.0.1_resolveModuleName/bar-pkg/package.json create mode 100644 test/execution-tests/3.0.1_resolveModuleName/foo-pkg/index.d.ts create mode 100644 test/execution-tests/3.0.1_resolveModuleName/foo-pkg/package.json create mode 100644 test/execution-tests/3.0.1_resolveModuleName/karma.conf.js create mode 100644 test/execution-tests/3.0.1_resolveModuleName/main.js create mode 100644 test/execution-tests/3.0.1_resolveModuleName/package.json create mode 100644 test/execution-tests/3.0.1_resolveModuleName/src/app.ts create mode 100644 test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts create mode 100644 test/execution-tests/3.0.1_resolveModuleName/tsconfig.json create mode 100644 test/execution-tests/3.0.1_resolveModuleName/webpack.config.js create mode 100644 test/execution-tests/3.0.1_resolveModuleName/yarn.lock diff --git a/test/execution-tests/3.0.1_resolveModuleName/bar-pkg/index.d.ts b/test/execution-tests/3.0.1_resolveModuleName/bar-pkg/index.d.ts new file mode 100644 index 000000000..0f0896fa3 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/bar-pkg/index.d.ts @@ -0,0 +1,3 @@ +import {HelloWorld} from 'foo'; + +export type HelloBuilder = (hello: number, world: number) => HelloWorld; diff --git a/test/execution-tests/3.0.1_resolveModuleName/bar-pkg/package.json b/test/execution-tests/3.0.1_resolveModuleName/bar-pkg/package.json new file mode 100644 index 000000000..0a9d633ad --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/bar-pkg/package.json @@ -0,0 +1,5 @@ +{ + "name": "bar", + "version": "1.0.0", + "typings": "./index.d.ts" +} diff --git a/test/execution-tests/3.0.1_resolveModuleName/foo-pkg/index.d.ts b/test/execution-tests/3.0.1_resolveModuleName/foo-pkg/index.d.ts new file mode 100644 index 000000000..ca025da24 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/foo-pkg/index.d.ts @@ -0,0 +1,4 @@ +export type HelloWorld = { + hello: number, + world: number, +}; diff --git a/test/execution-tests/3.0.1_resolveModuleName/foo-pkg/package.json b/test/execution-tests/3.0.1_resolveModuleName/foo-pkg/package.json new file mode 100644 index 000000000..d20a6c558 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/foo-pkg/package.json @@ -0,0 +1,5 @@ +{ + "name": "foo", + "version": "1.0.0", + "typings": "./index.d.ts" +} diff --git a/test/execution-tests/3.0.1_resolveModuleName/karma.conf.js b/test/execution-tests/3.0.1_resolveModuleName/karma.conf.js new file mode 100644 index 000000000..f0cb98bc7 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/karma.conf.js @@ -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 + }); +}; diff --git a/test/execution-tests/3.0.1_resolveModuleName/main.js b/test/execution-tests/3.0.1_resolveModuleName/main.js new file mode 100644 index 000000000..118f90112 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/main.js @@ -0,0 +1,2 @@ +const testsContext = require.context('./', true, /\.tests\.ts(x?)$/); +testsContext.keys().forEach(testsContext); diff --git a/test/execution-tests/3.0.1_resolveModuleName/package.json b/test/execution-tests/3.0.1_resolveModuleName/package.json new file mode 100644 index 000000000..12bc6974a --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/package.json @@ -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" + } +} diff --git a/test/execution-tests/3.0.1_resolveModuleName/src/app.ts b/test/execution-tests/3.0.1_resolveModuleName/src/app.ts new file mode 100644 index 000000000..64cd35b67 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/src/app.ts @@ -0,0 +1,6 @@ +import {HelloBuilder} from 'bar'; + +export const makeHello: HelloBuilder = (hello: number, world: number) => ({ + hello, + world, +}); diff --git a/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts b/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts new file mode 100644 index 000000000..2c30c6d81 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts @@ -0,0 +1,12 @@ +import * as app from '../src/app'; + +// We don't actually care about the result of the following operations; +// what we care about is typescript resolving json as modules +// 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); + }); +}); diff --git a/test/execution-tests/3.0.1_resolveModuleName/tsconfig.json b/test/execution-tests/3.0.1_resolveModuleName/tsconfig.json new file mode 100644 index 000000000..2b5235dbf --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "noEmitOnError": true, + "resolveJsonModule": true + } +} \ No newline at end of file diff --git a/test/execution-tests/3.0.1_resolveModuleName/webpack.config.js b/test/execution-tests/3.0.1_resolveModuleName/webpack.config.js new file mode 100644 index 000000000..64266e968 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/webpack.config.js @@ -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") } } diff --git a/test/execution-tests/3.0.1_resolveModuleName/yarn.lock b/test/execution-tests/3.0.1_resolveModuleName/yarn.lock new file mode 100644 index 000000000..2bf33ec6c --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/yarn.lock @@ -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" From 289d904cfe99f19846b02f33f4205b13d68cb2fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 27 Oct 2018 19:18:00 +0100 Subject: [PATCH 04/11] Adresses feedback --- .../3.0.1_resolveModuleName/test/app.tests.ts | 4 ++-- test/execution-tests/3.0.1_resolveModuleName/tsconfig.json | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts b/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts index 2c30c6d81..8c653b522 100644 --- a/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts +++ b/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts @@ -1,8 +1,8 @@ import * as app from '../src/app'; // We don't actually care about the result of the following operations; -// what we care about is typescript resolving json as modules -// allowing this code to compile without errors, hence this: +// what we care about is typescript resolving modules using our custom +// logic, allowing this code to compile without errors, hence this: // "noEmitOnError": true // in tsconfig.json describe("app", () => { diff --git a/test/execution-tests/3.0.1_resolveModuleName/tsconfig.json b/test/execution-tests/3.0.1_resolveModuleName/tsconfig.json index 2b5235dbf..0f0acf626 100644 --- a/test/execution-tests/3.0.1_resolveModuleName/tsconfig.json +++ b/test/execution-tests/3.0.1_resolveModuleName/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "noEmitOnError": true, - "resolveJsonModule": true + "noEmitOnError": true } -} \ No newline at end of file +} From 9a0540a2734f18b8a27741fb9c5924753ae392e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 27 Oct 2018 21:21:04 +0100 Subject: [PATCH 05/11] Expands the test scope --- .../3.0.1_resolveModuleName/baz-pkg/index.ts | 6 ++++++ .../3.0.1_resolveModuleName/baz-pkg/package.json | 5 +++++ test/execution-tests/3.0.1_resolveModuleName/src/app.ts | 8 +++----- .../3.0.1_resolveModuleName/test/app.tests.ts | 5 +++-- .../3.0.1_resolveModuleName/webpack.config.js | 4 +++- 5 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.ts create mode 100644 test/execution-tests/3.0.1_resolveModuleName/baz-pkg/package.json diff --git a/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.ts b/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.ts new file mode 100644 index 000000000..64cd35b67 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.ts @@ -0,0 +1,6 @@ +import {HelloBuilder} from 'bar'; + +export const makeHello: HelloBuilder = (hello: number, world: number) => ({ + hello, + world, +}); diff --git a/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/package.json b/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/package.json new file mode 100644 index 000000000..58ebd538c --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/package.json @@ -0,0 +1,5 @@ +{ + "name": "baz", + "version": "1.0.0", + "main": "index.ts" +} diff --git a/test/execution-tests/3.0.1_resolveModuleName/src/app.ts b/test/execution-tests/3.0.1_resolveModuleName/src/app.ts index 64cd35b67..91c04e831 100644 --- a/test/execution-tests/3.0.1_resolveModuleName/src/app.ts +++ b/test/execution-tests/3.0.1_resolveModuleName/src/app.ts @@ -1,6 +1,4 @@ -import {HelloBuilder} from 'bar'; +import {HelloWorld} from 'foo'; +import {makeHello} from 'baz'; -export const makeHello: HelloBuilder = (hello: number, world: number) => ({ - hello, - world, -}); +export const def: HelloWorld = makeHello(1, 2); diff --git a/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts b/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts index 8c653b522..04fda62af 100644 --- a/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts +++ b/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts @@ -6,7 +6,8 @@ import * as app from '../src/app'; // "noEmitOnError": true // in tsconfig.json describe("app", () => { - it("makeHello to exist", () => { - expect(!!app.makeHello).toBe(true); + it("app.def to have been setup", () => { + expect(app.def.hello).toEqual(1); + expect(app.def.world).toEqual(2); }); }); diff --git a/test/execution-tests/3.0.1_resolveModuleName/webpack.config.js b/test/execution-tests/3.0.1_resolveModuleName/webpack.config.js index 64266e968..00da136ae 100644 --- a/test/execution-tests/3.0.1_resolveModuleName/webpack.config.js +++ b/test/execution-tests/3.0.1_resolveModuleName/webpack.config.js @@ -7,7 +7,8 @@ module.exports = { filename: 'bundle.js' }, resolve: { - extensions: ['.ts', '.js'] + extensions: ['.ts', '.js'], + alias: { baz: path.join(__dirname, 'baz-pkg') }, }, module: { rules: [ @@ -16,6 +17,7 @@ module.exports = { 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); + case 'baz': return parentResolver(path.join(__dirname, 'baz-pkg'), containingFile, compilerOptions, compilerHost); default: return parentResolver(moduleName, containingFile, compilerOptions, compilerHost); } }, From 0bc78bdced1c5b37b0168179991f7db3095f4bb5 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Sun, 28 Oct 2018 06:17:26 +0000 Subject: [PATCH 06/11] Fix validateLoaderOptionNames test --- .../expectedOutput-3.1/bundle.js | 2 +- .../expectedOutput-3.1/output.txt | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/comparison-tests/validateLoaderOptionNames/expectedOutput-3.1/bundle.js b/test/comparison-tests/validateLoaderOptionNames/expectedOutput-3.1/bundle.js index 07e172697..f469245b1 100644 --- a/test/comparison-tests/validateLoaderOptionNames/expectedOutput-3.1/bundle.js +++ b/test/comparison-tests/validateLoaderOptionNames/expectedOutput-3.1/bundle.js @@ -93,7 +93,7 @@ /*! no static exports found */ /***/ (function(module, exports) { -eval("throw new Error(\"Module build failed (from C:/source/ts-loader/index.js):/nError: ts-loader was supplied with an unexpected loader option: notRealOption/n/nPlease take a look at the options you are supplying; the following are valid options:/nsilent / logLevel / logInfoToStdOut / instance / compiler / context / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / onlyCompileBundledFiles / happyPackMode / getCustomTransformers / reportFiles / experimentalWatchApi / allowTsInNodeModules / experimentalFileCaching / projectReferences/n/n at validateLoaderOptions (C://source//ts-loader//dist//index.js:151:19)/n at getLoaderOptions (C://source//ts-loader//dist//index.js:110:5)/n at Object.loader (C://source//ts-loader//dist//index.js:16:21)\");\n\n//# sourceURL=webpack:///./app.ts?"); +eval("throw new Error(\"Module build failed (from C:/source/ts-loader/index.js):/nError: ts-loader was supplied with an unexpected loader option: notRealOption/n/nPlease take a look at the options you are supplying; the following are valid options:/nsilent / logLevel / logInfoToStdOut / instance / compiler / context / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / onlyCompileBundledFiles / happyPackMode / getCustomTransformers / reportFiles / experimentalWatchApi / allowTsInNodeModules / experimentalFileCaching / projectReferences / resolveModuleName/n/n at validateLoaderOptions (C://source//ts-loader//dist//index.js:152:19)/n at getLoaderOptions (C://source//ts-loader//dist//index.js:110:5)/n at Object.loader (C://source//ts-loader//dist//index.js:16:21)\");\n\n//# sourceURL=webpack:///./app.ts?"); /***/ }) diff --git a/test/comparison-tests/validateLoaderOptionNames/expectedOutput-3.1/output.txt b/test/comparison-tests/validateLoaderOptionNames/expectedOutput-3.1/output.txt index cb1108e95..6f71f590f 100644 --- a/test/comparison-tests/validateLoaderOptionNames/expectedOutput-3.1/output.txt +++ b/test/comparison-tests/validateLoaderOptionNames/expectedOutput-3.1/output.txt @@ -1,15 +1,15 @@ - Asset Size Chunks Chunk Names -bundle.js 4.58 KiB main [emitted] main + Asset Size Chunks Chunk Names +bundle.js 4.6 KiB main [emitted] main Entrypoint main = bundle.js -[./app.ts] 835 bytes {main} [built] [failed] [1 error] +[./app.ts] 855 bytes {main} [built] [failed] [1 error] ERROR in ./app.ts Module build failed (from /index.js): Error: ts-loader was supplied with an unexpected loader option: notRealOption Please take a look at the options you are supplying; the following are valid options: -silent / logLevel / logInfoToStdOut / instance / compiler / context / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / onlyCompileBundledFiles / happyPackMode / getCustomTransformers / reportFiles / experimentalWatchApi / allowTsInNodeModules / experimentalFileCaching / projectReferences +silent / logLevel / logInfoToStdOut / instance / compiler / context / configFile / transpileOnly / ignoreDiagnostics / errorFormatter / colors / compilerOptions / appendTsSuffixTo / appendTsxSuffixTo / onlyCompileBundledFiles / happyPackMode / getCustomTransformers / reportFiles / experimentalWatchApi / allowTsInNodeModules / experimentalFileCaching / projectReferences / resolveModuleName - at validateLoaderOptions (dist\index.js:151:19) + at validateLoaderOptions (dist\index.js:152:19) at getLoaderOptions (dist\index.js:110:5) at Object.loader (dist\index.js:16:21) \ No newline at end of file From 8b694a8f8cd21ecd77a67e76258b7ca3544dacdd Mon Sep 17 00:00:00 2001 From: John Reilly Date: Sun, 28 Oct 2018 06:28:11 +0000 Subject: [PATCH 07/11] prettier formatting --- src/servicesHost.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/servicesHost.ts b/src/servicesHost.ts index a3d5e17ef..b98f5d992 100644 --- a/src/servicesHost.ts +++ b/src/servicesHost.ts @@ -465,7 +465,13 @@ function isJsImplementationOfTypings( ); } -function applyTsResolver(compiler: typeof typescript, moduleName: string, containingFile: string, compilerOptions: typescript.CompilerOptions, moduleResolutionHost: typescript.ModuleResolutionHost) { +function applyTsResolver( + compiler: typeof typescript, + moduleName: string, + containingFile: string, + compilerOptions: typescript.CompilerOptions, + moduleResolutionHost: typescript.ModuleResolutionHost +) { return compiler.resolveModuleName( moduleName, containingFile, @@ -515,8 +521,20 @@ function resolveModuleName( } catch (e) {} const tsResolution = customResolveModuleName - ? customResolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost, applyTsResolver.bind(null, compiler)) - : applyTsResolver(compiler, moduleName, containingFile, compilerOptions, moduleResolutionHost); + ? customResolveModuleName( + moduleName, + containingFile, + compilerOptions, + moduleResolutionHost, + applyTsResolver.bind(null, compiler) + ) + : applyTsResolver( + compiler, + moduleName, + containingFile, + compilerOptions, + moduleResolutionHost + ); if (tsResolution.resolvedModule !== undefined) { const resolvedFileName = path.normalize( From afa570d8500c8ba182c11b18e29d97edc9fe20e4 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Sun, 28 Oct 2018 06:34:45 +0000 Subject: [PATCH 08/11] Be explicit about the possible value of customResolveModuleName --- src/servicesHost.ts | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/servicesHost.ts b/src/servicesHost.ts index b98f5d992..c4d8254cc 100644 --- a/src/servicesHost.ts +++ b/src/servicesHost.ts @@ -520,21 +520,22 @@ function resolveModuleName( // tslint:disable-next-line:no-empty } catch (e) {} - const tsResolution = customResolveModuleName - ? customResolveModuleName( - moduleName, - containingFile, - compilerOptions, - moduleResolutionHost, - applyTsResolver.bind(null, compiler) - ) - : applyTsResolver( - compiler, - moduleName, - containingFile, - compilerOptions, - moduleResolutionHost - ); + const tsResolution = + customResolveModuleName !== undefined + ? customResolveModuleName( + moduleName, + containingFile, + compilerOptions, + moduleResolutionHost, + applyTsResolver.bind(null, compiler) + ) + : applyTsResolver( + compiler, + moduleName, + containingFile, + compilerOptions, + moduleResolutionHost + ); if (tsResolution.resolvedModule !== undefined) { const resolvedFileName = path.normalize( From ad4ed7b7b0757ecfe3a5438f3fc6de67ede5f10f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sun, 28 Oct 2018 23:34:17 +0000 Subject: [PATCH 09/11] Feedback round 3 --- src/servicesHost.ts | 14 +++++++++++++- .../3.0.1_resolveModuleName/baz-pkg/index.d.ts | 3 +++ .../3.0.1_resolveModuleName/baz-pkg/index.js | 4 ++++ .../3.0.1_resolveModuleName/baz-pkg/index.ts | 6 ------ .../3.0.1_resolveModuleName/test/app.tests.ts | 5 ----- 5 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.d.ts create mode 100644 test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.js delete mode 100644 test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.ts diff --git a/src/servicesHost.ts b/src/servicesHost.ts index c4d8254cc..6aff1d552 100644 --- a/src/servicesHost.ts +++ b/src/servicesHost.ts @@ -527,7 +527,19 @@ function resolveModuleName( containingFile, compilerOptions, moduleResolutionHost, - applyTsResolver.bind(null, compiler) + ( + moduleName: string, + containingFile: string, + compilerOptions: typescript.CompilerOptions, + moduleResolutionHost: typescript.ModuleResolutionHost + ) => + applyTsResolver( + compiler, + moduleName, + containingFile, + compilerOptions, + moduleResolutionHost + ) ) : applyTsResolver( compiler, diff --git a/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.d.ts b/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.d.ts new file mode 100644 index 000000000..169529593 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.d.ts @@ -0,0 +1,3 @@ +import {HelloBuilder} from 'bar'; + +export declare const makeHello: HelloBuilder; diff --git a/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.js b/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.js new file mode 100644 index 000000000..02a917ea2 --- /dev/null +++ b/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.js @@ -0,0 +1,4 @@ +export const makeHello = (hello, world) => ({ + hello, + world, +}); diff --git a/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.ts b/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.ts deleted file mode 100644 index 64cd35b67..000000000 --- a/test/execution-tests/3.0.1_resolveModuleName/baz-pkg/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -import {HelloBuilder} from 'bar'; - -export const makeHello: HelloBuilder = (hello: number, world: number) => ({ - hello, - world, -}); diff --git a/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts b/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts index 04fda62af..e223dcb42 100644 --- a/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts +++ b/test/execution-tests/3.0.1_resolveModuleName/test/app.tests.ts @@ -1,10 +1,5 @@ import * as app from '../src/app'; -// We don't actually care about the result of the following operations; -// what we care about is typescript resolving modules using our custom -// logic, allowing this code to compile without errors, hence this: -// "noEmitOnError": true -// in tsconfig.json describe("app", () => { it("app.def to have been setup", () => { expect(app.def.hello).toEqual(1); From d8f3735e305b1b52756513294cabfccb5e1109db Mon Sep 17 00:00:00 2001 From: John Reilly Date: Mon, 29 Oct 2018 05:57:59 +0000 Subject: [PATCH 10/11] Fix lint issues --- src/servicesHost.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/servicesHost.ts b/src/servicesHost.ts index 6aff1d552..fcae7366b 100644 --- a/src/servicesHost.ts +++ b/src/servicesHost.ts @@ -528,17 +528,17 @@ function resolveModuleName( compilerOptions, moduleResolutionHost, ( - moduleName: string, - containingFile: string, - compilerOptions: typescript.CompilerOptions, - moduleResolutionHost: typescript.ModuleResolutionHost + moduleNameFromCustomFn: string, + containingFileFromCustomFn: string, + compilerOptionsFromCustomFn: typescript.CompilerOptions, + moduleResolutionHostFromCustomFn: typescript.ModuleResolutionHost ) => applyTsResolver( compiler, - moduleName, - containingFile, - compilerOptions, - moduleResolutionHost + moduleNameFromCustomFn, + containingFileFromCustomFn, + compilerOptionsFromCustomFn, + moduleResolutionHostFromCustomFn ) ) : applyTsResolver( From bee1c373b9e784852cfa4560febab325e041d31d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Tue, 30 Oct 2018 13:25:59 -0700 Subject: [PATCH 11/11] Bumps package.json, updates changelog --- CHANGELOG.md | 6 +++++- package.json | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddbcda8b4..25fad9fe5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 5.3.0 + +* [feat: Exposes a `resolveNodeModule` option](https://github.com/TypeStrong/ts-loader/pull/862) - thanks @arcanis! + ## 5.2.2 * [feat: Micro-optimizations](https://github.com/TypeStrong/ts-loader/pull/855) - thanks @johnnyreilly @@ -14,7 +18,7 @@ ## 5.1.1 -* [fix(getTranspilationEmit): pass the raw path to transpileModule](https://github.com/TypeStrong/ts-loader/pull/835) - thanks @Brooooooklyn +* [fix(getTranspilationEmit): pass the raw path to transpileModule](https://github.com/TypeStrong/ts-loader/pull/835) - thanks @Brooooooklyn ## 5.1.0 diff --git a/package.json b/package.json index 460c16bbb..e0c866d4b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-loader", - "version": "5.2.2", + "version": "5.3.0", "description": "TypeScript loader for webpack", "main": "index.js", "types": "dist/types/index.d.ts",