Skip to content

feat: add support for vue-i18n custom blocks by default #227

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

Merged
merged 4 commits into from
Mar 19, 2020

Conversation

JessicaSachs
Copy link
Contributor

@JessicaSachs JessicaSachs commented Mar 10, 2020

This PR adds support for an extensible API to parse and process custom blocks.

See the rendered README for API changes and writing a processor

// jest.config.js
module.exports = {
  // ...
  globals: {
    'vue-jest': {
      transform: {
        'your-custom-block': './your-transformer.js'
      }
    }
  }
}

@JessicaSachs JessicaSachs force-pushed the feat/support-custom-i18n-blocks-natively branch 2 times, most recently from da97129 to 3013e13 Compare March 10, 2020 23:08
@JessicaSachs JessicaSachs force-pushed the feat/support-custom-i18n-blocks-natively branch from 3013e13 to 832450e Compare March 10, 2020 23:18
@JessicaSachs JessicaSachs force-pushed the feat/support-custom-i18n-blocks-natively branch from 832450e to c49525e Compare March 10, 2020 23:23
@kazupon
Copy link
Member

kazupon commented Mar 11, 2020

@JessicaSachs
Thank you for your support !

I'm glad you support i18n custom blocks by default in vue-jest.
However, if we will consider the maintainability of vue-jest, I think that it's should provide an interface that supports transform to code for custom blocks.

vue-jest seem that have already a mechanism that user can transform to code.
You can see the code in processStyle of lib/process-style.js.

https://github.com/vuejs/vue-jest/blob/master/lib/process-style.js#L67-L93

In the above, the custom transformer path gets from the jest config and require it, and the code is generated by custom transformer.

As well as, I think we can do custom blocks.
The custom transformer need to provide with library author like vue-i18n, but I don't think it's a problem because it's the responsibility of side providing custom blocks.

@JessicaSachs
Copy link
Contributor Author

JessicaSachs commented Mar 11, 2020 via email

@JessicaSachs JessicaSachs force-pushed the feat/support-custom-i18n-blocks-natively branch from 1dc4982 to 7a2e92a Compare March 18, 2020 08:36
@JessicaSachs
Copy link
Contributor Author

OK! @kazupon, @lmiller1990, @afontcu, and @dobromir-hristov would love feedback from any and all of you 🙇‍♀

Copy link
Member

@lmiller1990 lmiller1990 left a comment

Choose a reason for hiding this comment

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

I think the code looks fine, mostly questions rather than real feedback!

return JSON.stringify(JSON.parse(convert(content, langKnown)))
}

try {
Copy link
Member

Choose a reason for hiding this comment

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

do we need a warning here? "Your file is not valid JSON or YAML".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could do. I matched the existing code.

const codes = blocks.map(block => {
if (block.type === 'i18n') {
const value = parseLanguageAndContent(block, filename)
.replace(/\u2028/g, '\\u2028')
Copy link
Member

Choose a reason for hiding this comment

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

What do these codes represent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They're line seperators, but really, I have no idea, I copied them from vue-i18n-jest

Copy link
Member

Choose a reason for hiding this comment

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

ok, vue-i18n is battle tested, going to assume kazupon knows what he is doing

const { getVueJestConfig, getCustomTransformer } = require('./utils')
const vueOptionsNamespace = require('./constants').vueOptionsNamespace

function applyTransformer(
Copy link
Member

Choose a reason for hiding this comment

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

Should we try to match the jest process api a bit more closer? That would be `applyTransform(blocks, filename, config, transformer, vueOptionsNamespace) I guess. Although maybe more awkard to call then - just thinking out loud

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 wasn't sure about this either. I think if there was no external context, like if jest process didn't exist, maybe an object-based api would be best, because there's a lot of optional params.

Copy link
Member

Choose a reason for hiding this comment

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

Object based works well for me too - what do you think? I really like to use an object when there are more than 1-2 params, since it is easier to read.

If it's not too much trouble that change could go with that? I don't mind too much, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updating it for this

@@ -21,7 +24,7 @@ module.exports = function generateCode(
}

output +=
`var __options__ = typeof exports.default === 'function' ` +
`var ${namespace} = typeof exports.default === 'function' ` +
Copy link
Member

Choose a reason for hiding this comment

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

Any specific reason to move the constant to another file? It seems to be used elsewhere when it wasn't before - why is that?

Copy link
Contributor Author

@JessicaSachs JessicaSachs Mar 18, 2020

Choose a reason for hiding this comment

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

Because it was being hardcoded inside of vue-i18n-jest and Edd refactored this in v4, which broke vue-i18n-jest if we were to have released v4 of vue-jest... We should make any required constants passed in rather than requiring developers to dig into vue-jest source code to figure out what constants we're using when they're writing their processors

Copy link
Member

Choose a reason for hiding this comment

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

Right, makes sense, sounds good

@JessicaSachs JessicaSachs force-pushed the feat/support-custom-i18n-blocks-natively branch from d53e699 to df2bfa2 Compare March 19, 2020 02:20
@kazupon
Copy link
Member

kazupon commented Mar 19, 2020

@JessicaSachs
Thanks a lot!.

I'm realy gload to support vue-i18n as default at vue-jest.
But the supporting at least one custom block will seem unfair to the creator of the library that provides the custom block.

I hope that vue-jest will offer I/F only for custome blocks without natively supporting vue-i18n. :)

@JessicaSachs
Copy link
Contributor Author

JessicaSachs commented Mar 19, 2020 via email

@JessicaSachs JessicaSachs force-pushed the feat/support-custom-i18n-blocks-natively branch from 1684495 to 2f4901a Compare March 19, 2020 18:09
@JessicaSachs JessicaSachs force-pushed the feat/support-custom-i18n-blocks-natively branch from 2f4901a to a71a99b Compare March 19, 2020 18:11
@JessicaSachs JessicaSachs merged commit d6c4e47 into master Mar 19, 2020
@JessicaSachs
Copy link
Contributor Author

JessicaSachs commented Mar 19, 2020

@kazupon Here is the file tree with the final implementation of vue-i18n natively. As discussed, I removed it from this PR.

Please feel free to adapt this into your own package. I made some improvements on the existing wrapper you built, so please take a look 🙇‍♀

@kazupon
Copy link
Member

kazupon commented Mar 20, 2020

@JessicaSachs
Okay! Thanks!

@@ -72,7 +73,8 @@
"chalk": "^2.1.0",
"convert-source-map": "^1.6.0",
"extract-from-css": "^0.4.4",
"source-map": "^0.5.6",
Copy link

Choose a reason for hiding this comment

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

@JessicaSachs this package is used here

const sourceMap = require('source-map')

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.

3 participants