forked from redmine-theme/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor main.js with separating build step
- Loading branch information
Showing
6 changed files
with
110 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
// Get command-line arguments | ||
const args = process.argv.slice(2); | ||
const targetDir = args[0]; // First argument: target directory | ||
const basePath = args[1]; // Second argument: base path for assets with leading/trailing slashes | ||
|
||
// Check if required arguments are provided | ||
if (!targetDir || !basePath) { | ||
console.error('Usage: node update-assets-paths.js <target directory> <base path>'); | ||
process.exit(1); | ||
} | ||
|
||
function updateAssetsPaths(targetDir, basePath) { | ||
const files = fs.readdirSync(targetDir); | ||
|
||
files.forEach((file) => { | ||
const filePath = path.join(targetDir, file); | ||
const stat = fs.statSync(filePath); | ||
|
||
if (stat.isDirectory()) { | ||
updateAssetsPaths(filePath, basePath); // Recursively process subdirectories | ||
} else if (file.endsWith('.html') || file.endsWith('.css') || file.endsWith('.js')) { | ||
let content = fs.readFileSync(filePath, 'utf8'); | ||
|
||
if (content.match(/(["'\(])\/(assets|attachments|images|javascripts|stylesheets)\//g)) { | ||
// Add base path to assets paths | ||
const updatedContent = content.replace( | ||
/(["'\(])\/(assets|attachments|images|javascripts|stylesheets)\//g, | ||
`$1${basePath}$2/` | ||
); | ||
|
||
fs.writeFileSync(filePath, updatedContent, 'utf8'); | ||
console.log(`Updated: ${filePath}`); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
try { | ||
updateAssetsPaths(targetDir, basePath); | ||
console.log('Processing completed successfully.'); | ||
} catch (error) { | ||
console.error('Error during processing:', error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import fs from 'fs'; | ||
|
||
// Add escaping process with reference to vite-plugin-raw(https://github.com/xingw-z/vite-plugin-raw) | ||
function vitePluginHtmlForServe() { | ||
return { | ||
name: 'vite-plugin-html-for-server', | ||
apply: 'serve', | ||
async transform (_, id) { | ||
const files = [/\.html$/]; | ||
if (files.find((v) => v.test(id))) { | ||
const buf = fs.readFileSync(id, 'utf-8'); | ||
return { | ||
code: `export default ${JSON.stringify(buf)}` | ||
}; | ||
} else { | ||
return {}; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Add escaping process with reference at build time (https://github.com/vitejs/vite/discussions/12788#discussioncomment-5557866) | ||
function vitePluginHtmlForBuild() { | ||
const postfix = '?html-import'; | ||
const postfixRegExp = /\?html-import$/; | ||
return { | ||
name: 'vite-plugin-html-for-build', | ||
enforce: 'pre', | ||
apply: 'build', | ||
async resolveId (id, importer, options) { | ||
if (/\.html$/.test(id) && !options.isEntry) { | ||
const res = await this.resolve(id, importer, { | ||
skipSelf: true, | ||
...options | ||
}); | ||
if (!res || res.external) { | ||
return res; | ||
} | ||
return res.id + postfix; | ||
} | ||
}, | ||
async load(id) { | ||
if (!id.endsWith(postfix)) { | ||
return; | ||
} | ||
const buf = fs.readFileSync(id.replace(postfixRegExp, ''), 'utf-8'); | ||
return `export default ${JSON.stringify(buf)}`; | ||
} | ||
} | ||
} | ||
|
||
export default function vitePluginHtml() { | ||
return [ | ||
vitePluginHtmlForServe(), | ||
vitePluginHtmlForBuild() | ||
] | ||
} |