Also publish core Node.js package

This commit is contained in:
Wilson Lin 2021-04-06 13:43:55 +10:00
parent 2c76bb3dd8
commit 9edd5cf682
12 changed files with 143 additions and 126 deletions

View File

@ -9,10 +9,10 @@ on:
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-11.0, ubuntu-latest, windows-latest]
feature: [core, js]
include:
- os: macos-11.0
ARCH: macos-x86_64
@ -20,6 +20,7 @@ jobs:
ARCH: linux-x86_64
- os: windows-latest
ARCH: windows-x86_64
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
@ -31,7 +32,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@master
with:
node-version: 14.x
node-version: 15.x
- name: Set up Rust
uses: actions-rs/toolchain@v1
@ -57,6 +58,7 @@ jobs:
working-directory: ./nodejs
shell: bash
run: |
node buildnative.js ${{ matrix.feature }}
npm install
npm run build
node compress.js
@ -69,7 +71,7 @@ jobs:
run: |
binary_name="$(node -e 'console.log([process.platform, process.arch].join("__"))')"
b2 authorize-account ${{ secrets.CICD_CLI_B2_KEY_ID }} ${{ secrets.CICD_CLI_B2_APPLICATION_KEY }}
b2 upload-file ${{ secrets.CICD_CLI_B2_BUCKET_NAME }} ./nodejs/index.node.gz "minify-html/bin/nodejs/${{ steps.version.outputs.VERSION }}/$binary_name.node.gz"
b2 upload-file ${{ secrets.CICD_CLI_B2_BUCKET_NAME }} ./nodejs/index.node.gz "minify-html/bin/nodejs/${{ steps.version.outputs.VERSION }}/${{ matrix.feature }}/$binary_name.node.gz"
package:
runs-on: ubuntu-latest
needs: build
@ -82,7 +84,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@master
with:
node-version: 14.x
node-version: 15.x
- name: Pack and publish package
working-directory: ./nodejs
run: |

3
nodejs/.gitignore vendored
View File

@ -2,5 +2,6 @@
/index.node
/native/Cargo.lock
/native/target
node_modules/
/package-lock.json
/package.json
node_modules/

View File

@ -85,27 +85,19 @@ napi_value node_method_create_configuration(napi_env env, napi_callback_info inf
napi_value obj_arg = argv[0];
// Get `minifyJs` property.
bool minify_js = false;
napi_value minify_js_value;
if (napi_get_named_property(env, obj_arg, "minifyJs", &minify_js_value) != napi_ok) {
assert_ok(napi_throw_type_error(env, NULL, "Failed to get minifyJs property"));
return undefined;
}
bool minify_js;
if (napi_get_value_bool(env, minify_js_value, &minify_js) != napi_ok) {
assert_ok(napi_throw_type_error(env, NULL, "Failed to get minifyJs boolean property"));
return undefined;
if (napi_get_named_property(env, obj_arg, "minifyJs", &minify_js_value) == napi_ok) {
// It's OK if this fails.
napi_get_value_bool(env, minify_js_value, &minify_js);
}
// Get `minifyCss` property.
bool minify_css = false;
napi_value minify_css_value;
if (napi_get_named_property(env, obj_arg, "minifyCss", &minify_css_value) != napi_ok) {
assert_ok(napi_throw_type_error(env, NULL, "Failed to get minifyCss property"));
return undefined;
}
bool minify_css;
if (napi_get_value_bool(env, minify_css_value, &minify_css) != napi_ok) {
assert_ok(napi_throw_type_error(env, NULL, "Failed to get minifyCss boolean property"));
return undefined;
if (napi_get_named_property(env, obj_arg, "minifyCss", &minify_css_value) == napi_ok) {
// It's OK if this fails.
napi_get_value_bool(env, minify_css_value, &minify_css);
}
Cfg const* cfg = ffi_create_cfg(minify_js, minify_css);

View File

@ -15,27 +15,11 @@
],
}],
["OS!='win'", {
"actions": [
{
"action_name": "build minify-html-ffi static library",
"inputs": ["native/src/lib.rs"],
"outputs": ["native/target/release/libminify_html_ffi.a", "native/target/release/minify_html_ffi.h"],
"action": ["cargo", "build", "--manifest-path", "native/Cargo.toml", "--release"],
},
],
"libraries": [
"../native/target/release/libminify_html_ffi.a",
],
}],
["OS=='win'", {
"actions": [
{
"action_name": "build minify-html-ffi static library",
"inputs": ["native/src/lib.rs"],
"outputs": ["native/target/release/minify_html_ffi.lib", "native/target/release/minify_html_ffi.h"],
"action": ["cargo build --manifest-path ../native/Cargo.toml --release"],
},
],
"libraries": [
"advapi32.lib", "ws2_32.lib", "userenv.lib", "msvcrt.lib",
"../native/target/release/minify_html_ffi.lib",

17
nodejs/buildnative.js Normal file
View File

@ -0,0 +1,17 @@
const cp = require("child_process");
const pkg = require("./package.json");
cp.spawnSync(
"cargo",
[
"build",
"--manifest-path",
"native/Cargo.toml",
"--release",
"--features",
pkg.name.split("/")[1],
],
{
stdio: "inherit",
}
);

View File

@ -1,7 +1,7 @@
const {readFileSync, writeFileSync} = require('fs');
const {join} = require('path');
const {gzipSync} = require('zlib');
const { readFileSync, writeFileSync } = require("fs");
const { join } = require("path");
const { gzipSync } = require("zlib");
const src = readFileSync(join(__dirname, 'index.node'));
const src = readFileSync(join(__dirname, "index.node"));
const out = gzipSync(src);
writeFileSync(join(__dirname, 'index.node.gz'), out);
writeFileSync(join(__dirname, "index.node.gz"), out);

4
nodejs/index.d.ts vendored
View File

@ -11,11 +11,11 @@ export function createConfiguration (options: {
/**
* If enabled, content in `<script>` tags with a JS or no [MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) will be minified using [esbuild-rs](https://github.com/wilsonzlin/esbuild-rs).
*/
minifyJs: boolean;
minifyJs?: boolean;
/**
* If enabled, CSS in `<style>` tags will be minified using [esbuild-rs](https://github.com/wilsonzlin/esbuild-rs).
*/
minifyCss: boolean;
minifyCss?: boolean;
}): Cfg;
/**

View File

@ -10,9 +10,13 @@ edition = "2018"
name = "minify_html_ffi"
crate-type = ["staticlib"]
[features]
core = ["minify-html"]
js = ["minify-html/js-esbuild"]
[build-dependencies]
cbindgen = "0.14"
[dependencies]
libc = "0.2"
minify-html = { path = "../..", features = ["js-esbuild"] }
minify-html = { path = "../..", optional = true }

View File

@ -1,51 +0,0 @@
{
"name": "@minify-html/js",
"version": "0.4.3",
"description": "Extremely fast and smart HTML + JS + CSS minifier",
"main": "index.node",
"types": "index.d.ts",
"files": [
"postinstall.js",
"index.d.ts"
],
"scripts": {
"build": "node-gyp build && shx mv build/Release/index.node index.node",
"clean": "cd native && cargo clean && cd .. && node-gyp clean && node-gyp configure && shx rm -f index.node",
"postinstall": "node postinstall.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/wilsonzlin/minify-html.git"
},
"author": {
"email": "npm@wilsonl.in",
"name": "Wilson Lin",
"url": "https://wilsonl.in/"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/wilsonzlin/minify-html/issues"
},
"engines": {
"node": ">= 8.6.0"
},
"homepage": "https://github.com/wilsonzlin/minify-html#readme",
"devDependencies": {
"@types/node": "^14.6.0",
"node-gyp": "^7.0.0",
"shx": "^0.3.2"
},
"keywords": [
"build",
"builder",
"compress",
"compressor",
"fast",
"html",
"minifier",
"minify",
"preprocess",
"preprocessor",
"rust"
]
}

View File

@ -0,0 +1,61 @@
const fs = require("fs");
const common = {
version: "0.4.1",
main: "index.node",
types: "index.d.ts",
files: ["postinstall.js", "index.d.ts"],
scripts: {
build:
"node buildnative.js && node-gyp build && shx mv build/Release/index.node index.node",
clean:
"cd native && cargo clean && cd .. && node-gyp clean && node-gyp configure && shx rm -f index.node",
postinstall: "node postinstall.js",
},
repository: {
type: "git",
url: "git+https://github.com/wilsonzlin/minify-html.git",
},
author: {
email: "npm@wilsonl.in",
name: "Wilson Lin",
url: "https://wilsonl.in/",
},
license: "MIT",
bugs: {
url: "https://github.com/wilsonzlin/minify-html/issues",
},
engines: {
node: ">= 8.6.0",
},
homepage: "https://github.com/wilsonzlin/minify-html#readme",
devDependencies: {
"@types/node": "^14.6.0",
"node-gyp": "^7.0.0",
shx: "^0.3.2",
},
keywords: ["compress", "compressor", "fast", "html", "minifier", "minify"],
};
const specifics = {
core: {
name: "@minify-html/core",
description: "Extremely fast and smart HTML minifier",
},
js: {
name: "@minify-html/js",
description: "Extremely fast and smart HTML + JS + CSS minifier",
},
}[process.argv[2]];
fs.writeFileSync(
"package.json",
JSON.stringify(
{
...common,
...specifics,
},
null,
2
)
);

View File

@ -1,40 +1,45 @@
const fs = require('fs');
const https = require('https');
const path = require('path');
const zlib = require('zlib');
const pkg = require('./package.json');
const fs = require("fs");
const https = require("https");
const path = require("path");
const zlib = require("zlib");
const pkg = require("./package.json");
const MAX_DOWNLOAD_ATTEMPTS = 4;
const binaryName = [process.platform, process.arch].join('__');
const binaryPath = path.join(__dirname, 'index.node');
const binaryName = [process.platform, process.arch].join("__");
const binaryPath = path.join(__dirname, "index.node");
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
class StatusError extends Error {
constructor (status) {
constructor(status) {
super(`Bad status of ${status}`);
this.status = status;
}
}
const fetch = url => new Promise((resolve, reject) => {
const stream = https.get(url, resp => {
if (!resp.statusCode || resp.statusCode < 200 || resp.statusCode > 299) {
return reject(new StatusError(resp.statusCode));
}
const parts = [];
resp.on('data', chunk => parts.push(chunk));
resp.on('end', () => resolve(Buffer.concat(parts)));
const fetch = (url) =>
new Promise((resolve, reject) => {
const stream = https.get(url, (resp) => {
if (!resp.statusCode || resp.statusCode < 200 || resp.statusCode > 299) {
return reject(new StatusError(resp.statusCode));
}
const parts = [];
resp.on("data", (chunk) => parts.push(chunk));
resp.on("end", () => resolve(Buffer.concat(parts)));
});
stream.on("error", reject);
});
stream.on('error', reject);
});
const downloadNativeBinary = async () => {
for (let attempt = 0; ; attempt++) {
let binary;
try {
binary = await fetch(`https://wilsonl.in/minify-html/bin/nodejs/${pkg.version}/${binaryName}.node.gz`);
binary = await fetch(
`https://wilsonl.in/minify-html/bin/nodejs/${
pkg.version
}/${pkg.name.split("/")[1]}/${binaryName}.node.gz`
);
} catch (e) {
if (e instanceof StatusError && attempt < MAX_DOWNLOAD_ATTEMPTS) {
await wait(Math.random() * 2500 + 500);
@ -48,13 +53,15 @@ const downloadNativeBinary = async () => {
}
};
if (!fs.existsSync(path.join(__dirname, '.no-postinstall')) && !fs.existsSync(binaryPath)) {
downloadNativeBinary()
.then(
() => console.log(`Downloaded ${pkg.name}`),
err => {
console.error(`Failed to download ${pkg.name}: ${err}`);
process.exit(1);
},
);
if (
!fs.existsSync(path.join(__dirname, ".no-postinstall")) &&
!fs.existsSync(binaryPath)
) {
downloadNativeBinary().then(
() => console.log(`Downloaded ${pkg.name}`),
(err) => {
console.error(`Failed to download ${pkg.name}: ${err}`);
process.exit(1);
}
);
}

View File

@ -89,7 +89,7 @@ for (const f of ['java/pom.xml', 'README.md']) {
replaceInFile(f, /(<artifactId>minify-html<\/artifactId>\s*<version>)\d+\.\d+\.\d+(<\/version>)/, `$1${NEW_VERSION}$2`);
}
for (const f of ['nodejs/package.json']) {
for (const f of ['nodejs/package.json.gen.js']) {
replaceInFile(f, /^(\s*"version": )"\d+\.\d+\.\d+",\s*$/m, `$1"${NEW_VERSION}",`);
}