-
Notifications
You must be signed in to change notification settings - Fork 930
Add tooling to verify dependency chain #3033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
f21776b
ea41857
7df1038
035091c
2124354
73ea558
9544284
040f141
ea361a4
4701cde
a0e8c26
f8995f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export function foo(): string; | ||
export function bar(): string; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export { foo, bar } from './src/api/foobar'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export function foo(): string { | ||
return bar(); | ||
} | ||
|
||
export function bar(): string { | ||
return 'bar'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"foo": { | ||
"dependencies": [ | ||
"bar", | ||
"foo" | ||
], | ||
"sizeInBytes": 67 | ||
}, | ||
"bar": { | ||
"dependencies": [ | ||
"bar" | ||
], | ||
"sizeInBytes": 39 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is per function? seems like a pain to always update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is auto-updated via the script, but the script is not run automatically. I'll send this to @Feiyang1 and we can ponder how we can automate this step. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { extractDependencies } from '../../../../../scripts/exp/extract-deps.helpers'; | ||
|
||
import * as dependencies from './dependencies.json'; | ||
import * as pkg from '../../../package.json'; | ||
import { forEach } from '../../../src/util/obj'; | ||
|
||
describe('Dependencies', () => { | ||
forEach(dependencies, (api, { dependencies }) => { | ||
it(api, () => { | ||
return extractDependencies(api, pkg.exp).then(extractedDependencies => { | ||
dependencies.sort(); | ||
expect(extractedDependencies).to.have.members(dependencies); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed. |
||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import typescriptPlugin from 'rollup-plugin-typescript2'; | ||
import typescript from 'typescript'; | ||
|
||
import { resolveNodeExterns } from './rollup.shared'; | ||
|
||
import pkg from './package.json'; | ||
|
||
const defaultPlugins = [ | ||
typescriptPlugin({ | ||
typescript, | ||
tsconfigOverride: { | ||
compilerOptions: { | ||
target: 'es2017' | ||
} | ||
}, | ||
clean: true | ||
}) | ||
]; | ||
|
||
const nodeBuilds = [ | ||
{ | ||
input: './exp/index.node.ts', | ||
output: { | ||
file: pkg.exp, | ||
format: 'es' | ||
}, | ||
plugins: defaultPlugins, | ||
external: resolveNodeExterns, | ||
treeshake: { | ||
tryCatchDeoptimization: false | ||
} | ||
} | ||
]; | ||
|
||
export default [...nodeBuilds]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as tmp from 'tmp'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import * as rollup from 'rollup'; | ||
import * as terser from 'terser'; | ||
import * as ts from 'typescript'; | ||
|
||
/** Contains the dependencies and the size of their code for a single export. */ | ||
export type ExportData = { dependencies: string[]; sizeInBytes: number }; | ||
|
||
/** | ||
* This functions builds a simple JS app that only depends on the provided | ||
* export. It then uses Rollup to gather all top-level classes and functions | ||
* that that the export depends on. | ||
* | ||
* @param exportName The name of the export to verify | ||
* @param jsBundle The file name of the source bundle that contains the export | ||
* @return A list of dependencies for the given export | ||
*/ | ||
export async function extractDependencies( | ||
exportName: string, | ||
jsBundle: string | ||
): Promise<string[]> { | ||
const { dependencies } = await extractDependenciesAndSize( | ||
exportName, | ||
jsBundle | ||
); | ||
return dependencies; | ||
} | ||
|
||
/** | ||
* Helper for extractDependencies that extracts the dependencies and the size | ||
* of the minified build. | ||
*/ | ||
export async function extractDependenciesAndSize( | ||
exportName: string, | ||
jsBundle: string | ||
): Promise<ExportData> { | ||
const input = tmp.fileSync().name + '.js'; | ||
const output = tmp.fileSync().name + '.js'; | ||
|
||
// JavaScript content that exports a single API from the bundle | ||
const beforeContent = `export { ${exportName} } from '${path.resolve( | ||
jsBundle | ||
)}';`; | ||
fs.writeFileSync(input, beforeContent); | ||
|
||
// Run Rollup on the JavaScript above to produce a tree-shaken build | ||
const bundle = await rollup.rollup({ | ||
input, | ||
external: id => id.startsWith('@firebase/') | ||
}); | ||
await bundle.write({ file: output, format: 'es' }); | ||
|
||
const dependencies = extractFunctionsAndClasses(output); | ||
dependencies.sort(); | ||
|
||
// Extract size of minified build | ||
const afterContent = fs.readFileSync(output, 'utf-8'); | ||
const { code } = terser.minify(afterContent, { | ||
output: { | ||
comments: false | ||
}, | ||
mangle: false, | ||
compress: false | ||
}); | ||
|
||
fs.unlinkSync(input); | ||
fs.unlinkSync(output); | ||
|
||
return { dependencies, sizeInBytes: Buffer.byteLength(code!, 'utf-8') }; | ||
} | ||
|
||
/** | ||
* Extracts all top-level function and classes using the TypeScript compiler | ||
* API. | ||
*/ | ||
export function extractFunctionsAndClasses(jsFile: string): string[] { | ||
const program = ts.createProgram([jsFile], { allowJs: true }); | ||
const sourceFile = program.getSourceFile(jsFile); | ||
if (!sourceFile) { | ||
throw new Error('Failed to parse file: ' + jsFile); | ||
} | ||
|
||
const exports: string[] = []; | ||
ts.forEachChild(sourceFile, node => { | ||
if (ts.isFunctionDeclaration(node)) { | ||
exports.push(node.name!.text); | ||
} else if (ts.isClassDeclaration(node)) { | ||
exports.push(node.name!.text); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be interesting to see variable declarations too, e.g. the error message map. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be pretty easy to do as part of this PR. I will report back in my conversation with myself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, done. |
||
}); | ||
return exports; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2020 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# extract-deps --- Invokes extract-deps using TS_NODE | ||
|
||
set -o nounset | ||
set -o errexit | ||
|
||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
NPM_BIN_DIR="$(npm bin)" | ||
TSNODE="$NPM_BIN_DIR/ts-node" | ||
GENERATE_DEPS_JS="$DIR/extract-deps.ts" | ||
|
||
export TS_NODE_CACHE=NO | ||
export TS_NODE_COMPILER_OPTIONS='{"module":"commonjs"}' | ||
export TS_NODE_PROJECT="$DIR/../../config/tsconfig.base.json" | ||
|
||
$TSNODE $GENERATE_DEPS_JS "$@" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This file is generated by the Node CLI tool and then used in the unit test as the source of truth. It is checked in and as such provides a nice diff as we make changes.
"sizeInBytes" is for reference only.