Skip to content

Commit 83198e4

Browse files
committed
fix(Provider): use fs.readFile instead of require to load the configuration
1 parent ad19034 commit 83198e4

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

providers/EncoreProvider.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFile } from 'fs/promises'
12
import { IocContract } from '@adonisjs/fold'
23
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
34

@@ -9,45 +10,50 @@ export default class EncoreProvider {
910
throw new Error('You need to install the @adonisjs/view package to use this package.')
1011
}
1112

12-
this.$container.with(['Adonis/Core/Application', 'Adonis/Core/View'], (Application: ApplicationContract, View) => {
13-
const entrypointsFilePath = Application.publicPath('/build/entrypoints.json')
14-
const { entrypoints, integrity } = require(entrypointsFilePath)
13+
this.$container.with(['Adonis/Core/Application', 'Adonis/Core/View'], async (Application: ApplicationContract, View) => {
14+
try {
15+
const entrypointsFilePath = Application.publicPath('/build/entrypoints.json')
16+
const file = await readFile(entrypointsFilePath)
17+
const { entrypoints, integrity } = JSON.parse(file.toString())
1518

16-
View.global('encoreLink', (entry: string) => {
17-
const files = entrypoints[entry]['css']
19+
View.global('encoreLink', (entry: string) => {
20+
const files = entrypoints[entry]['css']
1821

19-
return files.map((file: string) => {
20-
let html = `<link rel="stylesheet" href="${file}"`
22+
return files.map((file: string) => {
23+
let html = `<link rel="stylesheet" href="${file}"`
2124

22-
if (integrity && integrity[file]) {
23-
html += ` integrity="${integrity[file]}"`
24-
}
25+
if (integrity && integrity[file]) {
26+
html += ` integrity="${integrity[file]}"`
27+
}
2528

26-
html += '>'
29+
html += '>'
2730

28-
return html
29-
}).join('')
30-
})
31+
return html
32+
}).join('')
33+
})
3134

32-
View.global('encoreScript', (entry: string, defer = true) => {
33-
const files = entrypoints[entry]['js']
35+
View.global('encoreScript', (entry: string, defer = true) => {
36+
const files = entrypoints[entry]['js']
3437

35-
return files.map((file: string) => {
36-
let html = `<script src="${file}"`
38+
return files.map((file: string) => {
39+
let html = `<script src="${file}"`
3740

38-
if (integrity && integrity[file]) {
39-
html += ` integrity="${integrity[file]}"`
40-
}
41+
if (integrity && integrity[file]) {
42+
html += ` integrity="${integrity[file]}"`
43+
}
4144

42-
if (defer) {
43-
html += ' defer'
44-
}
45+
if (defer) {
46+
html += ' defer'
47+
}
4548

46-
html += '></script>'
49+
html += '></script>'
4750

48-
return html
49-
}).join('')
50-
})
51+
return html
52+
}).join('')
53+
})
54+
} catch (e) {
55+
//
56+
}
5157
})
5258
}
5359
}

0 commit comments

Comments
 (0)