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 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.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"@babel/plugin-transform-react-jsx": "^7.9.4",
"@babel/preset-env": "^7.6.3",
"@babel/types": "^7.6.3",
"@rollup/plugin-commonjs": "~11.0.0",
"@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",
"@rollup/plugin-replace": "^2.1.0",
Expand Down
7 changes: 6 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface SnowpackConfig {
exclude: string[];
knownEntrypoints: string[];
webDependencies?: {[packageName: string]: string};
aliases?: {[key: string]: string};
scripts: DevScripts;
devOptions: {
port: number;
Expand Down Expand Up @@ -97,10 +98,14 @@ const configSchema = {
webDependencies: {
type: ['object'],
additionalProperties: {type: 'string'},
},
aliases: {
type: 'object',
additionalProperties: {type: 'string'},
},
installOptions: {
type: 'object',
properties: {
properties: {
babel: {type: 'boolean'},
clean: {type: 'boolean'},
dest: {type: 'string'},
Expand Down
21 changes: 19 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 isNodeBuiltin from 'is-builtin-module';
Expand Down Expand Up @@ -247,6 +248,7 @@ export async function install(
config: SnowpackConfig,
) {
const {
aliases = {},
webDependencies,
installOptions: {
installTypes,
Expand All @@ -267,8 +269,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)
.map((specifier) => aliases[specifier] || specifier)
.sort(),
);
const installEntrypoints: {[targetName: string]: string} = {};
const assetEntrypoints: {[targetName: string]: string} = {};
const importMap: ImportMap = {imports: {}};
Expand All @@ -289,6 +295,11 @@ export async function install(
if (targetType === 'JS') {
installEntrypoints[targetName] = targetLoc;
importMap.imports[installSpecifier] = `./${targetName}.js`;
Object.entries(aliases)
.filter(([key, value]) => value === installSpecifier)
.forEach(([key, value]) => {
importMap.imports[key] = `./${targetName}.js`;
});
installTargetsMap[targetLoc] = installTargets.filter(
(t) => installSpecifier === t.specifier,
);
Expand Down Expand Up @@ -337,6 +348,12 @@ export async function install(
installTypes,
log: (url) => logUpdate(chalk.dim(url)),
}),
rollupPluginAlias({
entries: Object.entries(aliases).map(([alias, mod]) => ({
find: alias,
replacement: mod,
})),
}),
rollupPluginNodeResolve({
mainFields: ['browser:module', 'module', 'browser', 'main'].filter(isTruthy),
extensions: ['.mjs', '.cjs', '.js', '.json'], // Default: [ '.mjs', '.js', '.json', '.node' ]
Expand Down