Skip to content

Commit

Permalink
bundler tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
paperdave committed Apr 26, 2023
1 parent 68ab71e commit 6a5b3f0
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 65 deletions.
49 changes: 49 additions & 0 deletions test/bundler/bundler_edgecase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,53 @@ describe("bundler", () => {
ARBITRARY: "secret environment stuff!",
},
});
itBundled("edgecase/StarExternal", {
files: {
"/entry.js": /* js */ `
import { foo } from './foo';
import { bar } from './bar';
console.log(foo);
`,
},
external: ["*"],
});
itBundled("edgecase/ImportNamespaceAndDefault", {
files: {
"/entry.js": /* js */ `
import def2, * as ns2 from './c'
console.log(def2, JSON.stringify(ns2))
`,
},
external: ["*"],
runtimeFiles: {
"/c.js": /* js */ `
export default 1
export const ns = 2
export const def2 = 3
`,
},
run: {
stdout: '1 {"def2":3,"default":1,"ns":2}',
},
});
itBundled("edgecase/ExternalES6ConvertedToCommonJSSimplified", {
files: {
"/entry.js": /* js */ `
console.log(JSON.stringify(require('./e')));
`,
"/e.js": `export * from 'x'`,
},
external: ["x"],
runtimeFiles: {
"/node_modules/x/index.js": /* js */ `
export const ns = 123
export const ns2 = 456
`,
},
run: {
stdout: `
{"ns":123,"ns2":456}
`,
},
});
});
37 changes: 37 additions & 0 deletions test/bundler/bundler_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,41 @@ describe("bundler", () => {
},
};
});
itBundled("plugin/ManyFiles", ({ root }) => {
const FILES = 200;
const create = (fn: (i: number) => string) => new Array(FILES).fill(0).map((_, i) => fn(i));

let onResolveCount = 0;
let importers: string[] = [];
return {
files: {
"index.ts": /* ts */ `
${create(i => `import * as foo${i} from "./${i}.magic";`).join("\n")}
${create(i => `console.log(foo${i}.foo);`).join("\n")}
`,
},
plugins(builder) {
builder.onResolve({ filter: /\.magic$/ }, async args => {
importers.push(args.importer);
onResolveCount++;
return {
path: args.path,
namespace: "magic",
};
});
builder.onLoad({ filter: /\.magic$/, namespace: "magic" }, async args => {
return {
contents: `export const foo = "${args.path}";`,
loader: "js",
};
});
},
run: {
stdout: create(i => `./${i}.magic`).join("\n"),
},
onAfterBundle(api) {},
};
});
});

// TODO: add async on resolve stuff
59 changes: 19 additions & 40 deletions test/bundler/esbuild/dce.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import dedent from "dedent";
import { itBundled, testForFile } from "../expectBundled";
import { ESBUILD, itBundled, testForFile } from "../expectBundled";
var { describe, test, expect } = testForFile(import.meta.path);

// Tests ported from:
Expand Down Expand Up @@ -88,7 +88,7 @@ describe("bundler", () => {
`,
},
run: {
stdout: 'hello\n{"default":{"foo":123},"foo":123}',
stdout: 'hello\n{"foo":123}',
},
});
itBundled("dce/PackageJsonSideEffectsTrueKeepES6", {
Expand Down Expand Up @@ -704,7 +704,6 @@ describe("bundler", () => {
},
});
itBundled("dce/PackageJsonSideEffectsFalseIntermediateFilesChainAll", {
// GENERATED
files: {
"/Users/user/project/src/entry.js": /* js */ `
import {foo} from "a"
Expand Down Expand Up @@ -1038,35 +1037,6 @@ describe("bundler", () => {
stdout: `["F",{"children":[null,{"children":["div",{}]}]}]`,
},
});
// TODO: Unsure how to port this: https://github.com/evanw/esbuild/blob/main/internal/bundler_tests/bundler_dce_test.go#L1249
itBundled("dce/DisableTreeShaking", {
notImplemented: true,
// GENERATED
files: {
"/entry.jsx": /* jsx */ `
import './remove-me'
function RemoveMe1() {}
let removeMe2 = 0
class RemoveMe3 {}
import './keep-me'
function KeepMe1() {}
let keepMe2 = <KeepMe1/>
function keepMe3() { console.log('side effects') }
let keepMe4 = /* @__PURE__ */ keepMe3()
let keepMe5 = pure()
let keepMe6 = some.fn()
`,
"/remove-me.js": `export default 'unused'`,
"/keep-me/index.js": `console.log('side effects')`,
"/keep-me/package.json": `{ "sideEffects": false }`,
},
ignoreDCEAnnotations: true,
define: {
pure: "???",
"some.fn": "???",
},
});
itBundled("dce/DeadCodeFollowingJump", {
notImplemented: true,
files: {
Expand Down Expand Up @@ -1308,6 +1278,7 @@ describe("bundler", () => {
dce: true,
});
itBundled("dce/TreeShakingClassStaticProperty", {
notImplemented: true,
files: {
"/entry.js": /* js */ `
let remove1 = class { static x }
Expand Down Expand Up @@ -1421,6 +1392,7 @@ describe("bundler", () => {
format: "iife",
});
itBundled("dce/TreeShakingNoBundleESM", {
notImplemented: true,
files: {
"/entry.js": /* js */ `
function keep() {}
Expand All @@ -1434,27 +1406,27 @@ describe("bundler", () => {
dce: true,
});
itBundled("dce/TreeShakingNoBundleCJS", {
// GENERATED
files: {
"/entry.js": /* js */ `
function keep() {}
function REMOVE() {}
keep()
`,
},
dce: true,
format: "cjs",
treeShaking: true,
mode: "transform",
});
itBundled("dce/TreeShakingNoBundleIIFE", {
// GENERATED
files: {
"/entry.js": /* js */ `
function keep() {}
function REMOVE() {}
keep()
`,
},
dce: true,
format: "iife",
treeShaking: true,
mode: "transform",
Expand Down Expand Up @@ -1483,7 +1455,6 @@ describe("bundler", () => {
},
});
itBundled("dce/DCETypeOf", {
// GENERATED
files: {
"/entry.js": /* js */ `
// These should be removed because they have no side effects
Expand Down Expand Up @@ -1690,6 +1661,7 @@ describe("bundler", () => {
dce: true,
});
itBundled("dce/RemoveUnusedImports", {
notImplemented: true,
files: {
"/entry.js": /* js */ `
import REMOVE1 from 'a'
Expand All @@ -1698,8 +1670,8 @@ describe("bundler", () => {
`,
},
minifySyntax: true,
mode: "transform",
dce: true,
external: ["a", "b", "c"],
onAfterBundle(api) {
api.expectFile("/out.js").toBe(
dedent`
Expand All @@ -1721,6 +1693,7 @@ describe("bundler", () => {
},
minifySyntax: true,
mode: "transform",
external: ["a", "b", "c"],
dce: true,
});
itBundled("dce/RemoveUnusedImportsEvalTS", {
Expand Down Expand Up @@ -2157,6 +2130,7 @@ describe("bundler", () => {
},
});
itBundled("dce/InlineFunctionCallBehaviorChanges", {
notImplemented: true,
files: {
// At the time of writing, using a template string here triggered a bug in bun's transpiler
// making it impossible to run the test.
Expand Down Expand Up @@ -2238,6 +2212,7 @@ describe("bundler", () => {
dce: true,
});
itBundled("dce/ConstValueInliningNoBundle", {
notImplemented: true,
files: {
"/top-level.js": /* js */ `
// These should be kept because they are top-level and tree shaking is not enabled
Expand Down Expand Up @@ -2290,6 +2265,7 @@ describe("bundler", () => {
s_keep, s_keep,
)
}
console.log(nested())
`,
"/namespace-export.ts": /* ts */ `
namespace ns {
Expand Down Expand Up @@ -2343,10 +2319,12 @@ describe("bundler", () => {
function foo() {
return y_REMOVE
}
console.log(foo)
}
console.log(nested());
`,
"/disabled-tdz.js": /* js */ `
foo()
console.log(foo())
const x_keep = 1
function foo() {
return x_keep
Expand All @@ -2369,6 +2347,7 @@ describe("bundler", () => {
y, y,
)
}
console.log(foo())
`,
},
entryPoints: [
Expand All @@ -2386,11 +2365,11 @@ describe("bundler", () => {
"/backwards-reference-top-level.js",
"/backwards-reference-nested-function.js",
],
mode: "transform",
minifySyntax: true,
dce: true,
dceKeepMarkerCount: {
"/out/top-level.js": 7,
"/out/top-level.js": 5,
"/out/nested-function.js": 3,
"/out/namespace-export.js": 1,
},
});
Expand Down Expand Up @@ -2539,6 +2518,7 @@ describe("bundler", () => {
},
});
itBundled("dce/ConstValueInliningDirectEval", {
notImplemented: true,
files: {
"/top-level-no-eval.js": /* js */ `
const keep = 1
Expand Down Expand Up @@ -2873,7 +2853,6 @@ describe("bundler", () => {
},
});
itBundled("dce/NestedFunctionInliningWithSpread", {
// GENERATED
files: {
"/entry.js": /* js */ `
function empty1() {}
Expand Down
Loading

0 comments on commit 6a5b3f0

Please sign in to comment.