Skip to content

Improve @vue/babel-preset-app API by using explicit configuration #2031

@ntsim

Description

@ntsim

What problem does this feature solve?

Currently @vue/babel-preset-app receives a variety of configuration explicitly as options, but also receives a bunch of environment variable based configuration to bootstrap itself. Whilst innocent looking enough, this creates hidden dependencies that can only be ascertained by looking at the source code. This is somewhat at odds with other Babel presets/plugins/etc that usually require more explicit configurations.

The end result is somewhat 'magical' behaviour that can create a confusing user experience in certain scenarios.

Example

In my specific case, I was using one of the Jetbrains editors (Webstorm, etc), where it can run entire subsets of tests directly from the GUI. When using Jest, this is achieved by invoking Jest directly with a specific test pattern. A Babel transpilation step is also no problem as long as the configuration can explicitly be set somewhere i.e. .babelrc or babel.config.js.

Unfortunately, the GUI-based test runner does not work out-of-the-box with @vue/babel-preset-app because of the need for the environment variables to be set.

Workarounds

  1. Set the required environment variables directly in the GUI's test runner configuration. This could be confusing to such users and does not lead to a streamlined experience.

  2. Set the environment variables explicitly in the babel.config.js. This feels unclean and begs the question why the configuration options are not available directly in @vue/babel-preset-app.

    if (process.env.NODE_ENV === 'test') {
      process.env.VUE_CLI_BABEL_TARGET_NODE = true;
      process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true;
    }
    
    module.exports = {
      presets: [
        '@vue/app',
      ],
    };

What does the proposed API look like?

The configuration API of @vue/babel-preset-app could be improved by making it more transparent and explicit. More options could be added to either replace the existing environment variables or live alongside them (with less priority given to environment variables).

This could then be used in combination with a more 'typical' Babel configuration where we define different environment configurations:

module.exports = {
  presets: [
    '@vue/app'
  ],
  env: {
    test: {
      presets: [
        ['@vue/app', {
  	  targetNode: true,
  	  transpileModules: true,
  	}],
      ],
    },
  },
};

Note that the options given above do not have to be the same as above, but they are just mapped directly from the current environment variables that are used when running tests i.e. VUE_CLI_BABEL_TRANSPILE_MODULES and VUE_CLI_BABEL_TARGET_NODE.

This should remove any hidden dependencies on environment variables and allows us to have more fine-grained control over how @vue/babel-preset-app bootstraps Babel.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions