Skip to content

Commit

Permalink
support custom transformers for ts 2.3 (#535)
Browse files Browse the repository at this point in the history
* support custom transformers for ts 2.3

* First go at comparison test

* Suggested tweaks by @Igorbek

* Made cross platform test
  • Loading branch information
longlho authored and johnnyreilly committed Jun 21, 2017
1 parent 3a68b00 commit 6594328
Show file tree
Hide file tree
Showing 23 changed files with 557 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ env:
- TYPESCRIPT=typescript@2.1.5
- TYPESCRIPT=typescript@2.2.1
- TYPESCRIPT=typescript@2.3.1
- TYPESCRIPT=typescript@next
# - TYPESCRIPT=typescript@next
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
- TYPESCRIPT: typescript@2.1.5
- TYPESCRIPT: typescript@2.2.1
- TYPESCRIPT: typescript@2.3.1
- TYPESCRIPT: typescript@next
# - TYPESCRIPT: typescript@next
install:
- ps: Install-Product node $env:nodejs_version
- npm install
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"mocha": "^3.1.0",
"phantomjs-prebuilt": "^2.1.2",
"rimraf": "^2.4.2",
"typescript": "^2.2.1",
"typescript": "^2.4.0",
"typings": "^2.0.0",
"webpack": "^2.2.0"
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function getLoaderOptions(loader: interfaces.Webpack) {
visualStudioErrorFormat: false,
compilerOptions: {},
appendTsSuffixTo: [],
transformers: {},
entryFileIsJs: false,
happyPackMode: false,
}, configFileOptions, queryOptions);
Expand Down Expand Up @@ -185,6 +186,7 @@ function getTranspilationEmit(

const { outputText, sourceMapText, diagnostics } = instance.compiler.transpileModule(contents, {
compilerOptions: instance.compilerOptions,
transformers: instance.transformers,
reportDiagnostics: true,
fileName,
});
Expand Down
7 changes: 5 additions & 2 deletions src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export function getTypeScriptInstance(
const compilerOptions = compilerSetup.getCompilerOptions(compilerCompatible, compiler, configParseResult);
const files: interfaces.TSFiles = {};

const getCustomTransformers = loaderOptions.getCustomTransformers || Function.prototype;

if (loaderOptions.transpileOnly) {
// quick return for transpiling
// we do need to check for any issues with TS options though
Expand All @@ -70,8 +72,8 @@ export function getTypeScriptInstance(
loader._module.errors,
utils.formatErrors(diagnostics, loaderOptions, compiler, {file: configFilePath || 'tsconfig.json'}));
}
return { instance: instances[loaderOptions.instance] = { compiler, compilerOptions, loaderOptions, files, dependencyGraph: {}, reverseDependencyGraph: {} }};

return { instance: instances[loaderOptions.instance] = { compiler, compilerOptions, loaderOptions, files, dependencyGraph: {}, reverseDependencyGraph: {}, transformers: getCustomTransformers() }};
}

// Load initial files (core lib files, any files specified in tsconfig.json)
Expand Down Expand Up @@ -103,6 +105,7 @@ export function getTypeScriptInstance(
files,
languageService: null,
version: 0,
transformers: getCustomTransformers(),
dependencyGraph: {},
reverseDependencyGraph: {},
modifiedFiles: null,
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export interface TSInstance {
dependencyGraph: DependencyGraph;
reverseDependencyGraph: ReverseDependencyGraph;
filesWithErrors?: TSFiles;
transformers: typescript.CustomTransformers;
}

export interface LoaderOptionsCache {
Expand Down Expand Up @@ -255,6 +256,7 @@ export interface LoaderOptions {
appendTsSuffixTo: RegExp[];
entryFileIsJs: boolean;
happyPackMode: boolean;
getCustomTransformers?(): typescript.CustomTransformers | undefined;
}

export interface TSFile {
Expand Down
9 changes: 5 additions & 4 deletions src/servicesHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ function makeServicesHost(
getDefaultLibFileName: (options: typescript.CompilerOptions) => compiler.getDefaultLibFilePath(options),
getNewLine: () => newLine,
log: log.log,
resolveModuleNames: (moduleNames: string[], containingFile: string) =>
resolveModuleNames: (moduleNames: string[], containingFile: string) =>
resolveModuleNames(
resolveSync, moduleResolutionHost, appendTsSuffixTo, scriptRegex, instance,
moduleNames, containingFile)
moduleNames, containingFile),
getCustomTransformers: () => instance.transformers
};
}

Expand All @@ -85,7 +86,7 @@ function resolveModuleNames(
moduleNames: string[],
containingFile: string
) {
const resolvedModules = moduleNames.map(moduleName =>
const resolvedModules = moduleNames.map(moduleName =>
resolveModuleName(resolveSync, moduleResolutionHost, appendTsSuffixTo, scriptRegex, instance,
moduleName, containingFile)
);
Expand All @@ -106,7 +107,7 @@ function resolveModuleName(
containingFile: string
) {
const { compiler, compilerOptions } = instance;

let resolutionResult: interfaces.ResolvedModule;

try {
Expand Down
2 changes: 2 additions & 0 deletions test/comparison-tests/customTransformer/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var message = "Hello from me!"
console.log(message);
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

var message = "HELLO FROM ME!";
console.log(message);


/***/ })
/******/ ]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

var message = "Hello from me!";
console.log(message);


/***/ })
/******/ ]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Asset Size Chunks Chunk Names
bundle.js 2.69 kB 0 [emitted] main
chunk {0} bundle.js (main) 54 bytes [entry] [rendered]
[0] ./.test/customTransformer/app.ts 54 bytes {0} [built]
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

var message = "HELLO FROM HIM!";
console.log(message);


/***/ })
/******/ ]);
Loading

0 comments on commit 6594328

Please sign in to comment.