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

[wip] support aliases #237

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 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 @@ -47,6 +47,7 @@
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"@babel/types": "^7.6.3",
"@rollup/plugin-alias": "^3.0.1",
"@rollup/plugin-commonjs": "^11.0.0",
"@rollup/plugin-json": "^4.0.0",
"@rollup/plugin-node-resolve": "^7.1.0",
Expand Down
18 changes: 17 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,23 @@ const configSchema = {
type: 'object',
properties: {
source: {type: 'string'},
webDependencies: {type: 'array', items: {type: 'string'}},
webDependencies: {
type: 'array',
items: {
anyOf: [
{type: 'string'},
{
type: 'array',
items: [
{
type: 'string',
},
{type: 'string'},
],
},
],
},
},
dedupe: {
type: 'array',
items: {type: 'string'},
Expand Down
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import rollupPluginCommonjs from '@rollup/plugin-commonjs';
import rollupPluginJson from '@rollup/plugin-json';
import rollupPluginNodeResolve from '@rollup/plugin-node-resolve';
import rollupPluginReplace from '@rollup/plugin-replace';
import rollupPluginAlias from '@rollup/plugin-alias';
import chalk from 'chalk';
import fs from 'fs';
import hasha from 'hasha';
Expand Down Expand Up @@ -258,11 +259,17 @@ export async function install(
const installTargetsMap: {[targetLoc: string]: InstallTarget[]} = {};
const skipFailures = !isExplicit;

for (const installSpecifier of allInstallSpecifiers) {
for (const installSpecifierOrArr of allInstallSpecifiers) {
const installSpecifier = Array.isArray(installSpecifierOrArr)
? installSpecifierOrArr[1]
: installSpecifierOrArr;
const installSpecifierOrAlias = Array.isArray(installSpecifierOrArr)
? installSpecifierOrArr[0]
: installSpecifierOrArr;
const targetName = getWebDependencyName(installSpecifier);
if (lockfile && lockfile.imports[installSpecifier]) {
installEntrypoints[targetName] = lockfile.imports[installSpecifier];
importMap.imports[installSpecifier] = `./${targetName}.js`;
importMap.imports[installSpecifierOrAlias] = `./${targetName}.js`;
installResults.push([targetName, 'SUCCESS']);
logUpdate(formatInstallResults(skipFailures));
continue;
Expand All @@ -272,7 +279,7 @@ export async function install(
if (targetType === 'JS') {
const hashQs = useHash ? `?rev=${await generateHashFromFile(targetLoc)}` : '';
installEntrypoints[targetName] = targetLoc;
importMap.imports[installSpecifier] = `./${targetName}.js${hashQs}`;
importMap.imports[installSpecifierOrAlias] = `./${targetName}.js${hashQs}`;
installTargetsMap[targetLoc] = installTargets.filter(t => installSpecifier === t.specifier);
installResults.push([installSpecifier, 'SUCCESS']);
} else if (targetType === 'ASSET') {
Expand Down Expand Up @@ -317,6 +324,11 @@ export async function install(
rollupPluginReplace({
'process.env.NODE_ENV': isOptimized ? '"production"' : '"development"',
}),
rollupPluginAlias({
entries: Array.from(allInstallSpecifiers)
.filter(spec => Array.isArray(spec))
.map(([alias, mod]) => ({find: alias, replacement: mod})),
}),
rollupPluginNodeResolve({
mainFields: ['browser:module', 'module', 'browser', !isStrict && 'main'].filter(isTruthy),
modulesOnly: isStrict, // Default: false
Expand Down
5 changes: 4 additions & 1 deletion src/resolve-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ export async function resolveTargetsFromRemoteCDN(
const newLockfile: ImportMap = {imports: {}};

const allInstallSpecifiers = new Set(installTargets.map(dep => dep.specifier));
for (const installSpecifier of allInstallSpecifiers) {
for (const installSpecifierOrArr of allInstallSpecifiers) {
const installSpecifier = Array.isArray(installSpecifierOrArr)
? installSpecifierOrArr[1]
: installSpecifierOrArr;
const installSemver: string =
(pkgManifest.dependencies || {})[installSpecifier] ||
(pkgManifest.devDependencies || {})[installSpecifier] ||
Expand Down
13 changes: 9 additions & 4 deletions src/scan-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const DEFAULT_IMPORT_REGEX = /import\s+(\w)+(,\s\{[\w\s]*\})?\s+from/s;
* are metadata about what is actually being imported.
*/
export type InstallTarget = {
specifier: string;
specifier: string | [string, string];
all: boolean;
default: boolean;
namespace: boolean;
Expand All @@ -33,7 +33,7 @@ function stripJsExtension(dep: string): string {
return dep.replace(/\.m?js$/i, '');
}

function createInstallTarget(specifier: string, all = true): InstallTarget {
function createInstallTarget(specifier: string | [string, string], all = true): InstallTarget {
return {
specifier,
all,
Expand Down Expand Up @@ -124,7 +124,8 @@ export function scanDepList(depList: string[], cwd: string): InstallTarget[] {
const nodeModulesLoc = nodePath.join(cwd, 'node_modules');
return depList
.map(whitelistItem => {
if (!glob.hasMagic(whitelistItem)) {
const safelistName = Array.isArray(whitelistItem) ? whitelistItem[1] : whitelistItem;
if (!glob.hasMagic(safelistName)) {
return [createInstallTarget(whitelistItem, true)];
} else {
return scanDepList(glob.sync(whitelistItem, {cwd: nodeModulesLoc, nodir: true}), cwd);
Expand Down Expand Up @@ -158,5 +159,9 @@ export async function scanImports({include, exclude}: ScanImportsParams): Promis
.map(filePath => [filePath, fs.readFileSync(filePath, 'utf8')])
.map(([filePath, code]) => getInstallTargetsForFile(filePath, code))
.reduce((flat, item) => flat.concat(item), [])
.sort((impA, impB) => impA.specifier.localeCompare(impB.specifier));
.sort((impA, impB) => {
const specifierA = Array.isArray(impA.specifier) ? impA.specifier[1] : impA.specifier;
const specifierB = Array.isArray(impB.specifier) ? impB.specifier[1] : impB.specifier;
return specifierA.localeCompare(specifierB);
});
}