@rollup-use/core
Advanced tools
+48
-49
@@ -8,76 +8,75 @@ "use strict"; | ||
| function report(dir = "dist", extensions = [".js", ".css"]) { | ||
| const report = {}; | ||
| dir = dir.replace(/(\\|\/)$/, ""); | ||
| const filePaths = glob.sync(`${dir}/**/*`); | ||
| for (const filePath of filePaths) { | ||
| if (extensions.find((v) => filePath.endsWith(v))) { | ||
| const code = fs.readFileSync(filePath); | ||
| const size = getSize(code); | ||
| const sizeGzipped = getSize(zlib.gzipSync(code)); | ||
| report[filePath] = { size, sizeGzipped }; | ||
| } | ||
| const report = {}; | ||
| dir = dir.replace(/(\\|\/)$/, ""); | ||
| const filePaths = glob.sync(`${dir}/**/*`); | ||
| for (const filePath of filePaths) { | ||
| if (extensions.find((v) => filePath.endsWith(v))) { | ||
| const code = fs.readFileSync(filePath); | ||
| const size = getSize(code); | ||
| const sizeGzipped = getSize(zlib.gzipSync(code)); | ||
| report[filePath] = { size, sizeGzipped }; | ||
| } | ||
| console.table(report); | ||
| return report; | ||
| } | ||
| console.table(report); | ||
| return report; | ||
| } | ||
| exports.report = report; | ||
| function getSize(code) { | ||
| return (code.length / 1024).toFixed(2) + " KiB"; | ||
| return (code.length / 1024).toFixed(2) + " KiB"; | ||
| } | ||
| exports.getSize = getSize; | ||
| function resolveUMDDependencies(pkg) { | ||
| return [ | ||
| ...Object.keys(pkg.devDependencies || {}), | ||
| ...Object.keys(pkg.peerDependencies || {}), | ||
| ]; | ||
| return [ | ||
| ...Object.keys(pkg.devDependencies || {}), | ||
| ...Object.keys(pkg.peerDependencies || {}), | ||
| ]; | ||
| } | ||
| exports.resolveUMDDependencies = resolveUMDDependencies; | ||
| function resolveAllDependencies(pkg) { | ||
| return [ | ||
| ...resolveUMDDependencies(pkg), | ||
| ...Object.keys(pkg.dependencies || {}), | ||
| ]; | ||
| return [ | ||
| ...resolveUMDDependencies(pkg), | ||
| ...Object.keys(pkg.dependencies || {}), | ||
| ]; | ||
| } | ||
| exports.resolveAllDependencies = resolveAllDependencies; | ||
| function belongsTo(source, dependencePatterns) { | ||
| for (const pattern of dependencePatterns) { | ||
| if (pattern instanceof RegExp) { | ||
| if (pattern.test(source)) { | ||
| return true; | ||
| } | ||
| } | ||
| else { | ||
| if (pattern === source) { | ||
| return true; | ||
| } | ||
| let t = pattern.replace(/^(\/|\\)/, "").replace(/(\/|\\)$/, ""); | ||
| let reg = new RegExp(`^(.*?[/\\\\])?${t}([/\\\\].*?)?$`); | ||
| if (reg.test(source)) { | ||
| return true; | ||
| } | ||
| } | ||
| for (const pattern of dependencePatterns) { | ||
| if (pattern instanceof RegExp) { | ||
| if (pattern.test(source)) { | ||
| return true; | ||
| } | ||
| } else { | ||
| if (pattern === source) { | ||
| return true; | ||
| } | ||
| let t = pattern.replace(/^(\/|\\)/, "").replace(/(\/|\\)$/, ""); | ||
| let reg = new RegExp(`^(.*?[/\\\\])?${t}([/\\\\].*?)?$`); | ||
| if (reg.test(source)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| exports.belongsTo = belongsTo; | ||
| function camelize(str) { | ||
| return str | ||
| .replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) { | ||
| return index === 0 ? word.toLowerCase() : word.toUpperCase(); | ||
| return str | ||
| .replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) { | ||
| return index === 0 ? word.toLowerCase() : word.toUpperCase(); | ||
| }) | ||
| .replace(/\s+/g, "") | ||
| .replace(/-/g, ""); | ||
| .replace(/\s+/g, "") | ||
| .replace(/-/g, ""); | ||
| } | ||
| exports.camelize = camelize; | ||
| function resolveOutputName(pkgName) { | ||
| if (pkgName.includes("/")) { | ||
| // like '@babel/core', remove '@babel/' | ||
| let t = pkgName.split("/"); | ||
| pkgName = t[t.length - 1]; | ||
| } | ||
| return pkgName; | ||
| if (pkgName.includes("/")) { | ||
| // like '@babel/core', remove '@babel/' | ||
| let t = pkgName.split("/"); | ||
| pkgName = t[t.length - 1]; | ||
| } | ||
| return pkgName; | ||
| } | ||
| exports.resolveOutputName = resolveOutputName; | ||
| function resolveModuleName(pkgName) { | ||
| return camelize(resolveOutputName(pkgName)); | ||
| return camelize(resolveOutputName(pkgName)); | ||
| } | ||
| exports.resolveModuleName = resolveModuleName; |
+4
-5
| { | ||
| "name": "@rollup-use/core", | ||
| "version": "0.0.4", | ||
| "version": "0.0.5", | ||
| "description": "A pure-js npm library bundle tool use rollup. 此工具使用 rollup, 可以打包纯 js 的 npm 库项目.", | ||
@@ -40,8 +40,7 @@ "author": "phphe <phphe@outlook.com>", | ||
| "rollup": "^2.56.1", | ||
| "rollup-plugin-terser": "^7.0.2" | ||
| }, | ||
| "devDependencies": { | ||
| "rollup-plugin-terser": "^7.0.2", | ||
| "ts-node": "^10.2.0", | ||
| "typescript": "^4.3.5" | ||
| }, | ||
| "gitHead": "bb2fedc9857be2b1294b1caf8cd6f6e6cb870047" | ||
| "gitHead": "bc640b0f8a1603c8c150cfd4c96744f947567dbf" | ||
| } |
+2
-4
@@ -22,7 +22,6 @@ # `@rollup-use/core` | ||
| Create directory `scripts`, copy `build.ts` into it. Add 3 lines to `scripts` of `package.json` like follow: | ||
| Copy directory `scripts` into your project. Add 2 npm scripts to `scripts` of `package.json` like follow: | ||
| ```json | ||
| "scripts": { | ||
| "compile-build-lib": "tsc --target ES6 --module CommonJS scripts/build.ts", | ||
| "build-lib": "rollup -c scripts/build.js & node scripts/build.js --report", | ||
@@ -50,7 +49,6 @@ "watch-lib": "rollup -c scripts/build.js --watch" | ||
| 创建目录 `scripts`, 把`build.ts`复制进去. 参照如下, 添加 3 行到`package.json`的`scripts`: | ||
| 复制目录 `scripts` 到你的项目. 参照如下, 添加 3 条 npm script 到`package.json`的`scripts`: | ||
| ```json | ||
| "scripts": { | ||
| "compile-build-lib": "tsc --target ES6 --module CommonJS scripts/build.ts", | ||
| "build-lib": "rollup -c scripts/build.js & node scripts/build.js --report", | ||
@@ -57,0 +55,0 @@ "watch-lib": "rollup -c scripts/build.js --watch" |
+8
-134
@@ -1,135 +0,9 @@ | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| // run npm run compile-build when you changed this file to generate build.js | ||
| const core_1 = require("@rollup-use/core"); | ||
| const path = require("path"); | ||
| const plugin_babel_1 = require("@rollup/plugin-babel"); | ||
| const plugin_node_resolve_1 = require("@rollup/plugin-node-resolve"); | ||
| const rollup_plugin_terser_1 = require("rollup-plugin-terser"); // to minify bundle | ||
| // don't convert follow to imponst xx from 'xx' | ||
| const cjs = require("@rollup/plugin-commonjs"); | ||
| const json = require("@rollup/plugin-json"); | ||
| // @ts-ignore | ||
| const pkg = require("../package.json"); | ||
| // quick config | ||
| const input = "src/index.js"; | ||
| const outDir = "dist"; | ||
| const outputName = core_1.resolveOutputName(pkg.name); // the built file name is outDir/outputName.format.js. You can modify it. | ||
| const moduleName = core_1.resolveModuleName(pkg.name); // for umd, amd. You can modify it. | ||
| const outputExports = "auto"; // You might get warning 'Mixing named and default exports'. https://rollupjs.org/guide/en/#outputexports | ||
| const external = [...core_1.resolveAllDependencies(pkg)]; | ||
| const umdExternal = [...core_1.resolveUMDDependencies(pkg)]; // umd should bundle dependencies | ||
| const getBabelConfig = () => ({ | ||
| // .babelrc | ||
| presets: [ | ||
| [ | ||
| "@babel/preset-env", | ||
| { | ||
| useBuiltIns: false, | ||
| targets: "defaults", // default browsers, coverage 90% | ||
| }, | ||
| ], | ||
| ], | ||
| plugins: [ | ||
| "@babel/plugin-transform-runtime", | ||
| // Stage 1 | ||
| "@babel/plugin-proposal-optional-chaining", | ||
| // Stage 2 | ||
| "@babel/plugin-proposal-export-namespace-from", | ||
| // Stage 3 | ||
| "@babel/plugin-syntax-dynamic-import", | ||
| "@babel/plugin-syntax-import-meta", | ||
| "@babel/plugin-proposal-class-properties", | ||
| "@babel/plugin-proposal-json-strings", | ||
| ], | ||
| assumptions: { | ||
| /** | ||
| * When true, class properties are compiled to use an assignment expression instead of Object.defineProperty. Check: https://babeljs.io/docs/en/babel-plugin-proposal-class-properties#options | ||
| * 当设置为 true 时,类属性将被编译为赋值表达式而不是 Object.defineProperty。参考: https://babel.docschina.org/docs/en/babel-plugin-proposal-class-properties/#%E9%80%89%E9%A1%B9 | ||
| */ | ||
| // "setPublicClassFields": true | ||
| }, | ||
| // for rollup babel plugin | ||
| babelHelpers: "runtime", | ||
| exclude: [/@babel\/runtime/, /@babel\\runtime/, /regenerator-runtime/], | ||
| extensions: [".js", ".jsx", ".es6", ".es", ".mjs", ".vue", ".ts", ".tsx"], | ||
| babelrc: false, | ||
| require("ts-node").register({ | ||
| transpileOnly: true, | ||
| compilerOptions: { | ||
| module: "CommonJS", | ||
| allowJs: false, | ||
| }, | ||
| }); | ||
| const esmBabelConfig = getBabelConfig(); | ||
| const cjsBabelConfig = getBabelConfig(); | ||
| cjsBabelConfig.plugins.push(["module-extension", { mjs: "js" }]); // replace .mjs to .js | ||
| const umdBabelConfig = getBabelConfig(); | ||
| exports.default = [ | ||
| // esm | ||
| { | ||
| input, | ||
| external: (source) => core_1.belongsTo(source, external), | ||
| plugins: [plugin_babel_1.default(esmBabelConfig), plugin_node_resolve_1.default(), cjs(), json()], | ||
| output: { | ||
| file: path.resolve(outDir, `${outputName}.esm.js`), | ||
| format: "esm", | ||
| banner: getBanner(pkg), | ||
| sourcemap: false, | ||
| exports: outputExports, | ||
| }, | ||
| }, | ||
| // cjs | ||
| { | ||
| input, | ||
| external: (source) => core_1.belongsTo(source, external), | ||
| plugins: [plugin_babel_1.default(cjsBabelConfig), plugin_node_resolve_1.default(), cjs(), json()], | ||
| output: { | ||
| file: path.resolve(outDir, `${outputName}.cjs.js`), | ||
| format: "cjs", | ||
| banner: getBanner(pkg), | ||
| sourcemap: false, | ||
| exports: outputExports, | ||
| }, | ||
| }, | ||
| // umd | ||
| { | ||
| input, | ||
| external: (source) => core_1.belongsTo(source, umdExternal), | ||
| plugins: [plugin_babel_1.default(umdBabelConfig), plugin_node_resolve_1.default(), cjs(), json()], | ||
| output: { | ||
| file: path.resolve(outDir, `${outputName}.js`), | ||
| format: "umd", | ||
| banner: getBanner(pkg), | ||
| sourcemap: false, | ||
| name: moduleName, | ||
| exports: outputExports, | ||
| }, | ||
| }, | ||
| // umd min | ||
| { | ||
| input, | ||
| external: (source) => core_1.belongsTo(source, umdExternal), | ||
| plugins: [ | ||
| plugin_babel_1.default(umdBabelConfig), | ||
| plugin_node_resolve_1.default(), | ||
| cjs(), | ||
| json(), | ||
| rollup_plugin_terser_1.terser(), // to minify bundle | ||
| ], | ||
| output: { | ||
| file: path.resolve(outDir, `${outputName}.min.js`), | ||
| format: "umd", | ||
| banner: getBanner(pkg), | ||
| sourcemap: false, | ||
| name: moduleName, | ||
| exports: outputExports, | ||
| }, | ||
| }, | ||
| ]; | ||
| if (process.argv.includes("--report")) { | ||
| core_1.report(outDir); | ||
| } | ||
| function getBanner(pkg) { | ||
| return ` | ||
| /*! | ||
| * ${pkg.name} v${pkg.version} | ||
| * (c) ${pkg.author} | ||
| * Homepage: ${pkg.homepage || null} | ||
| * Released under the ${pkg.license} License. | ||
| */`.trim(); | ||
| } | ||
| module.exports = require("./build.ts"); |
0
-100%13829
-27.9%22
10%301
-29.84%62
-3.12%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added