-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dev): transform import for commonjs dependencies (#837)
* fix(dev): preserve named exports when optimizing cjs deps * chore: delete my vscode config * feat(dev): allow users to manually specify cjs named exports * test: add test * test: fix test * test: fix test * fix(dev): transform import for comm onjs dependencies * fix: delete unused code * test: fix test * chore: remove unused dep * fix: remove unused config * test: fix test * fix: fix code style * fix: remove console.log * fix(dev): simplify dynamic import rewriting logic * feat(dev): use cjs detection from @rollup/plugin-commonjs * fix(dev): add exception handling * fix: use official @rollup/plugin-commonjs * fix: fix build error * fix: better error handling Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
12 changed files
with
338 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<template> | ||
<h2>Optimize cjs dep with named export</h2> | ||
<p class="cjs-dep-named-export-static"> | ||
static import result: {{ staticImport }} | ||
</p> | ||
<p class="cjs-dep-named-export-dynamic"> | ||
dynamic import result: {{ dynamicImport }} | ||
</p> | ||
<button class="cjs-dep-named-export-dynamic-load" @click="loadDynamic()"> | ||
load dynamic | ||
</button> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue' | ||
import React, { useState, createContext } from 'react' | ||
import { default as React2, useState as useState2 } from 'react' | ||
import * as ReactStar from 'react' | ||
import PropTypes, { oneOfType } from 'prop-types' | ||
export default { | ||
setup() { | ||
let staticImport | ||
if ( | ||
isFunction(React.useState) && | ||
isFunction(useState) && | ||
isFunction(createContext) && | ||
isFunction(React2.useState) && | ||
isFunction(useState2) && | ||
isFunction(ReactStar.useState) && | ||
isFunction(PropTypes.oneOfType) && | ||
isFunction(oneOfType) | ||
) { | ||
staticImport = 'success' | ||
} else { | ||
staticImport = 'fail' | ||
} | ||
const dynamicImport = ref('dynamic not loaded') | ||
function loadDynamic() { | ||
// dynamic import cjs dep and get named-export | ||
import('react-dom').then(({ render }) => { | ||
dynamicImport.value = isFunction(render) ? 'success' : 'fail' | ||
}) | ||
} | ||
return { | ||
staticImport, | ||
dynamicImport, | ||
loadDynamic | ||
} | ||
} | ||
} | ||
function isFunction(v) { | ||
return typeof v === 'function' | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Plugin } from 'rollup' | ||
|
||
export interface OptimizeAnalysisResult { | ||
isCommonjs: { [name: string]: true } | ||
} | ||
|
||
export function entryAnalysisPlugin(): Plugin { | ||
const analysis: OptimizeAnalysisResult = { isCommonjs: {} } | ||
return { | ||
name: 'vite:cjs-entry-named-export', | ||
async generateBundle(options, bundles) { | ||
Object.values(bundles).forEach((bundle) => { | ||
if (bundle.type === 'chunk' && bundle.isEntry) { | ||
if (bundle.facadeModuleId) { | ||
const facadeInfo = this.getModuleInfo(bundle.facadeModuleId) | ||
// this info is exposed by rollup commonjs plugin | ||
if (facadeInfo?.meta?.commonjs?.isCommonJS) { | ||
analysis.isCommonjs[bundle.name] = true | ||
} | ||
} | ||
} | ||
}) | ||
this.emitFile({ | ||
type: 'asset', | ||
fileName: '_analysis.json', | ||
source: JSON.stringify(analysis) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.