打包支持

This commit is contained in:
iqudoo
2026-04-30 15:47:30 +08:00
parent 51738b1936
commit 21ae621c6e
9 changed files with 51 additions and 59 deletions

4
dist/index.d.ts vendored
View File

@@ -22,10 +22,6 @@ interface ScanConfigOptions {
* 网页扫码成功提示音是否启用,默认启用 * 网页扫码成功提示音是否启用,默认启用
*/ */
webScanBeepEnabled?: boolean, webScanBeepEnabled?: boolean,
/**
* BarcodeDetector polyfill 地址,默认使用 jsDelivr CDN
*/
webBarcodeDetectorPolyfillUrl?: string,
/** /**
* 微信JSSDK配置微信环境才会生效配置后会自动初始化微信JSSDK * 微信JSSDK配置微信环境才会生效配置后会自动初始化微信JSSDK
*/ */

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

BIN
dist/lib/reader.wasm vendored Normal file

Binary file not shown.

View File

@@ -16,36 +16,38 @@
"types" "types"
], ],
"devDependencies": { "devDependencies": {
"barcode-detector": "^3.0.8",
"autoprefixer": "^7.2.3", "autoprefixer": "^7.2.3",
"axios": "0.21.1", "axios": "0.21.1",
"babel-core": "^6.25.0", "babel-core": "^6.25.0",
"babel-loader": "^7.1.1", "babel-loader": "^7.1.1",
"babel-plugin-transform-class-properties": "^6.24.1", "babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.0", "babel-preset-env": "^1.6.0",
"barcode-detector": "^3.0.8",
"browserify-cipher": "1.0.1",
"clean-webpack-plugin": "3.0.0", "clean-webpack-plugin": "3.0.0",
"concurrently": "^3.5.0", "concurrently": "^3.5.0",
"cp-webpack-plugin": "^1.0.0", "cp-webpack-plugin": "^1.0.0",
"html-webpack-plugin": "4.5.2",
"cross-env": "5.2.1", "cross-env": "5.2.1",
"css-loader": "^0.28.4", "css-loader": "^0.28.4",
"es6-promise": "4.2.6", "es6-promise": "4.2.6",
"exports-loader": "^0.6.4", "exports-loader": "^0.6.4",
"file-loader": "^0.11.2", "file-loader": "^0.11.2",
"file-saver": "^1.3.3", "file-saver": "^1.3.3",
"html-webpack-plugin": "4.5.2",
"http-server": "0.11.1", "http-server": "0.11.1",
"imports-loader": "^0.7.1", "imports-loader": "^0.7.1",
"postcss-loader": "^2.0.6", "postcss-loader": "^2.0.6",
"quagga": "0.12.1",
"str-webpack-plugin": "1.0.0", "str-webpack-plugin": "1.0.0",
"style-loader": "^0.18.2", "style-loader": "^0.18.2",
"terser-webpack-plugin": "1.4.6",
"uglifyjs-webpack-plugin": "^1.2.2", "uglifyjs-webpack-plugin": "^1.2.2",
"url-loader": "^0.5.9", "url-loader": "^0.5.9",
"vconsole": "3.3.0", "vconsole": "3.3.0",
"quagga": "0.12.1",
"browserify-cipher": "1.0.1",
"webpack": "^4.12.0", "webpack": "^4.12.0",
"webpack-bundle-analyzer": "3.6.0", "webpack-bundle-analyzer": "3.6.0",
"webpack-cli": "^3.3.9" "webpack-cli": "^3.3.9",
"zxing-wasm": "3.0.2"
}, },
"repository": {}, "repository": {},
"author": "", "author": "",

6
pnpm-lock.yaml generated
View File

@@ -80,6 +80,9 @@ importers:
style-loader: style-loader:
specifier: ^0.18.2 specifier: ^0.18.2
version: 0.18.2 version: 0.18.2
terser-webpack-plugin:
specifier: 1.4.6
version: 1.4.6(webpack@4.47.0)
uglifyjs-webpack-plugin: uglifyjs-webpack-plugin:
specifier: ^1.2.2 specifier: ^1.2.2
version: 1.3.0(webpack@4.47.0) version: 1.3.0(webpack@4.47.0)
@@ -98,6 +101,9 @@ importers:
webpack-cli: webpack-cli:
specifier: ^3.3.9 specifier: ^3.3.9
version: 3.3.12(webpack@4.47.0) version: 3.3.12(webpack@4.47.0)
zxing-wasm:
specifier: 3.0.2
version: 3.0.2(@types/emscripten@1.41.5)
packages: packages:

View File

@@ -1,3 +1,4 @@
import { BarcodeDetector as BarcodeDetectorPonyfill, prepareZXingModule } from "barcode-detector/dist/cjs/index.js";
import { createUUID } from "../../utils/uuid"; import { createUUID } from "../../utils/uuid";
import { getConfig } from "../config"; import { getConfig } from "../config";
import scanBeepAudio from "../../../res/scan_beep.ogg"; import scanBeepAudio from "../../../res/scan_beep.ogg";
@@ -8,9 +9,9 @@ const scanWeb = {
} }
const DEFAULT_SCAN_BEEP_AUDIO = scanBeepAudio; const DEFAULT_SCAN_BEEP_AUDIO = scanBeepAudio;
const BARCODE_DETECTOR_POLYFILL_URL = "https://fastly.jsdelivr.net/npm/barcode-detector@3/dist/iife/index.min.js"; const ZXING_READER_WASM_URL = "./lib/reader.wasm";
let barcodeDetectorPolyfillPromise = null; let barcodeDetectorPreparePromise = null;
function removeEl(id) { function removeEl(id) {
try { try {
@@ -70,48 +71,32 @@ function getBarcodeDetectorClass() {
if (typeof BarcodeDetector !== 'undefined') { if (typeof BarcodeDetector !== 'undefined') {
return BarcodeDetector; return BarcodeDetector;
} }
if (typeof window !== 'undefined' return BarcodeDetectorPonyfill;
&& window.BarcodeDetectionAPI
&& window.BarcodeDetectionAPI.BarcodeDetector) {
return window.BarcodeDetectionAPI.BarcodeDetector;
}
return null;
} }
function loadBarcodeDetectorPolyfill() { function prepareBarcodeDetector() {
if (getBarcodeDetectorClass()) { const BarcodeDetectorClass = getBarcodeDetectorClass();
return Promise.resolve(getBarcodeDetectorClass()); if (typeof BarcodeDetector !== 'undefined' || !prepareZXingModule) {
return Promise.resolve(BarcodeDetectorClass);
} }
if (barcodeDetectorPolyfillPromise) { if (!barcodeDetectorPreparePromise) {
return barcodeDetectorPolyfillPromise; prepareZXingModule({
} overrides: {
barcodeDetectorPolyfillPromise = new Promise((resolve, reject) => { locateFile: path => {
if (typeof document === 'undefined') { if (path && path.indexOf(".wasm") !== -1) {
reject(new Error("BarcodeDetector is not supported")); return ZXING_READER_WASM_URL;
return; }
} return path;
const scriptUrl = getConfig("webBarcodeDetectorPolyfillUrl") || BARCODE_DETECTOR_POLYFILL_URL; }
const script = document.createElement("script");
script.src = scriptUrl;
script.onload = () => {
const BarcodeDetectorClass = getBarcodeDetectorClass();
if (BarcodeDetectorClass) {
resolve(BarcodeDetectorClass);
} else {
reject(new Error("BarcodeDetector polyfill is not ready"));
} }
}; });
script.onerror = reject; barcodeDetectorPreparePromise = Promise.resolve(BarcodeDetectorClass);
document.head.appendChild(script); }
}).catch(err => { return barcodeDetectorPreparePromise;
barcodeDetectorPolyfillPromise = null;
throw err;
});
return barcodeDetectorPolyfillPromise;
} }
function createBarcodeDetector(scanType) { function createBarcodeDetector(scanType) {
return loadBarcodeDetectorPolyfill().then(BarcodeDetectorClass => { return prepareBarcodeDetector().then(BarcodeDetectorClass => {
if (!BarcodeDetectorClass) { if (!BarcodeDetectorClass) {
throw new Error("BarcodeDetector is not supported"); throw new Error("BarcodeDetector is not supported");
} }

4
types/index.d.ts vendored
View File

@@ -22,10 +22,6 @@ interface ScanConfigOptions {
* 网页扫码成功提示音是否启用,默认启用 * 网页扫码成功提示音是否启用,默认启用
*/ */
webScanBeepEnabled?: boolean, webScanBeepEnabled?: boolean,
/**
* BarcodeDetector polyfill 地址,默认使用 jsDelivr CDN
*/
webBarcodeDetectorPolyfillUrl?: string,
/** /**
* 微信JSSDK配置微信环境才会生效配置后会自动初始化微信JSSDK * 微信JSSDK配置微信环境才会生效配置后会自动初始化微信JSSDK
*/ */

View File

@@ -3,6 +3,7 @@ const rules = require('./webpack.rules.js');
const HtmlWebpackPlugin = require('html-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin');
const CpWebpackPlugin = require('cp-webpack-plugin'); const CpWebpackPlugin = require('cp-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const zxingReaderWasm = require.resolve('zxing-wasm/reader/zxing_reader.wasm');
const output = path.resolve('./dist'); const output = path.resolve('./dist');
@@ -29,6 +30,7 @@ const config = {
}), }),
new CpWebpackPlugin([ new CpWebpackPlugin([
{ from: path.resolve('./types'), to: path.resolve('./dist') }, { from: path.resolve('./types'), to: path.resolve('./dist') },
{ from: zxingReaderWasm, to: path.resolve('./dist/lib/reader.wasm') },
]) ])
], ],
devtool: 'cheap-module-souce-map', devtool: 'cheap-module-souce-map',

View File

@@ -1,17 +1,22 @@
const baseConfig = require('./webpack.base.js'); const baseConfig = require('./webpack.base.js');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); const TerserPlugin = require('terser-webpack-plugin');
const { EnvWebPackage } = require('./webpack.plugin.js'); const { EnvWebPackage } = require('./webpack.plugin.js');
module.exports = Object.assign({}, baseConfig, { module.exports = Object.assign({}, baseConfig, {
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
output: {
comments: false
}
},
extractComments: false
})
]
},
plugins: [ plugins: [
...baseConfig.plugins, ...baseConfig.plugins,
new UglifyJsPlugin({
uglifyOptions: {
output: {
comments: false
}
}
}),
new EnvWebPackage({ new EnvWebPackage({
entry: baseConfig.entry, entry: baseConfig.entry,
output: baseConfig.output.path, output: baseConfig.output.path,