-
Notifications
You must be signed in to change notification settings - Fork 130
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
Conversation
83732b0
to
e2907e8
Compare
@ivogabe can you please review? |
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 An alternative would be to change the type to |
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semicolon
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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}` | ||
) |
There was a problem hiding this comment.
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}` | ||
) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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(), |
There was a problem hiding this comment.
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?
test/customTransformers/gulptask.js
Outdated
module.exports = function(newTS, lib, output, reporter) { | ||
var getCustomTransformers = function () { | ||
return { | ||
before: [transformer], |
There was a problem hiding this comment.
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)
@ivogabe thanks for feedback!
Nope, you're fully right.
Looks good to me because I have already thought about this 👍 I will make these changes tomorrow. |
Great! Let me know if you run into any problems. |
…nto custom-transformers # Conflicts: # lib/compiler.ts
@ivogabe it seems that I made all requested changes. Can you please take a look? 😄 |
Thanks Alexander for your work! Changes look good. It will be part of the next release. |
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. |
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.