Skip to content
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

support custom transformers for ts 2.3 #535

Merged
merged 4 commits into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
import submodule = require('./submodule/submodule');
console.log(submodule);
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/******/ (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 = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

var message = "HELLO FROM SUBMODULE";
module.exports = message;


/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
var submodule = __webpack_require__(0);
console.log(submodule);


/***/ })
/******/ ]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/******/ (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 = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

var message = "Hello from submodule";
module.exports = message;


/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
var submodule = __webpack_require__(0);
console.log(submodule);


/***/ })
/******/ ]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Asset Size Chunks Chunk Names
bundle.js 2.92 kB 0 [emitted] main
chunk {0} bundle.js (main) 193 bytes [entry] [rendered]
[0] ./.test/customTransformer/submodule/submodule.ts 78 bytes {0} [built]
[1] ./.test/customTransformer/app.ts 115 bytes {0} [built]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Asset Size Chunks Chunk Names
bundle.js 2.92 kB 0 [emitted] main
chunk {0} bundle.js (main) 193 bytes [entry] [rendered]
[0] ./.test/customTransformer/SUBMODULE/SUBMODULE.ts 78 bytes {0} [built]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transformer uppercased the module import as well :) it was unintentional, but ok ))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In actual fact that is the reason the test is failing on Travis right now I think. Case insensitive Windows Vs case sensitive Linux 😄 I'll change the test so it is 1 file only (no imports) tonight and then we should have a reliable test that works cross platform. Then I think we're ready to merge!

[1] ./.test/customTransformer/app.ts 115 bytes {0} [built]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var message = "Hello from submodule"
export = message
5 changes: 5 additions & 0 deletions test/comparison-tests/customTransformer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
exports.__esModule = true;
var ts = require("typescript");
var transformer = function (context) {
var visitor = function (node) {
if (node.kind === ts.SyntaxKind.StringLiteral) {
var text = node.text;
if (text !== text.toUpperCase()) {
return ts.createLiteral(text.toUpperCase());
}
}
return ts.visitEachChild(node, visitor, context);
};
return function (node) { return ts.visitNode(node, visitor); };
};
exports["default"] = transformer;
28 changes: 28 additions & 0 deletions test/comparison-tests/customTransformer/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var path = require('path')

var uppercaseStringLiteralTransformer = require('./uppercaseStringLiteralTransformer').default;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sample transformer exports the transformer itself, and not a factory function that typescript-plugin-styled-components does. So you don't need to call the function:

var uppercaseStringLiteralTransformer = require('./uppercaseStringLiteralTransformer').default;

module.exports = {
entry: './app.ts',
output: {
filename: 'bundle.js'
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
loader: 'ts-loader',
options: {
getCustomTransformers: () => ({ // note parens
before: [uppercaseStringLiteralTransformer]
})
}
}
]
}
}


5 changes: 5 additions & 0 deletions test/custom-transformers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {

}
}