diff --git a/package-lock.json b/package-lock.json index b3a6a63ac7..91c10fb651 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1722,6 +1722,21 @@ "integrity": "sha512-1ZsOHGc0qJDofO+/98PfchHJqJjtfZL3liVGi4QZ28GtLmTVuZ4SUJFa5NgbsYawnrr//pdNOfx9JiaLFKpzrA==", "dev": true }, + "@rollup/plugin-alias": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@rollup/plugin-alias/-/plugin-alias-3.0.1.tgz", + "integrity": "sha512-ReSy6iPl3GsWLMNeshXAfgItZFMoMOTYC7MZQQM5va4pqxiGgwl1xZUZfHW6zGyZPK+k8TBadxx+kdmepiUa+g==", + "requires": { + "slash": "^3.0.0" + }, + "dependencies": { + "slash": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz", + "integrity": "sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==" + } + } + }, "@rollup/plugin-commonjs": { "version": "11.0.2", "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-11.0.2.tgz", diff --git a/package.json b/package.json index e49e142252..7238d16905 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/config.ts b/src/config.ts index 3fb653404e..6eed93a33f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -28,6 +28,7 @@ export interface SnowpackConfig { exclude: string[]; knownEntrypoints: string[]; webDependencies?: {[packageName: string]: string}; + aliases?: {[key: string]: string}; scripts: DevScripts; devOptions: { port: number; @@ -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'}, diff --git a/src/index.ts b/src/index.ts index 1b243a3a7e..0b696f2496 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -247,6 +248,7 @@ export async function install( config: SnowpackConfig, ) { const { + aliases = {}, webDependencies, installOptions: { installTypes, @@ -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: {}}; @@ -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, ); @@ -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' ]