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

Add "ignore" option #69

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,32 @@ _The import map to extend._

An initial import map to start with - can be from a previous install or provide custom mappings.

#### ignore

> Type: string[] | undefined<br/>
Default: []

Allows ignoring certain module specifiers during the tracing process.
It can be useful, for example, when you provide an `inputMap` that contains a mapping that can't be traced in current context,
but you know it will work in the context where the generated map is going to be used.

```js
const generator = new Generator({
inputMap: {
imports: {
"react": "./my/own/react.js",
}
},
ignore: ["react"]
});

// Even though `@react-three/fiber@7` depends upon `react`,
// `generator` will not try to trace and resolve `react`,
// so the mapping provided in `inputMap` will end up in the resulting import map.
await generator.install("@react-three/fiber@7")
```


#### defaultProvider

> Type: 'jspm' | 'jspm.system' | 'nodemodules' | 'skypack' | 'jsdelivr' | 'unpkg'<br/>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export interface GeneratorOptions {
stdlib?: string;
customProviders?: Record<string, Provider>;
providers?: Record<string, string>;
/**
* List of module specifiers to ignore during tracing.
*/
ignore?: string[];
}

export interface ModuleAnalysis {
Expand Down Expand Up @@ -61,7 +65,8 @@ export class Generator {
customProviders = undefined,
providers = {},
cache = true,
stdlib = '@jspm/core'
stdlib = '@jspm/core',
ignore = []
}: GeneratorOptions = {}) {
let fetchOpts = undefined;
if (cache === 'offline')
Expand Down Expand Up @@ -90,7 +95,8 @@ export class Generator {
env,
defaultProvider,
providers,
inputMap
inputMap,
ignore
}, log, resolver);
}

Expand Down
15 changes: 13 additions & 2 deletions src/trace/tracemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export interface TraceMapOptions extends InstallOptions {
// or an exact trace for the provided entry points
// (currently unused)
fullMap?: boolean;

// List of module specifiers to ignore during tracing
ignore?: string[]
}

interface TraceGraph {
Expand Down Expand Up @@ -125,7 +128,7 @@ export default class TraceMap {
const [specifier, parentUrl] = trace.split('##');
try {
const resolved = await this.trace(specifier, new URL(parentUrl), this.tracedUrls?.[parentUrl]?.wasCJS ? ['require', ...this.env] : ['import', ...this.env]);
traceResolutions[trace] = resolved;
if (resolved) traceResolutions[trace] = resolved;
}
catch (e) {
throw e;
Expand Down Expand Up @@ -207,7 +210,13 @@ export default class TraceMap {
// }
// }

async trace (specifier: string, parentUrl = this.mapBase, env = ['import', ...this.env]): Promise<string> {
/**
* @returns `resolved` - either a URL `string` pointing to the module or `null` if the specifier should be ignored.
*/
async trace (specifier: string, parentUrl = this.mapBase, env = ['import', ...this.env]): Promise<string | null> {
// TODO: support ignoring prefixes?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to support react/ type boundaries yeah. Possibly even scoped ignores as well so similar structures to the import map itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, react* could work.

if (this.opts.ignore?.includes(specifier)) return null

const parentPkgUrl = await this.resolver.getPackageBase(parentUrl.href);
if (!parentPkgUrl)
throwInternalError();
Expand Down Expand Up @@ -365,6 +374,8 @@ export default class TraceMap {
return;
}
const resolved = await this.trace(dep, resolvedUrlObj, env);
if (resolved === null) return

if (deps.includes(dep))
traceEntry.deps[dep] = resolved;
if (dynamicDeps.includes(dep))
Expand Down
41 changes: 41 additions & 0 deletions test/api/ignore.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Generator } from '@jspm/generator';
import assert from 'assert';

const generator = new Generator({
mapUrl: import.meta.url,
inputMap: {
"imports": {
"react": "./location/that/cant/be/traced.js"
}
},
ignore: ['react'],
env: ['production', 'browser', 'module']
});

await generator.install('@react-three/fiber@7.0.15')

const json = generator.getMap();

console.log(json)

assert.deepEqual(json, {
"imports": {
"react": "./location/that/cant/be/traced.js",
"@react-three/fiber": "https://ga.jspm.io/npm:@react-three/fiber@7.0.15/dist/react-three-fiber.esm.js",
},
"scopes": {
"https://ga.jspm.io/": {
"debounce": "https://ga.jspm.io/npm:debounce@1.2.1/index.js",
"fast-deep-equal": "https://ga.jspm.io/npm:fast-deep-equal@3.1.3/index.js",
"object-assign": "https://ga.jspm.io/npm:object-assign@4.1.1/index.js",
"react-merge-refs": "https://ga.jspm.io/npm:react-merge-refs@1.1.0/dist/index.js",
"react-reconciler": "https://ga.jspm.io/npm:react-reconciler@0.26.2/index.js",
"react-use-measure": "https://ga.jspm.io/npm:react-use-measure@2.0.4/dist/web.cjs.js",
"scheduler": "https://ga.jspm.io/npm:scheduler@0.20.2/index.js",
"three": "https://ga.jspm.io/npm:three@0.133.1/build/three.module.js",
"use-asset": "https://ga.jspm.io/npm:use-asset@1.0.4/dist/index.cjs.js",
"zustand": "https://ga.jspm.io/npm:zustand@3.5.13/esm/index.js",
"zustand/shallow": "https://ga.jspm.io/npm:zustand@3.5.13/esm/shallow.js"
}
}
});