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

fix: tree shaking package when imports include file extensions #972

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
16 changes: 6 additions & 10 deletions snowpack/src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
writeLockfile,
isPackageAliasEntry,
findMatchingAliasEntry,
getWebDependencyName,
} from '../util.js';

type InstallResultCode = 'SUCCESS' | 'ASSET' | 'FAIL';
Expand Down Expand Up @@ -77,16 +78,6 @@ function isImportOfPackage(importUrl: string, packageName: string) {
return packageName === importUrl || importUrl.startsWith(packageName + '/');
}

/**
* Formats the snowpack dependency name from a "webDependencies" input value:
* 2. Remove any ".js"/".mjs" extension (will be added automatically by Rollup)
*/
function getWebDependencyName(dep: string): string {
return validatePackageName(dep).validForNewPackages
? dep.replace(/\.js$/i, 'js') // if this is a top-level package ending in .js, replace with js (e.g. tippy.js -> tippyjs)
: dep.replace(/\.m?js$/i, ''); // otherwise simply strip the extension (Rollup will resolve it)
}

/**
* Resolve a "webDependencies" input value to the correct absolute file location.
* Supports both npm package names, and file paths relative to the node_modules directory.
Expand Down Expand Up @@ -392,6 +383,11 @@ ${colors.dim(
format: 'esm',
sourcemap: sourceMap,
exports: 'named',
entryFileNames: (chunk) => {
const targetName = getWebDependencyName(chunk.name);
const proxiedName = sanitizePackageName(targetName);
return `${proxiedName}.js`;
},
chunkFileNames: 'common/[name]-[hash].js',
};
if (Object.keys(installEntrypoints).length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import {Plugin} from 'rollup';
import {logger} from '../logger';
import {InstallTarget} from '../types/snowpack';
import {getWebDependencyName} from '../util.js';

function autoDetectExports(fileLoc: string): string[] | undefined {
try {
Expand Down Expand Up @@ -44,7 +45,7 @@ export function rollupPluginWrapInstallTargets(
const input = inputOptions.input as {[entryAlias: string]: string};
for (const [key, val] of Object.entries(input)) {
const allInstallTargets = installTargets.filter(
(imp) => imp.specifier.replace(/\.js$/, 'js') === key,
(imp) => getWebDependencyName(imp.specifier) === key,
);
const installTargetSummary = allInstallTargets.reduce((summary, imp) => {
summary.all = summary.all || imp.all;
Expand Down
11 changes: 11 additions & 0 deletions snowpack/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import mkdirp from 'mkdirp';
import open from 'open';
import path from 'path';
import rimraf from 'rimraf';
import validatePackageName from 'validate-npm-package-name';
import {ImportMap, SnowpackConfig} from './types/snowpack';
import {removeTrailingSlash, addTrailingSlash} from './config';

Expand Down Expand Up @@ -338,3 +339,13 @@ export function cssSourceMappingURL(code: string, sourceMappingURL: string) {
export function jsSourceMappingURL(code: string, sourceMappingURL: string) {
return code.replace(/\n*$/, '') + `\n//# sourceMappingURL=${sourceMappingURL}\n`; // strip ending lines & append source map (with linebreaks for safety)
}

/**
* Formats the snowpack dependency name from a "webDependencies" input value:
* 2. Remove any ".js"/".mjs" extension (will be added automatically by Rollup)
*/
export function getWebDependencyName(dep: string): string {
return validatePackageName(dep).validForNewPackages
? dep.replace(/\.js$/i, 'js') // if this is a top-level package ending in .js, replace with js (e.g. tippy.js -> tippyjs)
: dep.replace(/\.m?js$/i, ''); // otherwise simply strip the extension (Rollup will resolve it)
}
Loading