Skip to content
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

feat: analyze node modules #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/analyzer/config.md
Expand Up @@ -85,4 +85,20 @@ Using the `--config` flag in the CLI you can supply a custom path to your config

```bash
cem analyze --config "../configs/custom-elements-manifest.js"
```

### Analyzing dependencies

By default, the analyzer doesn't analyze any code inside `node_modules/`. For several reasons; you dont want all of `lodash` to accidentally get analyzed and output in your manifest, but also, we don't actually know which dependencies your project uses _until_ we're analyzing the code, by which time glob collection and compilation has already happened.

If you want to analyze third-party dependencies, you're responsible for providing the globs for them as either a cli option, or in your `custom-elements-manifest.config.js` configuration file.

Example:
```js
export default {
globs: [
'src/**/*.js',
'node_modules/foo/**/*.js'
]
}
```
3 changes: 3 additions & 0 deletions packages/analyzer/CHANGELOG.md
@@ -1,3 +1,6 @@
## 0.6.0
- Allow analyzing of `node_modules/` by user-provided globs

## Release 0.5.5
- Pick up `@property` decorator in mixins as well

Expand Down
2 changes: 1 addition & 1 deletion packages/analyzer/browser/index.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion packages/analyzer/custom-elements-manifest.config.js
Expand Up @@ -19,7 +19,9 @@
let typeChecker;

export default {
globs: ['fixtures/-default/package/**/*.{js,ts}'],
globs: [
'fixtures/-default/package/**/*.{js,ts}',
],
exclude: [],
dev: false,
plugins: [
Expand Down
44 changes: 21 additions & 23 deletions packages/analyzer/custom-elements.json
Expand Up @@ -7,45 +7,43 @@
"path": "fixtures/-default/package/bar.js",
"declarations": [
{
"kind": "mixin",
"kind": "class",
"description": "",
"name": "InputMixin",
"name": "MyEl",
"superclass": {
"name": "SuperFoo",
"package": "foo"
},
"members": [
{
"kind": "field",
"name": "disabled",
"type": {
"text": "boolean"
},
"default": "false",
"description": "this description never gets picked up by the analyzer. \nso we lose some info about default values and the fact it is both property and attribute",
"attribute": "disabled"
"kind": "method",
"name": "superMethod",
"inheritedFrom": {
"name": "SuperFoo",
"module": "fixtures/-default/package/node_modules/foo/index.js"
}
}
],
"attributes": [
"events": [
{
"name": "disabled",
"name": "super-event",
"type": {
"text": "boolean"
"text": "CustomEvent"
},
"default": "false",
"description": "this description never gets picked up by the analyzer. \nso we lose some info about default values and the fact it is both property and attribute",
"fieldName": "disabled"
}
],
"parameters": [
{
"name": "superClass"
"inheritedFrom": {
"name": "SuperFoo",
"module": "fixtures/-default/package/node_modules/foo/index.js"
}
}
]
}
],
"exports": [
{
"kind": "js",
"name": "InputMixin",
"name": "MyEl",
"declaration": {
"name": "InputMixin",
"name": "MyEl",
"module": "fixtures/-default/package/bar.js"
}
}
Expand Down
12 changes: 2 additions & 10 deletions packages/analyzer/fixtures/-default/package/bar.js
@@ -1,11 +1,3 @@
export function InputMixin(superClass) {
class InputElement extends superClass {
/**
* this description never gets picked up by the analyzer.
* so we lose some info about default values and the fact it is both property and attribute
*/
@property({ type: Boolean }) disabled = false
}
import { SuperFoo } from 'foo';

return InputElement;
}
export class MyEl extends SuperFoo {}
2 changes: 1 addition & 1 deletion packages/analyzer/package.json
@@ -1,6 +1,6 @@
{
"name": "@custom-elements-manifest/analyzer",
"version": "0.5.5",
"version": "0.6.0",
"description": "",
"license": "MIT",
"type": "module",
Expand Down
2 changes: 2 additions & 0 deletions packages/analyzer/src/features/index.js
Expand Up @@ -27,6 +27,7 @@ import { cleanupClassesPlugin } from './link-phase/cleanup-classes.js';
* POST-PROCESSING
*/
import { removeUnexportedDeclarationsPlugin } from './post-processing/remove-unexported-declarations.js';
import { removeNodeModulesPlugin } from './post-processing/remove-node-modules.js';
import { resolveInitializersPlugin } from './post-processing/resolve-initializers.js';
import { isCustomElementPlugin } from './post-processing/is-custom-element.js';
import { linkClassToTagnamePlugin } from './post-processing/link-class-to-tagname.js';
Expand Down Expand Up @@ -69,6 +70,7 @@ export const FEATURES = [
linkClassToTagnamePlugin(),
isCustomElementPlugin(),
applyInheritancePlugin(),
removeNodeModulesPlugin(),

/** FRAMEWORKS */
// litPlugin()
Expand Down
@@ -0,0 +1,15 @@
/**
* REMOVE-NODE-MODULES
*
* Removes any modules from `node_modules`
*/
export function removeNodeModulesPlugin() {
return {
name: 'CORE - REMOVE-NODE-MODULES',
packageLinkPhase({customElementsManifest}){
customElementsManifest.modules = customElementsManifest?.modules?.filter(mod => {
return !mod?.path?.includes('node_modules');
});
},
}
}
17 changes: 12 additions & 5 deletions packages/analyzer/src/utils/cli.js
Expand Up @@ -4,7 +4,7 @@ import path from 'path';
import commandLineArgs from 'command-line-args';
import { has } from './index.js';

const IGNORE = [
let IGNORE = [
'!node_modules/**/*.*',
'!bower_components/**/*.*',
'!**/*.test.{js,ts}',
Expand All @@ -13,16 +13,23 @@ const IGNORE = [
];

export function mergeGlobsAndExcludes(defaults, userConfig, cliConfig) {
const hasProvidedCliGlobs = has(cliConfig?.globs) || has(userConfig?.globs);
const cliGlobs = cliConfig?.globs || [];
const configGlobs = userConfig?.globs || [];
const userProvidedGlobs = [...cliGlobs, ...configGlobs];

const hasProvidedCliGlobs = has(cliConfig?.globs) || has(userConfig?.globs);
if(hasProvidedCliGlobs) {
defaults.globs = defaults.globs.filter(glob => glob !== '**/*.{js,ts,tsx}');
defaults.globs = defaults.globs.filter(g => g !== '**/*.{js,ts,tsx}');
}

const wantsToAnalyzeNodeModules = userProvidedGlobs.some(g => g.includes('node_modules'));
if(wantsToAnalyzeNodeModules) {
IGNORE = IGNORE.filter(g => g !== '!node_modules/**/*.*');
}

const merged = [
...defaults.globs,
...(userConfig?.globs || []),
...(cliConfig?.globs || []),
...userProvidedGlobs,
...(userConfig?.exclude?.map((i) => `!${i}`) || []),
...(cliConfig?.exclude?.map((i) => `!${i}`) || []),
...IGNORE,
Expand Down