init
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import { series, src, dest } from 'gulp';
|
||||
import clean from 'gulp-clean';
|
||||
import replace from 'gulp-replace';
|
||||
import vueTsc from './vueTemplateTsc';
|
||||
|
||||
export { default as tsc } from './vueTemplateTsc';
|
||||
|
||||
function cleanAll() {
|
||||
return src(['./target/*', '!./target/node_modules'], { allowEmpty: true }).pipe(clean({ force: true }));
|
||||
}
|
||||
|
||||
function copyIndex() {
|
||||
return src(['./index.html']).pipe(replace('.ts', '.js')).pipe(replace(' + TS', '')).pipe(dest('./target'));
|
||||
}
|
||||
|
||||
function copyPublic() {
|
||||
return src(['./public/**/*']).pipe(dest('./target/public'));
|
||||
}
|
||||
|
||||
function copyStyleAndJs() {
|
||||
return src(['./src/**/*.{less,css,js}']).pipe(dest('./target/src'));
|
||||
}
|
||||
|
||||
function copyAssets() {
|
||||
return src(['./src/assets/**/*']).pipe(dest('./target/src/assets'));
|
||||
}
|
||||
|
||||
function copyConfig() {
|
||||
return src([
|
||||
'./.env*',
|
||||
'./.gitignore',
|
||||
'.babelrc',
|
||||
'./*.{json,cjs}',
|
||||
'./LICENSE',
|
||||
'!./tsconfig.*',
|
||||
'!./package*.json',
|
||||
]).pipe(dest('./target'));
|
||||
}
|
||||
|
||||
function copyReadme() {
|
||||
return src(['./*.md']).pipe(replace('stepin-template.git', 'stepin-template-js.git')).pipe(dest('./target'));
|
||||
}
|
||||
|
||||
function copyPackageJson() {
|
||||
return src(['./package.json'])
|
||||
.pipe(replace(/,?[\r\n]+.*"(gulp[\-\w]*|vue-tsc|typescript)"[^,\r\n]*/g, ''))
|
||||
.pipe(replace(/vue-tsc --noEmit && /g, ''))
|
||||
.pipe(dest('./target'));
|
||||
}
|
||||
function cleanJsRepository() {
|
||||
return src([
|
||||
'../stepin-template-js/*',
|
||||
'!../stepin-template-js/node_modules',
|
||||
'!../stepin-template-js/.git',
|
||||
'!../stepin-template-js/.history',
|
||||
'!../stepin-template-js/.vscode',
|
||||
]).pipe(clean({ force: true }));
|
||||
}
|
||||
export function copyToJsRepository() {
|
||||
return src([
|
||||
'./target/**/*',
|
||||
'./.env*',
|
||||
'./.gitignore',
|
||||
'.babelrc',
|
||||
'!./target/node_modules/**/*',
|
||||
'!./target/node_modules',
|
||||
]).pipe(dest('../stepin-template-js'));
|
||||
}
|
||||
|
||||
export function copyDocs() {
|
||||
return src(['./docs/**/*', '!./docs/.vitepress/**/*', '!./docs/.vitepress']).pipe(dest('../stepin-template-js/docs'));
|
||||
}
|
||||
|
||||
export const makeJs = series(cleanJsRepository, copyToJsRepository, copyDocs);
|
||||
|
||||
export default series(
|
||||
cleanAll,
|
||||
vueTsc,
|
||||
copyIndex,
|
||||
copyPublic,
|
||||
copyStyleAndJs,
|
||||
copyConfig,
|
||||
copyReadme,
|
||||
copyAssets,
|
||||
copyPackageJson
|
||||
);
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Transform } from 'stream';
|
||||
|
||||
export class BufferTransform extends Transform {
|
||||
constructor(transform) {
|
||||
super({
|
||||
objectMode: true,
|
||||
transform(file, enc, callback) {
|
||||
if (file.isBuffer()) {
|
||||
file.contents = Buffer.from(transform(new String(file.contents)));
|
||||
}
|
||||
if (file.isStream()) {
|
||||
console.log('stream file', file.path);
|
||||
}
|
||||
return callback(null, file);
|
||||
},
|
||||
});
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
import { src, dest, series } from 'gulp';
|
||||
import rename from 'gulp-rename';
|
||||
import replace from 'gulp-replace';
|
||||
import { BufferTransform } from './transform';
|
||||
|
||||
import ts from 'typescript';
|
||||
import prettier from 'prettier';
|
||||
import tsConfig from '../tsconfig.json';
|
||||
|
||||
tsConfig.compilerOptions.sourceMap = false;
|
||||
tsConfig.compilerOptions.importsNotUsedAsValues = 'preserve';
|
||||
|
||||
/**
|
||||
* vue 文件代码格式化
|
||||
* @param {*} code
|
||||
* @returns
|
||||
*/
|
||||
const vueFormatter = () =>
|
||||
new BufferTransform((code) =>
|
||||
prettier.format(code, {
|
||||
trailingComma: 'es5',
|
||||
parser: 'vue',
|
||||
vueIndentScriptAndStyle: true,
|
||||
singleQuote: true,
|
||||
})
|
||||
);
|
||||
/**
|
||||
* ts 文件代码格式化
|
||||
* @param {*} code
|
||||
* @returns
|
||||
*/
|
||||
const tsFormatter = () =>
|
||||
new BufferTransform((code) =>
|
||||
prettier.format(code, {
|
||||
trailingComma: 'es5',
|
||||
parser: 'typescript',
|
||||
singleQuote: true,
|
||||
})
|
||||
);
|
||||
|
||||
/** ts编译器 */
|
||||
const tsToJs = (tsStrCode) => {
|
||||
const code = tsStrCode
|
||||
.replace(/defineEmits<[\s\S]*>\(\);/g, (match) =>
|
||||
match.replace(/'?([\w:]*)'?:\s*\[.*\];/g, "'$1',").replace(/\(e:\s*(\'[^',]*\')[^;]*;/g, '$1,')
|
||||
)
|
||||
.replace(/<{([^\{\}<>]*)}>\(\)/g, '([$1])');
|
||||
const result = ts.transpileModule(code, tsConfig);
|
||||
const jsStrCode = result.outputText;
|
||||
return jsStrCode;
|
||||
};
|
||||
|
||||
/**
|
||||
* SFC 文件 ts 代码编译
|
||||
* @param {*} tmpContent
|
||||
* @returns
|
||||
*/
|
||||
const handleTmpContent = (tmpContent) => {
|
||||
if (!tmpContent) {
|
||||
return tmpContent;
|
||||
}
|
||||
return tmpContent
|
||||
.replace(/(?<=((@|:|v-)[\w\-]*="))([^"]*)(?=")/g, function (match) {
|
||||
if (/\s*{[\s\S]*}\s*/.test(match)) {
|
||||
return tsToJs(`const patch = ${match}`).replace(/;\s*/g, '').replace('const patch = ', '');
|
||||
}
|
||||
return `${tsToJs(match).replace(/;\s*/g, '')}`;
|
||||
})
|
||||
.replace(/(?<=({{))([^{}]*)(?=}})/g, function (match) {
|
||||
if (/\s*{[\s\S]*}\s*/.test(match)) {
|
||||
return tsToJs(`const patch = ${match}`).replace(/;\s*/g, '').replace('const patch = ', '');
|
||||
}
|
||||
return `${tsToJs(match).replace(/;\s*$/, '')}`;
|
||||
});
|
||||
};
|
||||
|
||||
function transformVue(options) {
|
||||
return new BufferTransform((content) =>
|
||||
content
|
||||
.replace(/(?<=(\<template>))([\s\S]*)(?=(\<\/template>))/, function (match) {
|
||||
return handleTmpContent(match);
|
||||
})
|
||||
.replace(/(?<=(\<script lang="ts".*>))([\s\S]*)(?=(\<\/script>))/, (match) => tsToJs(match))
|
||||
.replace(/lang="ts"/, '')
|
||||
);
|
||||
}
|
||||
|
||||
function cleanTypeImport() {
|
||||
const types = [
|
||||
'MenuProps',
|
||||
'GuiderOption',
|
||||
'IconSelectGroup',
|
||||
'IconSelectOption',
|
||||
'PropType',
|
||||
'EChartsType',
|
||||
'Color',
|
||||
'FormInstance',
|
||||
'PaginationProps',
|
||||
'Dayjs',
|
||||
'Ref',
|
||||
'TreeSelectProps',
|
||||
'ComponentPublicInstance',
|
||||
];
|
||||
const typeImportRegexp = new RegExp(
|
||||
'(?<=(import\\s+[\\w,\\s]*{[^{]*[,\\s]+))(' + types.join('|') + '),?(?=([^}]*\\s*}\\s*from))',
|
||||
'g'
|
||||
);
|
||||
return new BufferTransform((content) =>
|
||||
content
|
||||
.replace(/import\s+{\s*Response\s*}\s+from\s+\'.*\';?/g, '')
|
||||
.replace(typeImportRegexp, '')
|
||||
.replace(/import\s+.*from\s+\'.*\/interface(.d.ts)?\';?/g, '')
|
||||
.replace(/import\s+{\s*}\s*from\s+\'.*\'/g, '')
|
||||
);
|
||||
}
|
||||
|
||||
function transformTs() {
|
||||
return new BufferTransform((content) => tsToJs(content));
|
||||
}
|
||||
|
||||
function tsc() {
|
||||
tsConfig.compilerOptions.importsNotUsedAsValues = 'preserve';
|
||||
tsConfig.compilerOptions.preserveValueImports = true;
|
||||
return src(['./src/**/*.vue'])
|
||||
.pipe(transformVue())
|
||||
.pipe(cleanTypeImport())
|
||||
.pipe(vueFormatter())
|
||||
.pipe(dest('./target/src'));
|
||||
}
|
||||
|
||||
function tsTsc() {
|
||||
tsConfig.compilerOptions.importsNotUsedAsValues = 'remove';
|
||||
tsConfig.compilerOptions.preserveValueImports = false;
|
||||
return src(['./src/**/*.ts', '!./src/**/*.d.ts'])
|
||||
.pipe(transformTs())
|
||||
.pipe(replace('{ts,vue,tsx}', '{js,vue}'))
|
||||
.pipe(replace('(js|ts|tsx|vue)', '(js|jsx|vue)'))
|
||||
.pipe(cleanTypeImport())
|
||||
.pipe(tsFormatter())
|
||||
.pipe(rename((path) => (path.extname = '.js')))
|
||||
.pipe(dest('./target/src'));
|
||||
}
|
||||
|
||||
function compileViteConfig() {
|
||||
return src(['./vite.config.ts'])
|
||||
.pipe(transformTs())
|
||||
.pipe(tsFormatter())
|
||||
.pipe(rename((path) => (path.extname = '.js')))
|
||||
.pipe(dest('./target'));
|
||||
}
|
||||
|
||||
export default series(tsc, tsTsc, compileViteConfig);
|
||||
Reference in New Issue
Block a user