|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const fs = require('fs'); |
4 | | -const path = require('path'); |
| 3 | +import { realpathSync, readFileSync, existsSync, mkdirSync } from 'fs'; |
| 4 | +import { join, extname, basename, dirname } from 'path'; |
5 | 5 |
|
6 | | -const rootPath = fs.realpathSync(__dirname + '../../../'); |
7 | | -const schema = JSON.parse(fs.readFileSync(rootPath + '/composer.json').toString()); |
| 6 | +const __filename = new URL(import.meta.url).pathname; |
| 7 | +const __dirname = dirname(__filename); |
| 8 | + |
| 9 | +const rootPath = realpathSync(__dirname + '../../../'); |
| 10 | +const schema = JSON.parse(readFileSync(rootPath + '/composer.json').toString()); |
8 | 11 | const ext = schema.name.split('/'); |
9 | 12 | const namespace = '@' + ext[0] + '_' + ext[1]; |
10 | | -const buildPath = path.join(rootPath, 'build', 'package', ext[0], ext[1]); |
| 13 | +const buildPath = join(rootPath, 'build', 'package', ext[0], ext[1]); |
11 | 14 |
|
12 | | -if (!fs.existsSync(buildPath)) { |
13 | | - fs.mkdirSync(buildPath, {recursive: true, mode: 0o755}); |
| 15 | +if (!existsSync(buildPath)) { |
| 16 | + mkdirSync(buildPath, { recursive: true, mode: 0o755 }); |
14 | 17 | } |
15 | 18 |
|
16 | | -exports.buildPath = buildPath; |
17 | | -exports.replaceAssetFile = function(file, html) { |
18 | | - const fileExt = path.extname(file); |
| 19 | +const replaceAssetFile = (file, html) => { |
| 20 | + const fileExt = extname(file); |
19 | 21 |
|
20 | 22 | if (file.endsWith('.min' + fileExt)) { |
21 | 23 | return html; |
22 | 24 | } |
23 | 25 |
|
24 | | - const isMinified = fs.existsSync(file.replace(fileExt, '.min' + fileExt)); |
| 26 | + const isMinified = existsSync(file.replace(fileExt, '.min' + fileExt)); |
25 | 27 |
|
26 | 28 | if (!isMinified) { |
27 | 29 | return html; |
28 | 30 | } |
29 | 31 |
|
30 | | - const filePath = path.basename(path.dirname(file)); |
31 | | - const fileName = path.basename(file); |
32 | | - const twigNamespace = path.join(namespace, filePath, fileName); |
| 32 | + const filePath = basename(dirname(file)); |
| 33 | + const fileName = basename(file); |
| 34 | + const twigNamespace = join(namespace, filePath, fileName); |
33 | 35 |
|
34 | 36 | if (!html.includes(twigNamespace)) { |
35 | 37 | return html; |
36 | 38 | } |
37 | 39 |
|
38 | | - html = html.replace(twigNamespace, twigNamespace.replace(fileExt, '.min' + fileExt)); |
| 40 | + html = html.replace( |
| 41 | + twigNamespace, |
| 42 | + twigNamespace.replace(fileExt, '.min' + fileExt) |
| 43 | + ); |
39 | 44 |
|
40 | 45 | return html; |
41 | 46 | }; |
| 47 | + |
| 48 | +export { buildPath, replaceAssetFile }; |
0 commit comments