-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dev-tool] Shared rollup config factory (#10923)
* WIP * [dev-tool] Abstract rollup config factory * fix typo * Fixed typeck error * dependency cleanup * Removed errant test config from template * Fixed template karma configuration * Removed ghost files, small tweak to base config * Removed explicit config env from template package.json * Removed IS_PRODUCTION gates * Removed dependency on terser * Removed dependency on fs-extra * Added flag to disable browser test bundle * Removed karma-typescript
- Loading branch information
1 parent
51d19c0
commit 8bb505f
Showing
24 changed files
with
1,917 additions
and
1,351 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
// This shim file simply loads ts-node and then requires the | ||
// TypeScript file corresponding to the rollup base config | ||
|
||
const { join } = require("path"); | ||
|
||
require("ts-node").register({ | ||
transpileOnly: true, | ||
project: join(__dirname, "../tsconfig.json") | ||
}); | ||
|
||
module.exports = require(join(__dirname, "../src/config/rollup.base.config.ts")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// Licensed under the MIT license. | ||
|
||
/** | ||
* Some delcarations for plugins that have missing/old declarations in | ||
* NPM. | ||
*/ | ||
|
||
// #region rollup | ||
|
||
type Plugin = import("rollup").Plugin; | ||
|
||
declare module "rollup-plugin-sourcemaps" { | ||
export default function(): Plugin; | ||
} | ||
|
||
declare module "rollup-plugin-node-globals" { | ||
export default function(): Plugin; | ||
} | ||
|
||
declare module "rollup-plugin-node-builtins" { | ||
export default function(): Plugin; | ||
} | ||
|
||
declare module "@rollup/plugin-multi-entry" { | ||
interface MultiEntryOptions { | ||
exports: boolean; | ||
} | ||
export default function(opts: MultiEntryOptions): Plugin; | ||
} | ||
|
||
// #endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { RollupOptions, RollupWarning, WarningHandler } from "rollup"; | ||
|
||
import nodeResolve from "@rollup/plugin-node-resolve"; | ||
import cjs from "@rollup/plugin-commonjs"; | ||
import sourcemaps from "rollup-plugin-sourcemaps"; | ||
import multiEntry from "@rollup/plugin-multi-entry"; | ||
import json from "@rollup/plugin-json"; | ||
import nodeBuiltinsPlugin from "rollup-plugin-node-builtins"; | ||
import nodeGlobals from "rollup-plugin-node-globals"; | ||
|
||
import nodeBuiltins from "builtin-modules"; | ||
|
||
interface PackageJson { | ||
name: string; | ||
module: string; | ||
dependencies: Record<string, string>; | ||
devDependencies: Record<string, string>; | ||
} | ||
|
||
// #region Warning Handler | ||
|
||
function ignoreNiseSinonEvalWarnings(warning: RollupWarning): boolean { | ||
return ( | ||
warning.code === "EVAL" && | ||
(warning.id?.includes("node_modules/nise") || warning.id?.includes("node_modules/sinon")) === | ||
true | ||
); | ||
} | ||
|
||
function ignoreChaiCircularDependencyWarnings(warning: RollupWarning): boolean { | ||
return ( | ||
warning.code === "CIRCULAR_DEPENDENCY" && | ||
warning.importer?.includes("node_modules/chai") === true | ||
); | ||
} | ||
|
||
const warningInhibitors: Array<(warning: RollupWarning) => boolean> = [ | ||
ignoreChaiCircularDependencyWarnings, | ||
ignoreNiseSinonEvalWarnings | ||
]; | ||
|
||
/** | ||
* Construct a warning handler for the shared rollup configuration | ||
* that ignores certain warnings that are not relevant to testing. | ||
*/ | ||
function makeOnWarnForTesting(): (warning: RollupWarning, warn: WarningHandler) => void { | ||
return (warning, warn) => { | ||
// If every inhibitor returns false (i.e. no inhibitors), then show the warning | ||
if (warningInhibitors.every((inhib) => !inhib(warning))) { | ||
warn(warning); | ||
} | ||
}; | ||
} | ||
|
||
// #endregion | ||
|
||
function makeBrowserTestConfig() { | ||
const config: RollupOptions = { | ||
input: ["dist-esm/test/{,!(node)/**/}*.spec.js"], | ||
output: { | ||
file: `dist-test/index.browser.js`, | ||
format: "umd", | ||
sourcemap: true, | ||
globals: { "fs-extra": "undefined" } | ||
}, | ||
preserveSymlinks: false, | ||
// fs-extra must be marked as external in order to avoid an initialization error | ||
external: ["fs-extra"], | ||
plugins: [ | ||
multiEntry({ exports: false }), | ||
nodeResolve({ | ||
mainFields: ["module", "browser"], | ||
preferBuiltins: true | ||
}), | ||
cjs({ | ||
namedExports: { | ||
chai: ["assert", "use"], | ||
"@opentelemetry/api": ["CanonicalCode", "SpanKind", "TraceFlags"] | ||
} | ||
}), | ||
json(), | ||
sourcemaps(), | ||
nodeGlobals(), | ||
nodeBuiltinsPlugin() | ||
//viz({ filename: "dist-test/browser-stats.html", sourcemap: true }) | ||
], | ||
onwarn: makeOnWarnForTesting(), | ||
// Disable tree-shaking of test code. In rollup-plugin-node-resolve@5.0.0, | ||
// rollup started respecting the "sideEffects" field in package.json. Since | ||
// our package.json sets "sideEffects=false", this also applies to test | ||
// code, which causes all tests to be removed by tree-shaking. | ||
treeshake: false | ||
}; | ||
|
||
// (config.external as string[]).push(...Object.keys(pkg.devDependencies)); | ||
|
||
return config; | ||
} | ||
|
||
export interface ConfigurationOptions { | ||
disableBrowserBundle: boolean; | ||
} | ||
|
||
const defaultConfigurationOptions: ConfigurationOptions = { | ||
disableBrowserBundle: false | ||
}; | ||
|
||
export function makeConfig(pkg: PackageJson, options?: Partial<ConfigurationOptions>) { | ||
options = { | ||
...defaultConfigurationOptions, | ||
...(options ?? {}) | ||
}; | ||
|
||
const baseConfig = { | ||
// Use the package's module field if it has one | ||
input: pkg["module"] ?? "dist-esm/src/index.js", | ||
external: [ | ||
...nodeBuiltins, | ||
...Object.keys(pkg.dependencies), | ||
...Object.keys(pkg.devDependencies) | ||
], | ||
output: { file: "dist/index.js", format: "cjs", sourcemap: true }, | ||
preserveSymlinks: false, | ||
plugins: [sourcemaps(), nodeResolve(), cjs()] | ||
}; | ||
|
||
const config: RollupOptions[] = [baseConfig as RollupOptions]; | ||
|
||
if (!options.disableBrowserBundle) { | ||
config.push(makeBrowserTestConfig()); | ||
} | ||
|
||
return config; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.