gs-rollup 1.0.0
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 +46 -0
- package/README.zh-CN.md +45 -0
- package/bin/gs-rollup +5 -0
- package/lib/core.cjs +42 -0
- package/lib/core.d.ts +11 -0
- package/lib/core.mjs +50 -0
- package/lib/dts.cjs +37 -0
- package/lib/dts.d.ts +6 -0
- package/lib/dts.mjs +41 -0
- package/lib/index.cjs +31 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.mjs +4 -0
- package/lib/plugins.cjs +108 -0
- package/lib/plugins.d.ts +27 -0
- package/lib/plugins.mjs +104 -0
- package/lib/tools.cjs +314 -0
- package/lib/tools.d.ts +34 -0
- package/lib/tools.mjs +332 -0
- package/lib/type.cjs +3 -0
- package/lib/type.d.ts +84 -0
- package/lib/type.mjs +5 -0
- package/package.json +74 -0
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# gs-rollup
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
|
|
5
|
+
This is a simple wrapper for rollup, mainly used for quickly packaging TypeScript modules. It can be used with zero configuration in most cases. It will automatically copy documentation and rebuild package.json to the packaging directory.
|
|
6
|
+
|
|
7
|
+
[中文](./README.zh-CN.md)
|
|
8
|
+
|
|
9
|
+
### Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npx yarn add -D gs-rollup
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Build
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
gs-rollup -c
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Generate Configuration File
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
gs-rollup -d
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Configuration
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { RollupOptions } from 'rollup'
|
|
32
|
+
import { defineJs, defineDts } from 'gs-rollup'
|
|
33
|
+
|
|
34
|
+
// If it's a multi-entry, it will automatically exclude mutual references
|
|
35
|
+
const input = [ 'src/index.ts' ]
|
|
36
|
+
|
|
37
|
+
export default <RollupOptions[]>[
|
|
38
|
+
...defineDts({
|
|
39
|
+
input,
|
|
40
|
+
buildPackageJson: {
|
|
41
|
+
deleteProps:/^(devDependencies|scripts)$/
|
|
42
|
+
}
|
|
43
|
+
}),
|
|
44
|
+
...defineJs({input})
|
|
45
|
+
]
|
|
46
|
+
```
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# gs-rollup
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
|
|
5
|
+
这是一个 rollup 的简单包装器,主要快速打包TS模块,大多数情况下可以零配置打包。会自动拷贝文档并重建 package.json 到打包目录。
|
|
6
|
+
|
|
7
|
+
[English](./README.md)
|
|
8
|
+
|
|
9
|
+
### 安装
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npx yarn add -D gs-rollup
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### 打包
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
gs-rollup -c
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### 生成配置文件
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
gs-rollup -d
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### 配置
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { RollupOptions } from 'rollup'
|
|
31
|
+
import { defineJs, defineDts } from 'gs-rollup'
|
|
32
|
+
|
|
33
|
+
// 如果是多入口,会自动互相排除引用
|
|
34
|
+
const input = [ 'src/index.ts' ]
|
|
35
|
+
|
|
36
|
+
export default <RollupOptions[]>[
|
|
37
|
+
...defineDts({
|
|
38
|
+
input,
|
|
39
|
+
buildPackageJson: {
|
|
40
|
+
deleteProps:/^(devDependencies|scripts)$/
|
|
41
|
+
}
|
|
42
|
+
}),
|
|
43
|
+
...defineJs({input})
|
|
44
|
+
]
|
|
45
|
+
```
|
package/bin/gs-rollup
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";var h=Object.create;var l=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var x=Object.getPrototypeOf,w=Object.prototype.hasOwnProperty;var y=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of v(t))!w.call(e,o)&&o!==n&&l(e,o,{get:()=>t[o],enumerable:!(i=m(t,o))||i.enumerable});return e};var q=(e,t,n)=>(n=e!=null?h(x(e)):{},y(t||!e||!e.__esModule?l(n,"default",{value:e,enumerable:!0}):n,e));var u=require('node:path'),s=require('node:fs'),_=require('node:child_process'),D=require('node:module'),P=require('../lib/dts'),R=require('../lib/core'),S=require('../lib/tools'),p=typeof document<"u"?document.currentScript:null;function b(e){const t={"-c":"--config","-i":"--init","-h":"--help"},n=[];for(let i=0;i<e.length;i++){let o=t[e[i]]||e[i];if(o.startsWith("--config=")){n.push("--config",o.split("=")[1]);continue}if(o==="--config"){const r=e[i+1];r&&!r.startsWith("-")?(n.push("--config",r),i++):n.push("--config");continue}n.push(o)}return n}function j(e){const t=e.indexOf("--config");if(t>=0&&e[t+1]&&!e[t+1].startsWith("-"))return e[t+1]}function C(e){const t=[];for(let n=0;n<e.length;n++){if(e[n]==="--config"){e[n+1]&&!e[n+1].startsWith("-")&&n++;continue}t.push(e[n])}return t}const U=D.createRequire(typeof document>"u"?require('url').pathToFileURL(__filename).href:p&&p.tagName.toUpperCase()==="SCRIPT"&&p.src||new URL("gs-rollup",document.baseURI).href);function c(e=[],t=!1){const n=U.resolve("rollup/dist/bin/rollup"),i=[];t&&i.push("--import","tsx/esm"),process.env.CHILD_DEBUG&&i.push("--inspect-brk","--inspect-port=9229");const o=[...i,n,...e];_.spawn(process.execPath,o,{stdio:"inherit"}).on("exit",r=>process.exit(r??0))}async function a(){const{rollup:e}=await import("rollup");for(const t of[...P.defineDts({buildPackageJson:{deleteProps:/^(devDependencies|scripts)$/}}),...R.defineJs()]){const n=await e(t),i=Array.isArray(t.output)?t.output:[t.output];for(const o of i)await n.write(o)}}const d="rollup.config.ts";async function J(){if(s.existsSync(d))return;const e=["import { RollupOptions } from 'rollup'","import { defineJs, defineDts } from 'gs-rollup'","",`const input = [ '${(S.detectEntry()||"src/index.ts").replace(/\\/g,"/")}' ]`,"","export default <RollupOptions[]>["," ...defineDts({"," input,"," buildPackageJson: {"," deleteProps: /^(devDependencies|scripts)$/"," }"," }),"," ...defineJs({input})","]",""];s.writeFileSync(d,e.join(`
|
|
3
|
+
`))}async function g(){const e=b(process.argv.slice(2)),t=n=>e.includes(n);if(!e.length||t("--help")){console.log(`
|
|
4
|
+
Use -i to initialize project.
|
|
5
|
+
`),c(e);return}if(t("--init")){await J();return}if(t("--config")){const n=j(e),i=C(e),o=u.join(process.cwd(),"rollup.config.ts"),r=u.join(process.cwd(),"rollup.config.js");if(!n){if(s.existsSync(o)){c(["--config",o,...i],!0);return}if(s.existsSync(r)){c(["--config",r,...i],!1);return}await a();return}const f=u.resolve(n);s.existsSync(f)?c(e,f.endsWith(".ts")):await a();return}c(e)}g().catch(e=>{console.error(e),process.exit(1)}),exports.index=g;
|
package/lib/core.cjs
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var copy = require('rollup-plugin-copy'), gsBase = require('gs-base'), tools = require('./tools'), resolve = require('@rollup/plugin-node-resolve'), esbuild = require('rollup-plugin-esbuild'), plugins = require('./plugins');
|
|
3
|
+
function defineCopy(srcOrTargets, dest = tools.DefaultValues.outputBase) {
|
|
4
|
+
return Array.isArray(srcOrTargets) && srcOrTargets.length && gsBase.isObject(srcOrTargets[0]) && copy({
|
|
5
|
+
targets: srcOrTargets.map((item) => ({ ...item, dest: item.dest || dest }))
|
|
6
|
+
}), copy({ targets: [{ src: srcOrTargets, dest }] });
|
|
7
|
+
}
|
|
8
|
+
function defineOutput(file, arg) {
|
|
9
|
+
let {
|
|
10
|
+
overwriteProps,
|
|
11
|
+
format,
|
|
12
|
+
extension,
|
|
13
|
+
outputCodeDir = tools.DefaultValues.outputCodeDir,
|
|
14
|
+
outputBase = tools.DefaultValues.outputBase
|
|
15
|
+
} = arg;
|
|
16
|
+
return {
|
|
17
|
+
format,
|
|
18
|
+
file: tools.formatOutput(file, outputBase, outputCodeDir, format, extension),
|
|
19
|
+
...overwriteProps
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const defaultEsbuildOption = { target: "esnext", minifySyntax: !0, charset: "utf8" };
|
|
23
|
+
function defineJs(arg) {
|
|
24
|
+
const { esbuild: esOpt = {} } = arg || {}, inputs = tools.formatInput(arg), plugins$1 = arg?.plugins || [];
|
|
25
|
+
plugins$1.push(resolve()), plugins$1.push(plugins.rawLoader()), plugins$1.push(esbuild({ ...defaultEsbuildOption, ...esOpt }));
|
|
26
|
+
const outputs = checkFormats(arg?.formats, arg), result = [], inputEntries = Object.entries(inputs);
|
|
27
|
+
for (const [file, input] of inputEntries)
|
|
28
|
+
result.push({
|
|
29
|
+
input,
|
|
30
|
+
external: tools.getExternalByInput(input, inputEntries, arg),
|
|
31
|
+
plugins: plugins$1,
|
|
32
|
+
output: outputs.map((out) => defineOutput(file, out))
|
|
33
|
+
});
|
|
34
|
+
return tools.itemAfterAddPlugin(result, arg);
|
|
35
|
+
}
|
|
36
|
+
function checkFormats(formats = ["esm", "cjs"], arg) {
|
|
37
|
+
return (Array.isArray(formats) ? formats : [formats]).map((format) => {
|
|
38
|
+
const result = gsBase.isString(format) ? { format } : format;
|
|
39
|
+
return { ...arg, ...result };
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
exports.defineCopy = defineCopy, exports.defineJs = defineJs, exports.defineOutput = defineOutput;
|
package/lib/core.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ICopyTarget, IDefineOutputOption, IDefineJsArg } from './type';
|
|
2
|
+
import { Plugin, OutputOptions, RollupOptions } from 'rollup';
|
|
3
|
+
|
|
4
|
+
declare function defineCopy(src: string | string[], dest?: string): Plugin;
|
|
5
|
+
declare function defineCopy(targets: ICopyTarget[], dest?: string): Plugin;
|
|
6
|
+
|
|
7
|
+
declare function defineOutput(file: string, arg: IDefineOutputOption): OutputOptions;
|
|
8
|
+
|
|
9
|
+
declare function defineJs(arg?: IDefineJsArg): RollupOptions[];
|
|
10
|
+
|
|
11
|
+
export { defineCopy, defineJs, defineOutput };
|
package/lib/core.mjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import copy from 'rollup-plugin-copy';
|
|
2
|
+
import { isObject, isString } from 'gs-base';
|
|
3
|
+
import { DefaultValues, formatOutput, formatInput, getExternalByInput, itemAfterAddPlugin } from './tools';
|
|
4
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
5
|
+
import esbuild from 'rollup-plugin-esbuild';
|
|
6
|
+
import { rawLoader } from './plugins';
|
|
7
|
+
function defineCopy(srcOrTargets, dest = DefaultValues.outputBase) {
|
|
8
|
+
return Array.isArray(srcOrTargets) && srcOrTargets.length && isObject(srcOrTargets[0]) && copy({
|
|
9
|
+
targets: srcOrTargets.map((item) => ({ ...item, dest: item.dest || dest }))
|
|
10
|
+
}), copy({ targets: [{ src: srcOrTargets, dest }] });
|
|
11
|
+
}
|
|
12
|
+
function defineOutput(file, arg) {
|
|
13
|
+
let {
|
|
14
|
+
overwriteProps,
|
|
15
|
+
format,
|
|
16
|
+
extension,
|
|
17
|
+
outputCodeDir = DefaultValues.outputCodeDir,
|
|
18
|
+
outputBase = DefaultValues.outputBase
|
|
19
|
+
} = arg;
|
|
20
|
+
return {
|
|
21
|
+
format,
|
|
22
|
+
file: formatOutput(file, outputBase, outputCodeDir, format, extension),
|
|
23
|
+
...overwriteProps
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const defaultEsbuildOption = { target: "esnext", minifySyntax: !0, charset: "utf8" };
|
|
27
|
+
function defineJs(arg) {
|
|
28
|
+
const { esbuild: esOpt = {} } = arg || {}, inputs = formatInput(arg), plugins = arg?.plugins || [];
|
|
29
|
+
plugins.push(resolve()), plugins.push(rawLoader()), plugins.push(esbuild({ ...defaultEsbuildOption, ...esOpt }));
|
|
30
|
+
const outputs = checkFormats(arg?.formats, arg), result = [], inputEntries = Object.entries(inputs);
|
|
31
|
+
for (const [file, input] of inputEntries)
|
|
32
|
+
result.push({
|
|
33
|
+
input,
|
|
34
|
+
external: getExternalByInput(input, inputEntries, arg),
|
|
35
|
+
plugins,
|
|
36
|
+
output: outputs.map((out) => defineOutput(file, out))
|
|
37
|
+
});
|
|
38
|
+
return itemAfterAddPlugin(result, arg);
|
|
39
|
+
}
|
|
40
|
+
function checkFormats(formats = ["esm", "cjs"], arg) {
|
|
41
|
+
return (Array.isArray(formats) ? formats : [formats]).map((format) => {
|
|
42
|
+
const result = isString(format) ? { format } : format;
|
|
43
|
+
return { ...arg, ...result };
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
defineCopy,
|
|
48
|
+
defineJs,
|
|
49
|
+
defineOutput
|
|
50
|
+
};
|
package/lib/dts.cjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var dts = require('rollup-plugin-dts'), tools = require('./tools'), core = require('./core'), plugins = require('./plugins');
|
|
3
|
+
function defineDts(arg) {
|
|
4
|
+
const {
|
|
5
|
+
exclude = "test/**/*.ts",
|
|
6
|
+
copyMd = !0,
|
|
7
|
+
output,
|
|
8
|
+
buildPackageJson = !0
|
|
9
|
+
} = arg || {}, inputs = tools.formatInput(arg), plugins2 = arg?.plugins || [];
|
|
10
|
+
plugins2.push(dts({ respectExternal: !1, exclude: Array.isArray(exclude) ? exclude : [exclude] }));
|
|
11
|
+
const result = [], inputEntries = Object.entries(inputs);
|
|
12
|
+
for (const [file, input] of inputEntries)
|
|
13
|
+
result.push({
|
|
14
|
+
input,
|
|
15
|
+
external: tools.getExternalByInput(input, inputEntries, arg),
|
|
16
|
+
plugins: plugins2,
|
|
17
|
+
output: output || core.defineOutput(file, { format: "esm", extension: ".d.ts" })
|
|
18
|
+
});
|
|
19
|
+
return copyMd && (result[0].plugins = [...result[0].plugins, core.defineCopy("*.md")]), buildPackageJson && (result[0].plugins = [...result[0].plugins, definePkgPlugin(arg)]), tools.itemAfterAddPlugin(result, arg);
|
|
20
|
+
}
|
|
21
|
+
function definePkgPlugin(arg) {
|
|
22
|
+
const {
|
|
23
|
+
buildPackageJson,
|
|
24
|
+
outputBase,
|
|
25
|
+
input,
|
|
26
|
+
outputCodeDir
|
|
27
|
+
} = arg || {};
|
|
28
|
+
return plugins.packageJson({
|
|
29
|
+
outputBase,
|
|
30
|
+
exports: {
|
|
31
|
+
input,
|
|
32
|
+
outputCodeDir
|
|
33
|
+
},
|
|
34
|
+
...buildPackageJson
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
exports.defineDts = defineDts;
|
package/lib/dts.d.ts
ADDED
package/lib/dts.mjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import dts from 'rollup-plugin-dts';
|
|
2
|
+
import { formatInput, getExternalByInput, itemAfterAddPlugin } from './tools';
|
|
3
|
+
import { defineOutput, defineCopy } from './core';
|
|
4
|
+
import { packageJson } from './plugins';
|
|
5
|
+
function defineDts(arg) {
|
|
6
|
+
const {
|
|
7
|
+
exclude = "test/**/*.ts",
|
|
8
|
+
copyMd = !0,
|
|
9
|
+
output,
|
|
10
|
+
buildPackageJson = !0
|
|
11
|
+
} = arg || {}, inputs = formatInput(arg), plugins = arg?.plugins || [];
|
|
12
|
+
plugins.push(dts({ respectExternal: !1, exclude: Array.isArray(exclude) ? exclude : [exclude] }));
|
|
13
|
+
const result = [], inputEntries = Object.entries(inputs);
|
|
14
|
+
for (const [file, input] of inputEntries)
|
|
15
|
+
result.push({
|
|
16
|
+
input,
|
|
17
|
+
external: getExternalByInput(input, inputEntries, arg),
|
|
18
|
+
plugins,
|
|
19
|
+
output: output || defineOutput(file, { format: "esm", extension: ".d.ts" })
|
|
20
|
+
});
|
|
21
|
+
return copyMd && (result[0].plugins = [...result[0].plugins, defineCopy("*.md")]), buildPackageJson && (result[0].plugins = [...result[0].plugins, definePkgPlugin(arg)]), itemAfterAddPlugin(result, arg);
|
|
22
|
+
}
|
|
23
|
+
function definePkgPlugin(arg) {
|
|
24
|
+
const {
|
|
25
|
+
buildPackageJson,
|
|
26
|
+
outputBase,
|
|
27
|
+
input,
|
|
28
|
+
outputCodeDir
|
|
29
|
+
} = arg || {};
|
|
30
|
+
return packageJson({
|
|
31
|
+
outputBase,
|
|
32
|
+
exports: {
|
|
33
|
+
input,
|
|
34
|
+
outputCodeDir
|
|
35
|
+
},
|
|
36
|
+
...buildPackageJson
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
defineDts
|
|
41
|
+
};
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var type = require('./type'), core = require('./core'), dts = require('./dts'), plugins = require('./plugins');
|
|
3
|
+
Object.keys(type).forEach(function(k) {
|
|
4
|
+
k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k) && Object.defineProperty(exports, k, {
|
|
5
|
+
enumerable: !0,
|
|
6
|
+
get: function() {
|
|
7
|
+
return type[k];
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
}), Object.keys(core).forEach(function(k) {
|
|
11
|
+
k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k) && Object.defineProperty(exports, k, {
|
|
12
|
+
enumerable: !0,
|
|
13
|
+
get: function() {
|
|
14
|
+
return core[k];
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}), Object.keys(dts).forEach(function(k) {
|
|
18
|
+
k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k) && Object.defineProperty(exports, k, {
|
|
19
|
+
enumerable: !0,
|
|
20
|
+
get: function() {
|
|
21
|
+
return dts[k];
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}), Object.keys(plugins).forEach(function(k) {
|
|
25
|
+
k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k) && Object.defineProperty(exports, k, {
|
|
26
|
+
enumerable: !0,
|
|
27
|
+
get: function() {
|
|
28
|
+
return plugins[k];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
package/lib/index.d.ts
ADDED
package/lib/index.mjs
ADDED
package/lib/plugins.cjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var fs = require('node:fs'), node_url = require('node:url'), fs$1 = require('node:fs/promises'), gsBase = require('gs-base'), tools = require('./tools'), fs$2 = require('fs'), node_path = require('node:path');
|
|
3
|
+
function _interopNamespaceDefault(e) {
|
|
4
|
+
var n = /* @__PURE__ */ Object.create(null);
|
|
5
|
+
return e && Object.keys(e).forEach(function(k) {
|
|
6
|
+
if (k !== "default") {
|
|
7
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
8
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
9
|
+
enumerable: !0,
|
|
10
|
+
get: function() {
|
|
11
|
+
return e[k];
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
}), n.default = e, Object.freeze(n);
|
|
16
|
+
}
|
|
17
|
+
var fs__namespace = /* @__PURE__ */ _interopNamespaceDefault(fs);
|
|
18
|
+
function rawLoader(encoding = "utf8") {
|
|
19
|
+
return {
|
|
20
|
+
name: "raw-loader",
|
|
21
|
+
load(id) {
|
|
22
|
+
if (id.endsWith("?raw")) {
|
|
23
|
+
const file = id.slice(0, -4);
|
|
24
|
+
return `export default ${JSON.stringify(fs__namespace.readFileSync(file, encoding))};`;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const SUPPORTED = /\.(ts|js|json)$/;
|
|
30
|
+
function configToJson(arg = {}) {
|
|
31
|
+
const entries = /* @__PURE__ */ new Map(), space = arg.minify ? void 0 : 2;
|
|
32
|
+
return {
|
|
33
|
+
name: "config-to-json",
|
|
34
|
+
buildStart(inputOptions) {
|
|
35
|
+
const input = typeof inputOptions.input == "string" ? { main: inputOptions.input } : inputOptions.input ?? {};
|
|
36
|
+
for (const [name, file] of Object.entries(input))
|
|
37
|
+
SUPPORTED.test(file) && entries.set(name, file);
|
|
38
|
+
},
|
|
39
|
+
load(id) {
|
|
40
|
+
return SUPPORTED.test(id) ? "export default {};" : null;
|
|
41
|
+
},
|
|
42
|
+
async generateBundle(outputOptions, bundle) {
|
|
43
|
+
for (const [fileName, item] of Object.entries(bundle)) {
|
|
44
|
+
if (item.type !== "chunk") continue;
|
|
45
|
+
const entry = [...entries.values()][0];
|
|
46
|
+
let data;
|
|
47
|
+
if (entry.endsWith(".json")) {
|
|
48
|
+
const raw = await fs$1.readFile(entry, "utf8");
|
|
49
|
+
data = JSON.parse(raw);
|
|
50
|
+
} else {
|
|
51
|
+
const mod = await import(node_url.pathToFileURL(entry).href + `?t=${Date.now()}`);
|
|
52
|
+
data = mod.default ?? mod;
|
|
53
|
+
}
|
|
54
|
+
arg.transform && (data = await arg.transform(data, {
|
|
55
|
+
name: fileName,
|
|
56
|
+
file: entry
|
|
57
|
+
}));
|
|
58
|
+
const json = JSON.stringify(data, null, space);
|
|
59
|
+
delete bundle[fileName], this.emitFile({
|
|
60
|
+
type: "asset",
|
|
61
|
+
fileName,
|
|
62
|
+
source: json
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const defaultImportReplaceRole = { search: /^(\.{2}\/)+/, replace: "./" }, esImtReg = /((?:import|from)\s*)(['"])([^'"\s]+)\2/g, cjsImtReg = /require\(\s*(['"])([^'"\s]+)\1\s*\)/g;
|
|
69
|
+
function importReplace(replace) {
|
|
70
|
+
const fn = parseFn(replace || defaultImportReplaceRole);
|
|
71
|
+
return {
|
|
72
|
+
name: "import-replace",
|
|
73
|
+
generateBundle({ format }, bundle) {
|
|
74
|
+
if (tools.isEsOrCjsFormat(format))
|
|
75
|
+
for (const chunk of Object.values(bundle)) {
|
|
76
|
+
let code = chunk.code || chunk.source.toString();
|
|
77
|
+
tools.isEsFormat(format) ? (code = processEsCode(code, fn), code = tools.margeEsImport(code)) : code = processCjsCode(code, fn), code && (chunk.code ? chunk.code = code : chunk.source = code);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function processCjsCode(code, fn) {
|
|
83
|
+
return code.replace(cjsImtReg, (_, p1, p2) => `require('${fn(p2)}')`);
|
|
84
|
+
}
|
|
85
|
+
function processEsCode(code, fn) {
|
|
86
|
+
return code.replace(esImtReg, (_, p1, p2, p3) => `${p1}'${fn(p3)}'`);
|
|
87
|
+
}
|
|
88
|
+
function parseFn(replace) {
|
|
89
|
+
if (gsBase.isFunction(replace))
|
|
90
|
+
return replace;
|
|
91
|
+
const roles = Array.isArray(replace) ? replace : [replace];
|
|
92
|
+
return (name) => {
|
|
93
|
+
for (const item of roles)
|
|
94
|
+
if (item.search.test(name))
|
|
95
|
+
return name.replace(item.search, item.replace);
|
|
96
|
+
return name;
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function packageJson(arg) {
|
|
100
|
+
return {
|
|
101
|
+
name: "package-json",
|
|
102
|
+
generateBundle() {
|
|
103
|
+
const { outputBase = tools.DefaultValues.outputBase } = arg || {};
|
|
104
|
+
fs$2.writeFileSync(node_path.join(outputBase, tools.defaultPackageJsonFileName), tools.processPackageJson(arg));
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
exports.configToJson = configToJson, exports.defaultImportReplaceRole = defaultImportReplaceRole, exports.importReplace = importReplace, exports.packageJson = packageJson, exports.rawLoader = rawLoader;
|
package/lib/plugins.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Plugin } from 'rollup';
|
|
2
|
+
import { IPackageJsonArg } from './type';
|
|
3
|
+
|
|
4
|
+
declare function rawLoader(encoding?: BufferEncoding): Plugin;
|
|
5
|
+
|
|
6
|
+
interface IConfigToJsonArg {
|
|
7
|
+
transform?: (data: any, ctx: {
|
|
8
|
+
name: string;
|
|
9
|
+
file: string;
|
|
10
|
+
}) => any | Promise<any>;
|
|
11
|
+
minify?: boolean;
|
|
12
|
+
}
|
|
13
|
+
declare function configToJson(arg?: IConfigToJsonArg): Plugin;
|
|
14
|
+
|
|
15
|
+
declare const defaultImportReplaceRole: IImportReplaceRole;
|
|
16
|
+
type IImportReplaceFn = (name: string) => string;
|
|
17
|
+
interface IImportReplaceRole {
|
|
18
|
+
search: RegExp;
|
|
19
|
+
replace: string | ((substring: string, ...args: any[]) => string);
|
|
20
|
+
}
|
|
21
|
+
type ImportReplaceRole = IImportReplaceRole | IImportReplaceRole[] | IImportReplaceFn;
|
|
22
|
+
declare function importReplace(replace?: ImportReplaceRole): Plugin;
|
|
23
|
+
|
|
24
|
+
declare function packageJson(arg?: IPackageJsonArg): Plugin;
|
|
25
|
+
|
|
26
|
+
export { configToJson, defaultImportReplaceRole, importReplace, packageJson, rawLoader };
|
|
27
|
+
export type { IConfigToJsonArg, IImportReplaceFn, IImportReplaceRole, ImportReplaceRole };
|
package/lib/plugins.mjs
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
import fs$1 from 'node:fs/promises';
|
|
4
|
+
import { isFunction } from 'gs-base';
|
|
5
|
+
import { isEsOrCjsFormat, isEsFormat, margeEsImport, DefaultValues, defaultPackageJsonFileName, processPackageJson } from './tools';
|
|
6
|
+
import fs$2 from 'fs';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
function rawLoader(encoding = "utf8") {
|
|
9
|
+
return {
|
|
10
|
+
name: "raw-loader",
|
|
11
|
+
load(id) {
|
|
12
|
+
if (id.endsWith("?raw")) {
|
|
13
|
+
const file = id.slice(0, -4);
|
|
14
|
+
return `export default ${JSON.stringify(fs.readFileSync(file, encoding))};`;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const SUPPORTED = /\.(ts|js|json)$/;
|
|
20
|
+
function configToJson(arg = {}) {
|
|
21
|
+
const entries = /* @__PURE__ */ new Map(), space = arg.minify ? void 0 : 2;
|
|
22
|
+
return {
|
|
23
|
+
name: "config-to-json",
|
|
24
|
+
buildStart(inputOptions) {
|
|
25
|
+
const input = typeof inputOptions.input == "string" ? { main: inputOptions.input } : inputOptions.input ?? {};
|
|
26
|
+
for (const [name, file] of Object.entries(input))
|
|
27
|
+
SUPPORTED.test(file) && entries.set(name, file);
|
|
28
|
+
},
|
|
29
|
+
load(id) {
|
|
30
|
+
return SUPPORTED.test(id) ? "export default {};" : null;
|
|
31
|
+
},
|
|
32
|
+
async generateBundle(outputOptions, bundle) {
|
|
33
|
+
for (const [fileName, item] of Object.entries(bundle)) {
|
|
34
|
+
if (item.type !== "chunk") continue;
|
|
35
|
+
const entry = [...entries.values()][0];
|
|
36
|
+
let data;
|
|
37
|
+
if (entry.endsWith(".json")) {
|
|
38
|
+
const raw = await fs$1.readFile(entry, "utf8");
|
|
39
|
+
data = JSON.parse(raw);
|
|
40
|
+
} else {
|
|
41
|
+
const mod = await import(pathToFileURL(entry).href + `?t=${Date.now()}`);
|
|
42
|
+
data = mod.default ?? mod;
|
|
43
|
+
}
|
|
44
|
+
arg.transform && (data = await arg.transform(data, {
|
|
45
|
+
name: fileName,
|
|
46
|
+
file: entry
|
|
47
|
+
}));
|
|
48
|
+
const json = JSON.stringify(data, null, space);
|
|
49
|
+
delete bundle[fileName], this.emitFile({
|
|
50
|
+
type: "asset",
|
|
51
|
+
fileName,
|
|
52
|
+
source: json
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const defaultImportReplaceRole = { search: /^(\.{2}\/)+/, replace: "./" }, esImtReg = /((?:import|from)\s*)(['"])([^'"\s]+)\2/g, cjsImtReg = /require\(\s*(['"])([^'"\s]+)\1\s*\)/g;
|
|
59
|
+
function importReplace(replace) {
|
|
60
|
+
const fn = parseFn(replace || defaultImportReplaceRole);
|
|
61
|
+
return {
|
|
62
|
+
name: "import-replace",
|
|
63
|
+
generateBundle({ format }, bundle) {
|
|
64
|
+
if (isEsOrCjsFormat(format))
|
|
65
|
+
for (const chunk of Object.values(bundle)) {
|
|
66
|
+
let code = chunk.code || chunk.source.toString();
|
|
67
|
+
isEsFormat(format) ? (code = processEsCode(code, fn), code = margeEsImport(code)) : code = processCjsCode(code, fn), code && (chunk.code ? chunk.code = code : chunk.source = code);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function processCjsCode(code, fn) {
|
|
73
|
+
return code.replace(cjsImtReg, (_, p1, p2) => `require('${fn(p2)}')`);
|
|
74
|
+
}
|
|
75
|
+
function processEsCode(code, fn) {
|
|
76
|
+
return code.replace(esImtReg, (_, p1, p2, p3) => `${p1}'${fn(p3)}'`);
|
|
77
|
+
}
|
|
78
|
+
function parseFn(replace) {
|
|
79
|
+
if (isFunction(replace))
|
|
80
|
+
return replace;
|
|
81
|
+
const roles = Array.isArray(replace) ? replace : [replace];
|
|
82
|
+
return (name) => {
|
|
83
|
+
for (const item of roles)
|
|
84
|
+
if (item.search.test(name))
|
|
85
|
+
return name.replace(item.search, item.replace);
|
|
86
|
+
return name;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function packageJson(arg) {
|
|
90
|
+
return {
|
|
91
|
+
name: "package-json",
|
|
92
|
+
generateBundle() {
|
|
93
|
+
const { outputBase = DefaultValues.outputBase } = arg || {};
|
|
94
|
+
fs$2.writeFileSync(join(outputBase, defaultPackageJsonFileName), processPackageJson(arg));
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
export {
|
|
99
|
+
configToJson,
|
|
100
|
+
defaultImportReplaceRole,
|
|
101
|
+
importReplace,
|
|
102
|
+
packageJson,
|
|
103
|
+
rawLoader
|
|
104
|
+
};
|