Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Trim relative portions from the paths #32

Closed
wants to merge 3 commits into from
Closed

Trim relative portions from the paths #32

wants to merge 3 commits into from

Conversation

toadzky
Copy link
Contributor

@toadzky toadzky commented Feb 3, 2016

If a file that has message descriptors resolves to a relative path starting with .., it will create the json files right next to the js files, instead of in the messagesDir. This fixes that, and I think will fix #23.

@@ -154,9 +154,11 @@ export default function () {
file.metadata['react-intl'] = {messages: descriptors};

if (opts.messagesDir && descriptors.length > 0) {
let relativePath = p.relative(process.cwd(), filename);
relativePath = relativePath.replace(/\.{2}\//g, '');
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be best to use

p.normalize(p.sep + relativePath);

@ericf
Copy link
Collaborator

ericf commented Feb 3, 2016

@toadzky have you run into this issue? If so, I'm curious to know why it came up in your project.

@toadzky
Copy link
Contributor Author

toadzky commented Feb 3, 2016

The project I'm working on is structured like this:

./
  i18n/
    messages/   <------ where i want the messages extracted to
  services/
    serviceA/
      code
    serviceB/
      code
  shared/
     common-components/
       code

We use a node module to resolve sibling directories without relative paths, so files in serviceA can just import things from 'shared/common-components'. Each service has it's own package.json and it's own build script. When the build script runs the babel plugin, the messages extracted from the service directory show up in i18n/messages correctly, but the messages extracted from shared are written to json files in the same directory as the original javascript it was extracted from.

Here's my plugin config:

plugins: [
            [require.resolve('babel-plugin-react-intl'), {
              "messagesDir": '../../i18n/messages',
              "enforceDescriptions": true
            }]
          ]

When I was debugging it, I hacked on a bunch of console.log into the plugin in my node_modules folder and saw that the result of the p.join(...) was ending up as just the relative path to the original file. So p.join('../../i18n/messages', '../../shared/common...', filename + '.json') ends up ../../shared/common.../filename.json. I'm not thoroughly familiar with the path module, so I'm not sure why.

@toadzky
Copy link
Contributor Author

toadzky commented Feb 5, 2016

@ericf is there anything else on this PR or can this be merged? I prefer not to depend on personal forks of projects for too long if I can avoid it.

@ericf
Copy link
Collaborator

ericf commented Feb 6, 2016

I rebased this into one commit and merged it. Thanks!

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

Successfully merging this pull request may close these issues.

directory gets created outside of messagesDir
2 participants