From 8cec113254a0c3ba409c2113f5cb00eb8be00bd1 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 18 Dec 2019 11:16:17 +0800 Subject: [PATCH 1/5] chore: version --- packages/ice-scripts/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ice-scripts/package.json b/packages/ice-scripts/package.json index 2b3b937..267e503 100644 --- a/packages/ice-scripts/package.json +++ b/packages/ice-scripts/package.json @@ -1,6 +1,6 @@ { "name": "ice-scripts", - "version": "2.1.14", + "version": "2.1.15", "description": "ICE SDK", "main": "index.js", "bin": { From 2156808d8300b29e975322268e63c136fbbe7761 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 18 Dec 2019 15:14:06 +0800 Subject: [PATCH 2/5] feat: generate declaration when compile ts --- packages/ice-plugin-component/CHANGELOG.md | 4 +++ .../lib/compile/component/buildSrc.js | 34 ++++++++++++++++++- packages/ice-plugin-component/package.json | 5 +-- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/packages/ice-plugin-component/CHANGELOG.md b/packages/ice-plugin-component/CHANGELOG.md index de91111..4640a76 100644 --- a/packages/ice-plugin-component/CHANGELOG.md +++ b/packages/ice-plugin-component/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.1.10 + + - [feat] generate declaration when compile ts + ## 0.1.7 - [feat] support option watch for compile library when source files changed diff --git a/packages/ice-plugin-component/lib/compile/component/buildSrc.js b/packages/ice-plugin-component/lib/compile/component/buildSrc.js index 73a4f68..8ac0b74 100644 --- a/packages/ice-plugin-component/lib/compile/component/buildSrc.js +++ b/packages/ice-plugin-component/lib/compile/component/buildSrc.js @@ -5,12 +5,13 @@ * - 生成 style.js 和 index.scss */ -const { createReadStream, createWriteStream, writeFileSync } = require('fs'); +const { createReadStream, createWriteStream, writeFileSync, ensureDirSync } = require('fs-extra'); const babel = require('@babel/core'); const glob = require('glob'); const mkdirp = require('mkdirp'); const path = require('path'); const rimraf = require('rimraf'); +const ts = require('typescript'); module.exports = function componentBuild({ babelConfig, rootDir, log }) { const srcDir = path.join(rootDir, 'src'); @@ -54,6 +55,7 @@ module.exports = function componentBuild({ babelConfig, rootDir, log }) { filename: file, })); writeFileSync(path.format(destData), code, 'utf-8'); + dtsCompile({ filePath: file, sourceFile: source, destPath: libDir }); log.info(`Compile ${file}`); } @@ -69,4 +71,34 @@ module.exports = function componentBuild({ babelConfig, rootDir, log }) { log.info(`Copy ${file}`); }); } + // https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file + function dtsCompile({ filePath, sourceFile, destPath }) { + const REG_TS = /\.(tsx?)$/; + const isTS = REG_TS.test(filePath); + if (!isTS) return; + const compilerOptions = { + allowJs: true, + declaration: true, + emitDeclarationOnly: true, + }; + const dtsPath = filePath.replace(REG_TS, '.d.ts'); + const targetPath = path.join(destPath, dtsPath); + // Create a Program with an in-memory emit + let createdFiles = {}; + const host = ts.createCompilerHost(compilerOptions); + host.writeFile = (fileName, contents) => createdFiles[fileName] = contents; + // Prepare and emit the d.ts files + const program = ts.createProgram([sourceFile], compilerOptions, host); + program.emit(); + const fileNamesDTS = sourceFile.replace(REG_TS, '.d.ts'); + const content = createdFiles[fileNamesDTS]; + // write file + if (content) { + ensureDirSync(path.dirname(targetPath)); + writeFileSync(targetPath, content, 'utf-8'); + log.info(`Generate ${path.basename(targetPath)}`); + } + // release + createdFiles = null; + } }; diff --git a/packages/ice-plugin-component/package.json b/packages/ice-plugin-component/package.json index 0781245..056837b 100644 --- a/packages/ice-plugin-component/package.json +++ b/packages/ice-plugin-component/package.json @@ -1,6 +1,6 @@ { "name": "ice-plugin-component", - "version": "0.1.8", + "version": "0.1.10", "description": "ice plugin for develop component", "main": "lib/index.js", "scripts": { @@ -27,6 +27,7 @@ "npmlog": "^4.1.2", "prismjs": "^1.16.0", "resolve": "^1.11.0", - "resolve-sass-import": "^0.1.0" + "resolve-sass-import": "^0.1.0", + "typescript": "^3.7.3" } } From 873cdac5b945758c141d893813c8a4b981fa4fa1 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 18 Dec 2019 15:33:55 +0800 Subject: [PATCH 3/5] feat: cli option for skip webpack compile --- CHANGELOG.md | 4 ++ packages/ice-scripts/bin/ice-scripts-build.js | 1 + packages/ice-scripts/lib/commands/build.js | 52 +++++++++++-------- packages/ice-scripts/package.json | 2 +- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbb852e..2f6ebe1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.1.15 + +- [feat] cli option --skip-compile for skip webpack compile + ## 2.1.14 - [feat] support postcssrc/postcss.config.js #2952 diff --git a/packages/ice-scripts/bin/ice-scripts-build.js b/packages/ice-scripts/bin/ice-scripts-build.js index c886f8b..6f8e86b 100755 --- a/packages/ice-scripts/bin/ice-scripts-build.js +++ b/packages/ice-scripts/bin/ice-scripts-build.js @@ -8,6 +8,7 @@ program .option('--config ', 'use custom config') .option('--analyzer', '开启构建分析') .option('--analyzer-port', '设置分析端口号') + .option('--skip-compile', 'skip webpack compile, excute hooks for component compile') .parse(process.argv); (async () => { diff --git a/packages/ice-scripts/lib/commands/build.js b/packages/ice-scripts/lib/commands/build.js index 31a105b..bb0c5ec 100644 --- a/packages/ice-scripts/lib/commands/build.js +++ b/packages/ice-scripts/lib/commands/build.js @@ -43,28 +43,34 @@ module.exports = async function (context) { } } - // empty output path - fse.emptyDirSync(webpackConfig.output.path); - return new Promise((resolve, reject) => { - webpack(webpackConfig, (error, stats) => { - if (error) { - return reject(error); - } - console.log( - stats.toString({ - colors: true, - chunks: false, - children: false, - modules: false, - chunkModules: false, - }) - ); - if (stats.hasErrors()) { - return reject(new Error('webpack compiled failed.')); - } - log.info('ICE build finished'); - applyHook('afterBuild', stats); - resolve(); + const skipCompile = commandArgs.skipCompile || process.env.SKIP_COMPILE; + if (skipCompile) { + applyHook('afterBuild', {}); + return Promise.resolve(); + } else { + // empty output path + fse.emptyDirSync(webpackConfig.output.path); + return new Promise((resolve, reject) => { + webpack(webpackConfig, (error, stats) => { + if (error) { + return reject(error); + } + console.log( + stats.toString({ + colors: true, + chunks: false, + children: false, + modules: false, + chunkModules: false, + }), + ); + if (stats.hasErrors()) { + return reject(new Error('webpack compiled failed.')); + } + log.info('ICE build finished'); + applyHook('afterBuild', stats); + resolve(); + }); }); - }); + } }; diff --git a/packages/ice-scripts/package.json b/packages/ice-scripts/package.json index 2b3b937..267e503 100644 --- a/packages/ice-scripts/package.json +++ b/packages/ice-scripts/package.json @@ -1,6 +1,6 @@ { "name": "ice-scripts", - "version": "2.1.14", + "version": "2.1.15", "description": "ICE SDK", "main": "index.js", "bin": { From b3b61ec0f447ff54480031475d50577d6fcc9c30 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 18 Dec 2019 15:35:21 +0800 Subject: [PATCH 4/5] fix: ensure dir before copy --- packages/ice-plugin-component/lib/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/ice-plugin-component/lib/index.js b/packages/ice-plugin-component/lib/index.js index 191f368..fcda363 100644 --- a/packages/ice-plugin-component/lib/index.js +++ b/packages/ice-plugin-component/lib/index.js @@ -124,6 +124,7 @@ module.exports = ({ context, chainWebpack, onHook, log }, opts = {}) => { if (hasAdaptor) { // generate adaptor index.scss const sassContent = resolveSassImport('main.scss', path.resolve(rootDir, 'src')); + fse.ensureDirSync(path.join(rootDir, 'build')); fse.writeFileSync(path.resolve(rootDir, 'build/index.scss'), sassContent, 'utf-8'); // adaptor build reRun(); From 33e8359890c2a9f63826e8e2a7eff9dddb34adb3 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Sat, 28 Dec 2019 09:46:33 +0800 Subject: [PATCH 5/5] feat: support process env to disable pv collect --- CHANGELOG.md | 4 ++ packages/ice-scripts/lib/commands/build.js | 7 +--- packages/ice-scripts/lib/commands/dev.js | 8 +--- packages/ice-scripts/lib/utils/goldlog.js | 44 ---------------------- packages/ice-scripts/package.json | 2 +- 5 files changed, 7 insertions(+), 58 deletions(-) delete mode 100644 packages/ice-scripts/lib/utils/goldlog.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f6ebe1..ded2aec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.1.16 + +- [feat] support process.env.DISABLE_COLLECT to disable pv collect + ## 2.1.15 - [feat] cli option --skip-compile for skip webpack compile diff --git a/packages/ice-scripts/lib/commands/build.js b/packages/ice-scripts/lib/commands/build.js index bb0c5ec..255b39e 100644 --- a/packages/ice-scripts/lib/commands/build.js +++ b/packages/ice-scripts/lib/commands/build.js @@ -3,7 +3,6 @@ const webpack = require('webpack'); const { collectDetail } = require('@alifd/fusion-collector'); const iceScriptsPkgData = require('../../package.json'); -const goldlog = require('../utils/goldlog'); const log = require('../utils/log'); const checkDepsInstalled = require('../utils/checkDepsInstalled'); @@ -16,10 +15,6 @@ const checkDepsInstalled = require('../utils/checkDepsInstalled'); */ module.exports = async function (context) { const { applyHook, commandArgs, rootDir, webpackConfig, pkg } = context; - goldlog('version', { - version: iceScriptsPkgData.version, - }); - goldlog('build', commandArgs); log.verbose('build cliOptions', commandArgs); await applyHook('beforeBuild'); @@ -29,7 +24,7 @@ module.exports = async function (context) { return Promise.reject(new Error('项目依赖未安装,请先安装依赖。')); } - if (!pkg.componentConfig && !pkg.blockConfig) { + if (!pkg.componentConfig && !pkg.blockConfig && !process.env.DISABLE_COLLECT) { // only collect project try { collectDetail({ diff --git a/packages/ice-scripts/lib/commands/dev.js b/packages/ice-scripts/lib/commands/dev.js index 4d509a2..583e28b 100644 --- a/packages/ice-scripts/lib/commands/dev.js +++ b/packages/ice-scripts/lib/commands/dev.js @@ -14,18 +14,12 @@ const WebpackDevServer = require('webpack-dev-server'); const openBrowser = require('react-dev-utils/openBrowser'); const iceworksClient = require('../utils/iceworksClient'); const prepareUrLs = require('../utils/prepareURLs'); -const goldlog = require('../utils/goldlog'); const pkgData = require('../../package.json'); const log = require('../utils/log'); const checkDepsInstalled = require('../utils/checkDepsInstalled'); module.exports = async function(context, subprocess) { const { applyHook, commandArgs, rootDir, webpackConfig, pkg } = context; - - goldlog('version', { - version: pkgData.version, - }); - goldlog('dev', commandArgs); log.verbose('dev cliOptions', commandArgs); await applyHook('beforeDev'); @@ -43,7 +37,7 @@ module.exports = async function(context, subprocess) { return Promise.reject(new Error('项目依赖未安装,请先安装依赖。')); } - if (!pkg.componentConfig && !pkg.blockConfig) { + if (!pkg.componentConfig && !pkg.blockConfig && !process.env.DISABLE_COLLECT) { // only collect project try { collectDetail({ diff --git a/packages/ice-scripts/lib/utils/goldlog.js b/packages/ice-scripts/lib/utils/goldlog.js deleted file mode 100644 index 4c90fa4..0000000 --- a/packages/ice-scripts/lib/utils/goldlog.js +++ /dev/null @@ -1,44 +0,0 @@ - -const axios = require('axios'); - -module.exports = goldlog; - -/** - * 发送日志埋点,记录到 aplus 平台 - * - * @param {String} action 类型 - * @param {Object} extraData 其他参数 - */ -function goldlog(action, extraData = {}) { - const realData = { - action: `ice-scripts-${action}`, - data: { - ...extraData, - // 这里可以加一些全局参数 - }, - }; - - const dataKeyArray = Object.keys(realData); - const gokey = dataKeyArray.reduce((finnalStr, currentKey, index) => { - const currentData = - typeof realData[currentKey] === 'string' - ? realData[currentKey] - : JSON.stringify(realData[currentKey]); - return `${finnalStr}${currentKey}=${currentData}${ - dataKeyArray.length - 1 === index ? '' : '&' - }`; - }, ''); - - axios({ - method: 'post', - url: 'http://gm.mmstat.com/iceteam.iceworks.log', - data: { - cache: Math.random(), - gmkey: 'CLK', - gokey: encodeURIComponent(gokey), - logtype: '2', - }, - }).then(() => { - }).catch(() => { - }); -} diff --git a/packages/ice-scripts/package.json b/packages/ice-scripts/package.json index 267e503..3042a3a 100644 --- a/packages/ice-scripts/package.json +++ b/packages/ice-scripts/package.json @@ -1,6 +1,6 @@ { "name": "ice-scripts", - "version": "2.1.15", + "version": "2.1.16", "description": "ICE SDK", "main": "index.js", "bin": {