-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
719aa6e
commit 0685191
Showing
10 changed files
with
604 additions
and
385 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
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,47 @@ | ||
// @ts-check | ||
const { extname } = require('path') | ||
const fs = require('fs') | ||
|
||
/** | ||
* @param {boolean} isDevelopment | ||
* @returns {import('vite').Plugin} | ||
*/ | ||
const customFilePlugin = (isDevelopment) => { | ||
const fileRegex = /\.(jsx?|css)$/ | ||
const customPaths = [] | ||
return { | ||
name: 'vite-plugin-custom-file-checker', | ||
load(id) { | ||
if (fileRegex.test(id) && !/node_modules/.test(id)) { | ||
const ext = extname(id) | ||
const newPath = id.replace(ext, `.custom${ext}`) | ||
if (fs.existsSync(newPath)) { | ||
customPaths.push(newPath) | ||
return { | ||
code: fs.readFileSync(newPath, 'utf8'), | ||
map: null, | ||
} | ||
} | ||
} | ||
}, | ||
buildEnd() { | ||
if (customPaths.length && !isDevelopment) { | ||
this.warn(` | ||
====================================================== | ||
WARNING: | ||
Custom files aren't officially supported | ||
Be sure to watch for breaking changes! | ||
${customPaths.map((x, i) => ` ${i + 1}. src/${x.split('src/')[1]}`).join('\n')} | ||
====================================================== | ||
`) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
module.exports = { | ||
customFilePlugin, | ||
} |
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,26 @@ | ||
// @ts-check | ||
const { resolve } = require('path') | ||
const fs = require('fs') | ||
|
||
/** | ||
* @returns {import('vite').Plugin} | ||
*/ | ||
const faviconPlugin = () => ({ | ||
name: 'vite-plugin-locales', | ||
generateBundle() { | ||
const favicon = fs.existsSync( | ||
resolve(__dirname, '../../../public/favicon/favicon.ico'), | ||
) | ||
? resolve(__dirname, '../../../public/favicon/favicon.ico') | ||
: resolve(__dirname, '../../../public/favicon/fallback.ico') | ||
this.emitFile({ | ||
type: 'asset', | ||
fileName: 'favicon.ico', | ||
source: fs.readFileSync(favicon), | ||
}) | ||
}, | ||
}) | ||
|
||
module.exports = { | ||
faviconPlugin, | ||
} |
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,11 @@ | ||
const { localePlugin } = require('./locale') | ||
const { faviconPlugin } = require('./favicon') | ||
const { muteWarningsPlugin } = require('./muteWarnings') | ||
const { customFilePlugin } = require('./customFile') | ||
|
||
module.exports = { | ||
localePlugin, | ||
faviconPlugin, | ||
muteWarningsPlugin, | ||
customFilePlugin, | ||
} |
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,34 @@ | ||
// @ts-check | ||
const { join } = require('path') | ||
|
||
const { create, writeAll } = require('@rm/locales') | ||
|
||
/** | ||
* @param {boolean} isDevelopment | ||
* @returns {import('vite').Plugin} | ||
*/ | ||
const localePlugin = (isDevelopment) => ({ | ||
name: 'vite-plugin-locales', | ||
async buildStart() { | ||
if (!isDevelopment) return | ||
const localeObj = await create() | ||
await writeAll(localeObj, true, __dirname, './public/locales') | ||
}, | ||
async generateBundle() { | ||
if (isDevelopment) return | ||
const localeObj = await create() | ||
|
||
Object.entries(localeObj).forEach(([locale, translations]) => { | ||
const fileName = join('locales', locale, 'translation.json') | ||
this.emitFile({ | ||
type: 'asset', | ||
fileName, | ||
source: JSON.stringify(translations), | ||
}) | ||
}) | ||
}, | ||
}) | ||
|
||
module.exports = { | ||
localePlugin, | ||
} |
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,51 @@ | ||
// @ts-check | ||
|
||
/** | ||
* @param {string[][]} warningsToIgnore | ||
* @returns {import('vite').Plugin} | ||
*/ | ||
const muteWarningsPlugin = (warningsToIgnore) => { | ||
const mutedMessages = new Set() | ||
return { | ||
name: 'mute-warnings', | ||
enforce: 'pre', | ||
config: (userConfig) => ({ | ||
build: { | ||
rollupOptions: { | ||
onwarn(warning, defaultHandler) { | ||
if (warning.code) { | ||
const muted = warningsToIgnore.find( | ||
([code, message]) => | ||
code == warning.code && warning.message.includes(message), | ||
) | ||
|
||
if (muted) { | ||
mutedMessages.add(muted.join()) | ||
return | ||
} | ||
} | ||
|
||
if (userConfig.build?.rollupOptions?.onwarn) { | ||
userConfig.build.rollupOptions.onwarn(warning, defaultHandler) | ||
} else { | ||
defaultHandler(warning) | ||
} | ||
}, | ||
}, | ||
}, | ||
}), | ||
closeBundle() { | ||
const diff = warningsToIgnore.filter((x) => !mutedMessages.has(x.join())) | ||
if (diff.length > 0) { | ||
this.warn( | ||
'Some of your muted warnings never appeared during the build process:', | ||
) | ||
diff.forEach((m) => this.warn(`- ${m.join(': ')}`)) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
module.exports = { | ||
muteWarningsPlugin, | ||
} |
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,20 @@ | ||
{ | ||
"name": "@rm/vite-plugins", | ||
"version": "0.1.0", | ||
"private": true, | ||
"main": "./lib/index.js", | ||
"scripts": { | ||
"sort": "npx sort-package-json", | ||
"depCheck": "npx depcheck", | ||
"lint": "eslint \"**/*.{js,jsx}\"", | ||
"lint:fix": "eslint \"**/*.{js,jsx}\" --fix", | ||
"prettier": "prettier --check \"**/*.{css,html,js,jsx,yml}\"", | ||
"prettier:fix": "prettier --write \"**/*.{css,html,js,jsx,yml}\"" | ||
}, | ||
"dependencies": { | ||
"@rm/locales": "*" | ||
}, | ||
"devDependencies": { | ||
"vite": "5.0.12" | ||
} | ||
} |
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
Oops, something went wrong.