-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: the production environment can be dynamically configured
- Loading branch information
1 parent
e83cb06
commit bb3b8f8
Showing
29 changed files
with
562 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,7 @@ VITE_GLOB_API_URL=/api | |
|
||
# Interface prefix | ||
VITE_GLOB_API_URL_PREFIX= | ||
|
||
|
||
# TODO use Cdn | ||
VITE_USE_CDN = true |
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,21 @@ | ||
const css = ['//cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.min.css']; | ||
|
||
// TODO use esm? | ||
const js = [ | ||
'//cdn.bootcdn.net/ajax/libs/vue/3.0.0/vue.global.prod.js', | ||
'//cdn.bootcdn.net/ajax/libs/vue-router/4.0.0-beta.13/vue-router.global.min.js', | ||
'//cdn.bootcdn.net/ajax/libs/vuex/4.0.0-beta.4/vuex.global.prod.js', | ||
'//cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js', | ||
'//cdn.bootcdn.net/ajax/libs/qs/6.9.4/qs.min.js', | ||
'//cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.min.js', | ||
// '//cdn.bootcdn.net/ajax/libs/lodash.js/4.17.15/lodash.min.js', | ||
// '//cdn.bootcdn.net/ajax/libs/crypto-js/3.3.0/crypto-js.min.js', | ||
// '//cdn.bootcdn.net/ajax/libs/vue-i18n/8.18.1/vue-i18n.min.js', | ||
]; | ||
|
||
export const externals = ['vue', 'vuex', 'vue-router', 'axios', 'qs', 'nprogress']; | ||
|
||
export const cdnConf = { | ||
css, | ||
js, | ||
}; |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const GLOB_CONFIG_FILE_NAME = '_app.config.js'; |
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,5 @@ | ||
export const getShortName = (env: any) => { | ||
return `__PRODUCTION__${env.VITE_GLOB_APP_SHORT_NAME || '__APP'}__CONF__` | ||
.toUpperCase() | ||
.replace(/\s/g, ''); | ||
}; |
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,36 @@ | ||
// js调用cli 兼容调用ts | ||
|
||
const { sh } = require('tasksfile'); | ||
const { argv } = require('yargs'); | ||
|
||
let command = ``; | ||
|
||
Object.keys(argv).forEach((key) => { | ||
if (!/^\$/.test(key) && key !== '_') { | ||
// @ts-ignore | ||
if (argv[key]) { | ||
command += `--${key} `; | ||
} | ||
} | ||
}); | ||
|
||
// 执行任务名称 | ||
let taskList = argv._; | ||
|
||
let NODE_ENV = process.env.NODE_ENV || 'development'; | ||
|
||
if (taskList.includes('build') || taskList.includes('report') || taskList.includes('preview')) { | ||
NODE_ENV = 'production'; | ||
} | ||
|
||
if (taskList && Array.isArray(taskList) && taskList.length) { | ||
sh( | ||
`cross-env NODE_ENV=${NODE_ENV} ts-node --project ./build/tsconfig.json ./build/script/cli.ts ${taskList.join( | ||
' ' | ||
)} ${command}`, | ||
{ | ||
async: true, | ||
nopipe: true, | ||
} | ||
); | ||
} |
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,28 @@ | ||
// #!/usr/bin/env node | ||
|
||
import { sh } from 'tasksfile'; | ||
import { argv } from 'yargs'; | ||
import { runBuildConfig } from './buildConf'; | ||
import { runUpdateHtml } from './updateHtml'; | ||
import { errorConsole, successConsole } from '../utils'; | ||
|
||
export const runBuild = async () => { | ||
try { | ||
const argvList = argv._; | ||
let cmd = `cross-env NODE_ENV=production vite build`; | ||
await sh(cmd, { | ||
async: true, | ||
nopipe: true, | ||
}); | ||
|
||
// Generate configuration file | ||
if (!argvList.includes('no-conf')) { | ||
await runBuildConfig(); | ||
} | ||
await runUpdateHtml(); | ||
successConsole('Vite Build successfully!'); | ||
} catch (error) { | ||
errorConsole('Vite Build Error\n' + error); | ||
process.exit(1); | ||
} | ||
}; |
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,44 @@ | ||
import { GLOB_CONFIG_FILE_NAME } from '../constant'; | ||
import fs, { writeFileSync } from 'fs-extra'; | ||
|
||
import viteConfig from '../../vite.config'; | ||
import { errorConsole, successConsole, getCwdPath, getEnvConfig } from '../utils'; | ||
|
||
const getShortName = (env: any) => { | ||
return `__PRODUCTION__${env.VITE_GLOB_APP_SHORT_NAME || '__APP'}__CONF__` | ||
.toUpperCase() | ||
.replace(/\s/g, ''); | ||
}; | ||
|
||
function createConfig( | ||
{ | ||
configName, | ||
config, | ||
configFileName = GLOB_CONFIG_FILE_NAME, | ||
}: { configName: string; config: any; configFileName?: string } = { configName: '', config: {} } | ||
) { | ||
try { | ||
const windowConf = `window.${configName}`; | ||
const outDir = viteConfig.outDir || 'dist'; | ||
const configStr = `${windowConf}=${JSON.stringify(config)}; | ||
Object.freeze(${windowConf}); | ||
Object.defineProperty(window, "${configName}", { | ||
configurable: false, | ||
writable: false, | ||
}); | ||
`; | ||
fs.mkdirp(getCwdPath(outDir)); | ||
writeFileSync(getCwdPath(`${outDir}/${configFileName}`), configStr); | ||
|
||
successConsole('The configuration file is build successfully!'); | ||
} catch (error) { | ||
errorConsole('Configuration file configuration file failed to package\n' + error); | ||
} | ||
} | ||
|
||
export function runBuildConfig() { | ||
const config = getEnvConfig(); | ||
const configFileName = getShortName(config); | ||
createConfig({ config, configName: configFileName }); | ||
} |
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,45 @@ | ||
#!/usr/bin/env node | ||
|
||
import chalk from 'chalk'; | ||
import { argv } from 'yargs'; | ||
|
||
import { runChangeLog } from './changelog'; | ||
import { runPostInstall } from './postinstall'; | ||
import { runPreview } from './preview'; | ||
import { runPreserve } from './preserve'; | ||
import { runBuild } from './build'; | ||
|
||
const task = (argv._ || [])[0]; | ||
|
||
console.log('Run Task: ' + chalk.cyan(task)); | ||
|
||
switch (task) { | ||
// change log | ||
case 'log': | ||
runChangeLog(); | ||
break; | ||
|
||
case 'build': | ||
runBuild(); | ||
break; | ||
|
||
case 'preserve': | ||
runPreserve(); | ||
break; | ||
|
||
case 'postinstall': | ||
runPostInstall(); | ||
break; | ||
|
||
case 'preview': | ||
runPreview(); | ||
break; | ||
|
||
// TODO | ||
case 'gzip': | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
export default {}; |
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,12 @@ | ||
// 百度统计代码 用于站点部署 | ||
// 只在build:site开启 | ||
export const hmScript = `<script> | ||
var _hmt = _hmt || []; | ||
(function() { | ||
var hm = document.createElement("script"); | ||
hm.src = "https://hm.baidu.com/hm.js?384d6046e02f6ac4ea075357bd0e9b43"; | ||
var s = document.getElementsByTagName("script")[0]; | ||
s.parentNode.insertBefore(hm, s); | ||
})(); | ||
</script> | ||
`; |
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
Oops, something went wrong.