Skip to content

Commit

Permalink
fix: don't break when another plugin declares an asset that only late…
Browse files Browse the repository at this point in the history
…r receives a source. Fixes #162
  • Loading branch information
wessberg committed Jan 3, 2022
1 parent a2e88e5 commit 3ec4c86
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/plugin/typescript-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
generateBundle(this: PluginContext, outputOptions: OutputOptions, bundle: OutputBundle): void {
// If debugging is active, log the outputted files
for (const file of Object.values(bundle)) {
if (!("fileName" in file)) continue;

const normalizedFileName = path.normalize(file.fileName);
const text = "code" in file ? file.code : file.source.toString();
if (shouldDebugEmit(pluginOptions.debug, normalizedFileName, text, "javascript")) {
Expand All @@ -481,7 +483,6 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
if (Boolean(parsedCommandLineResult.parsedCommandLine.options.incremental) || Boolean(parsedCommandLineResult.parsedCommandLine.options.composite)) {
emitBuildInfo({
host,
bundle,
outputOptions,
pluginOptions,
pluginContext: this
Expand Down
3 changes: 1 addition & 2 deletions src/service/emit/tsbuildinfo/emit-build-info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {OutputBundle, OutputOptions, PluginContext} from "rollup";
import {OutputOptions, PluginContext} from "rollup";
import {CompilerHost} from "../../compiler-host/compiler-host";
import {TypescriptPluginOptions} from "../../../plugin/typescript-plugin-options";
import path from "crosspath";
Expand All @@ -9,7 +9,6 @@ import {logEmit} from "../../../util/logging/log-emit";

export interface EmitBuildInfoOptions {
pluginContext: PluginContext;
bundle: OutputBundle;
host: CompilerHost;
pluginOptions: TypescriptPluginOptions;
outputOptions: OutputOptions;
Expand Down
9 changes: 8 additions & 1 deletion src/util/is-output-chunk/is-output-chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ import {OutputChunk, OutputAsset} from "rollup";
* Returns true if the given asset is an OutputChunk
*/
export function isOutputChunk(thing: OutputChunk | OutputAsset): thing is OutputChunk {
return !("isAsset" in thing);
return thing.type === "chunk";
}

/**
* Returns true if the given asset is an OutputChunk
*/
export function isOutputAssetOrOutputChunk(thing: OutputChunk | OutputAsset): thing is OutputChunk|OutputAsset {
return thing.type === "chunk" || thing.type === "asset";
}
2 changes: 1 addition & 1 deletion src/util/take-bundled-filenames/take-bundled-filenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function takeBundledFilesNames(bundle: OutputBundle): Set<string> {
Object.values(bundle).forEach(value => {
if (isOutputChunk(value)) {
Object.keys(value.modules).forEach(fileName => bundledFilenames.add(path.normalize(fileName)));
} else {
} else if ("fileName" in value) {
bundledFilenames.add(path.normalize(value.fileName));
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/babel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ test.serial("Will use the proper @babel/runtime/helpers/esm helpers when format
}
} = bundle;

t.deepEqual(formatCode(file.code), formatCode(`import '@babel/runtime/helpers/esm/typeof';`));
t.true(formatCode(file.code).includes(`@babel/runtime/helpers/esm/typeof`));
});

test.serial("Will use the proper @babel/runtime/helpers helpers when format is CJS. #1", withTypeScript, async (t, {typescript}) => {
Expand Down
46 changes: 46 additions & 0 deletions test/deferred-assets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import test from "ava";
import {withTypeScript} from "./util/ts-macro";
import {generateRollupBundle} from "./setup/setup-rollup";
import {formatCode} from "./util/format-code";

test.serial("Won't break when other plugins declare deferred assets. #1", withTypeScript, async (t, {typescript}) => {
let asset: string | undefined;

const bundle = await generateRollupBundle(
[
{
entry: true,
fileName: "index.ts",
text: `\
console.log('whatever');`
}
],
{
typescript,
postPlugins: [
{
name: "my-plugin",
buildStart() {
asset = this.emitFile({
type: "asset",
fileName: "test.txt"
});
},
generateBundle() {
this.setAssetSource(asset!, "whatever");
}
}
]
}
);
const {
declarations: [file]
} = bundle;

t.deepEqual(
formatCode(file.code),
formatCode(`\
export {};
`)
);
});
26 changes: 14 additions & 12 deletions test/setup/test-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,20 @@ export function createBuiltInModuleTestFiles(...modules: ("fs" | "globals" | "bu
}
`
},
{
entry: false,
fileName: "node_modules/@types/node/index.d.ts",
text: `
/// <reference path="./${module}.d.ts" />
`
},
...modules.map(module => ({
entry: false,
fileName: `node_modules/@types/node/${module}.d.ts`,
text: fsWorker.readFile(path.native.join(nodeTypesDir, `${module}.d.ts`)) ?? ""
}))
...modules.flatMap(module => [
{
entry: false,
fileName: "node_modules/@types/node/index.d.ts",
text: `
/// <reference path="./${module}.d.ts" />
`
},
{
entry: false,
fileName: `node_modules/@types/node/${module}.d.ts`,
text: fsWorker.readFile(path.native.join(nodeTypesDir, `${module}.d.ts`)) ?? ""
}
])
];
}

Expand Down

0 comments on commit 3ec4c86

Please sign in to comment.