UNPKG

@sanpjs/bundler-webpack

Version:

@sanpjs/bundler-webpack

86 lines 3.75 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * @file 检查产出是否包含es6代码 */ const path_1 = __importDefault(require("path")); const lodash_uniqBy_1 = __importDefault(require("lodash.uniqBy")); const lodash_compact_1 = __importDefault(require("lodash.compact")); const fs_extra_1 = __importDefault(require("fs-extra")); const glob_1 = __importDefault(require("glob")); const acorn = __importStar(require("acorn")); const inspect_1 = require("../utils/inspect"); const extractOutPath = (compilations) => { const pathList = compilations.reduce((res, { outputPath = '' }) => { outputPath && res.push(outputPath); return res; }, []); // @ts-ignore return (0, lodash_uniqBy_1.default)((0, lodash_compact_1.default)(pathList)); }; exports.default = (compilations, esChekcConfig) => { const processor = { config: esChekcConfig, defaultValue: {}, // includes 增加的路径 相对于output路径 // excludes 不包含的路径 相对于output路径 // parseOptions acorn的解析参数见:https://www.npmjs.com/package/acorn check: async ({ includes, excludes, parseOptions = { ecmaVersion: 5 } }, { report }) => { const pathList = extractOutPath(compilations); // 1.默认检测产出的文件,从compilations或监听output文件目录,得到所有的文件列表 let fileList = []; for (const p of pathList) { if (fs_extra_1.default.statSync(p).isDirectory()) { const files = glob_1.default.sync('**/*', { cwd: p, dot: true, ignore: ['.DS_Store'] }); fileList = files.reduce((res, file) => { const absFile = path_1.default.join(p, file); if (file.endsWith('.js') && (0, inspect_1.isIncluded)(absFile, includes, excludes)) { res.push(absFile); } return res; }, fileList); } } // 2. 遍历读取每个文件内容,使用acorn解析 return fileList.reduce((result, file) => { const code = fs_extra_1.default.readFileSync(file, 'utf8'); try { acorn.parse(code, parseOptions); } catch (err) { report(`EsCheck: found ${file}\n\t${err}`); result = false; } return result; }, true); } }; return processor; }; //# sourceMappingURL=esCheck.js.map