@lspriv/plugin-taro
Version:
小程序日历组件Taro插件
223 lines • 10.1 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const fs_extra_1 = require("fs-extra");
const utils_1 = require("./utils");
require("colors");
const TPL_RLT_DIR = '.lspriv';
const PKG_JSON = 'package.json';
const VITE_PLUGIN_NAME = '@lspriv/plugin-taro:vite';
const WEBPACK_PLUGIN_NAME = '@lspriv/plugin-taro:webpack';
const MNP_API_TYPINGS = 'miniprogram-api-typings';
const createDependencies = (pkg, plugins) => {
const deps = Object.assign(Object.assign({}, pkg.dependencies), pkg.devDependencies);
return [utils_1.WX_CALENDAR_PKG_NAME, ...plugins].reduce((dependencies, name) => {
return Object.assign(Object.assign({}, dependencies), { [name]: deps[name] || utils_1.PKG_LATEST_VERSION });
}, {});
};
const handleTplBundleChunks = (bundle, tplDir) => {
const keys = Object.keys(bundle);
let i = keys.length;
while (i--) {
const key = keys[i];
const filename = bundle[key].fileName || key;
if (filename.startsWith(tplDir)) {
delete bundle[key];
}
}
};
const improveTypeDeclare = (wxCalJsonPathsAddon) => {
const tsconfigPath = (0, path_1.resolve)(process.cwd(), 'tsconfig.json');
if ((0, fs_extra_1.pathExistsSync)(tsconfigPath)) {
const content = (0, utils_1.removeComments)((0, fs_extra_1.readFileSync)(tsconfigPath, 'utf8'));
const tsconfig = JSON.parse(content);
wxCalJsonPathsAddon(tsconfig);
(0, fs_extra_1.outputFileSync)(tsconfigPath, (0, utils_1.formatJson)(tsconfig), 'utf8');
}
else {
const jsconfigPath = (0, path_1.resolve)(process.cwd(), 'jsconfig.json');
let jsconfig = {};
if ((0, fs_extra_1.pathExistsSync)(jsconfigPath)) {
const content = (0, utils_1.removeComments)((0, fs_extra_1.readFileSync)(jsconfigPath, 'utf8'));
jsconfig = JSON.parse(content);
}
wxCalJsonPathsAddon(jsconfig);
(0, fs_extra_1.outputFileSync)(jsconfigPath, (0, utils_1.formatJson)(jsconfig), 'utf8');
}
};
const resolveMnpApiTypings = (npm, installer) => {
if ((0, fs_extra_1.pathExistsSync)((0, path_1.resolve)(process.cwd(), npm, MNP_API_TYPINGS)))
return;
installer(`${MNP_API_TYPINGS}@^3.12.0`, ['-D']);
};
const checkMnpBuilding = (output) => {
const mnp_calendar_path = (0, path_1.resolve)(output, 'miniprogram_npm', utils_1.WX_CALENDAR_PKG_NAME);
return (0, fs_extra_1.pathExistsSync)(mnp_calendar_path);
};
const createLog = (tag) => (spinner, message, type = 'succeed') => {
const color = type === 'succeed' ? 'green' : type === 'start' ? 'blue' : 'red';
spinner[type](`[${tag}] `.grey + message[color]);
};
/**
* 编译过程扩展
*/
exports.default = (ctx, opts = {}) => {
var _a;
const ora = require('ora');
const spinner = ora();
const log = createLog(require(`../${PKG_JSON}`).name);
if (process.env.TARO_ENV !== 'weapp') {
log(spinner, '日历组件目前仅支持微信小程序', 'fail');
return;
}
opts.resolveMnpApiTypings = (_a = opts.resolveMnpApiTypings) !== null && _a !== void 0 ? _a : true;
const output = ctx.paths.outputPath;
const { NPM_DIR, NODE_MODULES } = ctx.helper;
const TPL_NPM_DIR = `${NPM_DIR}/${TPL_RLT_DIR}`;
const PKG_TPL_DIR = `${NODE_MODULES}/${TPL_RLT_DIR}`;
const cli = (0, utils_1.createCli)(output);
let installer;
try {
if (opts.pkgManager) {
installer = utils_1.PKG_INSTALLER[opts.pkgManager];
if (!installer)
throw new Error('不支持的包管理工具,仅支持 npm | cnpm | pnpm');
}
else {
installer = (0, utils_1.getDefaultPmInstaller)();
}
}
catch (e) {
log(spinner, e.message, 'fail');
return;
}
const tplPkgDir = (0, path_1.resolve)(process.cwd(), PKG_TPL_DIR, utils_1.WX_CALENDAR_PKG_NAME);
const compEntryPath = (0, path_1.resolve)(tplPkgDir, 'index.js');
const compWxmlPath = (0, path_1.resolve)(tplPkgDir, 'index.wxml');
const compJsonPath = (0, path_1.resolve)(tplPkgDir, 'index.json');
const tplRegexp = new RegExp(`("|'|\`)([^"'\`/]*/)*(${NODE_MODULES}|${NPM_DIR})/${TPL_RLT_DIR}/${utils_1.WX_CALENDAR_PKG_NAME}(/[^"'\`]*)?("|'|\`)?`);
ctx.modifyRunnerOpts(({ opts }) => {
opts.alias = opts.alias || {};
opts.alias[utils_1.WX_CALENDAR_PKG_NAME] = tplPkgDir;
});
ctx.onBuildStart(() => {
try {
improveTypeDeclare(json => {
var _a;
opts.resolveMnpApiTypings && resolveMnpApiTypings(NODE_MODULES, installer.bind(null, process.cwd()));
const compilerOptions = json.compilerOptions || {};
compilerOptions.paths = compilerOptions.paths || {};
compilerOptions.paths[`${utils_1.WX_CALENDAR_PKG_NAME}/*`] = [`./${NODE_MODULES}/${utils_1.WX_CALENDAR_PKG_NAME}/dist/*`];
json.compilerOptions = compilerOptions;
if (opts.resolveMnpApiTypings && !((_a = json.files) === null || _a === void 0 ? void 0 : _a.find(item => item.includes(MNP_API_TYPINGS)))) {
const files = json.files || [];
files.push(`${NODE_MODULES}/${MNP_API_TYPINGS}/index.d.ts`);
json.files = files;
}
});
(0, fs_extra_1.outputFileSync)(compJsonPath, (0, utils_1.createCompJson)(), 'utf8');
(0, fs_extra_1.outputFileSync)(compEntryPath, utils_1.TPL_COMP_CONTENT, 'utf8');
(0, fs_extra_1.outputFileSync)(compWxmlPath, utils_1.TPL_COMP_WXML, 'utf8');
}
catch (e) {
console.log(e);
}
});
ctx.modifyViteConfig(({ viteConfig }) => {
var _a;
const build = viteConfig.build || {};
build.rollupOptions = Object.assign(Object.assign({}, build.rollupOptions), { external: [...(((_a = build.rollupOptions) === null || _a === void 0 ? void 0 : _a.external) || []), /^@lspriv\//] });
viteConfig.build = build;
viteConfig.plugins.push({
name: VITE_PLUGIN_NAME,
generateBundle: (_, bundle) => {
handleTplBundleChunks(bundle, TPL_NPM_DIR);
}
});
});
ctx.modifyWebpackChain(({ chain }) => {
var _a;
const WX_CAL_LIB = `${utils_1.WX_CALENDAR_PKG_NAME}/lib`;
chain.externals(Object.assign({ [WX_CAL_LIB]: `commonjs ${WX_CAL_LIB}` }, (_a = opts.plugins) === null || _a === void 0 ? void 0 : _a.reduce((modules, module) => {
modules[module] = `commonjs ${module}`;
return modules;
}, {})));
chain.plugin(WEBPACK_PLUGIN_NAME).use(class {
apply(compiler) {
compiler.hooks.thisCompilation.tap(WEBPACK_PLUGIN_NAME, compilation => {
compilation.hooks.processAssets.tap({
name: WEBPACK_PLUGIN_NAME,
stage: compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
}, assets => {
handleTplBundleChunks(assets, TPL_NPM_DIR);
});
});
}
});
});
ctx.modifyBuildAssets(({ assets }) => {
const keys = Object.keys(assets);
keys.forEach(key => {
const ext = (0, path_1.extname)(key);
if (ext === '.json' || ext === '.js') {
const content = assets[key].source();
if (!content)
return;
if (tplRegexp.test(content)) {
assets[key] = {
source: () => content.replace(new RegExp(tplRegexp, 'g'), `$1${utils_1.WX_CALENDAR_PKG_NAME}$1`)
};
}
}
});
const pkgConfig = (0, utils_1.getPkgConfig)();
const dependencies = createDependencies(pkgConfig, opts.plugins || []);
if (assets[PKG_JSON]) {
const content = assets[PKG_JSON].source();
const pkgJson = content ? JSON.parse(content) : {};
pkgJson.dependencies = Object.assign(Object.assign({}, pkgJson.dependencies), dependencies);
assets[PKG_JSON] = {
source: () => (0, utils_1.formatJson)(pkgJson)
};
}
else {
const prjConfig = (0, utils_1.getPrjConfig)();
const pkgJson = {};
pkgJson.name = prjConfig.projectname || pkgConfig.name;
pkgJson.version = pkgConfig.version;
pkgJson.author = pkgConfig.author || '';
pkgJson.description = prjConfig.description || pkgConfig.description;
pkgJson.dependencies = dependencies;
ctx.writeFileToDist({ filePath: PKG_JSON, content: (0, utils_1.formatJson)(pkgJson) });
}
});
ctx.onBuildFinish(async () => {
if (checkMnpBuilding(output))
return;
try {
log(spinner, 'building npm', 'start');
installer.call(null, output);
cli('build-npm --compile-type miniprogram', { stdio: 'ignore', withPrj: true, cli: opts.cli });
log(spinner, 'building npm completed', 'succeed');
}
catch (e) {
log(spinner, e.message, 'fail');
}
});
};
__exportStar(require("./element"), exports);
//# sourceMappingURL=index.js.map