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

Wrapped plugin not working #475

Open
Kal-Aster opened this issue Nov 20, 2024 · 0 comments
Open

Wrapped plugin not working #475

Kal-Aster opened this issue Nov 20, 2024 · 0 comments
Labels
kind: support Asking for support with something or a specific use case solution: Rollup behavior This is Rollup's behavior and not specific to this plugin solution: workaround available There is a workaround available for this issue

Comments

@Kal-Aster
Copy link

Kal-Aster commented Nov 20, 2024

Troubleshooting

  1. Does tsc have the same output? If so, please explain why this is incorrect behavior

  2. Does your Rollup plugin order match this plugin's compatibility? If not, please elaborate

  3. Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction

What happens and why it is incorrect

I didn't answer the previous questions because I don't think this is a bug but probably a misuse of mine, but I don't understand what I'm doing wrong so I came here just asking help.

I pasted below my rollup config. There I'm using this plugin to transpile regular ts files and it works like a charm. Then I use transformRiotTypescript that leverages this same plugin just for some specific files.

Here the implementation of my plugin:

import typescript from "rollup-plugin-typescript2";

import { readFileSync } from "fs";

export default function transformRiotTypescript(options = {}, ) {
    const tsPlugin = typescript({
        ...options,
        include: [ "{,**/}*.riot" ]
    });

    const plugin = {
        ...tsPlugin,

        name: "transform-riot-typescript",

        transform(code, id) {
            if (!id.endsWith(".riot")) {
                return null;
            }
            try {
                const originalContent = readFileSync(id, "utf-8");

                const shouldTransform = originalContent && (
                    originalContent.includes(`<script lang="ts">`) ||
                    originalContent.includes(`<script type="ts">`)
                );
                
                if (shouldTransform) {
                    return tsPlugin.transform.call(this, code, id);
                }
            } catch (error) {
                this.warn(`Could not read original content of ${id}`);
            }


            return null;
        }
    };

    return plugin;
}

As you can see, I use the plugin as it is just overriding the transform function and the name.
So I expect it to work the same, but it actually doesn't work well in watch mode.

It does use the content of the files at the first load, and at each new build it doesn't update it, so the resulting transformed code is always the same as the first build.
That seemed weird to me since the "non-wrapped" plugin worked good.
After a bit (or not so bit >.>) of debugging I noticed that each build, the function buildStart is invoked and the servicesHost is created again losing all the versions, that then are set to 1 that is the same version of the previous build so it returns the same source file and doesn't update the content.

I worked around this behavior overriding the buildStart in my wrapping plugin as follows:

// ...
    let built = false;

    const plugin = {
        ...tsPlugin,

        name: "transform-riot-typescript",

        buildStart() {
            if (built) {
                return;
            }
            tsPlugin.buildStart.call(this);
            built = true;
        },
// ...

Doing so the plugin update the content correctly and it works very well.
Now: if the buildStart hook is invoked also for the "non-wrapped" plugin why don't they behave the same? Am I doing something that breaks the plugin?

Environment

Versions

  System:
    OS: Windows 10 10.0.19045
    CPU: (4) x64 Intel(R) Core(TM) i3-4160 CPU @ 3.60GHz
    Memory: 2.19 GB / 7.87 GB
  Binaries:
    Node: 20.11.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.8.1 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    rollup: 4.25 => 4.25.0
    rollup-plugin-typescript2: ^0.36.0 => 0.36.0
    typescript: ^5.6.3 => 5.6.3

rollup.config.js

:
import { config } from "dotenv";
config();

import riot from "rollup-plugin-riot";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "rollup-plugin-typescript2";
import nodeResolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";

import copyAfterReplacements from "./rollup-plugins/copyAfterReplacements.mjs";
import copyPackageFiles from "./rollup-plugins/copyPackageFiles.mjs";
import transformRiotTypescript from "./rollup-plugins/transformRiotTypescript.mjs";

const BASE_URL = process.env.BASE_URL;

export default [
    {
        input: "src/index.ts",
        plugins: [
            replace({
                "$BASE_URL$": BASE_URL,
                preventAssignment: true,
                delimiters: ["", ""]
            }),

            nodeResolve(),
            typescript(),
            commonjs(),

            riot(),
            transformRiotTypescript(),

            copyPackageFiles([
                {
                    source: "driver.js/dist/driver.css",
                    destination: "build/driver.css"
                }
            ]),
            copyAfterReplacements([
                {
                    source: "src/index.html",
                    destination: "build/index.html"
                },
                {
                    source: "src/site.webmanifest",
                    destination: "build/site.webmanifest"
                }
            ], [
                { from: "$BASE_URL$", to: BASE_URL }
            ])
        ],
        output: {
            file: "build/js/index.js",
            format: "umd"
        }
    }
];
@agilgur5 agilgur5 added solution: workaround available There is a workaround available for this issue kind: support Asking for support with something or a specific use case solution: Rollup behavior This is Rollup's behavior and not specific to this plugin labels Nov 21, 2024
@agilgur5 agilgur5 changed the title Reused plugin not working Wrapped plugin not working Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: support Asking for support with something or a specific use case solution: Rollup behavior This is Rollup's behavior and not specific to this plugin solution: workaround available There is a workaround available for this issue
Projects
None yet
Development

No branches or pull requests

2 participants