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

aliases as a separate config object #249

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type DeepPartial<T> = {
export interface SnowpackConfig {
source: 'local' | 'pika';
webDependencies?: string[];
aliases?: {[key: string]: string};
dedupe?: string[];
namedExports?: {[filepath: string]: string[]};
installOptions: {
Expand Down Expand Up @@ -74,6 +75,10 @@ const configSchema = {
properties: {
source: {type: 'string'},
webDependencies: {type: 'array', items: {type: 'string'}},
aliases: {
type: 'object',
additionalProperties: {type: 'string'},
},
dedupe: {
type: 'array',
items: {type: 'string'},
Expand Down
22 changes: 20 additions & 2 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 @@ -224,6 +225,7 @@ export async function install(
config: SnowpackConfig,
) {
const {
aliases = {},
dedupe,
namedExports,
source,
Expand All @@ -250,8 +252,12 @@ export async function install(
logError('no "node_modules" directory exists. Did you run "npm install" first?');
return;
}

const allInstallSpecifiers = new Set(installTargets.map(dep => dep.specifier).sort());
const allInstallSpecifiers = new Set(
installTargets
.map(dep => dep.specifier)
.concat(Object.values(aliases))
ChristopherBiscardi marked this conversation as resolved.
Show resolved Hide resolved
.sort(),
);
const installEntrypoints: {[targetName: string]: string} = {};
const assetEntrypoints: {[targetName: string]: string} = {};
const importMap: ImportMap = {imports: {}};
Expand All @@ -273,6 +279,12 @@ export async function install(
const hashQs = useHash ? `?rev=${await generateHashFromFile(targetLoc)}` : '';
installEntrypoints[targetName] = targetLoc;
importMap.imports[installSpecifier] = `./${targetName}.js${hashQs}`;
Object.entries(aliases)
.filter(([key, value]) => value === installSpecifier)
.forEach(([key, value]) => {
importMap.imports[key] = `./${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 +329,12 @@ export async function install(
rollupPluginReplace({
'process.env.NODE_ENV': isOptimized ? '"production"' : '"development"',
}),
rollupPluginAlias({
entries: Object.entries(aliases).map(([alias, mod]) => ({
find: alias,
replacement: mod,
})),
}),
rollupPluginNodeResolve({
mainFields: ['browser:module', 'module', 'browser', !isStrict && 'main'].filter(isTruthy),
modulesOnly: isStrict, // Default: false
Expand Down