Skip to content

Commit

Permalink
Some updates to the bundle command
Browse files Browse the repository at this point in the history
  • Loading branch information
witemple-msft committed Jan 13, 2022
1 parent 11cee51 commit ae9f7a6
Showing 1 changed file with 59 additions and 24 deletions.
83 changes: 59 additions & 24 deletions common/tools/dev-tool/src/commands/run/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import path from "path";

import * as rollup from "rollup";
import nodeBuiltins from "builtin-modules";

Expand All @@ -12,9 +14,9 @@ import json from "@rollup/plugin-json";
import multiEntry from "@rollup/plugin-multi-entry";

import { leafCommand, makeCommandInfo } from "../../framework/command";
import { resolveProject } from "../../util/resolveProject";
import { makeOnWarnForTesting } from "../../config/rollup.base.config";
import { resolveProject, resolveRoot } from "../../util/resolveProject";
import { createPrinter } from "../../util/printer";
import { makeOnWarnForTesting } from "../../config/rollup.base.config";

const log = createPrinter("bundle");

Expand All @@ -32,38 +34,66 @@ export const commandInfo = makeCommandInfo(
default: true,
description: "build a bundle for browser testing",
},
"polyfill-node": {
kind: "boolean",
default: true,
description: "include a polyfill for Node.js builtin modules",
},
}
);

export default leafCommand(commandInfo, async ({ production, "browser-test": browserTest }) => {
export default leafCommand(commandInfo, async (options) => {
const info = await resolveProject(process.cwd());

if (production) {
if (!info.packageJson.module) {
log.error(info.name, "does not specify a `module` field.");
return false;
}

if (options.production) {
const baseConfig: rollup.RollupOptions = {
// Use the package's module field if it has one
input: info.packageJson["module"] ?? "dist-esm/src/index.js",
input: info.packageJson.module,
external: [
...nodeBuiltins,
...Object.keys(info.packageJson.dependencies),
...Object.keys(info.packageJson.devDependencies),
],
preserveSymlinks: false,
plugins: [sourcemaps(), nodeResolve(), cjs()],
plugins: [nodeResolve(), sourcemaps()],
};

const bundle = await rollup.rollup(baseConfig);
try {
const bundle = await rollup.rollup(baseConfig);

await bundle.write({
file: "dist/index.js",
format: "cjs",
sourcemap: true,
exports: "named",
});
await bundle.write({
file: "dist/index.js",
format: "cjs",
sourcemap: true,
exports: "named",
});
} catch (error) {
log.error(error);
return false;
}

log.success("Created production CommonJS bundle.");
}

if (browserTest) {
if (options["browser-test"]) {
const pnpmStore = path
.relative(
process.cwd(),
path.join(await resolveRoot(), "common", "temp", "node_modules", ".pnpm")
)
.replace(new RegExp(path.sep, "g"), "/");

log.debug("Computed PNPM store relative path:", pnpmStore);

// Get a glob for a package name in the PNPM store
const globFromStore = (name: string): string =>
[pnpmStore, name.split("/").join("+"), "@*", "**/*.js"].join("/");

const browserTestConfig = {
input: {
include: ["dist-esm/test/**/*.spec.js"],
Expand All @@ -76,13 +106,13 @@ export default leafCommand(commandInfo, async ({ production, "browser-test": bro
mainFields: ["module", "browser"],
preferBuiltins: false,
}),
nodePolyfills(),
...(options["polyfill-node"] ? [nodePolyfills({ sourceMap: true })] : []),
cjs({
dynamicRequireTargets: ["**/*/node_modules/**/chai@*/**/*"],
dynamicRequireTargets: [globFromStore("chai")],
esmExternals: true,
}),
json(),
sourcemaps(),
//viz({ filename: "dist-test/browser-stats.html", sourcemap: true })
json(),
],
onwarn: makeOnWarnForTesting(),
// Disable tree-shaking of test code. In rollup-plugin-node-resolve@5.0.0,
Expand All @@ -92,13 +122,18 @@ export default leafCommand(commandInfo, async ({ production, "browser-test": bro
treeshake: false,
};

const browserBundle = await rollup.rollup(browserTestConfig as any);
try {
const browserBundle = await rollup.rollup(browserTestConfig as any);

await browserBundle.write({
file: `dist-test/index.browser.js`,
format: "umd",
sourcemap: true,
});
await browserBundle.write({
file: `dist-test/index.browser.js`,
format: "umd",
sourcemap: true,
});
} catch (error) {
log.error(error);
return false;
}

log.success("Created browser testing bundle.");
}
Expand Down

0 comments on commit ae9f7a6

Please sign in to comment.