@es-pkg/compile 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/cjs/index.d.ts +3 -0
- package/cjs/index.js +190 -0
- package/package.json +13 -9
- package/src/index.ts +184 -160
package/README.md
CHANGED
|
@@ -29,4 +29,4 @@
|
|
|
29
29
|
|
|
30
30
|
#### default
|
|
31
31
|
|
|
32
|
-
* default(done:`Undertaker.TaskCallback`): `void` | `EventEmitter`<`DefaultEventMap`\> | `internal.Stream` | `"child_process".ChildProcess` | `asyncDone.Observable`<`any`\>
|
|
32
|
+
* default(done:`Undertaker.TaskCallback`): `void` | `EventEmitter`<`DefaultEventMap`\> | `internal.Stream` | `PromiseLike`<`any`\> | `"child_process".ChildProcess` | `asyncDone.Observable`<`any`\>
|
package/cjs/index.d.ts
ADDED
package/cjs/index.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var gulp = require('@es-pkg/gulp');
|
|
6
|
+
var utils = require('@es-pkg/utils');
|
|
7
|
+
var config = require('@es-pkg/config');
|
|
8
|
+
var rollup = require('rollup');
|
|
9
|
+
var resolve = require('@rollup/plugin-node-resolve');
|
|
10
|
+
var commonjs = require('@rollup/plugin-commonjs');
|
|
11
|
+
var typescript = require('@rollup/plugin-typescript');
|
|
12
|
+
var json = require('@rollup/plugin-json');
|
|
13
|
+
var postcss = require('rollup-plugin-postcss');
|
|
14
|
+
var autoprefixer = require('autoprefixer');
|
|
15
|
+
var cssnano = require('cssnano');
|
|
16
|
+
var path = require('node:path');
|
|
17
|
+
var fs = require('node:fs');
|
|
18
|
+
|
|
19
|
+
const clean = () => {
|
|
20
|
+
utils.log(`清除 ${config.relativeToApp(config.config.es)} & ${config.relativeToApp(config.config.cjs)} 目录---开始`);
|
|
21
|
+
const promises = [
|
|
22
|
+
utils.remove(config.config.publishDir, true),
|
|
23
|
+
utils.remove(config.config.es, true),
|
|
24
|
+
utils.remove(config.config.cjs, true),
|
|
25
|
+
utils.remove(config.config.iife, true),
|
|
26
|
+
];
|
|
27
|
+
return Promise.all(promises).then(res => {
|
|
28
|
+
utils.log(`清除 ${config.relativeToApp(config.config.es)} & ${config.relativeToApp(config.config.cjs)} 目录---结束`);
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
function getPostcss(extract) {
|
|
32
|
+
return postcss({
|
|
33
|
+
// 处理 Less(需安装 less)
|
|
34
|
+
extensions: ['.less', '.scss', '.sass'], // 识别 less 扩展名
|
|
35
|
+
// 为不同类型的文件指定对应的编译器(关键修改)
|
|
36
|
+
use: {
|
|
37
|
+
"stylus": ['sass'],
|
|
38
|
+
'less': ['less'], //.less 文件用 less 编译
|
|
39
|
+
'sass': ['sass'] //.sass 文件用 sass 编译(缩进语法)
|
|
40
|
+
},
|
|
41
|
+
// 配置 PostCSS 插件(自动前缀、压缩)
|
|
42
|
+
plugins: [
|
|
43
|
+
autoprefixer({ overrideBrowserslist: config.config.css.browserslist }),
|
|
44
|
+
cssnano() // 生产环境压缩 CSS
|
|
45
|
+
],
|
|
46
|
+
// 输出配置:提取为单独的 CSS 文件(推荐)
|
|
47
|
+
extract, // 提取到 ${name}.min.css
|
|
48
|
+
// 可选:不提取,嵌入到 JS 中(通过 import 会生成 style 标签)
|
|
49
|
+
// extract: false
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const name = utils.getValidPkgName(config.pkg.name);
|
|
53
|
+
// 1. 配置输入选项
|
|
54
|
+
function getInputOptions(isIIFE, declarationDir) {
|
|
55
|
+
function isNodeModule(id) {
|
|
56
|
+
// 获取模块绝对路径
|
|
57
|
+
try {
|
|
58
|
+
const resolved = require.resolve(id, { paths: [process.cwd()] });
|
|
59
|
+
return resolved.includes('node_modules');
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// 无法 resolve 的,认为是本地模块
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return ({
|
|
67
|
+
input: config.shallowInputs,
|
|
68
|
+
external: id => {
|
|
69
|
+
// 排除本地相对路径和绝对路径,别名映射到本地也不会 external
|
|
70
|
+
if (id.startsWith('.') || path.isAbsolute(id))
|
|
71
|
+
return false;
|
|
72
|
+
// node_modules 下的模块才 external
|
|
73
|
+
return isNodeModule(id);
|
|
74
|
+
},
|
|
75
|
+
plugins: [
|
|
76
|
+
json(),
|
|
77
|
+
isIIFE && resolve(), // 解析 node_modules
|
|
78
|
+
commonjs(), // 转换 CommonJS 模块
|
|
79
|
+
typescript({
|
|
80
|
+
tsconfig: config.resolveApp('tsconfig.json'), // 可选,指定 tsconfig
|
|
81
|
+
compilerOptions: {
|
|
82
|
+
noImplicitAny: true,
|
|
83
|
+
isolatedModules: false,
|
|
84
|
+
declaration: !!declarationDir,
|
|
85
|
+
allowImportingTsExtensions: false,
|
|
86
|
+
declarationDir,
|
|
87
|
+
noEmit: false,
|
|
88
|
+
emitDeclarationOnly: !!declarationDir,
|
|
89
|
+
esModuleInterop: true,
|
|
90
|
+
resolveJsonModule: true,
|
|
91
|
+
skipLibCheck: true,
|
|
92
|
+
removeComments: false,
|
|
93
|
+
rootDir: config.resolveApp('src'), // ✅ 指定源代码根目录
|
|
94
|
+
}
|
|
95
|
+
}),
|
|
96
|
+
getPostcss(config.config.css.extract)
|
|
97
|
+
].filter(Boolean)
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
async function buildExtraCss() {
|
|
101
|
+
const extras = config.config.css.extra;
|
|
102
|
+
if (!extras?.length)
|
|
103
|
+
return;
|
|
104
|
+
const srcRoot = config.resolveApp('src');
|
|
105
|
+
const esRoot = config.resolveApp(config.config.es);
|
|
106
|
+
const tasks = extras.map(async (v) => {
|
|
107
|
+
try {
|
|
108
|
+
const absPath = config.resolveApp(v); // 源文件绝对路径
|
|
109
|
+
const relativePath = path.relative(srcRoot, absPath); // 相对于 src 的路径
|
|
110
|
+
const dirname = path.dirname(relativePath); // 相对目录
|
|
111
|
+
const filename = path.basename(v, path.extname(v)); // 文件名去掉扩展名
|
|
112
|
+
// rollup 打包
|
|
113
|
+
const bundle = await rollup.rollup({
|
|
114
|
+
input: [v],
|
|
115
|
+
plugins: getPostcss(path.join(dirname, `${filename}.min.css`))
|
|
116
|
+
});
|
|
117
|
+
await bundle.write({
|
|
118
|
+
dir: config.config.es,
|
|
119
|
+
format: 'es',
|
|
120
|
+
sourcemap: false,
|
|
121
|
+
preserveModules: true,
|
|
122
|
+
preserveModulesRoot: srcRoot
|
|
123
|
+
});
|
|
124
|
+
// 删除生成的 JS 文件
|
|
125
|
+
const jsFile = path.join(esRoot, dirname, `${filename}${path.extname(v)}.js`);
|
|
126
|
+
if (fs.existsSync(jsFile))
|
|
127
|
+
fs.unlinkSync(jsFile);
|
|
128
|
+
// 复制到其他目标目录
|
|
129
|
+
[config.config.cjs, config.config.iife].forEach(targetRoot => {
|
|
130
|
+
const dest = path.join(config.resolveApp(targetRoot), dirname, `${filename}.min.css`);
|
|
131
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
132
|
+
fs.copyFileSync(path.join(esRoot, dirname, `${filename}.min.css`), dest);
|
|
133
|
+
});
|
|
134
|
+
utils.log.success(`✅ 编译完成: ${v}`);
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
utils.log.error(`❌ 编译失败: ${v}`, err);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
await Promise.all(tasks);
|
|
141
|
+
utils.log.success('✅ 所有额外 CSS 编译完成');
|
|
142
|
+
}
|
|
143
|
+
async function build() {
|
|
144
|
+
// 2. 配置输出选项
|
|
145
|
+
let outputOptions = [
|
|
146
|
+
{
|
|
147
|
+
dir: config.config.es,
|
|
148
|
+
format: 'es', // 输出 ES Module
|
|
149
|
+
sourcemap: false,
|
|
150
|
+
preserveModules: true,
|
|
151
|
+
preserveModulesRoot: config.resolveApp('src'), // ✅ 指定源代码根目录
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
dir: config.config.cjs,
|
|
155
|
+
format: 'cjs', // 输出 COMMONJS
|
|
156
|
+
sourcemap: false,
|
|
157
|
+
preserveModules: true,
|
|
158
|
+
preserveModulesRoot: config.resolveApp('src'), // ✅ 指定源代码根目录
|
|
159
|
+
exports: "named",
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
dir: config.config.iife,
|
|
163
|
+
format: 'iife', // 输出 iife
|
|
164
|
+
sourcemap: false,
|
|
165
|
+
exports: "named",
|
|
166
|
+
name: utils.toPascalCase(name)
|
|
167
|
+
}
|
|
168
|
+
];
|
|
169
|
+
if (!config.config.iife) {
|
|
170
|
+
outputOptions = outputOptions.filter(op => op.format !== 'iife');
|
|
171
|
+
}
|
|
172
|
+
for (const output of outputOptions) {
|
|
173
|
+
// 3. 调用 rollup 打包
|
|
174
|
+
const bundle = await rollup.rollup(getInputOptions(output.format === 'iife'));
|
|
175
|
+
// 4. 写入文件
|
|
176
|
+
await bundle.write(output);
|
|
177
|
+
}
|
|
178
|
+
{
|
|
179
|
+
const bundle = await rollup.rollup(getInputOptions(false, config.config.es));
|
|
180
|
+
await bundle.write(outputOptions[0]);
|
|
181
|
+
}
|
|
182
|
+
await buildExtraCss();
|
|
183
|
+
utils.log.success('✅ Build complete!');
|
|
184
|
+
}
|
|
185
|
+
const copyTds = () => {
|
|
186
|
+
return gulp.src([`${config.config.es}/**/*.d.ts`]).pipe(gulp.dest(config.config.cjs));
|
|
187
|
+
};
|
|
188
|
+
var index = gulp.series(clean, build, copyTds);
|
|
189
|
+
|
|
190
|
+
exports.default = index;
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@es-pkg/compile",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "组件打包工具",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "cjs/index.js",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"package",
|
|
8
8
|
"es-pkg"
|
|
@@ -21,12 +21,14 @@
|
|
|
21
21
|
"author": "pan",
|
|
22
22
|
"license": "ISC",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
24
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
25
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
26
|
+
"rollup": "^4.52.5",
|
|
27
|
+
"rollup-plugin-postcss": "^4.0.2",
|
|
28
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
29
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
30
|
+
"autoprefixer": "^10.4.21",
|
|
31
|
+
"cssnano": "latest",
|
|
30
32
|
"sass": "^1.53.0",
|
|
31
33
|
"@es-pkg/gulp": "latest",
|
|
32
34
|
"@es-pkg/gulp-logger": "latest",
|
|
@@ -37,8 +39,10 @@
|
|
|
37
39
|
"access": "public",
|
|
38
40
|
"registry": "https://registry.npmjs.org"
|
|
39
41
|
},
|
|
42
|
+
"module": "src/index.ts",
|
|
40
43
|
"types": "src/index.ts",
|
|
41
44
|
"files": [
|
|
42
|
-
"src"
|
|
45
|
+
"src",
|
|
46
|
+
"cjs"
|
|
43
47
|
]
|
|
44
48
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,29 +1,17 @@
|
|
|
1
|
-
import gulp, {series
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
|
|
15
|
-
const sass = gulpSass(Sass)
|
|
16
|
-
const include = getIncludeFiles()
|
|
17
|
-
|
|
18
|
-
function getMatchFiles(callback: (path: string) => (string[]) | string, contains: boolean = true) {
|
|
19
|
-
return include.flatMap(item => {
|
|
20
|
-
if (contains && !item.isDirectory) {
|
|
21
|
-
return [item.path]
|
|
22
|
-
}
|
|
23
|
-
const res = callback(item.path);
|
|
24
|
-
return Array.isArray(res) ? res : [res]
|
|
25
|
-
})
|
|
26
|
-
}
|
|
1
|
+
import gulp, {series} from "@es-pkg/gulp";
|
|
2
|
+
import {remove, log, getValidPkgName, toPascalCase} from "@es-pkg/utils";
|
|
3
|
+
import {config, shallowInputs, pkg, relativeToApp, resolveApp} from "@es-pkg/config";
|
|
4
|
+
import {OutputOptions, rollup, RollupOptions} from 'rollup';
|
|
5
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
6
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
7
|
+
import typescript from '@rollup/plugin-typescript';
|
|
8
|
+
import json from '@rollup/plugin-json';
|
|
9
|
+
import postcss from 'rollup-plugin-postcss';
|
|
10
|
+
import autoprefixer from 'autoprefixer';
|
|
11
|
+
import cssnano from 'cssnano';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import fs from 'node:fs'
|
|
14
|
+
import {builtinModules} from 'node:module';
|
|
27
15
|
|
|
28
16
|
const clean = () => {
|
|
29
17
|
log(`清除 ${relativeToApp(config.es)} & ${relativeToApp(config.cjs)} 目录---开始`);
|
|
@@ -38,149 +26,185 @@ const clean = () => {
|
|
|
38
26
|
})
|
|
39
27
|
}
|
|
40
28
|
|
|
29
|
+
function getPostcss(extract?: string | boolean) {
|
|
30
|
+
return postcss({
|
|
31
|
+
// 处理 Less(需安装 less)
|
|
32
|
+
extensions: ['.less', '.scss', '.sass'], // 识别 less 扩展名
|
|
33
|
+
// 为不同类型的文件指定对应的编译器(关键修改)
|
|
34
|
+
use: {
|
|
35
|
+
"stylus": ['sass'],
|
|
36
|
+
'less': ['less'], //.less 文件用 less 编译
|
|
37
|
+
'sass': ['sass'] //.sass 文件用 sass 编译(缩进语法)
|
|
38
|
+
},
|
|
41
39
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
before: `copyScss...`,
|
|
48
|
-
after: 'copyScss complete!',
|
|
49
|
-
extname: '.scss',
|
|
50
|
-
showChange: true
|
|
51
|
-
}))
|
|
52
|
-
.pipe(gulp.dest(dest))
|
|
53
|
-
}
|
|
54
|
-
const compileScss = () => {
|
|
55
|
-
return gulp.src(
|
|
56
|
-
getMatchFiles((path) => `${path}/**/*.scss`, false)
|
|
57
|
-
)
|
|
58
|
-
.pipe(sass({outputStyle: 'compressed', outFile: 'xx'}).on('error', sass.logError))
|
|
59
|
-
.pipe(autoPreFixer())
|
|
60
|
-
.pipe(rename((path) => {
|
|
61
|
-
path.extname = ".min.css"
|
|
62
|
-
}))
|
|
63
|
-
.pipe(plumber())
|
|
64
|
-
.pipe(gulp.dest(config.cjs));
|
|
65
|
-
}
|
|
66
|
-
return parallel(copy(config.es), copy(config.cjs), compileScss)
|
|
67
|
-
}
|
|
40
|
+
// 配置 PostCSS 插件(自动前缀、压缩)
|
|
41
|
+
plugins: [
|
|
42
|
+
autoprefixer({overrideBrowserslist: config.css.browserslist}),
|
|
43
|
+
cssnano() // 生产环境压缩 CSS
|
|
44
|
+
],
|
|
68
45
|
|
|
46
|
+
// 输出配置:提取为单独的 CSS 文件(推荐)
|
|
47
|
+
extract, // 提取到 ${name}.min.css
|
|
69
48
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
)
|
|
74
|
-
.pipe(logger({
|
|
75
|
-
before: 'copyScssToLib...',
|
|
76
|
-
after: 'copyScssToLib complete!',
|
|
77
|
-
extname: '.scss',
|
|
78
|
-
showChange: true
|
|
79
|
-
}))
|
|
80
|
-
.pipe(gulp.dest(config.cjs));
|
|
49
|
+
// 可选:不提取,嵌入到 JS 中(通过 import 会生成 style 标签)
|
|
50
|
+
// extract: false
|
|
51
|
+
})
|
|
81
52
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
"jsx": "react",
|
|
97
|
-
"noImplicitAny": true,
|
|
98
|
-
...tsConfig.config.compilerOptions,
|
|
99
|
-
"module": "esnext",
|
|
100
|
-
"target": "esnext",
|
|
101
|
-
"newLine": 'crlf',
|
|
102
|
-
"allowImportingTsExtensions": false,
|
|
103
|
-
"baseUrl": "./",
|
|
104
|
-
"isolatedModules": false,
|
|
105
|
-
"removeComments": false,
|
|
106
|
-
"declaration": true,
|
|
107
|
-
"noEmit": false,
|
|
108
|
-
"esModuleInterop": true,
|
|
109
|
-
"resolveJsonModule": true,
|
|
110
|
-
"skipLibCheck": true,
|
|
111
|
-
"moduleResolution": 'node',
|
|
112
|
-
}))
|
|
113
|
-
.on('error', (e) => {
|
|
114
|
-
console.error(e)
|
|
115
|
-
throw e;
|
|
116
|
-
})
|
|
117
|
-
.pipe(logger({
|
|
118
|
-
before: 'writing to es...',
|
|
119
|
-
after: 'write complete!',
|
|
120
|
-
extname: '.ts',
|
|
121
|
-
showChange: true,
|
|
122
|
-
display: 'name'
|
|
123
|
-
}))
|
|
124
|
-
.pipe(plumber())
|
|
125
|
-
.pipe(gulp.dest(target))
|
|
126
|
-
.pipe(gulp.src(config.include.map(item => `${item}/**/*.json`)))
|
|
127
|
-
.pipe(gulp.dest(target));
|
|
53
|
+
|
|
54
|
+
const name = getValidPkgName(pkg.name);
|
|
55
|
+
|
|
56
|
+
// 1. 配置输入选项
|
|
57
|
+
function getInputOptions(isIIFE?: boolean, declarationDir?: string): RollupOptions {
|
|
58
|
+
function isNodeModule(id: string) {
|
|
59
|
+
// 获取模块绝对路径
|
|
60
|
+
try {
|
|
61
|
+
const resolved = require.resolve(id, {paths: [process.cwd()]});
|
|
62
|
+
return resolved.includes('node_modules');
|
|
63
|
+
} catch {
|
|
64
|
+
// 无法 resolve 的,认为是本地模块
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
128
67
|
}
|
|
129
68
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
69
|
+
return ({
|
|
70
|
+
input: shallowInputs.filter(item => !item.endsWith('.d.ts')),
|
|
71
|
+
external: id => {
|
|
72
|
+
// Node 内置模块或者 npm 包都 external
|
|
73
|
+
if (builtinModules.includes(id)) return true;
|
|
74
|
+
// 排除本地相对路径和绝对路径,别名映射到本地也不会 external
|
|
75
|
+
if (id.startsWith('.') || path.isAbsolute(id)) return false;
|
|
76
|
+
// node_modules 下的模块才 external
|
|
77
|
+
return isNodeModule(id);
|
|
78
|
+
},
|
|
79
|
+
plugins: [
|
|
80
|
+
json(),
|
|
81
|
+
isIIFE && resolve(), // 解析 node_modules
|
|
82
|
+
commonjs(), // 转换 CommonJS 模块
|
|
83
|
+
typescript({
|
|
84
|
+
tsconfig: resolveApp('tsconfig.json'), // 可选,指定 tsconfig
|
|
85
|
+
compilerOptions: {
|
|
86
|
+
noImplicitAny: true,
|
|
87
|
+
isolatedModules: false,
|
|
88
|
+
declaration: !!declarationDir,
|
|
89
|
+
allowImportingTsExtensions: false,
|
|
90
|
+
declarationDir,
|
|
91
|
+
noEmit: false,
|
|
92
|
+
emitDeclarationOnly: !!declarationDir,
|
|
93
|
+
esModuleInterop: true,
|
|
94
|
+
resolveJsonModule: true,
|
|
95
|
+
skipLibCheck: true,
|
|
96
|
+
removeComments: false,
|
|
97
|
+
rootDir: resolveApp('src'), // ✅ 指定源代码根目录
|
|
98
|
+
}
|
|
99
|
+
}),
|
|
100
|
+
getPostcss(config.css.extract)
|
|
101
|
+
].filter(Boolean)
|
|
141
102
|
})
|
|
142
|
-
return series(tasks)
|
|
143
103
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
104
|
+
|
|
105
|
+
async function buildExtraCss() {
|
|
106
|
+
const extras = config.css.extra;
|
|
107
|
+
if (!extras?.length) return;
|
|
108
|
+
|
|
109
|
+
const srcRoot = resolveApp('src');
|
|
110
|
+
const esRoot = resolveApp(config.es);
|
|
111
|
+
|
|
112
|
+
const tasks = extras.map(async (v) => {
|
|
113
|
+
try {
|
|
114
|
+
const absPath = resolveApp(v); // 源文件绝对路径
|
|
115
|
+
const relativePath = path.relative(srcRoot, absPath); // 相对于 src 的路径
|
|
116
|
+
const dirname = path.dirname(relativePath); // 相对目录
|
|
117
|
+
const filename = path.basename(v, path.extname(v)); // 文件名去掉扩展名
|
|
118
|
+
|
|
119
|
+
// rollup 打包
|
|
120
|
+
const bundle = await rollup({
|
|
121
|
+
input: [v],
|
|
122
|
+
plugins: getPostcss(path.join(dirname, `${filename}.min.css`))
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
await bundle.write({
|
|
126
|
+
dir: config.es,
|
|
127
|
+
format: 'es',
|
|
128
|
+
sourcemap: false,
|
|
129
|
+
preserveModules: true,
|
|
130
|
+
preserveModulesRoot: srcRoot
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// 删除生成的 JS 文件
|
|
134
|
+
const jsFile = path.join(esRoot, dirname, `${filename}${path.extname(v)}.js`);
|
|
135
|
+
if (fs.existsSync(jsFile)) fs.unlinkSync(jsFile);
|
|
136
|
+
|
|
137
|
+
// 复制到其他目标目录
|
|
138
|
+
[config.cjs, config.iife].forEach(targetRoot => {
|
|
139
|
+
const dest = path.join(resolveApp(targetRoot), dirname, `${filename}.min.css`);
|
|
140
|
+
fs.mkdirSync(path.dirname(dest), {recursive: true});
|
|
141
|
+
fs.copyFileSync(
|
|
142
|
+
path.join(esRoot, dirname, `${filename}.min.css`),
|
|
143
|
+
dest
|
|
144
|
+
);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
log.success(`✅ 编译完成: ${v}`);
|
|
148
|
+
} catch (err) {
|
|
149
|
+
log.error(`❌ 编译失败: ${v}`, err);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
await Promise.all(tasks);
|
|
154
|
+
log.success('✅ 所有额外 CSS 编译完成');
|
|
148
155
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
156
|
+
|
|
157
|
+
async function build() {
|
|
158
|
+
|
|
159
|
+
// 2. 配置输出选项
|
|
160
|
+
let outputOptions: OutputOptions[] = [
|
|
161
|
+
{
|
|
162
|
+
dir: config.es,
|
|
163
|
+
format: 'es', // 输出 ES Module
|
|
164
|
+
sourcemap: false,
|
|
165
|
+
preserveModules: true,
|
|
166
|
+
preserveModulesRoot: resolveApp('src'), // ✅ 指定源代码根目录
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
dir: config.cjs,
|
|
170
|
+
format: 'cjs', // 输出 COMMONJS
|
|
171
|
+
sourcemap: false,
|
|
172
|
+
preserveModules: true,
|
|
173
|
+
preserveModulesRoot: resolveApp('src'), // ✅ 指定源代码根目录
|
|
174
|
+
exports: "named",
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
dir: config.iife,
|
|
178
|
+
format: 'iife', // 输出 iife
|
|
179
|
+
sourcemap: false,
|
|
180
|
+
exports: "named",
|
|
181
|
+
name: toPascalCase(name)
|
|
182
|
+
}
|
|
183
|
+
];
|
|
184
|
+
if (!config.iife) {
|
|
185
|
+
outputOptions = outputOptions.filter(op => op.format !== 'iife')
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
for (const output of outputOptions) {
|
|
189
|
+
// 3. 调用 rollup 打包
|
|
190
|
+
const bundle = await rollup(getInputOptions(output.format === 'iife'));
|
|
191
|
+
// 4. 写入文件
|
|
192
|
+
await bundle.write(output);
|
|
193
|
+
}
|
|
194
|
+
{
|
|
195
|
+
const bundle = await rollup(getInputOptions(false, config.es));
|
|
196
|
+
await bundle.write(outputOptions[0]);
|
|
197
|
+
}
|
|
198
|
+
await buildExtraCss();
|
|
199
|
+
log.success('✅ Build complete!');
|
|
182
200
|
}
|
|
183
201
|
|
|
184
|
-
const compileEsAndLib = series(compileEs(), copyTds, compileLib)
|
|
185
202
|
|
|
186
|
-
|
|
203
|
+
const copySrcTds = () => {
|
|
204
|
+
return gulp.src(config.include.map(t=>`${t}/**/*.d.ts`)).pipe(gulp.dest(config.es));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const copyTds = () => {
|
|
208
|
+
return gulp.src([`${config.es}/**/*.d.ts`]).pipe(gulp.dest(config.cjs));
|
|
209
|
+
}
|
|
210
|
+
export default series(clean, build, copySrcTds, copyTds)
|