UNPKG

webpack-extjs-loader

Version:

A ExtJs project module loader for webpack

170 lines (158 loc) 7.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireWildcard(require("path")); var _colors = _interopRequireDefault(require("colors")); var _loader = require("@webpack-utilities/loader"); var _schemaUtils = _interopRequireDefault(require("schema-utils")); var _fileParser = _interopRequireDefault(require("./fileParser")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _Promise = typeof Promise === 'undefined' ? require('es6-promise').Promise : Promise; // const schema = { // type: 'object', // properties: { // test: { // type: 'string' // } // } // }; //Парсер файлов написанных с помощью фрэймворка ExtJS let parser = new _fileParser.default({ ignoreOverrides: false }); //Парсинг файла const fileParse = file => { return new _Promise((resolve, reject) => { parser.parseContent(file).then(() => { resolve(parser); }).catch(e => { reject(e); }); }); }; //Является ли модулем ExtJS const isExtModule = module => /^Ext./.test(module); //Преобразуем модуль формата ExtJS в путь файловой системы const toPath = (module, options) => { let p = module.replace(/\./g, `${_path.default.sep}`) + '.js'; if (options.pathSrc) p = `${options.pathSrc}${_path.default.sep}${p}`; return p; }; //Существует ли файл const existsFile = file => _fs.default.existsSync(file); //Функция формирования по полному пути файла информации для дальнейшего использования const getImport = (file, currentFile) => { const dir = (0, _path.dirname)(file), mPath = dir.replace(/\//g, "$"), rDir = _path.default.relative((0, _path.dirname)(currentFile), dir), module = (0, _path.basename)(file, '.js'); let data = { variable: dir === '.' ? `$${module}` : `$${mPath}$${module}`, from: dir === '.' ? `'.${_path.default.sep}${module}'` : rDir ? `'.${_path.default.sep}${rDir}${_path.default.sep}${module}'` : `'.${_path.default.sep}${module}'`, file, dir, module }; return data; }; const getNamespace = module => { const arr = module.split('.'); if (arr && arr.length) return arr[0]; }; //Главная функция для Webpack - именно она вызывается для обработки исходных кодов function _default(source, map) { const module = {}; const existsModule = variable => { if (variable in module) return true; module[variable] = true; return false; }; var callback = this.async(); // console.log(colors.blue('module '), colors.green(this.resourcePath)) const options = (0, _loader.getOptions)(this); // validate(schema, options, { // name: 'Example Loader', // baseDataPath: 'options' // }); // Apply some transformations to the source... let res = ''; fileParse(source).then(parser => { const deps = parser.filesRequires.content; if (deps.extend && !isExtModule(deps.extend)) { if (getNamespace(deps.extend) == options.namespace) { const imp = getImport(toPath(deps.extend, options), this.resourcePath); if (!existsModule(imp.variable)) { // res += `import ${imp.variable} from ${imp.from}\n` res += `import ${imp.from}\n`; } } } //requires if (deps.requires && deps.requires.length) { deps.requires.forEach(m => { const imp = getImport(toPath(m, options), this.resourcePath); if (existsFile(toPath(m, options))) { if (!existsModule(imp.variable)) { // res += `import ${imp.variable} from ${imp.from}\n` res += `import ${imp.from}\n`; } } else console.log(_colors.default.red(`No module found '${toPath(m, options)}' which is required to ${this.resourcePath}`)); }); } //mixins if (deps.mixins && deps.mixins.length) { deps.mixins.forEach(m => { if (isExtModule(m)) return; const imp = getImport(toPath(m, options), this.resourcePath); if (existsFile(toPath(m, options))) { if (!existsModule(imp.variable)) { // res += `import ${imp.variable} from ${imp.from}\n` res += `import ${imp.from}\n`; } } else console.log(_colors.default.red(`No module found '${toPath(m, options)}' which is mixins to ${this.resourcePath}`)); }); } //model if (deps.model) { if (existsFile(toPath(deps.model, options))) { const imp = getImport(toPath(deps.model, options), this.resourcePath); if (!existsModule(imp.variable)) { // res += `import ${imp.variable} from ${imp.from}\n` res += `import ${imp.from}\n`; } } else console.log(_colors.default.red('not found module', toPath(deps.model, options))); } //controllers if (deps.controllers && deps.controllers.length) { deps.controllers.forEach(c => { if (existsFile(toPath(c, options))) { const imp = getImport(toPath(c, options), this.resourcePath); if (!existsModule(imp.variable)) { // res += `import ${imp.variable} from ${imp.from}\n` res += `import ${imp.from}\n`; } } else console.log(_colors.default.red('not found module ', toPath(c, options))); }); } //require if (deps.require && deps.require.length) { deps.require.forEach(r => { if (existsFile(toPath(r, options))) { const imp = getImport(toPath(r, options), this.resourcePath); if (!existsModule(imp.variable)) { // res += `import ${imp.variable} from ${imp.from}\n` res += `import ${imp.from}\n`; } } else console.log(_colors.default.red('not found module ', toPath(r, options))); }); } callback(null, `${res}${source}`, map); }).catch(e => { callback(e); }); }