Skip to content

Commit

Permalink
bugfix: The credits was shown in so many times for each bundle, now i…
Browse files Browse the repository at this point in the history
…t is only shown once
  • Loading branch information
klich3 committed Oct 10, 2022
1 parent 471f00a commit aacbfad
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 41 deletions.
1 change: 1 addition & 0 deletions _test_project/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<title>test</title>
<script type="module" crossorigin src="/src/assets/js/index.js"></script>
<link rel="modulepreload" crossorigin href="/src/assets/js/vendor.d6c9cb9d.js">
</head>

<body>
Expand Down
4 changes: 2 additions & 2 deletions _test_project/dist/src/assets/js/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions _test_project/dist/src/assets/js/vendor.d6c9cb9d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class o{constructor(){}log(){console.log("ok test.js")}}console.log("Main.js content");const s=new o;s.log();
9 changes: 9 additions & 0 deletions _test_project/src/lib/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Test {
constructor() {}

log() {
console.log("ok test.js");
}
}

export default Test;
7 changes: 6 additions & 1 deletion _test_project/src/main.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
console.log("Main.js content")
import Test from "./lib/test";

console.log("Main.js content");

const test = new Test();
test.log();
10 changes: 4 additions & 6 deletions _test_project/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ export default defineConfig({
chunkFileNames: "src/assets/js/[name].[hash].js",
entryFileNames: "src/assets/js/[name].js",
manualChunks: (id) => {
if (id.includes("node_modules"))
return id
.toString()
.split("node_modules/")[1]
.split("/")[0]
.toString();
//added for separate scripts
if (id.includes("node_modules") || id.includes("src")) {
return "vendor";
}
},
},
},
Expand Down
64 changes: 33 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,48 @@ module.exports = function (options = {}) {
return {
name: "credits-log",

banner() {
const pkg = require(resolve(".", "package.json"));
async renderChunk(code, chunk) {
if (chunk.isEntry) {
const pkg = require(resolve(".", "package.json"));

if (!pkg.credits) {
console.log(
"No credits array found in your 'pkg.json' file. Run command 'credits-log help'."
);
return;
}
if (!pkg.credits) {
console.log(
"No credits array found in your 'pkg.json' file. Run command 'credits-log help'."
);
return;
}

const cl_data = pkg.credits.join("");
let authorEntry = `.replace("%a", "")`;
let contributorsEntry = `.replace("%c", "")`;
let licenseEntry = `.replace("%l", "")`;
const version = `.replace("%b", "Build: ${pkg.version}")`;
const cl_data = pkg.credits.join("");
let authorEntry = `.replace("%a", "")`;
let contributorsEntry = `.replace("%c", "")`;
let licenseEntry = `.replace("%l", "")`;
const version = `.replace("%b", "Build: ${pkg.version}")`;

const name = pkg.projectName || pkg.name;
let projectName = `.replace("%p", "Project: ${name}")`;
const name = pkg.projectName || pkg.name;
let projectName = `.replace("%p", "Project: ${name}")`;

if (pkg.author) {
const author = /string/gim.test(typeof pkg.author)
? "\\t" + pkg.author
: "\\t" + pkg.author.join("\\n");
authorEntry = `.replace("%a", "Author: \\n${author}")`;
}
if (pkg.author) {
const author = /string/gim.test(typeof pkg.author)
? "\\t" + pkg.author
: "\\t" + pkg.author.join("\\n");
authorEntry = `.replace("%a", "Author: \\n${author}")`;
}

if (pkg.contributors) {
const contributors = /string/gim.test(typeof pkg.contributors)
? "\\t" + pkg.contributors
: "\\t" + pkg.contributors.join("\\n\\t");
if (pkg.contributors) {
const contributors = /string/gim.test(typeof pkg.contributors)
? "\\t" + pkg.contributors
: "\\t" + pkg.contributors.join("\\n\\t");

contributorsEntry = `.replace("%c", "Contributors: \\n${contributors}")`;
}
contributorsEntry = `.replace("%c", "Contributors: \\n${contributors}")`;
}

if (pkg.license)
licenseEntry = `.replace("%l", "License: ${pkg.license}")`;
if (pkg.license)
licenseEntry = `.replace("%l", "License: ${pkg.license}")`;

const banner = String.raw`(function(){const d="${cl_data}";const c=['color:#808080;font-size:12px;font-family:"Helvetica Light", "Helvetica",Arial,sans-serif;font-weight:lighter;', d.match(new RegExp(".{1,4}", "g")).map((r) => String.fromCodePoint(r)).join("")${projectName}${authorEntry}${contributorsEntry}${licenseEntry}${version}];console.info("%c "+c[1],c[0])})()`;
const resultOut = String.raw`(function(){const d="${cl_data}";const c=['color:#808080;font-size:12px;font-family:"Helvetica Light", "Helvetica",Arial,sans-serif;font-weight:lighter;', d.match(new RegExp(".{1,4}", "g")).map((r) => String.fromCodePoint(r)).join("")${projectName}${authorEntry}${contributorsEntry}${licenseEntry}${version}];console.info("%c "+c[1],c[0])})();`;

return banner;
return `${resultOut}${code}`;
}
},

buildEnd() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "credits-log",
"version": "1.0.1",
"version": "1.0.2",
"description": "Rollup plugin to add credits message on debug console",
"main": "index.js",
"type": "commonjs",
Expand Down

0 comments on commit aacbfad

Please sign in to comment.