-
Notifications
You must be signed in to change notification settings - Fork 71
Description
Troubleshooting
-
Does
tschave the same output? If so, please explain why this is incorrect behavior -
Does your Rollup plugin order match this plugin's compatibility? If not, please elaborate
-
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
:
rollup.config.jsimport { 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"
}
}
];