Skip to content
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

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

Open
EugeneGarbuzovPublic opened this issue Jun 9, 2023 · 0 comments

Comments

@EugeneGarbuzovPublic
Copy link

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?

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

No branches or pull requests

1 participant