8
8
// -----------------------------------------------------------------------------
9
9
// ///////////////////////////////////////////////////////////////////// General
10
10
import Path from 'path'
11
+ import { readFileSync , readdirSync , writeFileSync } from 'fs'
12
+ import Klaw from 'klaw'
11
13
12
14
// ///////////////////////////////////////////////////////////////////// Plugins
13
15
const MethodsPlugin = Path . resolve ( __dirname , 'plugin.js' )
@@ -31,70 +33,111 @@ const registerPlugins = (instance, next) => {
31
33
if ( next ) { return next ( ) }
32
34
}
33
35
34
- // ------------------------------------------------------------------ parseRoute
35
- const parseRoute = ( route ) => {
36
- const relativity = '../' . repeat ( route . split ( '/' ) . length - 1 )
37
- return {
38
- // Make core nuxt files relative
39
- replaceSrc : route !== '/' ? `${ relativity } _nuxt/` : '_nuxt/' ,
40
- // Make static directory files relative -- MUST run all component paths through $relativity method
41
- replaceStatic : route !== '/' ? `${ relativity } ` : ''
42
- }
36
+ // ------------------------------------------------------------------------ seds
37
+ const seds = ( re , filepath , modifier ) => {
38
+ const original = readFileSync ( filepath , 'utf8' )
39
+ const modified = original . replace ( re , modifier )
40
+ writeFileSync ( filepath , modified )
41
+ return modified
43
42
}
44
43
45
- // -------------------------------------------------------------------- addHooks
46
- const addHooks = ( instance ) => {
47
- let staticAssetsOpts
48
- let parsed
49
-
50
- /*
51
- Grab the static asset path options to be applied in the render:routeContext
52
- hook (need the dir and the bundle version)
53
- */
54
-
55
- instance . nuxt . hook ( 'generate:before' , ( generator , generateOptions ) => {
56
- staticAssetsOpts = generateOptions . staticAssets
57
- } )
58
-
59
- /*
60
- Force the loading of all Javascript files
61
- */
44
+ // ------------------------------------------------------------------ relativize
45
+ const relativize = ( filepath ) => {
46
+ ( [ , filepath ] = filepath . split ( '/dist/' ) )
47
+ let prefix = [ ]
48
+ let length = filepath . split ( '/' ) . length - 1
49
+ for ( let i = 0 ; i < length ; i ++ ) {
50
+ prefix . push ( '..' )
51
+ }
52
+ prefix = prefix . join ( '/' ) + '/'
53
+ return ( prefix === '/' || prefix === '' ) ? './' : prefix
54
+ }
62
55
63
- instance . nuxt . hook ( 'render:resourcesLoaded' , ( resources ) => {
64
- resources . clientManifest . initial = resources . clientManifest . initial . concat ( resources . clientManifest . async )
65
- initialScripts = resources . clientManifest . initial
66
- asyncScripts = resources . clientManifest . async
56
+ // ------------------------------------------------------------------------ walk
57
+ const walk = ( dir , ext ) => {
58
+ return new Promise ( ( next ) => {
59
+ const matches = [ ]
60
+ Klaw ( dir )
61
+ . on ( 'data' , ( { path } ) => {
62
+ if ( path && ! path . includes ( 'node_modules' ) && path . endsWith ( ext ) ) {
63
+ matches . push ( path )
64
+ }
65
+ } )
66
+ . on ( 'end' , ( ) => {
67
+ next ( matches )
68
+ } )
67
69
} )
70
+ }
68
71
69
- /*
70
- This block gives us access to the generated javascript before it is
71
- serialized
72
- */
73
-
74
- instance . nuxt . hook ( 'vue-renderer:ssr:context' , ( ctx ) => {
75
- parsed = parseRoute ( ctx . nuxt . routePath )
76
- // Apply url replacements to generated javascript before it is serialized
77
- ctx . staticAssetsBase = `${ parsed . replaceSrc } ${ staticAssetsOpts . dir } /${ staticAssetsOpts . version } `
78
- } )
72
+ // ------------------------------------------------------------ processHtmlFiles
73
+ const processHtmlFiles = async ( generateRoot ) => {
74
+ const htmlFiles = await walk ( generateRoot , '.html' )
75
+ for ( const htmlFile of htmlFiles ) {
76
+ const prefix = relativize ( htmlFile )
77
+ seds ( / b a s e P a t h : " \/ i p f s \/ h a s h \/ " / gm, htmlFile , (
78
+ 'basePath:(ipfsMatch=window.location.pathname.match(/\\/ip[fn]s\\/[^/]+\\//), ipfsMatch?ipfsMatch[0]:\'/\')'
79
+ ) )
80
+ seds ( / a s s e t s P a t h : " \/ _ n u x t \/ " / gm, htmlFile , (
81
+ 'assetsPath:(ipfsMatch=window.location.pathname.match(/\\/ip[fn]s\\/[^/]+\\//), ipfsMatch?ipfsMatch[0]:\'.\') + \'/_nuxt/\''
82
+ ) )
83
+ seds ( / s t a t i c A s s e t s B a s e : " ( \/ _ n u x t \/ s t a t i c \/ ) ( [ ^ " ] + ) / gm, htmlFile , ( _ , start , end ) => {
84
+ return `staticAssetsBase:(ipfsMatch=window.location.pathname.match(/\\/ip[fn]s\\/[^/]+/),(ipfsMatch?ipfsMatch[0]:'.'))+"\u002F_nuxt\u002Fstatic\u002F${ end } `
85
+ } )
86
+ seds ( / < b a s e h r e f = " \/ i p f s \/ h a s h \/ " > / , htmlFile , true ? '' : (
87
+ '<script>\n' +
88
+ 'base = document.createElement(\'base\')\n' +
89
+ 'base.href = (ipfsMatch=window.location.pathname.match(/\\/ip[fn]s\\/[^/]+/),(ipfsMatch?ipfsMatch[0]+\'/\':\'/\'))\n' +
90
+ 'document.getElementsByTagName(\'head\')[0].appendChild(base)\n' +
91
+ '</script>'
92
+ ) )
93
+ seds ( / " \/ i p f s \/ h a s h \/ / gms, htmlFile , `"${ prefix } ` )
94
+ seds ( / " .\/ i m a g e s \/ / gms, htmlFile , `"${ prefix } images/` )
95
+ seds ( / " \/ f a v i c o n \/ / gms, htmlFile , `"${ prefix } favicon/` )
96
+ seds ( / u r l \( \/ i p f s \/ h a s h \/ / gms, htmlFile , `url(${ prefix } ` )
97
+ }
98
+ }
79
99
80
- /*
81
- This block gives us access to the generated HTML
82
- */
100
+ // -------------------------------------------------------------- processJsFiles
101
+ const processJsFiles = async ( generateRoot ) => {
102
+ const nuxtRoot = Path . join ( generateRoot , '_nuxt' )
103
+ const jsFiles = await walk ( nuxtRoot , '.js' )
104
+ for ( const jsFile of jsFiles ) {
105
+ seds ( / " \/ i p f s \/ h a s h \/ _ n u x t \/ \" / , jsFile , (
106
+ '(ipfsMatch=window.location.pathname.match(/\\/ip[fn]s\\/[^/]+\\//), ipfsMatch?ipfsMatch[0]+\'\/_nuxt\/\':\'\/_nuxt\/\')'
107
+ ) )
108
+ seds ( / b a s e : ' \/ i p f s \/ h a s h \/ ' / gm, jsFile , (
109
+ 'base:(ipfsMatch=window.location.pathname.match(/\\/ip[fn]s\\/[^/]+\\//), ipfsMatch?ipfsMatch[0]:\'/\')'
110
+ ) )
111
+ seds ( / ( s t a t i c A s s e t s B a s e : " \\ u 0 0 2 F i p f s \\ u 0 0 2 F h a s h \\ u 0 0 2 F _ n u x t \\ u 0 0 2 F s t a t i c \\ u 0 0 2 F ) ( [ ^ " ] + ) / gm, jsFile , ( _ , start , end ) => {
112
+ return `staticAssetsBase:(ipfsMatch=window.location.pathname.match(/\\/ip[fn]s\\/[^/]+/),(ipfsMatch?ipfsMatch[0]:''))+"/_nuxt\u002Fstatic\u002F${ end } `
113
+ } )
114
+ seds ( / b a s e P a t h : " \\ u 0 0 2 F i p f s \\ u 0 0 2 F h a s h \\ u 0 0 2 F " / gm, jsFile , (
115
+ 'basePath:(ipfsMatch=window.location.pathname.match(/\\/ip[fn]s\\/[^/]+\\//), ipfsMatch?ipfsMatch[0]:\'/\')'
116
+ ) )
117
+ seds ( / a s s e t s P a t h : " \\ u 0 0 2 F i p f s \\ u 0 0 2 F h a s h \\ u 0 0 2 F _ n u x t \\ u 0 0 2 F " / gm, jsFile , (
118
+ 'assetsPath:(ipfsMatch=window.location.pathname.match(/\\/ip[fn]s\\/[^/]+\\//), ipfsMatch?ipfsMatch[0]:\'.\') + \'/_nuxt/\''
119
+ ) )
120
+ seds ( / r e t u r n _ _ w e b p a c k _ r e q u i r e _ _ \. p / gm, jsFile , (
121
+ 'return __webpack_require__.p.replace(\'./\', (ipfsMatch=window.location.pathname.match(/\\/ip[fn]s\\/[^/]+\\//), ipfsMatch?ipfsMatch[0]+\'/\':\'/\'))'
122
+ ) )
123
+ }
124
+ }
83
125
84
- instance . nuxt . hook ( 'generate:page' , ( payload ) => {
85
- parsed = parseRoute ( payload . route )
86
- // Apply url replacements to generated HTML
87
- payload . html = payload . html
88
- . replace ( / " \/ _ n u x t \/ / gi, `"${ parsed . replaceSrc } ` )
89
- . replace ( / \( \/ _ n u x t \/ / gi, `(${ parsed . replaceSrc } ` )
90
- . replace ( / \/ r e l a t i v i t y \/ / gi, parsed . replaceStatic )
126
+ // -------------------------------------------------------------------- addHooks
127
+ const addHooks = async ( instance ) => {
128
+ instance . nuxt . hook ( 'generate:done' , async ( ) => {
129
+ const { dir : generateRoot } = instance . options . generate
130
+ await processHtmlFiles ( generateRoot )
131
+ await processJsFiles ( generateRoot )
91
132
} )
92
133
}
93
134
94
135
// ////////////////////////////////////////////////////////////////// Initialize
95
136
// -----------------------------------------------------------------------------
96
137
function NuxtModuleIpfs ( ) {
97
- console . log ( `📦 [Module] NuxtModuleIpfs` )
138
+ if ( process . server ) {
139
+ console . log ( `📦 [Module] NuxtModuleIpfs` )
140
+ }
98
141
registerPlugins ( this , ( ) => {
99
142
if ( process . env . NODE_ENV !== 'development' ) {
100
143
addHooks ( this )
0 commit comments