Skip to content

Commit 4b1b230

Browse files
author
Loïc Mangeonjean
committed
refactor: resolve externals using resolveId function
1 parent 5972242 commit 4b1b230

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

rollup.config.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ const extensions = ['.js', '.ts']
2929
// But the polyfill doesn't export all required fields and make the build crash
3030
const IGNORED_NODE_POLYFILLS = new Set(['os', 'fs'])
3131

32+
function isExternal (source: string) {
33+
if (IGNORED_NODE_POLYFILLS.has(source)) {
34+
return true
35+
}
36+
if (source.startsWith('extensions/')) {
37+
return false
38+
}
39+
if (externals.some(external => source === external || source.startsWith(`${external}/`))) {
40+
return true
41+
}
42+
return false
43+
}
44+
3245
export default rollup.defineConfig({
3346
cache: false,
3447
input: {
@@ -38,21 +51,6 @@ export default rollup.defineConfig({
3851
annotations: true,
3952
moduleSideEffects: false
4053
},
41-
external: function isExternal (source, importer, isResolved) {
42-
if (IGNORED_NODE_POLYFILLS.has(source)) {
43-
return true
44-
}
45-
if (isResolved) {
46-
return false
47-
}
48-
if (source.startsWith('extensions/')) {
49-
return false
50-
}
51-
if (externals.some(external => source === external || source.startsWith(`${external}/`))) {
52-
return true
53-
}
54-
return false
55-
},
5654
output: [{
5755
chunkFileNames: '[name].js',
5856
hoistTransitiveImports: false,
@@ -62,6 +60,18 @@ export default rollup.defineConfig({
6260
preserveModulesRoot: 'src'
6361
}],
6462
plugins: [
63+
{
64+
name: 'external-resolver',
65+
resolveId (id) {
66+
if (isExternal(id)) {
67+
return {
68+
id,
69+
external: true
70+
}
71+
}
72+
return undefined
73+
}
74+
},
6575
typescript({
6676
noEmitOnError: true
6777
}),

0 commit comments

Comments
 (0)