@es-pkg/compile 1.0.4 → 1.0.6
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/cjs/index.js +216 -174
- package/esm/index.d.ts +3 -0
- package/{src/index.ts → esm/index.js} +201 -210
- package/package.json +5 -4
package/cjs/index.js
CHANGED
|
@@ -1,190 +1,232 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name2 in all)
|
|
11
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
!mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var src_exports = {};
|
|
31
|
+
__export(src_exports, {
|
|
32
|
+
default: () => src_default
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(src_exports);
|
|
35
|
+
var import_gulp = __toESM(require("@es-pkg/gulp"));
|
|
36
|
+
var import_utils = require("@es-pkg/utils");
|
|
37
|
+
var import_config = require("@es-pkg/config");
|
|
38
|
+
var import_rollup = require("rollup");
|
|
39
|
+
var import_plugin_node_resolve = __toESM(require("@rollup/plugin-node-resolve"));
|
|
40
|
+
var import_plugin_commonjs = __toESM(require("@rollup/plugin-commonjs"));
|
|
41
|
+
var import_plugin_typescript = __toESM(require("@rollup/plugin-typescript"));
|
|
42
|
+
var import_plugin_json = __toESM(require("@rollup/plugin-json"));
|
|
43
|
+
var import_rollup_plugin_postcss = __toESM(require("rollup-plugin-postcss"));
|
|
44
|
+
var import_autoprefixer = __toESM(require("autoprefixer"));
|
|
45
|
+
var import_cssnano = __toESM(require("cssnano"));
|
|
46
|
+
var import_node_path = __toESM(require("node:path"));
|
|
47
|
+
var import_node_fs = __toESM(require("node:fs"));
|
|
48
|
+
var import_rollup_plugin_esbuild = __toESM(require("rollup-plugin-esbuild"));
|
|
49
|
+
var import_node_module = require("node:module");
|
|
19
50
|
const clean = () => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
51
|
+
(0, import_utils.log)(`\u6E05\u9664 ${(0, import_config.relativeToApp)(import_config.config.es)} & ${(0, import_config.relativeToApp)(import_config.config.cjs)} \u76EE\u5F55---\u5F00\u59CB`);
|
|
52
|
+
const promises = [
|
|
53
|
+
(0, import_utils.remove)(import_config.config.publishDir, true),
|
|
54
|
+
(0, import_utils.remove)(import_config.config.es, true),
|
|
55
|
+
(0, import_utils.remove)(import_config.config.cjs, true),
|
|
56
|
+
(0, import_utils.remove)(import_config.config.iife, true)
|
|
57
|
+
];
|
|
58
|
+
return Promise.all(promises).then((res) => {
|
|
59
|
+
(0, import_utils.log)(`\u6E05\u9664 ${(0, import_config.relativeToApp)(import_config.config.es)} & ${(0, import_config.relativeToApp)(import_config.config.cjs)} \u76EE\u5F55---\u7ED3\u675F`);
|
|
60
|
+
});
|
|
30
61
|
};
|
|
31
62
|
function getPostcss(extract) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
63
|
+
return (0, import_rollup_plugin_postcss.default)({
|
|
64
|
+
// 处理 Less(需安装 less)
|
|
65
|
+
extensions: [".less", ".scss", ".sass"],
|
|
66
|
+
// 识别 less 扩展名
|
|
67
|
+
// 为不同类型的文件指定对应的编译器(关键修改)
|
|
68
|
+
use: {
|
|
69
|
+
"stylus": ["sass"],
|
|
70
|
+
"less": ["less"],
|
|
71
|
+
//.less 文件用 less 编译
|
|
72
|
+
"sass": ["sass"]
|
|
73
|
+
//.sass 文件用 sass 编译(缩进语法)
|
|
74
|
+
},
|
|
75
|
+
// 配置 PostCSS 插件(自动前缀、压缩)
|
|
76
|
+
plugins: [
|
|
77
|
+
(0, import_autoprefixer.default)({ overrideBrowserslist: import_config.config.css.browserslist }),
|
|
78
|
+
(0, import_cssnano.default)()
|
|
79
|
+
// 生产环境压缩 CSS
|
|
80
|
+
],
|
|
81
|
+
// 输出配置:提取为单独的 CSS 文件(推荐)
|
|
82
|
+
extract
|
|
83
|
+
// 提取到 ${name}.min.css
|
|
84
|
+
// 可选:不提取,嵌入到 JS 中(通过 import 会生成 style 标签)
|
|
85
|
+
// extract: false
|
|
86
|
+
});
|
|
51
87
|
}
|
|
52
|
-
const name =
|
|
53
|
-
|
|
54
|
-
function
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
catch {
|
|
62
|
-
// 无法 resolve 的,认为是本地模块
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
88
|
+
const name = (0, import_utils.getValidPkgName)(import_config.pkg.name);
|
|
89
|
+
function getInputOptions(format, declarationDir) {
|
|
90
|
+
function isNodeModule(id) {
|
|
91
|
+
try {
|
|
92
|
+
const resolved = require.resolve(id, { paths: [process.cwd()] });
|
|
93
|
+
return resolved.includes("node_modules");
|
|
94
|
+
} catch {
|
|
95
|
+
return false;
|
|
65
96
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
input: import_config.shallowInputs.filter((item) => !item.endsWith(".d.ts")),
|
|
100
|
+
external: (id) => {
|
|
101
|
+
if (import_node_module.builtinModules.includes(id))
|
|
102
|
+
return true;
|
|
103
|
+
if (id.startsWith(".") || import_node_path.default.isAbsolute(id))
|
|
104
|
+
return false;
|
|
105
|
+
return isNodeModule(id);
|
|
106
|
+
},
|
|
107
|
+
plugins: [
|
|
108
|
+
(0, import_plugin_json.default)(),
|
|
109
|
+
format === "iife" && (0, import_plugin_node_resolve.default)(),
|
|
110
|
+
// 解析 node_modules
|
|
111
|
+
format === "iife" && (0, import_plugin_commonjs.default)({ defaultIsModuleExports: true }),
|
|
112
|
+
// 转换 CommonJS 模块
|
|
113
|
+
(0, import_plugin_typescript.default)({
|
|
114
|
+
tsconfig: (0, import_config.resolveApp)("tsconfig.json"),
|
|
115
|
+
// 可选,指定 tsconfig
|
|
116
|
+
compilerOptions: {
|
|
117
|
+
module: "esnext",
|
|
118
|
+
importHelpers: false,
|
|
119
|
+
esModuleInterop: true,
|
|
120
|
+
allowSyntheticDefaultImports: true,
|
|
121
|
+
noImplicitAny: true,
|
|
122
|
+
isolatedModules: false,
|
|
123
|
+
declaration: !!declarationDir,
|
|
124
|
+
allowImportingTsExtensions: false,
|
|
125
|
+
declarationDir,
|
|
126
|
+
noEmit: false,
|
|
127
|
+
emitDeclarationOnly: !!declarationDir,
|
|
128
|
+
resolveJsonModule: true,
|
|
129
|
+
skipLibCheck: true,
|
|
130
|
+
removeComments: false,
|
|
131
|
+
rootDir: (0, import_config.resolveApp)("src")
|
|
132
|
+
// ✅ 指定源代码根目录
|
|
133
|
+
}
|
|
134
|
+
}),
|
|
135
|
+
["cjs", "commonjs"].includes(format) && (0, import_rollup_plugin_esbuild.default)({
|
|
136
|
+
target: "esnext",
|
|
137
|
+
format: "cjs"
|
|
138
|
+
}),
|
|
139
|
+
getPostcss(import_config.config.css.extract)
|
|
140
|
+
].filter(Boolean)
|
|
141
|
+
};
|
|
99
142
|
}
|
|
100
143
|
async function buildExtraCss() {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
await Promise.all(tasks);
|
|
141
|
-
utils.log.success('✅ 所有额外 CSS 编译完成');
|
|
144
|
+
const extras = import_config.config.css.extra;
|
|
145
|
+
if (!extras?.length)
|
|
146
|
+
return;
|
|
147
|
+
const srcRoot = (0, import_config.resolveApp)("src");
|
|
148
|
+
const esRoot = (0, import_config.resolveApp)(import_config.config.es);
|
|
149
|
+
const tasks = extras.map(async (v) => {
|
|
150
|
+
try {
|
|
151
|
+
const absPath = (0, import_config.resolveApp)(v);
|
|
152
|
+
const relativePath = import_node_path.default.relative(srcRoot, absPath);
|
|
153
|
+
const dirname = import_node_path.default.dirname(relativePath);
|
|
154
|
+
const filename = import_node_path.default.basename(v, import_node_path.default.extname(v));
|
|
155
|
+
const bundle = await (0, import_rollup.rollup)({
|
|
156
|
+
input: [v],
|
|
157
|
+
plugins: getPostcss(import_node_path.default.join(dirname, `${filename}.min.css`))
|
|
158
|
+
});
|
|
159
|
+
await bundle.write({
|
|
160
|
+
dir: import_config.config.es,
|
|
161
|
+
format: "es",
|
|
162
|
+
sourcemap: false,
|
|
163
|
+
preserveModules: true,
|
|
164
|
+
preserveModulesRoot: srcRoot
|
|
165
|
+
});
|
|
166
|
+
const jsFile = import_node_path.default.join(esRoot, dirname, `${filename}${import_node_path.default.extname(v)}.js`);
|
|
167
|
+
if (import_node_fs.default.existsSync(jsFile))
|
|
168
|
+
import_node_fs.default.unlinkSync(jsFile);
|
|
169
|
+
[import_config.config.cjs, import_config.config.iife].forEach((targetRoot) => {
|
|
170
|
+
const dest = import_node_path.default.join((0, import_config.resolveApp)(targetRoot), dirname, `${filename}.min.css`);
|
|
171
|
+
import_node_fs.default.mkdirSync(import_node_path.default.dirname(dest), { recursive: true });
|
|
172
|
+
import_node_fs.default.copyFileSync(import_node_path.default.join(esRoot, dirname, `${filename}.min.css`), dest);
|
|
173
|
+
});
|
|
174
|
+
import_utils.log.success(`\u2705 \u7F16\u8BD1\u5B8C\u6210: ${v}`);
|
|
175
|
+
} catch (err) {
|
|
176
|
+
import_utils.log.error(`\u274C \u7F16\u8BD1\u5931\u8D25: ${v}`, err);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
await Promise.all(tasks);
|
|
180
|
+
import_utils.log.success("\u2705 \u6240\u6709\u989D\u5916 CSS \u7F16\u8BD1\u5B8C\u6210");
|
|
142
181
|
}
|
|
143
182
|
async function build() {
|
|
144
|
-
|
|
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
|
-
}
|
|
183
|
+
let outputOptions = [
|
|
178
184
|
{
|
|
179
|
-
|
|
180
|
-
|
|
185
|
+
dir: import_config.config.es,
|
|
186
|
+
format: "es",
|
|
187
|
+
// 输出 ES Module
|
|
188
|
+
sourcemap: false,
|
|
189
|
+
preserveModules: true,
|
|
190
|
+
preserveModulesRoot: (0, import_config.resolveApp)("src")
|
|
191
|
+
// ✅ 指定源代码根目录
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
dir: import_config.config.cjs,
|
|
195
|
+
format: "cjs",
|
|
196
|
+
// 输出 COMMONJS
|
|
197
|
+
sourcemap: false,
|
|
198
|
+
preserveModules: true,
|
|
199
|
+
preserveModulesRoot: (0, import_config.resolveApp)("src"),
|
|
200
|
+
// ✅ 指定源代码根目录
|
|
201
|
+
exports: "named"
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
dir: import_config.config.iife,
|
|
205
|
+
format: "iife",
|
|
206
|
+
// 输出 iife
|
|
207
|
+
sourcemap: false,
|
|
208
|
+
exports: "named",
|
|
209
|
+
name: (0, import_utils.toPascalCase)(name)
|
|
181
210
|
}
|
|
182
|
-
|
|
183
|
-
|
|
211
|
+
];
|
|
212
|
+
if (!import_config.config.iife) {
|
|
213
|
+
outputOptions = outputOptions.filter((op) => op.format !== "iife");
|
|
214
|
+
}
|
|
215
|
+
for (const output of outputOptions) {
|
|
216
|
+
const bundle = await (0, import_rollup.rollup)(getInputOptions(output.format));
|
|
217
|
+
await bundle.write(output);
|
|
218
|
+
}
|
|
219
|
+
{
|
|
220
|
+
const bundle = await (0, import_rollup.rollup)(getInputOptions("es", import_config.config.es));
|
|
221
|
+
await bundle.write(outputOptions[0]);
|
|
222
|
+
}
|
|
223
|
+
await buildExtraCss();
|
|
224
|
+
import_utils.log.success("\u2705 Build complete!");
|
|
184
225
|
}
|
|
226
|
+
const copySrcTds = () => {
|
|
227
|
+
return import_gulp.default.src(import_config.config.include.map((t) => `${t}/**/*.d.ts`)).pipe(import_gulp.default.dest(import_config.config.es));
|
|
228
|
+
};
|
|
185
229
|
const copyTds = () => {
|
|
186
|
-
|
|
230
|
+
return import_gulp.default.src([`${import_config.config.es}/**/*.d.ts`]).pipe(import_gulp.default.dest(import_config.config.cjs));
|
|
187
231
|
};
|
|
188
|
-
var
|
|
189
|
-
|
|
190
|
-
exports.default = index;
|
|
232
|
+
var src_default = (0, import_gulp.series)(clean, build, copySrcTds, copyTds);
|
package/esm/index.d.ts
ADDED
|
@@ -1,210 +1,201 @@
|
|
|
1
|
-
import gulp, {series} from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
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
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
remove(config.
|
|
21
|
-
remove(config.
|
|
22
|
-
remove(config.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function getPostcss(extract
|
|
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']
|
|
38
|
-
},
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
resolveJsonModule: true,
|
|
95
|
-
skipLibCheck: true,
|
|
96
|
-
removeComments: false,
|
|
97
|
-
rootDir: resolveApp('src'), // ✅ 指定源代码根目录
|
|
98
|
-
}
|
|
99
|
-
}),
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
//
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
fs.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
{
|
|
162
|
-
dir: config.
|
|
163
|
-
format: '
|
|
164
|
-
sourcemap: false,
|
|
165
|
-
preserveModules: true,
|
|
166
|
-
preserveModulesRoot: resolveApp('src'), // ✅ 指定源代码根目录
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
{
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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)
|
|
1
|
+
import gulp, { series } from '@es-pkg/gulp';
|
|
2
|
+
import { getValidPkgName, log, remove, toPascalCase } from '@es-pkg/utils';
|
|
3
|
+
import { pkg, relativeToApp, config, resolveApp, shallowInputs } from '@es-pkg/config';
|
|
4
|
+
import { rollup } 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 esbuild from 'rollup-plugin-esbuild';
|
|
15
|
+
import { builtinModules } from 'node:module';
|
|
16
|
+
|
|
17
|
+
const clean = () => {
|
|
18
|
+
log(`清除 ${relativeToApp(config.es)} & ${relativeToApp(config.cjs)} 目录---开始`);
|
|
19
|
+
const promises = [
|
|
20
|
+
remove(config.publishDir, true),
|
|
21
|
+
remove(config.es, true),
|
|
22
|
+
remove(config.cjs, true),
|
|
23
|
+
remove(config.iife, true),
|
|
24
|
+
];
|
|
25
|
+
return Promise.all(promises).then(res => {
|
|
26
|
+
log(`清除 ${relativeToApp(config.es)} & ${relativeToApp(config.cjs)} 目录---结束`);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
function getPostcss(extract) {
|
|
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
|
+
},
|
|
39
|
+
// 配置 PostCSS 插件(自动前缀、压缩)
|
|
40
|
+
plugins: [
|
|
41
|
+
autoprefixer({ overrideBrowserslist: config.css.browserslist }),
|
|
42
|
+
cssnano() // 生产环境压缩 CSS
|
|
43
|
+
],
|
|
44
|
+
// 输出配置:提取为单独的 CSS 文件(推荐)
|
|
45
|
+
extract, // 提取到 ${name}.min.css
|
|
46
|
+
// 可选:不提取,嵌入到 JS 中(通过 import 会生成 style 标签)
|
|
47
|
+
// extract: false
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const name = getValidPkgName(pkg.name);
|
|
51
|
+
// 1. 配置输入选项
|
|
52
|
+
function getInputOptions(format, declarationDir) {
|
|
53
|
+
function isNodeModule(id) {
|
|
54
|
+
// 获取模块绝对路径
|
|
55
|
+
try {
|
|
56
|
+
const resolved = require.resolve(id, { paths: [process.cwd()] });
|
|
57
|
+
return resolved.includes('node_modules');
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
// 无法 resolve 的,认为是本地模块
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return ({
|
|
65
|
+
input: shallowInputs.filter(item => !item.endsWith('.d.ts')),
|
|
66
|
+
external: id => {
|
|
67
|
+
// Node 内置模块或者 npm 包都 external
|
|
68
|
+
if (builtinModules.includes(id))
|
|
69
|
+
return true;
|
|
70
|
+
// 排除本地相对路径和绝对路径,别名映射到本地也不会 external
|
|
71
|
+
if (id.startsWith('.') || path.isAbsolute(id))
|
|
72
|
+
return false;
|
|
73
|
+
// node_modules 下的模块才 external
|
|
74
|
+
return isNodeModule(id);
|
|
75
|
+
},
|
|
76
|
+
plugins: [
|
|
77
|
+
json(),
|
|
78
|
+
format === 'iife' && resolve(), // 解析 node_modules
|
|
79
|
+
format === 'iife' && commonjs({ defaultIsModuleExports: true }), // 转换 CommonJS 模块
|
|
80
|
+
typescript({
|
|
81
|
+
tsconfig: resolveApp('tsconfig.json'), // 可选,指定 tsconfig
|
|
82
|
+
compilerOptions: {
|
|
83
|
+
module: 'esnext',
|
|
84
|
+
importHelpers: false,
|
|
85
|
+
esModuleInterop: true,
|
|
86
|
+
allowSyntheticDefaultImports: true,
|
|
87
|
+
noImplicitAny: true,
|
|
88
|
+
isolatedModules: false,
|
|
89
|
+
declaration: !!declarationDir,
|
|
90
|
+
allowImportingTsExtensions: false,
|
|
91
|
+
declarationDir,
|
|
92
|
+
noEmit: false,
|
|
93
|
+
emitDeclarationOnly: !!declarationDir,
|
|
94
|
+
resolveJsonModule: true,
|
|
95
|
+
skipLibCheck: true,
|
|
96
|
+
removeComments: false,
|
|
97
|
+
rootDir: resolveApp('src'), // ✅ 指定源代码根目录
|
|
98
|
+
}
|
|
99
|
+
}),
|
|
100
|
+
["cjs", "commonjs"].includes(format) && esbuild({
|
|
101
|
+
target: 'esnext',
|
|
102
|
+
format: "cjs",
|
|
103
|
+
}),
|
|
104
|
+
getPostcss(config.css.extract)
|
|
105
|
+
].filter(Boolean)
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
async function buildExtraCss() {
|
|
109
|
+
const extras = config.css.extra;
|
|
110
|
+
if (!extras?.length)
|
|
111
|
+
return;
|
|
112
|
+
const srcRoot = resolveApp('src');
|
|
113
|
+
const esRoot = resolveApp(config.es);
|
|
114
|
+
const tasks = extras.map(async (v) => {
|
|
115
|
+
try {
|
|
116
|
+
const absPath = resolveApp(v); // 源文件绝对路径
|
|
117
|
+
const relativePath = path.relative(srcRoot, absPath); // 相对于 src 的路径
|
|
118
|
+
const dirname = path.dirname(relativePath); // 相对目录
|
|
119
|
+
const filename = path.basename(v, path.extname(v)); // 文件名去掉扩展名
|
|
120
|
+
// rollup 打包
|
|
121
|
+
const bundle = await rollup({
|
|
122
|
+
input: [v],
|
|
123
|
+
plugins: getPostcss(path.join(dirname, `${filename}.min.css`))
|
|
124
|
+
});
|
|
125
|
+
await bundle.write({
|
|
126
|
+
dir: config.es,
|
|
127
|
+
format: 'es',
|
|
128
|
+
sourcemap: false,
|
|
129
|
+
preserveModules: true,
|
|
130
|
+
preserveModulesRoot: srcRoot
|
|
131
|
+
});
|
|
132
|
+
// 删除生成的 JS 文件
|
|
133
|
+
const jsFile = path.join(esRoot, dirname, `${filename}${path.extname(v)}.js`);
|
|
134
|
+
if (fs.existsSync(jsFile))
|
|
135
|
+
fs.unlinkSync(jsFile);
|
|
136
|
+
// 复制到其他目标目录
|
|
137
|
+
[config.cjs, config.iife].forEach(targetRoot => {
|
|
138
|
+
const dest = path.join(resolveApp(targetRoot), dirname, `${filename}.min.css`);
|
|
139
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
140
|
+
fs.copyFileSync(path.join(esRoot, dirname, `${filename}.min.css`), dest);
|
|
141
|
+
});
|
|
142
|
+
log.success(`✅ 编译完成: ${v}`);
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
log.error(`❌ 编译失败: ${v}`, err);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
await Promise.all(tasks);
|
|
149
|
+
log.success('✅ 所有额外 CSS 编译完成');
|
|
150
|
+
}
|
|
151
|
+
async function build() {
|
|
152
|
+
// 2. 配置输出选项
|
|
153
|
+
let outputOptions = [
|
|
154
|
+
{
|
|
155
|
+
dir: config.es,
|
|
156
|
+
format: 'es', // 输出 ES Module
|
|
157
|
+
sourcemap: false,
|
|
158
|
+
preserveModules: true,
|
|
159
|
+
preserveModulesRoot: resolveApp('src'), // ✅ 指定源代码根目录
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
dir: config.cjs,
|
|
163
|
+
format: 'cjs', // 输出 COMMONJS
|
|
164
|
+
sourcemap: false,
|
|
165
|
+
preserveModules: true,
|
|
166
|
+
preserveModulesRoot: resolveApp('src'), // ✅ 指定源代码根目录
|
|
167
|
+
exports: "named",
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
dir: config.iife,
|
|
171
|
+
format: 'iife', // 输出 iife
|
|
172
|
+
sourcemap: false,
|
|
173
|
+
exports: "named",
|
|
174
|
+
name: toPascalCase(name)
|
|
175
|
+
}
|
|
176
|
+
];
|
|
177
|
+
if (!config.iife) {
|
|
178
|
+
outputOptions = outputOptions.filter(op => op.format !== 'iife');
|
|
179
|
+
}
|
|
180
|
+
for (const output of outputOptions) {
|
|
181
|
+
// 3. 调用 rollup 打包
|
|
182
|
+
const bundle = await rollup(getInputOptions(output.format));
|
|
183
|
+
// 4. 写入文件
|
|
184
|
+
await bundle.write(output);
|
|
185
|
+
}
|
|
186
|
+
{
|
|
187
|
+
const bundle = await rollup(getInputOptions('es', config.es));
|
|
188
|
+
await bundle.write(outputOptions[0]);
|
|
189
|
+
}
|
|
190
|
+
await buildExtraCss();
|
|
191
|
+
log.success('✅ Build complete!');
|
|
192
|
+
}
|
|
193
|
+
const copySrcTds = () => {
|
|
194
|
+
return gulp.src(config.include.map(t => `${t}/**/*.d.ts`)).pipe(gulp.dest(config.es));
|
|
195
|
+
};
|
|
196
|
+
const copyTds = () => {
|
|
197
|
+
return gulp.src([`${config.es}/**/*.d.ts`]).pipe(gulp.dest(config.cjs));
|
|
198
|
+
};
|
|
199
|
+
var index = series(clean, build, copySrcTds, copyTds);
|
|
200
|
+
|
|
201
|
+
export { index as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@es-pkg/compile",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "组件打包工具",
|
|
5
5
|
"main": "cjs/index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
26
26
|
"rollup": "^4.52.5",
|
|
27
27
|
"rollup-plugin-postcss": "^4.0.2",
|
|
28
|
+
"rollup-plugin-esbuild": "latest",
|
|
28
29
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
29
30
|
"@rollup/plugin-json": "^6.1.0",
|
|
30
31
|
"autoprefixer": "^10.4.21",
|
|
@@ -39,10 +40,10 @@
|
|
|
39
40
|
"access": "public",
|
|
40
41
|
"registry": "https://registry.npmjs.org"
|
|
41
42
|
},
|
|
42
|
-
"module": "
|
|
43
|
-
"types": "
|
|
43
|
+
"module": "esm/index.js",
|
|
44
|
+
"types": "esm/index.d.ts",
|
|
44
45
|
"files": [
|
|
45
|
-
"
|
|
46
|
+
"esm",
|
|
46
47
|
"cjs"
|
|
47
48
|
]
|
|
48
49
|
}
|