diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..5d43f54 --- /dev/null +++ b/.github/workflows/publish.yml @@ -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 diff --git a/.gitignore b/.gitignore index 350e313..a1c568f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ node_modules/ .idea/ -lib/ +dist/ package-lock.json diff --git a/.npmignore b/.npmignore index a83bb87..95464d3 100644 --- a/.npmignore +++ b/.npmignore @@ -1,2 +1,2 @@ build/ -.idea/ +src/ diff --git a/build/build.js b/build/build.js index afca604..bd05fec 100644 --- a/build/build.js +++ b/build/build.js @@ -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 - * }} 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(); diff --git a/package.json b/package.json index f227924..cad3c9f 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" } } diff --git a/src/index.js b/src/index.js index dc2f7df..1e79a8f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,15 +1,14 @@ -const loading = function (hook) { - hook.afterEach(function (html, next) { - html = html.replace(//g, function (match, p1) { - if (p1.indexOf('loading=') === -1) { - return match.replace('>', ' loading="lazy">'); +function loading(hook) { + hook.afterEach((html, next) => { + html = html.replace(/]*?)>/g, (match, attrs) => { + if (attrs.includes('loading=') || /src=["'][^"']+\.svg["']/.test(attrs)) { + return match; } - return match; + return ``; }); - next(html); }); -}; +} -$docsify = $docsify || {}; -$docsify.plugins = [].concat(loading, $docsify.plugins || []); +window.$docsify = window.$docsify || {}; +$docsify.plugins = [...($docsify.plugins || []), loading];