@es-pkg/compile 1.0.5 → 1.0.7

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