@es-pkg/compile 1.0.6 → 1.0.8
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 +150 -179
- package/esm/index.js +161 -177
- package/package.json +1 -1
package/cjs/index.js
CHANGED
|
@@ -1,232 +1,203 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
)
|
|
29
|
-
var
|
|
30
|
-
var
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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");
|
|
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 json = require('@rollup/plugin-json');
|
|
12
|
+
var postcss = require('rollup-plugin-postcss');
|
|
13
|
+
var autoprefixer = require('autoprefixer');
|
|
14
|
+
var cssnano = require('cssnano');
|
|
15
|
+
var path = require('node:path');
|
|
16
|
+
var fs = require('node:fs');
|
|
17
|
+
var esbuild = require('rollup-plugin-esbuild');
|
|
18
|
+
var node_module = require('node:module');
|
|
19
|
+
var ts = require('typescript');
|
|
20
|
+
|
|
21
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
22
|
+
|
|
23
|
+
var gulp__default = /*#__PURE__*/_interopDefault(gulp);
|
|
24
|
+
var resolve__default = /*#__PURE__*/_interopDefault(resolve);
|
|
25
|
+
var commonjs__default = /*#__PURE__*/_interopDefault(commonjs);
|
|
26
|
+
var json__default = /*#__PURE__*/_interopDefault(json);
|
|
27
|
+
var postcss__default = /*#__PURE__*/_interopDefault(postcss);
|
|
28
|
+
var autoprefixer__default = /*#__PURE__*/_interopDefault(autoprefixer);
|
|
29
|
+
var cssnano__default = /*#__PURE__*/_interopDefault(cssnano);
|
|
30
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
31
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
32
|
+
var esbuild__default = /*#__PURE__*/_interopDefault(esbuild);
|
|
33
|
+
var ts__default = /*#__PURE__*/_interopDefault(ts);
|
|
34
|
+
|
|
35
|
+
const name = utils.getValidPkgName(config.pkg.name);
|
|
50
36
|
const clean = () => {
|
|
51
|
-
|
|
37
|
+
utils.log(`\u6E05\u9664 ${config.relativeToApp(config.config.es)} & ${config.relativeToApp(config.config.cjs)} \u76EE\u5F55---\u5F00\u59CB`);
|
|
52
38
|
const promises = [
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
39
|
+
utils.remove(config.config.publishDir, true),
|
|
40
|
+
utils.remove(config.config.es, true),
|
|
41
|
+
utils.remove(config.config.cjs, true),
|
|
42
|
+
utils.remove(config.config.iife, true)
|
|
57
43
|
];
|
|
58
|
-
return Promise.all(promises).then((
|
|
59
|
-
|
|
44
|
+
return Promise.all(promises).then(() => {
|
|
45
|
+
utils.log(`\u6E05\u9664 ${config.relativeToApp(config.config.es)} & ${config.relativeToApp(config.config.cjs)} \u76EE\u5F55---\u7ED3\u675F`);
|
|
60
46
|
});
|
|
61
47
|
};
|
|
62
48
|
function getPostcss(extract) {
|
|
63
|
-
return
|
|
64
|
-
// 处理 Less(需安装 less)
|
|
49
|
+
return postcss__default.default({
|
|
65
50
|
extensions: [".less", ".scss", ".sass"],
|
|
66
|
-
// 识别 less 扩展名
|
|
67
|
-
// 为不同类型的文件指定对应的编译器(关键修改)
|
|
68
51
|
use: {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
"sass": ["sass"]
|
|
73
|
-
//.sass 文件用 sass 编译(缩进语法)
|
|
52
|
+
stylus: ["sass"],
|
|
53
|
+
less: ["less"],
|
|
54
|
+
sass: ["sass"]
|
|
74
55
|
},
|
|
75
|
-
|
|
76
|
-
plugins: [
|
|
77
|
-
(0, import_autoprefixer.default)({ overrideBrowserslist: import_config.config.css.browserslist }),
|
|
78
|
-
(0, import_cssnano.default)()
|
|
79
|
-
// 生产环境压缩 CSS
|
|
80
|
-
],
|
|
81
|
-
// 输出配置:提取为单独的 CSS 文件(推荐)
|
|
56
|
+
plugins: [autoprefixer__default.default({ overrideBrowserslist: config.config.css.browserslist }), cssnano__default.default()],
|
|
82
57
|
extract
|
|
83
|
-
// 提取到 ${name}.min.css
|
|
84
|
-
// 可选:不提取,嵌入到 JS 中(通过 import 会生成 style 标签)
|
|
85
|
-
// extract: false
|
|
86
58
|
});
|
|
87
59
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
} catch {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
60
|
+
function isNodeModule(id) {
|
|
61
|
+
try {
|
|
62
|
+
const resolved = require.resolve(id, { paths: [process.cwd()] });
|
|
63
|
+
return resolved.includes("node_modules");
|
|
64
|
+
} catch (e) {
|
|
65
|
+
return false;
|
|
97
66
|
}
|
|
67
|
+
}
|
|
68
|
+
function getInputOptions(format) {
|
|
98
69
|
return {
|
|
99
|
-
input:
|
|
70
|
+
input: config.shallowInputs.filter((item) => !item.endsWith(".d.ts")),
|
|
100
71
|
external: (id) => {
|
|
101
|
-
if (
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return false;
|
|
105
|
-
return isNodeModule(id);
|
|
72
|
+
if (node_module.builtinModules.includes(id)) return true;
|
|
73
|
+
if (!id.startsWith(".") && !path__default.default.isAbsolute(id) && isNodeModule(id)) return true;
|
|
74
|
+
return false;
|
|
106
75
|
},
|
|
107
76
|
plugins: [
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
//
|
|
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"
|
|
77
|
+
json__default.default(),
|
|
78
|
+
resolve__default.default(),
|
|
79
|
+
esbuild__default.default({ target: "es2018", format: "esm" }),
|
|
80
|
+
commonjs__default.default({
|
|
81
|
+
defaultIsModuleExports: true,
|
|
82
|
+
esmExternals: true,
|
|
83
|
+
transformMixedEsModules: true
|
|
84
|
+
// 混合模块也转换
|
|
138
85
|
}),
|
|
139
|
-
getPostcss(
|
|
140
|
-
]
|
|
86
|
+
getPostcss(config.config.css.extract)
|
|
87
|
+
]
|
|
141
88
|
};
|
|
142
89
|
}
|
|
90
|
+
async function buildDeclarations() {
|
|
91
|
+
const tsConfig = ts__default.default.readConfigFile(config.resolveApp("tsconfig.json"), ts__default.default.sys.readFile);
|
|
92
|
+
if (tsConfig.error) {
|
|
93
|
+
console.log(tsConfig.error.messageText);
|
|
94
|
+
}
|
|
95
|
+
const entryFiles = config.collectInputs.filter((item) => [".ts", ".tsx"].some((suf) => item.endsWith(suf)));
|
|
96
|
+
const compilerOptions = {
|
|
97
|
+
...tsConfig.config,
|
|
98
|
+
declaration: true,
|
|
99
|
+
emitDeclarationOnly: true,
|
|
100
|
+
outDir: config.config.es,
|
|
101
|
+
rootDir: config.resolveApp("src"),
|
|
102
|
+
skipLibCheck: true,
|
|
103
|
+
esModuleInterop: true,
|
|
104
|
+
moduleResolution: ts__default.default.ModuleResolutionKind.NodeJs,
|
|
105
|
+
target: ts__default.default.ScriptTarget.ESNext,
|
|
106
|
+
module: ts__default.default.ModuleKind.ESNext
|
|
107
|
+
};
|
|
108
|
+
const host = ts__default.default.createCompilerHost(compilerOptions);
|
|
109
|
+
host.writeFile = (fileName, contents) => {
|
|
110
|
+
const dir = path__default.default.dirname(fileName);
|
|
111
|
+
fs__default.default.mkdirSync(dir, { recursive: true });
|
|
112
|
+
fs__default.default.writeFileSync(fileName, contents, "utf8");
|
|
113
|
+
};
|
|
114
|
+
const program = ts__default.default.createProgram(entryFiles, compilerOptions, host);
|
|
115
|
+
const emitResult = program.emit();
|
|
116
|
+
const diagnostics = ts__default.default.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
117
|
+
if (diagnostics.length > 0) {
|
|
118
|
+
console.error(
|
|
119
|
+
ts__default.default.formatDiagnosticsWithColorAndContext(diagnostics, {
|
|
120
|
+
getCanonicalFileName: (f) => f,
|
|
121
|
+
getCurrentDirectory: ts__default.default.sys.getCurrentDirectory,
|
|
122
|
+
getNewLine: () => ts__default.default.sys.newLine
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
} else {
|
|
126
|
+
console.log(`\u2705 Declarations generated in: ${path__default.default.resolve(config.config.es)}`);
|
|
127
|
+
}
|
|
128
|
+
utils.log.success("\u2705 Declarations build complete!");
|
|
129
|
+
}
|
|
143
130
|
async function buildExtraCss() {
|
|
144
|
-
const extras =
|
|
145
|
-
if (!extras
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
const esRoot = (0, import_config.resolveApp)(import_config.config.es);
|
|
131
|
+
const extras = config.config.css.extra;
|
|
132
|
+
if (!(extras == null ? void 0 : extras.length)) return;
|
|
133
|
+
const srcRoot = config.resolveApp("src");
|
|
134
|
+
const esRoot = config.resolveApp(config.config.es);
|
|
149
135
|
const tasks = extras.map(async (v) => {
|
|
150
136
|
try {
|
|
151
|
-
const absPath =
|
|
152
|
-
const relativePath =
|
|
153
|
-
const dirname =
|
|
154
|
-
const filename =
|
|
155
|
-
const bundle = await
|
|
137
|
+
const absPath = config.resolveApp(v);
|
|
138
|
+
const relativePath = path__default.default.relative(srcRoot, absPath);
|
|
139
|
+
const dirname = path__default.default.dirname(relativePath);
|
|
140
|
+
const filename = path__default.default.basename(v, path__default.default.extname(v));
|
|
141
|
+
const bundle = await rollup.rollup({
|
|
156
142
|
input: [v],
|
|
157
|
-
plugins: getPostcss(
|
|
143
|
+
plugins: getPostcss(path__default.default.join(dirname, `${filename}.min.css`))
|
|
158
144
|
});
|
|
159
145
|
await bundle.write({
|
|
160
|
-
dir:
|
|
146
|
+
dir: config.config.es,
|
|
161
147
|
format: "es",
|
|
162
148
|
sourcemap: false,
|
|
163
149
|
preserveModules: true,
|
|
164
150
|
preserveModulesRoot: srcRoot
|
|
165
151
|
});
|
|
166
|
-
const jsFile =
|
|
167
|
-
if (
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
import_node_fs.default.copyFileSync(import_node_path.default.join(esRoot, dirname, `${filename}.min.css`), dest);
|
|
152
|
+
const jsFile = path__default.default.join(esRoot, dirname, `${filename}${path__default.default.extname(v)}.js`);
|
|
153
|
+
if (fs__default.default.existsSync(jsFile)) fs__default.default.unlinkSync(jsFile);
|
|
154
|
+
[config.config.cjs, config.config.iife].forEach((targetRoot) => {
|
|
155
|
+
const dest = path__default.default.join(config.resolveApp(targetRoot), dirname, `${filename}.min.css`);
|
|
156
|
+
fs__default.default.mkdirSync(path__default.default.dirname(dest), { recursive: true });
|
|
157
|
+
fs__default.default.copyFileSync(path__default.default.join(esRoot, dirname, `${filename}.min.css`), dest);
|
|
173
158
|
});
|
|
174
|
-
|
|
159
|
+
utils.log.success(`\u2705 \u7F16\u8BD1\u5B8C\u6210: ${v}`);
|
|
175
160
|
} catch (err) {
|
|
176
|
-
|
|
161
|
+
utils.log.error(`\u274C \u7F16\u8BD1\u5931\u8D25: ${v}`, err);
|
|
177
162
|
}
|
|
178
163
|
});
|
|
179
164
|
await Promise.all(tasks);
|
|
180
|
-
|
|
165
|
+
utils.log.success("\u2705 \u6240\u6709\u989D\u5916 CSS \u7F16\u8BD1\u5B8C\u6210");
|
|
181
166
|
}
|
|
182
|
-
async function
|
|
183
|
-
|
|
167
|
+
async function buildJS() {
|
|
168
|
+
const outputOptions = [
|
|
184
169
|
{
|
|
185
|
-
dir:
|
|
170
|
+
dir: config.config.es,
|
|
186
171
|
format: "es",
|
|
187
|
-
// 输出 ES Module
|
|
188
|
-
sourcemap: false,
|
|
189
172
|
preserveModules: true,
|
|
190
|
-
preserveModulesRoot:
|
|
191
|
-
// ✅ 指定源代码根目录
|
|
173
|
+
preserveModulesRoot: config.resolveApp("src")
|
|
192
174
|
},
|
|
193
175
|
{
|
|
194
|
-
dir:
|
|
176
|
+
dir: config.config.cjs,
|
|
195
177
|
format: "cjs",
|
|
196
|
-
// 输出 COMMONJS
|
|
197
|
-
sourcemap: false,
|
|
198
178
|
preserveModules: true,
|
|
199
|
-
preserveModulesRoot:
|
|
200
|
-
// ✅ 指定源代码根目录
|
|
201
|
-
exports: "named"
|
|
202
|
-
},
|
|
203
|
-
{
|
|
204
|
-
dir: import_config.config.iife,
|
|
205
|
-
format: "iife",
|
|
206
|
-
// 输出 iife
|
|
207
|
-
sourcemap: false,
|
|
179
|
+
preserveModulesRoot: config.resolveApp("src"),
|
|
208
180
|
exports: "named",
|
|
209
|
-
|
|
210
|
-
}
|
|
181
|
+
interop: "auto"
|
|
182
|
+
},
|
|
183
|
+
...config.config.iife ? [
|
|
184
|
+
{
|
|
185
|
+
dir: config.config.iife,
|
|
186
|
+
format: "iife",
|
|
187
|
+
exports: "named",
|
|
188
|
+
name: utils.toPascalCase(name)
|
|
189
|
+
}
|
|
190
|
+
] : []
|
|
211
191
|
];
|
|
212
|
-
if (!import_config.config.iife) {
|
|
213
|
-
outputOptions = outputOptions.filter((op) => op.format !== "iife");
|
|
214
|
-
}
|
|
215
192
|
for (const output of outputOptions) {
|
|
216
|
-
const bundle = await
|
|
193
|
+
const bundle = await rollup.rollup(getInputOptions(output.format));
|
|
217
194
|
await bundle.write(output);
|
|
218
195
|
}
|
|
219
|
-
{
|
|
220
|
-
const bundle = await (0, import_rollup.rollup)(getInputOptions("es", import_config.config.es));
|
|
221
|
-
await bundle.write(outputOptions[0]);
|
|
222
|
-
}
|
|
223
196
|
await buildExtraCss();
|
|
224
|
-
|
|
197
|
+
utils.log.success("\u2705 JS Build complete!");
|
|
225
198
|
}
|
|
226
|
-
const copySrcTds = () => {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
};
|
|
232
|
-
var src_default = (0, import_gulp.series)(clean, build, copySrcTds, copyTds);
|
|
199
|
+
const copySrcTds = () => gulp__default.default.src(config.config.include.map((t) => `${t}/**/*.d.ts`)).pipe(gulp__default.default.dest(config.config.es));
|
|
200
|
+
const copyTds = () => gulp__default.default.src([`${config.config.es}/**/*.d.ts`]).pipe(gulp__default.default.dest(config.config.cjs));
|
|
201
|
+
var src_default = gulp.series(clean, buildJS, buildDeclarations, copySrcTds, copyTds);
|
|
202
|
+
|
|
203
|
+
exports.default = src_default;
|
package/esm/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import gulp, { series } from '@es-pkg/gulp';
|
|
2
2
|
import { getValidPkgName, log, remove, toPascalCase } from '@es-pkg/utils';
|
|
3
|
-
import { pkg, relativeToApp, config, resolveApp, shallowInputs } from '@es-pkg/config';
|
|
3
|
+
import { pkg, relativeToApp, config, resolveApp, collectInputs, shallowInputs } from '@es-pkg/config';
|
|
4
4
|
import { rollup } from 'rollup';
|
|
5
5
|
import resolve from '@rollup/plugin-node-resolve';
|
|
6
6
|
import commonjs from '@rollup/plugin-commonjs';
|
|
7
|
-
import typescript from '@rollup/plugin-typescript';
|
|
8
7
|
import json from '@rollup/plugin-json';
|
|
9
8
|
import postcss from 'rollup-plugin-postcss';
|
|
10
9
|
import autoprefixer from 'autoprefixer';
|
|
@@ -13,189 +12,174 @@ import path from 'node:path';
|
|
|
13
12
|
import fs from 'node:fs';
|
|
14
13
|
import esbuild from 'rollup-plugin-esbuild';
|
|
15
14
|
import { builtinModules } from 'node:module';
|
|
15
|
+
import ts from 'typescript';
|
|
16
16
|
|
|
17
|
+
const name = getValidPkgName(pkg.name);
|
|
17
18
|
const clean = () => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
log(`\u6E05\u9664 ${relativeToApp(config.es)} & ${relativeToApp(config.cjs)} \u76EE\u5F55---\u5F00\u59CB`);
|
|
20
|
+
const promises = [
|
|
21
|
+
remove(config.publishDir, true),
|
|
22
|
+
remove(config.es, true),
|
|
23
|
+
remove(config.cjs, true),
|
|
24
|
+
remove(config.iife, true)
|
|
25
|
+
];
|
|
26
|
+
return Promise.all(promises).then(() => {
|
|
27
|
+
log(`\u6E05\u9664 ${relativeToApp(config.es)} & ${relativeToApp(config.cjs)} \u76EE\u5F55---\u7ED3\u675F`);
|
|
28
|
+
});
|
|
28
29
|
};
|
|
29
30
|
function getPostcss(extract) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
});
|
|
31
|
+
return postcss({
|
|
32
|
+
extensions: [".less", ".scss", ".sass"],
|
|
33
|
+
use: {
|
|
34
|
+
stylus: ["sass"],
|
|
35
|
+
less: ["less"],
|
|
36
|
+
sass: ["sass"]
|
|
37
|
+
},
|
|
38
|
+
plugins: [autoprefixer({ overrideBrowserslist: config.css.browserslist }), cssnano()],
|
|
39
|
+
extract
|
|
40
|
+
});
|
|
49
41
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
});
|
|
42
|
+
function isNodeModule(id) {
|
|
43
|
+
try {
|
|
44
|
+
const resolved = require.resolve(id, { paths: [process.cwd()] });
|
|
45
|
+
return resolved.includes("node_modules");
|
|
46
|
+
} catch (e) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
107
49
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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 编译完成');
|
|
50
|
+
function getInputOptions(format) {
|
|
51
|
+
return {
|
|
52
|
+
input: shallowInputs.filter((item) => !item.endsWith(".d.ts")),
|
|
53
|
+
external: (id) => {
|
|
54
|
+
if (builtinModules.includes(id)) return true;
|
|
55
|
+
if (!id.startsWith(".") && !path.isAbsolute(id) && isNodeModule(id)) return true;
|
|
56
|
+
return false;
|
|
57
|
+
},
|
|
58
|
+
plugins: [
|
|
59
|
+
json(),
|
|
60
|
+
resolve(),
|
|
61
|
+
esbuild({ target: "es2018", format: "esm" }),
|
|
62
|
+
commonjs({
|
|
63
|
+
defaultIsModuleExports: true,
|
|
64
|
+
esmExternals: true,
|
|
65
|
+
transformMixedEsModules: true
|
|
66
|
+
// 混合模块也转换
|
|
67
|
+
}),
|
|
68
|
+
getPostcss(config.css.extract)
|
|
69
|
+
]
|
|
70
|
+
};
|
|
150
71
|
}
|
|
151
|
-
async function
|
|
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
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
72
|
+
async function buildDeclarations() {
|
|
73
|
+
const tsConfig = ts.readConfigFile(resolveApp("tsconfig.json"), ts.sys.readFile);
|
|
74
|
+
if (tsConfig.error) {
|
|
75
|
+
console.log(tsConfig.error.messageText);
|
|
76
|
+
}
|
|
77
|
+
const entryFiles = collectInputs.filter((item) => [".ts", ".tsx"].some((suf) => item.endsWith(suf)));
|
|
78
|
+
const compilerOptions = {
|
|
79
|
+
...tsConfig.config,
|
|
80
|
+
declaration: true,
|
|
81
|
+
emitDeclarationOnly: true,
|
|
82
|
+
outDir: config.es,
|
|
83
|
+
rootDir: resolveApp("src"),
|
|
84
|
+
skipLibCheck: true,
|
|
85
|
+
esModuleInterop: true,
|
|
86
|
+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
|
|
87
|
+
target: ts.ScriptTarget.ESNext,
|
|
88
|
+
module: ts.ModuleKind.ESNext
|
|
89
|
+
};
|
|
90
|
+
const host = ts.createCompilerHost(compilerOptions);
|
|
91
|
+
host.writeFile = (fileName, contents) => {
|
|
92
|
+
const dir = path.dirname(fileName);
|
|
93
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
94
|
+
fs.writeFileSync(fileName, contents, "utf8");
|
|
95
|
+
};
|
|
96
|
+
const program = ts.createProgram(entryFiles, compilerOptions, host);
|
|
97
|
+
const emitResult = program.emit();
|
|
98
|
+
const diagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
99
|
+
if (diagnostics.length > 0) {
|
|
100
|
+
console.error(
|
|
101
|
+
ts.formatDiagnosticsWithColorAndContext(diagnostics, {
|
|
102
|
+
getCanonicalFileName: (f) => f,
|
|
103
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
104
|
+
getNewLine: () => ts.sys.newLine
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
} else {
|
|
108
|
+
console.log(`\u2705 Declarations generated in: ${path.resolve(config.es)}`);
|
|
109
|
+
}
|
|
110
|
+
log.success("\u2705 Declarations build complete!");
|
|
111
|
+
}
|
|
112
|
+
async function buildExtraCss() {
|
|
113
|
+
const extras = config.css.extra;
|
|
114
|
+
if (!(extras == null ? void 0 : extras.length)) return;
|
|
115
|
+
const srcRoot = resolveApp("src");
|
|
116
|
+
const esRoot = resolveApp(config.es);
|
|
117
|
+
const tasks = extras.map(async (v) => {
|
|
118
|
+
try {
|
|
119
|
+
const absPath = resolveApp(v);
|
|
120
|
+
const relativePath = path.relative(srcRoot, absPath);
|
|
121
|
+
const dirname = path.dirname(relativePath);
|
|
122
|
+
const filename = path.basename(v, path.extname(v));
|
|
123
|
+
const bundle = await rollup({
|
|
124
|
+
input: [v],
|
|
125
|
+
plugins: getPostcss(path.join(dirname, `${filename}.min.css`))
|
|
126
|
+
});
|
|
127
|
+
await bundle.write({
|
|
128
|
+
dir: config.es,
|
|
129
|
+
format: "es",
|
|
130
|
+
sourcemap: false,
|
|
131
|
+
preserveModules: true,
|
|
132
|
+
preserveModulesRoot: srcRoot
|
|
133
|
+
});
|
|
134
|
+
const jsFile = path.join(esRoot, dirname, `${filename}${path.extname(v)}.js`);
|
|
135
|
+
if (fs.existsSync(jsFile)) fs.unlinkSync(jsFile);
|
|
136
|
+
[config.cjs, config.iife].forEach((targetRoot) => {
|
|
137
|
+
const dest = path.join(resolveApp(targetRoot), dirname, `${filename}.min.css`);
|
|
138
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
139
|
+
fs.copyFileSync(path.join(esRoot, dirname, `${filename}.min.css`), dest);
|
|
140
|
+
});
|
|
141
|
+
log.success(`\u2705 \u7F16\u8BD1\u5B8C\u6210: ${v}`);
|
|
142
|
+
} catch (err) {
|
|
143
|
+
log.error(`\u274C \u7F16\u8BD1\u5931\u8D25: ${v}`, err);
|
|
185
144
|
}
|
|
145
|
+
});
|
|
146
|
+
await Promise.all(tasks);
|
|
147
|
+
log.success("\u2705 \u6240\u6709\u989D\u5916 CSS \u7F16\u8BD1\u5B8C\u6210");
|
|
148
|
+
}
|
|
149
|
+
async function buildJS() {
|
|
150
|
+
const outputOptions = [
|
|
186
151
|
{
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
152
|
+
dir: config.es,
|
|
153
|
+
format: "es",
|
|
154
|
+
preserveModules: true,
|
|
155
|
+
preserveModulesRoot: resolveApp("src")
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
dir: config.cjs,
|
|
159
|
+
format: "cjs",
|
|
160
|
+
preserveModules: true,
|
|
161
|
+
preserveModulesRoot: resolveApp("src"),
|
|
162
|
+
exports: "named",
|
|
163
|
+
interop: "auto"
|
|
164
|
+
},
|
|
165
|
+
...config.iife ? [
|
|
166
|
+
{
|
|
167
|
+
dir: config.iife,
|
|
168
|
+
format: "iife",
|
|
169
|
+
exports: "named",
|
|
170
|
+
name: toPascalCase(name)
|
|
171
|
+
}
|
|
172
|
+
] : []
|
|
173
|
+
];
|
|
174
|
+
for (const output of outputOptions) {
|
|
175
|
+
const bundle = await rollup(getInputOptions(output.format));
|
|
176
|
+
await bundle.write(output);
|
|
177
|
+
}
|
|
178
|
+
await buildExtraCss();
|
|
179
|
+
log.success("\u2705 JS Build complete!");
|
|
192
180
|
}
|
|
193
|
-
const copySrcTds = () => {
|
|
194
|
-
|
|
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);
|
|
181
|
+
const copySrcTds = () => gulp.src(config.include.map((t) => `${t}/**/*.d.ts`)).pipe(gulp.dest(config.es));
|
|
182
|
+
const copyTds = () => gulp.src([`${config.es}/**/*.d.ts`]).pipe(gulp.dest(config.cjs));
|
|
183
|
+
var src_default = series(clean, buildJS, buildDeclarations, copySrcTds, copyTds);
|
|
200
184
|
|
|
201
|
-
export {
|
|
185
|
+
export { src_default as default };
|