From a60e0255f46c25d3dc77ca31ab5ca245ee7cbb20 Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Sun, 13 Dec 2020 13:28:24 -0800 Subject: [PATCH] fix svelte treeshake (#1965) --- .../rollup-plugins/rollup-plugin-wrap-install-targets.ts | 5 ++++- esinstall/src/util.ts | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/esinstall/src/rollup-plugins/rollup-plugin-wrap-install-targets.ts b/esinstall/src/rollup-plugins/rollup-plugin-wrap-install-targets.ts index 143420757b..3be845ca10 100644 --- a/esinstall/src/rollup-plugins/rollup-plugin-wrap-install-targets.ts +++ b/esinstall/src/rollup-plugins/rollup-plugin-wrap-install-targets.ts @@ -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'); @@ -119,6 +119,9 @@ export function rollupPluginWrapInstallTargets( if (isRemoteUrl(val)) { continue; } + if (!isJavaScript(val)) { + continue; + } const allInstallTargets = installTargets.filter( (imp) => getWebDependencyName(imp.specifier) === key, ); diff --git a/esinstall/src/util.ts b/esinstall/src/util.ts index fc1d3f9d68..55d218460b 100644 --- a/esinstall/src/util.ts +++ b/esinstall/src/util.ts @@ -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. @@ -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