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 alias support to externals #88

Merged
merged 7 commits into from
Mar 13, 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
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
([#61](https://github.com/feltcoop/gro/pull/61),
[#71](https://github.com/feltcoop/gro/pull/71),
[#76](https://github.com/feltcoop/gro/pull/76),
[#81](https://github.com/feltcoop/gro/pull/81))
[#81](https://github.com/feltcoop/gro/pull/81),
[#88](https://github.com/feltcoop/gro/pull/88))
- make `createBuilder` pluggable allowing users to provide a compiler for each file
([#57](https://github.com/feltcoop/gro/pull/57))
- rename `compiler` to `builder`
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"fs-extra": "^9.0.1",
"kleur": "^4.1.3",
"mri": "^1.1.6",
"path-browserify": "^1.0.1",
"prettier": "^2.1.2",
"prettier-plugin-svelte": "^1.4.0",
"rollup": "^2.37.1",
Expand Down
11 changes: 9 additions & 2 deletions src/build/Filer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import {loadContents} from './load.js';
import {isExternalBrowserModule} from '../utils/module.js';
import {wrap} from '../utils/async.js';
import {
DEFAULT_EXTERNALS_ALIASES,
ExternalsAliases,
EXTERNALS_SOURCE_ID,
getExternalsBuilderState,
getExternalsBuildState,
Expand Down Expand Up @@ -64,10 +66,11 @@ export type FilerFile = SourceFile | BuildFile; // TODO or `Directory`?
export interface Options {
dev: boolean;
builder: Builder | null;
sourceDirs: string[];
servedDirs: ServedDir[];
buildConfigs: BuildConfig[] | null;
buildDir: string;
sourceDirs: string[];
servedDirs: ServedDir[];
externalsAliases: ExternalsAliases;
mapDependencyToSourceId: MapDependencyToSourceId;
sourceMap: boolean;
target: EcmaScriptTarget;
Expand Down Expand Up @@ -125,6 +128,7 @@ export const initOptions = (opts: InitialOptions): Options => {
return {
dev,
mapDependencyToSourceId,
externalsAliases: DEFAULT_EXTERNALS_ALIASES,
sourceMap: true,
target: DEFAULT_ECMA_SCRIPT_TARGET,
watch: true,
Expand Down Expand Up @@ -158,6 +162,7 @@ export class Filer implements BuildContext {
readonly sourceMap: boolean;
readonly target: EcmaScriptTarget; // TODO shouldn't build configs have this?
readonly servedDirs: readonly ServedDir[];
readonly externalsAliases: ExternalsAliases; // TODO should this allow aliasing anything? not just externals?
readonly state: BuilderState = {};
readonly buildingSourceFiles: Set<string> = new Set(); // needed by hacky externals code, used to check if the filer is busy

Expand All @@ -170,6 +175,7 @@ export class Filer implements BuildContext {
mapDependencyToSourceId,
sourceDirs,
servedDirs,
externalsAliases,
sourceMap,
target,
watch,
Expand All @@ -180,6 +186,7 @@ export class Filer implements BuildContext {
this.buildConfigs = buildConfigs;
this.buildDir = buildDir;
this.mapDependencyToSourceId = mapDependencyToSourceId;
this.externalsAliases = externalsAliases;
this.sourceMap = sourceMap;
this.target = target;
this.log = log;
Expand Down
8 changes: 7 additions & 1 deletion src/build/builder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {UnreachableError} from '../utils/error.js';
import {BuildConfig} from '../config/buildConfig.js';
import {toBuildOutPath} from '../paths.js';
import type {ExternalsBuilderState, EXTERNALS_BUILDER_STATE_KEY} from './externalsBuildHelpers.js';
import type {
ExternalsAliases,
ExternalsBuilderState,
EXTERNALS_BUILDER_STATE_KEY,
} from './externalsBuildHelpers.js';
import {EcmaScriptTarget} from './tsBuildHelpers.js';
import {ServedDir} from './ServedDir.js';
import {Logger} from '../utils/log.js';
Expand Down Expand Up @@ -29,13 +33,15 @@ export interface BuildResult<TBuild extends Build = Build> {
builds: TBuild[];
}

// For docs on these, see where they're implemented in the `Filer`.
export interface BuildContext {
readonly log: Logger;
readonly buildDir: string;
readonly dev: boolean;
readonly sourceMap: boolean;
readonly target: EcmaScriptTarget;
readonly servedDirs: readonly ServedDir[];
readonly externalsAliases: ExternalsAliases;
readonly state: BuilderState;
readonly buildingSourceFiles: Set<string>;
}
Expand Down
7 changes: 7 additions & 0 deletions src/build/externalsBuildHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ export const loadImportMapFromDisk = async (dest: string): Promise<ImportMap | u
return importMap;
};

export interface ExternalsAliases {
[key: string]: string;
}
export const DEFAULT_EXTERNALS_ALIASES: ExternalsAliases = {
path: 'path-browserify',
};

export interface DelayedPromise<T> {
promise: Promise<T>;
reset(): void;
Expand Down
7 changes: 3 additions & 4 deletions src/build/externalsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const createExternalsBuilder = (opts: InitialOptions = {}): ExternalsBuil
const build: ExternalsBuilder['build'] = async (
source,
buildConfig,
{buildDir, dev, sourceMap, target, state},
{buildDir, dev, sourceMap, target, state, externalsAliases},
) => {
// if (sourceMap) {
// log.warn('Source maps are not yet supported by the externals builder.');
Expand Down Expand Up @@ -102,15 +102,14 @@ export const createExternalsBuilder = (opts: InitialOptions = {}): ExternalsBuil
dest,
rollup: {plugins},
polyfillNode: true, // needed for some libs - maybe make customizable?
alias: externalsAliases,
});
log.info('install result', installResult);
// log.trace('previous import map', state.importMap); maybe diff?
buildState.importMap = installResult.importMap;

// TODO load all of the files in the import map
builds = [
...(await Promise.all(
Object.keys(installResult.importMap.imports).map(
Array.from(buildState.specifiers).map(
async (specifier): Promise<TextBuild> => {
const id = join(dest, installResult.importMap.imports[specifier]);
return {
Expand Down
3 changes: 3 additions & 0 deletions src/build/postprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export const postprocess = (
} else if (isExternalImport || source.id === EXTERNALS_SOURCE_ID) {
// handle regular externals
if (isBrowser) {
if (mappedSpecifier in ctx.externalsAliases) {
mappedSpecifier = ctx.externalsAliases[mappedSpecifier];
}
if (mappedSpecifier.endsWith(JS_EXTENSION) && shouldModifyDotJs(mappedSpecifier)) {
mappedSpecifier = mappedSpecifier.replace(/\.js$/, 'js');
}
Expand Down
4 changes: 3 additions & 1 deletion src/client/SourceMetaExpanderItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
$: hovered = sourceMeta === $hoveredSourceMeta;
$: selected = sourceMeta === $selectedSourceMeta;

const onPointerDown = () => {
const onPointerDown = (e: PointerEvent) => {
// TODO this needs to be done for all of the handlers..
if (e.button !== 0) return;
$selectedSourceMeta = selected ? null : sourceMeta;
};
const onPointerEnter = () => {
Expand Down
6 changes: 5 additions & 1 deletion src/client/SourceMetaRawItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
on:pointerleave={onPointerLeave}
class:hovering
>
{expandedText}
<span class="icon">{expandedText}</span>
<SourceId id={sourceMeta.data.sourceId} />
</button>
</div>
Expand Down Expand Up @@ -91,4 +91,8 @@
.deemphasized {
opacity: 0.62;
}
.icon {
opacity: 0.6;
padding-right: var(--spacing_sm);
}
</style>
4 changes: 2 additions & 2 deletions src/client/styles.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*,
*:before,
*:after {
*::before,
*::after {
box-sizing: border-box;
margin: 0;
overflow-wrap: break-word;
Expand Down
1 change: 1 addition & 0 deletions src/fs/importTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const importTs = async (
target: DEFAULT_ECMA_SCRIPT_TARGET,
// TODO these last two aren't needed, maybe the swc compiler's type should explicitly choose which options it uses?
servedDirs: [],
externalsAliases: {},
state: {},
buildingSourceFiles: new Set(),
};
Expand Down