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

Only tree-shake JS files #1965

Merged
merged 1 commit into from
Dec 13, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path';
import {Plugin} from 'rollup';
import {VM as VM2} from 'vm2';
import {AbstractLogger, InstallTarget} from '../types';
import {getWebDependencyName, isRemoteUrl, isTruthy} from '../util';
import {getWebDependencyName, isJavaScript, isRemoteUrl, isTruthy} from '../util';

// Use CJS intentionally here! ESM interface is async but CJS is sync, and this file is sync
const {parse} = require('cjs-module-lexer');
Expand Down Expand Up @@ -119,6 +119,9 @@ export function rollupPluginWrapInstallTargets(
if (isRemoteUrl(val)) {
continue;
}
if (!isJavaScript(val)) {
continue;
}
const allInstallTargets = installTargets.filter(
(imp) => getWebDependencyName(imp.specifier) === key,
);
Expand Down
7 changes: 6 additions & 1 deletion esinstall/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ export function createInstallTarget(specifier: string, all = true): InstallTarge
};
}

export function isJavaScript(pathname: string): boolean {
const ext = path.extname(pathname).toLowerCase();
return (ext === '.js' || ext === '.mjs' || ext === '.cjs');
}

/**
* Detect the web dependency "type" as either JS or ASSET:
* - BUNDLE: Install and bundle this file with Rollup engine.
Expand All @@ -198,7 +203,7 @@ export function createInstallTarget(specifier: string, all = true): InstallTarge
export function getWebDependencyType(pathname: string): 'ASSET' | 'BUNDLE' {
const ext = path.extname(pathname).toLowerCase();
// JavaScript should always be bundled.
if (ext === '.js' || ext === '.mjs' || ext === '.cjs') {
if (isJavaScript(pathname)) {
return 'BUNDLE';
}
// Svelte & Vue should always be bundled because we want to show the missing plugin
Expand Down