vuejs / component-compiler-utils

Lower level utilities for compiling Vue single file components
319 stars 75 forks source link

Pass the filename option to compiler #74

Closed NE-SmallTown closed 4 years ago

NE-SmallTown commented 4 years ago

Pass the filename option to compiler, so the compiler can get and pass it down in some cases like transformNode/preTransformNode/postTransformNode

NE-SmallTown commented 4 years ago

@sodatea Would you mind take a look?

NE-SmallTown commented 4 years ago

Is there anything I could help to move this forward?

sodatea commented 4 years ago

What's the usage of it?

sodatea commented 4 years ago

If it's for the purpose mentioned in this issue https://github.com/vuejs/vue-loader/issues/1603 Then as Evan said, it's out of the scope of this package.

NE-SmallTown commented 4 years ago

IMO this is not related to vuejs/vue-loader#1603 (because my attention here is not to parse script/style), I want to do something like below:

  {
        test: /\.vue$/,
        use: [
          {
            loader: 'vue-loader',
          }
        ],
        exclude: path.resolve(__dirname, 'xxx')
  },

  {
        test: /\.vue$/,
        use: [
          {
            loader: 'vue-loader',
            options: {
              compilerOptions: {
                modules: [
                  {
                    transformNode: el => {
                      ...

                      return el;
                    },
                  },
                ],
              },
            },
          }
        ],
       include: path.resolve(__dirname, 'xxx'),
 }

but you will find that it doesn't work, the compilerOptions will be adopted for all vue files, so I think the way of this PR is the only (correct)way I could imagine to do this

sodatea commented 4 years ago

Do you have a reproduction repository?

sodatea commented 4 years ago

Could possibly be a bug in vue-loader

NE-SmallTown commented 4 years ago

Here is the repro: https://github.com/NE-SmallTown/vue-compiler-options-bug-repro , sorry have no time to dig into the src code of vue-loder

sodatea commented 4 years ago

It's a bug in vue-loader/lib/plugin that it only checks for the first rule that matches .vue files.

Next time please raise an issue before creating a PR because it could be an XY Problem (See also https://coolshell.cn/articles/10804.html for the Chinese version)

NE-SmallTown commented 4 years ago

Thanks for your response, but from my personal perspective, this is not a XY problem because this PR is not trying to fix the bug(but yes, you can "fix" it with this PR), its purpose is to make the compilerOptions.modules more flexible(the bug fix is just one part of the "flexible"), the misunderstanding of your focus move to XY problem may because I delete the beginning word 'if' in 'I want to do something like below:' during one edit

sodatea commented 4 years ago

What else use cases could this feature resolve? BTW in your implementation, the filename option would be passed to the vue-template-compiler instance, methods in modules still could not get it.

NE-SmallTown commented 4 years ago

What else use cases could this feature resolve?

For example, I want to generate some Identities based on the filename, BTW, there is no secure/privacy problem because compilerOptions is Opt-in.

BTW in your implementation, the filename option would be passed to the vue-template-compiler instance, methods in modules still could not get it.

I think it could because I test it locally and after that I create this PR, you can check it from this line, the options passed to preTransforms is from the parse function which received options from this PR.

sodatea commented 4 years ago

I think it could because I test it locally and after that I create this PR, you can check it from this line, the options passed to preTransforms is from the parse function which received options from this PR.

Thanks for the note. It is supported, though not well documented.


I'm still struggling to find good use cases for this option. Need some more time to think about it.

sodatea commented 4 years ago

@znck any thoughts?

vue-bot commented 4 years ago

Hey @NE-SmallTown, thank you for your time and effort spent on this PR, contributions like yours help make Vue better for everyone. Cheers! 💚

NE-SmallTown commented 4 years ago

Thanks.

Need to address the new filename field in vue-template-compiler type definitions;

Did you mean the TemplateCompileOptions type definition? I find that the definitions have been existed in the both RADME.md and compilerTemplate.ts in the init commit of component-compiler-utils

Need to provide similar functionalities in the new compiler-sfc package in Vue 3.

Don't know this before, seems it's a "TODO" package, at least for compilerTemplate.ts, so I think currently maybe it's not a follow-up of this PR?

Another work I can imagine is to update the dependency version in vue-loader

sodatea commented 4 years ago

Yeah, not a follow up, just backlog.

sodatea commented 4 years ago

Ideally, the type definition in the vue-template-compiler package should be updated and exported and we just import that in this package to replace the definition here