@@ -2,6 +2,19 @@ import preserveDirectives from 'rollup-plugin-preserve-directives';
2
2
import { getBuildConfig } from 'tools' ;
3
3
import pkg from './package.json' with { type : 'json' } ;
4
4
5
+ function rewriteBundle ( regex , replaceFn ) {
6
+ return {
7
+ name : 'rewrite-bundle' ,
8
+ generateBundle ( options , bundle ) {
9
+ for ( const fileName of Object . keys ( bundle ) ) {
10
+ const chunk = bundle [ fileName ] ;
11
+ const updatedCode = chunk . code . replace ( regex , replaceFn ) ;
12
+ chunk . code = updatedCode ;
13
+ }
14
+ }
15
+ } ;
16
+ }
17
+
5
18
export default [
6
19
...getBuildConfig ( {
7
20
input : {
@@ -34,7 +47,20 @@ export default [
34
47
if ( warning . code === 'MODULE_LEVEL_DIRECTIVE' ) return ;
35
48
warn ( warning ) ;
36
49
} ,
37
- plugins : [ preserveDirectives ( ) ]
50
+ plugins : [
51
+ preserveDirectives ( ) ,
52
+
53
+ // Since we're writing our code with ESM, we have to import e.g. from
54
+ // `next/link.js`. While this can be used in production, since Next.js 15
55
+ // this somehow causes hard reloads when `next/link.js` is imported and
56
+ // used to link to another page. There might be some optimizations
57
+ // happening in the background that we can't control. Due to this, it
58
+ // seems safer to update imports to a version that doesn't have `.js`
59
+ // suffix and let the bundler optimize them.
60
+ rewriteBundle ( / [ ' " ] n e x t \/ ( \w + ) \. j s [ ' " ] / g, ( match , p1 ) =>
61
+ match . replace ( `next/${ p1 } .js` , `next/${ p1 } ` )
62
+ )
63
+ ]
38
64
} ) ,
39
65
...getBuildConfig ( {
40
66
env : [ 'development' ] ,
0 commit comments