@deot/dev-builder 2.0.2 → 2.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 CHANGED
@@ -3,7 +3,7 @@
3
3
  打包
4
4
 
5
5
  - 优先执行`scripts`下的`build`和`build:types`,如果声明`build`,打包由用户管理,`build:types`,类型编译由用户管理
6
- - 可被打包的文件匹配:`src/index.*.(j|t|s?cs)s`
6
+ - 可被打包的文件匹配:`src/index\.(.*)\.?(j|t|s?cs)s`
7
7
 
8
8
  ## 自定义配置
9
9
 
package/dist/index.cjs.js CHANGED
@@ -38,32 +38,66 @@ function _interopNamespaceDefault(e) {
38
38
 
39
39
  const path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
40
40
 
41
- const dirname$1 = path__namespace.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs.js', document.baseURI).href))));
41
+ const dirname$1 = path__namespace.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href))));
42
42
  const run$3 = async (options) => {
43
43
  const locals = devShared.Locals.impl();
44
- const { cwd } = locals;
45
- const { packageName, packageDir, packageOptions } = options || {};
44
+ const { cwd, workspace } = locals;
45
+ const { packageName, packageDir, packageOptions, commandOptions } = options || {};
46
+ const { scriptFormats, nodePackage } = commandOptions;
46
47
  const stats = [];
47
48
  const srcDir = path__namespace.resolve(packageDir, "./src");
48
49
  const outDir = path__namespace.resolve(packageDir, "./dist");
49
- let files = fs.existsSync(srcDir) ? fs.readdirSync(srcDir).filter((i) => /^index(.*)\.(t|j)s$/.test(i)) : [];
50
+ let files = fs.existsSync(srcDir) ? fs.readdirSync(srcDir).filter((i) => /^index\.(.*)\.?(t|j)s$/.test(i)) : [];
50
51
  if (!files.length)
51
52
  return stats;
52
- process.env.BUILD_OPTIONS = encodeURIComponent(JSON.stringify({
53
- files,
54
- packageName,
55
- packageDir,
56
- packageOptions
57
- }));
58
- let options$ = {};
59
- if (fs.existsSync(`${cwd}/build.config.ts`)) {
60
- options$.configFile = path__namespace.relative(cwd, path__namespace.resolve(cwd, "./build.config.ts"));
61
- } else if (fs.existsSync(`${cwd}/z.build.config.ts`)) {
62
- options$.configFile = path__namespace.relative(cwd, path__namespace.resolve(cwd, "./z.build.config.ts"));
63
- } else {
64
- options$.configFile = path__namespace.relative(cwd, path__namespace.resolve(dirname$1, "../shared.config.ts"));
65
- }
66
- await vite.build(options$);
53
+ const build = async (format) => {
54
+ process.env.BUILD_OPTIONS = encodeURIComponent(JSON.stringify({
55
+ files,
56
+ format,
57
+ workspace,
58
+ packageName,
59
+ packageDir,
60
+ packageOptions
61
+ }));
62
+ let options$ = {
63
+ build: {
64
+ write: false
65
+ }
66
+ };
67
+ if (fs.existsSync(`${cwd}/z.build.config.ts`)) {
68
+ options$.configFile = path__namespace.relative(cwd, path__namespace.resolve(cwd, "./z.build.config.ts"));
69
+ } else if (fs.existsSync(`${cwd}/build.config.ts`)) {
70
+ options$.configFile = path__namespace.relative(cwd, path__namespace.resolve(cwd, "./build.config.ts"));
71
+ } else {
72
+ options$.configFile = path__namespace.relative(cwd, path__namespace.resolve(dirname$1, "../shared.config.ts"));
73
+ }
74
+ let viteBuild = await vite.build(options$);
75
+ return viteBuild;
76
+ };
77
+ const needFilter = typeof nodePackage === "string" && (nodePackage === "*" || nodePackage.includes(packageName));
78
+ const formats = scriptFormats.split(",").filter((i) => {
79
+ return !needFilter || ["es", "cjs"].includes(i);
80
+ });
81
+ await formats.reduce(
82
+ (preProcess, format) => {
83
+ preProcess = preProcess.then(() => build(format)).then((outputs2) => {
84
+ outputs2.forEach((i) => {
85
+ i.output.forEach((j) => {
86
+ if (j.type === "asset") {
87
+ fs.outputFileSync(`${outDir}/${j.fileName}`, j.source);
88
+ return;
89
+ }
90
+ if (j.type === "chunk") {
91
+ fs.outputFileSync(`${outDir}/${j.name}.${format}.js`, j.code);
92
+ return;
93
+ }
94
+ });
95
+ });
96
+ });
97
+ return preProcess;
98
+ },
99
+ Promise.resolve()
100
+ );
67
101
  let outputs = fs.readdirSync(outDir).filter((i) => /^index(.*)(?!=\.d)\.js$/.test(i));
68
102
  outputs.forEach((file) => {
69
103
  let stat = fs.statSync(path__namespace.resolve(outDir, file));
@@ -79,7 +113,7 @@ const run$3 = async (options) => {
79
113
  const run$2 = async (options) => {
80
114
  const { packageDir } = options || {};
81
115
  const srcDir = path__namespace.resolve(packageDir, "./src");
82
- const styles = fs.existsSync(srcDir) ? fs.readdirSync(srcDir).filter((i) => /^index(.*)\.s?css$/.test(i)) : [];
116
+ const styles = fs.existsSync(srcDir) ? fs.readdirSync(srcDir).filter((i) => /^index\.(.*)\.?s?css$/.test(i)) : [];
83
117
  const stats = [];
84
118
  await styles.reduce(
85
119
  (preProcess, file) => {
@@ -104,7 +138,7 @@ const run$2 = async (options) => {
104
138
  return stats;
105
139
  };
106
140
 
107
- const dirname = path__namespace.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs.js', document.baseURI).href))));
141
+ const dirname = path__namespace.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href))));
108
142
  const run$1 = async (options) => {
109
143
  const { workspace } = devShared.Locals.impl();
110
144
  const { packageDir, packageFolderName, packageOptions, commandOptions } = options;
package/dist/index.es.js CHANGED
@@ -18,29 +18,63 @@ import { Extractor, ExtractorConfig } from '@microsoft/api-extractor';
18
18
  const dirname$1 = path.dirname(fileURLToPath(import.meta.url));
19
19
  const run$3 = async (options) => {
20
20
  const locals = Locals.impl();
21
- const { cwd } = locals;
22
- const { packageName, packageDir, packageOptions } = options || {};
21
+ const { cwd, workspace } = locals;
22
+ const { packageName, packageDir, packageOptions, commandOptions } = options || {};
23
+ const { scriptFormats, nodePackage } = commandOptions;
23
24
  const stats = [];
24
25
  const srcDir = path.resolve(packageDir, "./src");
25
26
  const outDir = path.resolve(packageDir, "./dist");
26
- let files = fs.existsSync(srcDir) ? fs.readdirSync(srcDir).filter((i) => /^index(.*)\.(t|j)s$/.test(i)) : [];
27
+ let files = fs.existsSync(srcDir) ? fs.readdirSync(srcDir).filter((i) => /^index\.(.*)\.?(t|j)s$/.test(i)) : [];
27
28
  if (!files.length)
28
29
  return stats;
29
- process.env.BUILD_OPTIONS = encodeURIComponent(JSON.stringify({
30
- files,
31
- packageName,
32
- packageDir,
33
- packageOptions
34
- }));
35
- let options$ = {};
36
- if (fs.existsSync(`${cwd}/build.config.ts`)) {
37
- options$.configFile = path.relative(cwd, path.resolve(cwd, "./build.config.ts"));
38
- } else if (fs.existsSync(`${cwd}/z.build.config.ts`)) {
39
- options$.configFile = path.relative(cwd, path.resolve(cwd, "./z.build.config.ts"));
40
- } else {
41
- options$.configFile = path.relative(cwd, path.resolve(dirname$1, "../shared.config.ts"));
42
- }
43
- await build$1(options$);
30
+ const build = async (format) => {
31
+ process.env.BUILD_OPTIONS = encodeURIComponent(JSON.stringify({
32
+ files,
33
+ format,
34
+ workspace,
35
+ packageName,
36
+ packageDir,
37
+ packageOptions
38
+ }));
39
+ let options$ = {
40
+ build: {
41
+ write: false
42
+ }
43
+ };
44
+ if (fs.existsSync(`${cwd}/z.build.config.ts`)) {
45
+ options$.configFile = path.relative(cwd, path.resolve(cwd, "./z.build.config.ts"));
46
+ } else if (fs.existsSync(`${cwd}/build.config.ts`)) {
47
+ options$.configFile = path.relative(cwd, path.resolve(cwd, "./build.config.ts"));
48
+ } else {
49
+ options$.configFile = path.relative(cwd, path.resolve(dirname$1, "../shared.config.ts"));
50
+ }
51
+ let viteBuild = await build$1(options$);
52
+ return viteBuild;
53
+ };
54
+ const needFilter = typeof nodePackage === "string" && (nodePackage === "*" || nodePackage.includes(packageName));
55
+ const formats = scriptFormats.split(",").filter((i) => {
56
+ return !needFilter || ["es", "cjs"].includes(i);
57
+ });
58
+ await formats.reduce(
59
+ (preProcess, format) => {
60
+ preProcess = preProcess.then(() => build(format)).then((outputs2) => {
61
+ outputs2.forEach((i) => {
62
+ i.output.forEach((j) => {
63
+ if (j.type === "asset") {
64
+ fs.outputFileSync(`${outDir}/${j.fileName}`, j.source);
65
+ return;
66
+ }
67
+ if (j.type === "chunk") {
68
+ fs.outputFileSync(`${outDir}/${j.name}.${format}.js`, j.code);
69
+ return;
70
+ }
71
+ });
72
+ });
73
+ });
74
+ return preProcess;
75
+ },
76
+ Promise.resolve()
77
+ );
44
78
  let outputs = fs.readdirSync(outDir).filter((i) => /^index(.*)(?!=\.d)\.js$/.test(i));
45
79
  outputs.forEach((file) => {
46
80
  let stat = fs.statSync(path.resolve(outDir, file));
@@ -56,7 +90,7 @@ const run$3 = async (options) => {
56
90
  const run$2 = async (options) => {
57
91
  const { packageDir } = options || {};
58
92
  const srcDir = path.resolve(packageDir, "./src");
59
- const styles = fs.existsSync(srcDir) ? fs.readdirSync(srcDir).filter((i) => /^index(.*)\.s?css$/.test(i)) : [];
93
+ const styles = fs.existsSync(srcDir) ? fs.readdirSync(srcDir).filter((i) => /^index\.(.*)\.?s?css$/.test(i)) : [];
60
94
  const stats = [];
61
95
  await styles.reduce(
62
96
  (preProcess, file) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deot/dev-builder",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "main": "dist/index.es.js",
5
5
  "module": "dist/index.es.js",
6
6
  "types": "dist/index.d.ts",
package/shared.config.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { createRequire } from "node:module";
1
2
  import * as path from 'node:path';
2
3
  import { defineConfig } from 'vite';
3
4
  import atImport from "postcss-import";
@@ -6,17 +7,51 @@ import flexBugs from "postcss-flexbugs-fixes";
6
7
  import cssnano from "cssnano";
7
8
  import autoprefixer from "autoprefixer";
8
9
 
10
+ const cwd = process.cwd();
11
+
9
12
  // options
10
13
  const buildOptions = JSON.parse(decodeURIComponent(process.env.BUILD_OPTIONS || '{}'));
11
14
 
12
- const { files = [], packageName, packageDir, packageOptions = {} } = buildOptions;
15
+ const {
16
+ format,
17
+ workspace,
18
+ files = [],
19
+ packageName,
20
+ packageDir,
21
+ packageOptions = {}
22
+ } = buildOptions;
23
+
24
+ const external = /(cjs|es)/.test(format)
25
+ ? [
26
+ /^node:/,
27
+ /^[a-zA-Z@]/,
28
+ ...Object
29
+ .keys({
30
+ ...packageOptions.dependencies,
31
+ ...packageOptions.peerDependencies
32
+ })
33
+ .map(i => new RegExp(`^${i}$`))
34
+ ]
35
+ : [];
36
+
37
+ // alias
38
+ const replacement = (name: string) => path.resolve(cwd, `./packages/${name}`);
39
+ const { name } = createRequire(cwd)(path.resolve(cwd, workspace ? `${workspace}/index` : '', 'package.json'));
40
+
41
+ const alias = workspace && !(/(cjs|es)/.test(format))
42
+ ? [
43
+ {
44
+ find: new RegExp(`^${name}$`),
45
+ replacement: replacement('index')
46
+ },
47
+ {
48
+
49
+ find: new RegExp(`^${name}-(.*?)$`),
50
+ replacement: replacement('$1')
51
+ }
52
+ ]
53
+ : [];
13
54
 
14
- const external = Object
15
- .keys({
16
- ...packageOptions.dependencies,
17
- ...packageOptions.peerDependencies
18
- })
19
- .map(i => new RegExp(`^${i}$`));
20
55
 
21
56
  export default defineConfig({
22
57
  plugins: [],
@@ -32,24 +67,15 @@ export default defineConfig({
32
67
  ]
33
68
  }
34
69
  },
70
+ resolve: { alias },
35
71
  build: {
36
72
  minify: false,
37
73
  target: 'esnext',
38
- outDir: path.resolve(packageDir, './dist'),
39
74
  lib: {
40
75
  entry: files.map((file: string) => path.resolve(packageDir, './src', file)),
41
- formats: ['es', 'cjs'], // iife需要控制单独控制external,目前还做不到
42
- name: packageName,
43
- fileName: (format, entryName) => {
44
- return `${entryName}.${format}.js`;
45
- }
76
+ formats: [format],
77
+ name: packageName.replace(/(_|-|^|.*\/)([^-_\/@])/g, (_match: any, _$1: any, $2: string) => $2.toUpperCase())
46
78
  },
47
- rollupOptions: {
48
- external: [
49
- /^node:/,
50
- /^[a-zA-Z@]/,
51
- ...external
52
- ]
53
- }
79
+ rollupOptions: { external }
54
80
  }
55
81
  });