forked from tailwindlabs/tailwindcss.com
-
Notifications
You must be signed in to change notification settings - Fork 3
/
file-replacer.js
38 lines (31 loc) · 1 KB
/
file-replacer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const { getOptions } = require("loader-utils")
const fs = require('fs')
const fsPromises = require('fs').promises
const path = require('path')
async function fileReplacer (source) {
const { base, replacement } = getOptions(this)
const filename = path.parse(this.resourcePath)
const saveFilename = filename.base
const targetFilename = (
base
?
path.join(base,String(replacement[saveFilename]))
:
replacement[saveFilename]
)
try{
await fsPromises.access(targetFilename, fs.constants.R_OK | fs.constants.W_OK)
const stat = await fsPromises.lstat(targetFilename)
if(stat.isFile()){
const translatedFileContent = await fsPromises.readFile(targetFilename)
console.log(`[file-replacer] replaced '${saveFilename}' to '${targetFilename}'`)
return translatedFileContent
} else {
throw `Target path "${targetFilename}" is not a file.`
}
} catch(e) {
// console.log(`[file-replacer] ${e}`)
return source
}
};
module.exports = fileReplacer