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

Add custom transformers option #589

Merged
merged 5 commits into from
Sep 22, 2018

Conversation

layershifter
Copy link
Contributor

@layershifter layershifter commented Aug 22, 2018

Fixes #502.


Adds functionality to use custom transformers: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API

Available via getCustomTransformers() option, the idea taken from awesome-typescript-loader.

@layershifter layershifter changed the title [WIP] Add custom transformers Add custom transformers option Sep 3, 2018
@layershifter
Copy link
Contributor Author

@ivogabe can you please review?

@ivogabe
Copy link
Owner

ivogabe commented Sep 11, 2018

Thanks @layershifter for the PR! Sorry for the delay, I've been busy with other projects and didn't have time to take a look at this yet. Overall the changes look good, though I was wondering whether it is a large limitation that we don't pass a ts.Program to getCustomTransformers? That seems to be the difference with awesome-typescript-loader. Correct me if I'm wrong, but I assume that we cannot pass the project to the getCustomTransformers as we don't have one when isolatedModules is enabled, as we are then using a file-based compiler.

An alternative would be to change the type to (string | ((program?: ts.Program) => ts.CustomTransformers | undefined)), and only pass the program if isolatedModules is disabled. I assume that you would need the ts.Program if you need some type info of the program and we simply don't have that with isolatedModules enabled. If you need that type info, you should just not use isolatedModules. What do you think about that? If you want to make the change, you should call getCustomTransformers in the FileCompiler and ProjectCompiler and not in checkAndNormalizeSettings.

gulpfile.js Outdated
@@ -149,6 +149,7 @@ gulp.task('test', gulp.series('test-run', function testVerify() {
let failed = false;
function onError(error) {
failed = true;
console.log(error)
Copy link
Owner

Choose a reason for hiding this comment

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

Could you add a semicolon here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will delete this line, it should not be there

lib/main.ts Outdated
@@ -33,6 +33,32 @@ function compile(param?: any, theReporter?: _reporter.Reporter): compile.Compile
return proj(theReporter);
}

function getFinalTransformers(getCustomTransformers?: GetCustomTransformers): FinalTransformers {
if (typeof getCustomTransformers === 'function') {
return getCustomTransformers
Copy link
Owner

Choose a reason for hiding this comment

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

Semicolon

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, seems we need tslint there :)

lib/main.ts Outdated

if (typeof getCustomTransformers === 'string') {
try {
getCustomTransformers = require(getCustomTransformers)
Copy link
Owner

Choose a reason for hiding this comment

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

Semicolon

lib/main.ts Outdated
} catch (err) {
throw new Error(
`Failed to load customTransformers from "${getCustomTransformers}": ${err.message}`
)
Copy link
Owner

Choose a reason for hiding this comment

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

Semicolon

lib/main.ts Outdated
if (typeof getCustomTransformers !== 'function') {
throw new Error(
`Custom transformers in "${getCustomTransformers}" should export a function, got ${typeof getCustomTransformers}`
)
Copy link
Owner

Choose a reason for hiding this comment

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

Semicolon

lib/main.ts Outdated
return getCustomTransformers
}

return null
Copy link
Owner

Choose a reason for hiding this comment

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

Semicolon

lib/main.ts Outdated
@@ -46,7 +72,7 @@ function getTypeScript(typescript: typeof ts) {
}

function checkAndNormalizeSettings(settings: compile.Settings): compile.Settings {
const { declarationFiles, noExternalResolve, sortOutput, typescript, ...standardSettings } = settings;
const { customTransformers, declarationFiles, noExternalResolve, sortOutput, typescript, ...standardSettings } = settings;
Copy link
Owner

Choose a reason for hiding this comment

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

Should this be getCustomTransformers?

It seems that you don't use the variable here, but we should keep it here as it will remove the property from standardSettings. That way we don't need the delete settings.getCustomTransformers; that you have later on.

lib/main.ts Outdated
@@ -166,6 +194,9 @@ module compile {
settings = fileNameOrSettings || {};
}

finalTransformers = getFinalTransformers(settings.getCustomTransformers);
delete settings.getCustomTransformers;
Copy link
Owner

Choose a reason for hiding this comment

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

Using delete here will cause a performance problem, we used it before for a similar reason as here. The field can better be removed in checkAndNormalizeSettings, see my comment there.

readme.md Outdated
```js
getCustomTransformers: () => ({
before: [
styledComponentsTransformer(),
Copy link
Owner

Choose a reason for hiding this comment

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

Could you also add the require call to be complete?

module.exports = function(newTS, lib, output, reporter) {
var getCustomTransformers = function () {
return {
before: [transformer],
Copy link
Owner

Choose a reason for hiding this comment

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

Add a comment what the transformer does (eg remove all code)

@layershifter
Copy link
Contributor Author

@ivogabe thanks for feedback!

Correct me if I'm wrong

Nope, you're fully right.

(string | ((program?: ts.Program) => ts.CustomTransformers | undefined))

Looks good to me because I have already thought about this 👍 I will make these changes tomorrow.

@ivogabe
Copy link
Owner

ivogabe commented Sep 11, 2018

Great! Let me know if you run into any problems.

Oleksandr Fediashov added 4 commits September 13, 2018 13:55
@layershifter
Copy link
Contributor Author

@ivogabe it seems that I made all requested changes. Can you please take a look? 😄

@ivogabe ivogabe merged commit d78c372 into ivogabe:master Sep 22, 2018
@ivogabe
Copy link
Owner

ivogabe commented Sep 22, 2018

Thanks Alexander for your work! Changes look good. It will be part of the next release.

@Yugloocamai
Copy link

Yugloocamai commented Nov 6, 2019

I am unable to get this feature working following the example in the docs :( The transformer is never called, even though it's imported successfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support custom transformers
3 participants