Skip to content

Feature request: add ability to dynamically determine inserted contents based on current file path #92

@EugeneGarbuzovPublic

Description

@EugeneGarbuzovPublic

Before version 4 it was possible to analyze the contents of the file being processed (and its path) and generate inserted content dynamically based on the current file data.

Starting from version 4, it seems to be impossible to analyze file data during modification.

Here is an example of how we used version 3 of the plugin:

new ModifySourcePlugin({
  rules: [
    {
      test: /antd\/[^/]+(\/[^/]+)?\/style\/[^/]+\.less/,
      modify: (src, filePath) => {
        const directoryPath = path.dirname(filePath);

        const variablesRelativePath = path.relative(
          directoryPath,
          'src/styles/common/variables.less',
        );

        const filePathMatch = filePath.match(
          /antd\/[^/]+\/([^/]+)\/style\/[^/]+\.less$/,
        );
        let overridesImport;

        if (!filePathMatch) {
          overridesImport = '';
        } else {
          const [, componentName] = filePathMatch;
          const overridesPath = `src/styles/ant/${componentName}.less`;
          const overridesRelativePath = path.relative(
            directoryPath,
            overridesPath,
          );
          overridesImport = existsSync(overridesPath)
            ? `@import '${overridesRelativePath}';${EOL}`
            : '';
        }

        return `${src}${EOL}@import '${variablesRelativePath}';${EOL}${overridesImport}`;
      },
    },
  ],
})

I thought that we needed to write a custom operation for it, but it seems like there is no way to get current file info inside the operation.

Is it possible to get the same behaviour with version 4?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions