Skip to content

Commit 1061bbd

Browse files
authoredJun 5, 2022
fix(optimizer): external require-import conversion (fixes #2492, #3409) (#8459)
1 parent 129b499 commit 1061bbd

File tree

9 files changed

+65
-3
lines changed

9 files changed

+65
-3
lines changed
 

‎packages/vite/src/node/optimizer/esbuildDepPlugin.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import {
1313
import { browserExternalId } from '../plugins/resolve'
1414
import type { ExportsData } from '.'
1515

16+
const externalWithConversionNamespace =
17+
'vite:dep-pre-bundle:external-conversion'
18+
const convertedExternalPrefix = 'vite-dep-pre-bundle-external:'
19+
1620
const externalTypes = [
1721
'css',
1822
// supported pre-processor types
@@ -80,20 +84,42 @@ export function esbuildDepPlugin(
8084
name: 'vite:dep-pre-bundle',
8185
setup(build) {
8286
// externalize assets and commonly known non-js file types
87+
// See #8459 for more details about this require-import conversion
8388
build.onResolve(
8489
{
8590
filter: new RegExp(`\\.(` + allExternalTypes.join('|') + `)(\\?.*)?$`)
8691
},
8792
async ({ path: id, importer, kind }) => {
93+
// if the prefix exist, it is already converted to `import`, so set `external: true`
94+
if (id.startsWith(convertedExternalPrefix)) {
95+
return {
96+
path: id.slice(convertedExternalPrefix.length),
97+
external: true
98+
}
99+
}
100+
88101
const resolved = await resolve(id, importer, kind)
89102
if (resolved) {
103+
// here it is not set to `external: true` to convert `require` to `import`
90104
return {
91105
path: resolved,
92-
external: true
106+
namespace: externalWithConversionNamespace
93107
}
94108
}
95109
}
96110
)
111+
build.onLoad(
112+
{ filter: /./, namespace: externalWithConversionNamespace },
113+
(args) => {
114+
// import itself with prefix (this is the actual part of require-import conversion)
115+
return {
116+
contents:
117+
`export { default } from "${convertedExternalPrefix}${args.path}";` +
118+
`export * from "${convertedExternalPrefix}${args.path}";`,
119+
loader: 'js'
120+
}
121+
}
122+
)
97123

98124
function resolveEntry(id: string) {
99125
const flatId = flattenId(id)

‎playground/optimize-deps/__tests__/optimize-deps.spec.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ test('dep with dynamic import', async () => {
7777
})
7878

7979
test('dep with css import', async () => {
80-
expect(await getColor('h1')).toBe('red')
80+
expect(await getColor('.dep-linked-include')).toBe('red')
81+
})
82+
83+
test('CJS dep with css import', async () => {
84+
expect(await getColor('.cjs-with-assets')).toBe('blue')
8185
})
8286

8387
test('dep w/ non-js files handled via plugin', async () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.cjs-with-assets {
2+
color: blue;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('./foo.css')
2+
3+
exports.a = 11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "dep-cjs-with-assets",
3+
"private": true,
4+
"version": "0.0.0",
5+
"main": "index.js"
6+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
body {
1+
.dep-linked-include {
22
color: red;
33
}

‎playground/optimize-deps/index.html

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ <h2>Detecting linked src package and optimizing its deps (lodash-es)</h2>
3535
<h2>Optimizing force included dep even when it's linked</h2>
3636
<div class="force-include"></div>
3737

38+
<h2>Dep with CSS</h2>
39+
<div class="dep-linked-include">This should be red</div>
40+
41+
<h2>CJS Dep with CSS</h2>
42+
<div class="cjs-with-assets">This should be blue</div>
43+
3844
<h2>import * as ...</h2>
3945
<div class="import-star"></div>
4046

@@ -91,6 +97,8 @@ <h2>Reused variable names</h2>
9197
text('.import-star', `[success] ${keys.join(', ')}`)
9298
}
9399

100+
import 'dep-cjs-with-assets'
101+
94102
import { env } from 'dep-node-env'
95103
text('.node-env', env)
96104

‎playground/optimize-deps/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"clipboard": "^2.0.11",
1414
"dep-cjs-compiled-from-cjs": "file:./dep-cjs-compiled-from-cjs",
1515
"dep-cjs-compiled-from-esm": "file:./dep-cjs-compiled-from-esm",
16+
"dep-cjs-with-assets": "file:./dep-cjs-with-assets",
1617
"dep-esbuild-plugin-transform": "file:./dep-esbuild-plugin-transform",
1718
"dep-linked": "link:./dep-linked",
1819
"dep-linked-include": "link:./dep-linked-include",

‎pnpm-lock.yaml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)