Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/ice-plugin-component/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 33 additions & 1 deletion packages/ice-plugin-component/lib/compile/component/buildSrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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}`);
}

Expand All @@ -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;
}
};
1 change: 1 addition & 0 deletions packages/ice-plugin-component/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions packages/ice-plugin-component/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
}
}