Skip to content

Commit

Permalink
[dev-tool] Shared rollup config factory (#10923)
Browse files Browse the repository at this point in the history
* 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
witemple-msft authored Sep 28, 2020
1 parent 51d19c0 commit 8bb505f
Show file tree
Hide file tree
Showing 24 changed files with 1,917 additions and 1,351 deletions.
2,585 changes: 1,699 additions & 886 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion common/tools/dev-tool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
},
"devDependencies": {
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@rollup/plugin-commonjs": "11.0.2",
"@rollup/plugin-json": "^4.0.0",
"@rollup/plugin-multi-entry": "^3.0.0",
"@rollup/plugin-node-resolve": "^8.0.0",
"@types/chai": "^4.1.6",
"@types/chai-as-promised": "^7.1.0",
"@types/chalk": "~2.2.0",
Expand All @@ -52,10 +56,16 @@
"@types/prettier": "~2.0.1",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"builtin-modules": "~3.1.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^6.1.0",
"mocha": "^7.1.1",
"rimraf": "^3.0.0"
"rimraf": "^3.0.0",
"rollup": "^1.16.3",
"rollup-plugin-node-builtins": "~2.1.2",
"rollup-plugin-node-globals": "~1.4.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-visualizer": "^4.0.4"
}
}
14 changes: 14 additions & 0 deletions common/tools/dev-tool/shared-config/rollup.js
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"));
32 changes: 32 additions & 0 deletions common/tools/dev-tool/src/ambient.d.ts
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
134 changes: 134 additions & 0 deletions common/tools/dev-tool/src/config/rollup.base.config.ts
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;
}
4 changes: 2 additions & 2 deletions sdk/formrecognizer/ai-form-recognizer/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ module.exports = function(config) {

// list of files / patterns to load in the browser
files: [
"test-browser/index.js",
{ pattern: "test-browser/index.js.map", type: "html", included: false, served: true }
"dist-test/index.browser.js",
{ pattern: "dist-test/index.browser.js.map", type: "html", included: false, served: true }
].concat(isPlaybackMode() || isSoftRecordMode() ? ["recordings/browsers/**/*.json"] : []),

// list of files / patterns to exclude
Expand Down
17 changes: 3 additions & 14 deletions sdk/formrecognizer/ai-form-recognizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"build:browser": "tsc -p . && cross-env ONLY_BROWSER=true rollup -c 2>&1",
"build:node": "tsc -p . && cross-env ONLY_NODE=true rollup -c 2>&1",
"build:samples": "dev-tool samples prep && cd dist-samples && tsc -p .",
"build:test": "tsc -p . && rollup -c rollup.test.config.js 2>&1",
"build:test": "tsc -p . && rollup -c 2>&1",
"build": "tsc -p . && rollup -c 2>&1 && api-extractor run --local",
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"clean": "rimraf dist dist-esm dist-browser dist-test test-browser temp types *.tgz *.log",
Expand All @@ -70,7 +70,7 @@
"test:node": "npm run build:test && npm run unit-test:node && npm run integration-test:node",
"test": "npm run build:test && npm run unit-test && npm run integration-test",
"unit-test:browser": "karma start --single-run",
"unit-test:node": "mocha --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 120000 --full-trace dist-test/index.node.js",
"unit-test:node": "mocha -r esm --require ts-node/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace \"test/{,!(browser)/**/}*.spec.ts\"",
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
},
"sideEffects": false,
Expand All @@ -92,11 +92,6 @@
"@azure/identity": "^1.1.0",
"@azure/test-utils-recorder": "^1.0.0",
"@microsoft/api-extractor": "7.7.11",
"@rollup/plugin-commonjs": "11.0.2",
"@rollup/plugin-json": "^4.0.0",
"@rollup/plugin-multi-entry": "^3.0.0",
"@rollup/plugin-node-resolve": "^8.0.0",
"@rollup/plugin-replace": "^2.2.0",
"@types/chai": "^4.1.6",
"@types/mocha": "^7.0.2",
"@types/node": "^8.0.0",
Expand Down Expand Up @@ -131,15 +126,9 @@
"prettier": "^1.16.4",
"rimraf": "^3.0.0",
"rollup": "^1.16.3",
"rollup-plugin-shim": "^1.0.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-terser": "^5.1.1",
"rollup-plugin-visualizer": "^4.0.4",
"sinon": "^9.0.2",
"source-map-support": "^0.5.9",
"typescript": "~3.9.3",
"fs-extra": "^8.1.0",
"@types/fs-extra": "^8.0.0"
"typescript": "~3.9.3"
},
"//smokeTestConfiguration": {
"skip": [
Expand Down
Loading

0 comments on commit 8bb505f

Please sign in to comment.