Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add GitHub Actions workflow for NPM package publishing and update bui…
…ld process
  • Loading branch information
sy-records committed Feb 2, 2025
commit 9327263206295e526f763c035f2bd4d9683de279
14 changes: 14 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Publish NPM Package to GitHub Packages and npm

on:
push:
branches:
- ci
tags:
- 'v*.*.*'

jobs:
publish:
if: github.repository_owner == 'sy-records'
uses: sy-records/.github/.github/workflows/npm-publish.yml@main
secrets: inherit
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules/
.idea/
lib/
dist/
package-lock.json
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
build/
.idea/
src/
124 changes: 45 additions & 79 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,54 @@
const rollup = require('rollup')
const buble = require('rollup-plugin-buble')
const commonjs = require('rollup-plugin-commonjs')
const nodeResolve = require('rollup-plugin-node-resolve')
const { uglify } = require('rollup-plugin-uglify')
const replace = require('rollup-plugin-replace')
const isProd = process.env.NODE_ENV === 'production'
const version = process.env.VERSION || require('../package.json').version
const fs = require('fs');
const path = require('path');
const { minify } = require('terser');

const rootDir = path.resolve(__dirname, '..');
const inputFile = path.join(rootDir, 'src', 'index.js');
const distDir = path.join(rootDir, 'dist');
const outputFile = path.join(distDir, 'index.js');
const outputMinFile = path.join(distDir, 'index.min.js');
const outputMinMapFile = path.join(distDir, 'index.min.js.map');

function cleanDist() {
if (fs.existsSync(distDir)) {
fs.rmSync(distDir, { recursive: true, force: true });
console.log('🧹 Cleaned up dist directory');
}
fs.mkdirSync(distDir, { recursive: true });
}

/**
* @param {{
* input: string,
* output?: string,
* globalName?: string,
* plugins?: Array<import('rollup').Plugin>
* }} opts
*/
async function build(opts) {
await rollup
.rollup({
input: opts.input,
plugins: (opts.plugins || []).concat([
buble(),
commonjs(),
nodeResolve(),
replace({
__VERSION__: version
})
]),
onwarn: function (message) {
if (message.code === 'UNRESOLVED_IMPORT') {
throw new Error(
`Could not resolve module ` +
message.source +
`. Try running 'npm install' or using rollup's 'external' option if this is an external dependency. ` +
`Module ${message.source} is imported in ${message.importer}`
)
}
}
})
.then(function (bundle) {
var dest = 'lib/' + (opts.output || opts.input)
async function build() {
cleanDist();

console.log(dest)
return bundle.write({
format: 'iife',
output: opts.globalName ? {name: opts.globalName} : {},
file: dest,
strict: false
})
})
}
if (!fs.existsSync(inputFile)) {
console.error(`❌ Source file not found: ${inputFile}`);
return;
}

async function buildPlugin() {
var plugins = [
{name: 'index', input: 'index.js'},
]
let sourceCode = fs.readFileSync(inputFile, 'utf-8');

const promises = plugins.map(item => {
return build({
input: 'src/' + item.input,
output: item.name + '.js'
})
})
const wrappedCode = `(function () {\n${sourceCode}\n})();`;
fs.writeFileSync(outputFile, wrappedCode, 'utf-8');
console.log('✅ Generated non-minified version (IIFE): dist/index.js');

if (isProd) {
plugins.forEach(item => {
promises.push(build({
input: 'src/' + item.input,
output: item.name + '.min.js',
plugins: [uglify()]
}))
})
}
try {
const minified = await minify(sourceCode, {
compress: true,
mangle: true,
sourceMap: {
filename: 'index.min.js',
url: 'index.min.js.map'
}
});

await Promise.all(promises)
}
fs.writeFileSync(outputMinFile, minified.code, 'utf-8');
console.log('✅ Generated minified version: dist/index.min.js');

async function main() {
await Promise.all([
buildPlugin()
])
fs.writeFileSync(outputMinMapFile, minified.map, 'utf-8');
console.log('✅ Generated minified source map: dist/index.min.js.map');
} catch (err) {
console.error('❌ Error during minification:', err);
}
}

main().catch((e) => {
console.error(e)
process.exit(1)
})
build();
15 changes: 4 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@sy-records/docsify-loading",
"version": "1.0.0",
"version": "1.1.0",
"description": "A plugin that supports the use of loading in docsify.",
"main": "src/index.js",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/sy-records/docsify-loading.git"
Expand All @@ -20,16 +20,9 @@
},
"homepage": "https://github.com/sy-records/docsify-loading#readme",
"devDependencies": {
"cross-env": "^6.0.3",
"rollup": "^1.23.1",
"rollup-plugin-async": "^1.2.0",
"rollup-plugin-buble": "^0.19.8",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-uglify": "^6.0.4"
"terser": "^5.37.0"
},
"scripts": {
"build": "cross-env NODE_ENV=production node build/build.js"
"build": "node build/build.js"
}
}
19 changes: 9 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const loading = function (hook) {
hook.afterEach(function (html, next) {
html = html.replace(/<img(.*?)>/g, function (match, p1) {
if (p1.indexOf('loading=') === -1) {
return match.replace('>', ' loading="lazy">');
function loading(hook) {
hook.afterEach((html, next) => {
html = html.replace(/<img\s+([^>]*?)>/g, (match, attrs) => {
if (attrs.includes('loading=') || /src=["'][^"']+\.svg["']/.test(attrs)) {
return match;
}
return match;
return `<img ${attrs} loading="lazy">`;
});

next(html);
});
};
}

$docsify = $docsify || {};
$docsify.plugins = [].concat(loading, $docsify.plugins || []);
window.$docsify = window.$docsify || {};
$docsify.plugins = [...($docsify.plugins || []), loading];