gulp-amd-wrap
Advanced tools
Comparing version 1.0.10 to 1.1.0
@@ -0,1 +1,23 @@ | ||
# [1.1.0](https://github.com/searchfe/gulp-amd-wrap/compare/v1.0.10...v1.1.0) (2019-10-23) | ||
### Bug Fixes | ||
* alias bug ([fd2eeaf](https://github.com/searchfe/gulp-amd-wrap/commit/fd2eeaf)) | ||
* never push function argumentds into deps array ([5787bcc](https://github.com/searchfe/gulp-amd-wrap/commit/5787bcc)) | ||
* never push function argumentds into deps array ([49c3750](https://github.com/searchfe/gulp-amd-wrap/commit/49c3750)) | ||
* test for keep var ([fb3c073](https://github.com/searchfe/gulp-amd-wrap/commit/fb3c073)) | ||
### Features | ||
* add alias ([f41e0e1](https://github.com/searchfe/gulp-amd-wrap/commit/f41e0e1)) | ||
* eslint upgrade ([483d3e4](https://github.com/searchfe/gulp-amd-wrap/commit/483d3e4)) | ||
* support cache ([a41ee37](https://github.com/searchfe/gulp-amd-wrap/commit/a41ee37)) | ||
* support cache ([730a2d5](https://github.com/searchfe/gulp-amd-wrap/commit/730a2d5)) | ||
* upgrade predeps ([ef62705](https://github.com/searchfe/gulp-amd-wrap/commit/ef62705)) | ||
* upgrade preinstall ([2e08a28](https://github.com/searchfe/gulp-amd-wrap/commit/2e08a28)) | ||
* upgrade preinstall ([fd9fa34](https://github.com/searchfe/gulp-amd-wrap/commit/fd9fa34)) | ||
* upgrade preinstall ([894d3bb](https://github.com/searchfe/gulp-amd-wrap/commit/894d3bb)) | ||
## [1.0.10](https://github.com/searchfe/gulp-amd-wrap/compare/v1.0.9...v1.0.10) (2019-09-19) | ||
@@ -2,0 +24,0 @@ |
@@ -26,7 +26,7 @@ "use strict"; | ||
} | ||
}, | ||
} | ||
}); | ||
} | ||
if (found) { | ||
file.contents = new Buffer(file.contents.toString() + | ||
file.contents = Buffer.from(file.contents.toString() + | ||
("\ndefine('" + moduleId + "', ['" + origin + "'], function(mod) {return mod; });")); | ||
@@ -36,5 +36,5 @@ } | ||
callback(null, file); | ||
}, | ||
} | ||
}); | ||
} | ||
exports.absolutize = absolutize; |
@@ -9,2 +9,3 @@ /** 针对Node节点分析所有require依赖 */ | ||
private baseUrl?; | ||
private limitDefineDepth; | ||
constructor( | ||
@@ -16,3 +17,3 @@ /** 当前路径 */ | ||
/** baseUrl of ModuleID */ | ||
baseUrl?: string | undefined); | ||
baseUrl?: string | undefined, limitDefineDepth?: number); | ||
/** 进行分析找出require并利用回调处理 最后返回依赖表 */ | ||
@@ -22,5 +23,5 @@ analysis(): void; | ||
/** | ||
* [{"type":"VariableDeclaration","declarations":[{"type":"VariableDeclarator","id": | ||
* {"type":"Identifier","name":"A"},"init":{"type":"CallExpression","callee":{"type": | ||
* "Identifier","name":"require"},"arguments":[{"type":"Literal","value":"A","raw":"'A'"}]}}],"kind":"var"}] | ||
*/ | ||
* [{"type":"VariableDeclaration","declarations":[{"type":"VariableDeclarator","id": | ||
* {"type":"Identifier","name":"A"},"init":{"type":"CallExpression","callee":{"type": | ||
* "Identifier","name":"require"},"arguments":[{"type":"Literal","value":"A","raw":"'A'"}]}}],"kind":"var"}] | ||
*/ |
@@ -7,3 +7,3 @@ "use strict"; | ||
* @Last Modified by: qiansc | ||
* @Last Modified time: 2019-09-19 17:47:04 | ||
* @Last Modified time: 2019-10-17 19:14:56 | ||
*/ | ||
@@ -20,6 +20,8 @@ var estraverse_1 = require("estraverse"); | ||
/** baseUrl of ModuleID */ | ||
baseUrl) { | ||
baseUrl, limitDefineDepth) { | ||
if (limitDefineDepth === void 0) { limitDefineDepth = 1; } | ||
this.cwd = cwd; | ||
this.ast = ast; | ||
this.baseUrl = baseUrl; | ||
this.limitDefineDepth = limitDefineDepth; | ||
} | ||
@@ -29,4 +31,11 @@ /** 进行分析找出require并利用回调处理 最后返回依赖表 */ | ||
var _this = this; | ||
var defineDepth = 0; | ||
estraverse_1.traverse(this.ast, { | ||
enter: function (node, parent) { | ||
if (node.type === 'CallExpression' && node.callee && node.callee.name === 'define') { | ||
defineDepth++; | ||
} | ||
if (defineDepth > _this.limitDefineDepth) { | ||
return; | ||
} | ||
if (matchAsyncRequireCallExpression(node)) { | ||
@@ -42,2 +51,7 @@ node.arguments[0].elements.forEach(function (element, index) { | ||
}, | ||
leave: function (node) { | ||
if (node.type === 'CallExpression' && node.callee && node.callee.name === 'define') { | ||
defineDepth--; | ||
} | ||
} | ||
}); | ||
@@ -53,8 +67,8 @@ }; | ||
node.arguments && node.arguments[0] && node.arguments[0].type === 'ArrayExpression' && | ||
node.arguments[0].elements); | ||
node.arguments[0].elements && node.arguments[0].elements.value); | ||
} | ||
/** | ||
* [{"type":"VariableDeclaration","declarations":[{"type":"VariableDeclarator","id": | ||
* {"type":"Identifier","name":"A"},"init":{"type":"CallExpression","callee":{"type": | ||
* "Identifier","name":"require"},"arguments":[{"type":"Literal","value":"A","raw":"'A'"}]}}],"kind":"var"}] | ||
*/ | ||
* [{"type":"VariableDeclaration","declarations":[{"type":"VariableDeclarator","id": | ||
* {"type":"Identifier","name":"A"},"init":{"type":"CallExpression","callee":{"type": | ||
* "Identifier","name":"require"},"arguments":[{"type":"Literal","value":"A","raw":"'A'"}]}}],"kind":"var"}] | ||
*/ |
@@ -0,1 +1,2 @@ | ||
import * as moduleID from './moduleID'; | ||
/** 针对Node节点分析所有require依赖 */ | ||
@@ -8,3 +9,7 @@ export declare class DependencyAnalyzer { | ||
/** baseUrl of ModuleID */ | ||
private prefix?; | ||
private alias?; | ||
private baseUrl?; | ||
private staticBaseUrl?; | ||
private limitDefineDepth; | ||
private dependencies; | ||
@@ -17,3 +22,3 @@ constructor( | ||
/** baseUrl of ModuleID */ | ||
baseUrl?: string | undefined); | ||
prefix?: string | undefined, alias?: moduleID.aliasConf[] | undefined, baseUrl?: string | undefined, staticBaseUrl?: string | undefined, limitDefineDepth?: number); | ||
/** 进行分析找出require并利用回调处理 最后返回依赖表 */ | ||
@@ -29,6 +34,1 @@ analysis(cb?: analysisCallback): IDependency[]; | ||
export {}; | ||
/** | ||
* require('A'); | ||
* [{"type":"ExpressionStatement","expression":{"type":"CallExpression","callee":{"type":"Identifier", | ||
* "name":"require"},"arguments":[{"type":"Literal","value":"A","raw":"'A'"}]}}] | ||
*/ |
@@ -7,5 +7,6 @@ "use strict"; | ||
* @Last Modified by: qiansc | ||
* @Last Modified time: 2019-04-29 16:14:06 | ||
* @Last Modified time: 2019-10-17 19:16:26 | ||
*/ | ||
var estraverse_1 = require("estraverse"); | ||
var path_1 = require("path"); | ||
var moduleID = require("./moduleID"); | ||
@@ -20,6 +21,11 @@ /** 针对Node节点分析所有require依赖 */ | ||
/** baseUrl of ModuleID */ | ||
baseUrl) { | ||
prefix, alias, baseUrl, staticBaseUrl, limitDefineDepth) { | ||
if (limitDefineDepth === void 0) { limitDefineDepth = 1; } | ||
this.cwd = cwd; | ||
this.ast = ast; | ||
this.prefix = prefix; | ||
this.alias = alias; | ||
this.baseUrl = baseUrl; | ||
this.staticBaseUrl = staticBaseUrl; | ||
this.limitDefineDepth = limitDefineDepth; | ||
this.dependencies = []; | ||
@@ -30,27 +36,41 @@ } | ||
var _this = this; | ||
var defineDepth = 0; | ||
estraverse_1.traverse(this.ast, { | ||
enter: function (node, parent) { | ||
if (node.type === 'CallExpression' && node.callee && node.callee.name === 'define') { | ||
defineDepth++; | ||
} | ||
if (defineDepth > _this.limitDefineDepth) { | ||
return; | ||
} | ||
if (matchRequireVariableDeclarator(node)) { | ||
var dep = getDependencyFromNode(node); | ||
dep.moduleID = moduleID.parseBase(_this.baseUrl || _this.cwd, moduleID.parseAbsolute(_this.cwd, dep.moduleID)); | ||
var prefix = dep.moduleID.match(/^\./) === null ? '' : _this.prefix; | ||
var baseUrl = path_1.extname(dep.moduleID) !== '.json' ? (_this.baseUrl || _this.cwd) | ||
: (_this.staticBaseUrl || _this.baseUrl || _this.cwd); | ||
dep.moduleID = moduleID.parseBase(baseUrl, moduleID.parseAbsolute(_this.cwd, dep.moduleID), prefix, _this.alias); | ||
var replaced = cb ? cb(dep, node, parent) : undefined; | ||
if (node.arguments && node.arguments[0] && | ||
node.arguments[0].value && | ||
node.arguments[0].value.match(/^\./) !== null) { | ||
node.arguments[0].value = dep.moduleID; | ||
node.arguments[0].raw = "\"" + dep.moduleID + "\""; | ||
} | ||
_this.dependencies.push(dep); | ||
} | ||
if (matchDefineDep(node)) { | ||
node.arguments[1].elements.forEach(function (item, index) { | ||
var prefix = item.value.match(/^\./) === null ? '' : _this.prefix; | ||
var baseUrl = path_1.extname(item.value) !== '.json' ? (_this.baseUrl || _this.cwd) | ||
: (_this.staticBaseUrl || _this.baseUrl || _this.cwd); | ||
item.value = moduleID.parseBase(baseUrl, moduleID.parseAbsolute(_this.cwd, item.value), prefix, _this.alias); | ||
node.arguments[1].elements[index] = item; | ||
}); | ||
} | ||
}, | ||
leave: function (node, parent) { | ||
if (node.type === 'VariableDeclaration') { | ||
var declarations = hasRequireDeclarations(node); | ||
if (declarations) { | ||
if (declarations.length) { | ||
node.declarations = declarations; | ||
} | ||
else { | ||
/** 这里有个非常坑的bug 没有办法拿到traverse return VisitorOption.Remove 也不会删除,只能置空 */ | ||
node.type = 'EmptyStatement'; | ||
// this.remove(); | ||
return estraverse_1.VisitorOption.Remove; | ||
} | ||
} | ||
leave: function (node) { | ||
if (node.type === 'CallExpression' && node.callee && node.callee.name === 'define') { | ||
defineDepth--; | ||
} | ||
}, | ||
} | ||
}); | ||
@@ -63,6 +83,8 @@ return this.dependencies; | ||
/** 判断当前node节点为require VariableDeclarator 以及 require Literal */ | ||
function matchRequireVariableDeclarator(node) { | ||
function matchRequireVariableDeclarator(node, dependencies) { | ||
if (dependencies === void 0) { dependencies = []; } | ||
return !!((node.type === 'VariableDeclarator' && node.id && node.id.name !== undefined && | ||
node.init && node.init.callee !== undefined && node.init.callee.name === 'require' && | ||
node.init.arguments && node.init.arguments[0] && node.init.arguments[0].value !== undefined) || | ||
node.init.arguments && node.init.arguments[0] && node.init.arguments[0].value !== undefined && | ||
dependencies.map(function (item) { return item.moduleID; }).indexOf(node.init.arguments[0].value) === -1) || | ||
(node.type === 'CallExpression' && node.arguments[0] && node.arguments[0].type === 'Literal' && | ||
@@ -72,3 +94,4 @@ node.callee && node.callee.name === 'require')); | ||
/** 判断当前node节点是否包含require VariableDeclarator 返回去除后正常的declarations */ | ||
function hasRequireDeclarations(node) { | ||
function hasRequireDeclarations(node, dependencies) { | ||
if (dependencies === void 0) { dependencies = []; } | ||
var notRequireDeclarations = []; | ||
@@ -78,3 +101,3 @@ var hasRequire = false; | ||
node.declarations.forEach(function (element) { | ||
if (matchRequireVariableDeclarator(element)) { | ||
if (matchRequireVariableDeclarator(element, dependencies)) { | ||
hasRequire = true; | ||
@@ -104,3 +127,3 @@ } | ||
name: node.id ? node.id.name : '', | ||
value: node.init ? node.init.arguments[0].value : node.arguments[0].value, | ||
value: node.init ? node.init.arguments[0].value : node.arguments[0].value | ||
}; | ||
@@ -113,1 +136,6 @@ } | ||
*/ | ||
function matchDefineDep(node) { | ||
return !!(node.type === 'CallExpression' && node.callee && node.callee.name === 'define' && | ||
node.arguments[1] && node.arguments[1].type === 'ArrayExpression' && | ||
node.arguments[1].elements.length > 0); | ||
} |
@@ -1,2 +0,6 @@ | ||
export declare function amdWrap(option: IAmdWrap): any; | ||
import { aliasConf } from './moduleID'; | ||
import { Transform } from 'gulp-transform-cache'; | ||
declare class AmdWrap extends Transform { | ||
} | ||
export declare function amdWrap(option: IAmdWrap): AmdWrap; | ||
interface IAmdWrap { | ||
@@ -13,12 +17,13 @@ /** 即项目根目录。用来配置模块查找根目录 */ | ||
/** 自定义moduleID模块 */ | ||
module?: IAmdWrapCustomOption[]; | ||
alias?: aliasConf[]; | ||
moduleId?: string; | ||
/** 不参与生成moduleId的模块 */ | ||
anonymousModule?: string[]; | ||
/** 配置文件路径 */ | ||
confAboutFile?: any; | ||
/** 静态资源的根目录 */ | ||
staticBaseUrl?: string; | ||
/** 生成的ModuleId 是否需要md5后缀来避免其他模块引用 如 @molecule/toptip2_134dfas */ | ||
useMd5?: any; | ||
} | ||
interface IAmdWrapCustomOption { | ||
/** 自定义moduleID */ | ||
moduleId: string; | ||
/** 自定义module path */ | ||
path: string; | ||
} | ||
export {}; |
"use strict"; | ||
/* | ||
* @Author: qiansc | ||
* @Date: 2019-04-23 11:17:36 | ||
* @Last Modified by: qiansc | ||
* @Last Modified time: 2019-10-18 20:03:30 | ||
*/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var stream = require("readable-stream"); | ||
var filter_1 = require("./filter"); | ||
var moduleID_1 = require("./moduleID"); | ||
var parser_1 = require("./parser"); | ||
var Transform = stream.Transform; | ||
var gulp_transform_cache_1 = require("gulp-transform-cache"); | ||
var path = require("path"); | ||
var AmdWrap = /** @class */ (function (_super) { | ||
__extends(AmdWrap, _super); | ||
function AmdWrap() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return AmdWrap; | ||
}(gulp_transform_cache_1.Transform)); | ||
function amdWrap(option) { | ||
return new Transform({ | ||
return new AmdWrap({ | ||
objectMode: true, | ||
transform: function (file, enc, callback) { | ||
if (path.extname(file.path) !== '.js') { | ||
return callback(null, file); | ||
} | ||
// 如果传入多个文件,且文件各配置不同,confAboutFile需要被传入,这样才能依据配置对文件进行处理。 | ||
if (option.confAboutFile && option.confAboutFile[path.relative(process.cwd(), file.path)]) { | ||
option = Object.assign(option.confAboutFile[path.relative(process.cwd(), file.path)], { confAboutFile: option.confAboutFile, alias: option.alias }); | ||
} | ||
// 传入baseUrl则moduleid基于baseUrl计算 | ||
var baseUrl = option.baseUrl || file.base; | ||
var prefix = option.prefix || ''; | ||
var useMd5 = option.useMd5 || false; | ||
var alias = []; | ||
if (option.alias) { | ||
option.alias.forEach(function (a) { | ||
alias.push({ | ||
moduleId: a.moduleId, | ||
path: moduleID_1.parseAbsolute(baseUrl, a.path), | ||
prefix: a.prefix || false | ||
}); | ||
}); | ||
} | ||
// let location = parseBase(file.path); | ||
if (filter_1.include(file.path, option.exclude, option.baseUrl)) { | ||
@@ -20,12 +66,15 @@ // 在exlude名单中 do nothing | ||
else { | ||
var parser = new parser_1.Parser(file.contents, file.path, baseUrl, prefix); | ||
var parser = new parser_1.Parser(file.contents, file.path, baseUrl, prefix, alias, option.staticBaseUrl); | ||
parser.hook({ | ||
removeModuleId: filter_1.include(file.path, option.anonymousModule, option.baseUrl), | ||
useMd5: useMd5 | ||
}); | ||
file.contents = new Buffer(parser.getContent()); | ||
file.contents = Buffer.from(parser.getContent()); | ||
file.moduleId = parser.getModuleId(); | ||
file.dependences = parser.getDependences(); | ||
callback(null, file); | ||
} | ||
}, | ||
} | ||
}); | ||
} | ||
exports.amdWrap = amdWrap; |
export { amdWrap } from './hook'; | ||
export { absolutize } from './absolutize'; | ||
export { addRequireConfig } from './addRequireConfig'; |
@@ -5,4 +5,4 @@ "use strict"; | ||
* @Date: 2019-04-23 11:18:08 | ||
* @Last Modified by: qiansc | ||
* @Last Modified time: 2019-06-05 17:33:33 | ||
* @Last Modified by: liangjiaying@baidu.com | ||
* @Last Modified time: 2019-08-08 19:20:06 | ||
*/ | ||
@@ -14,1 +14,3 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.absolutize = absolutize_1.absolutize; | ||
var addRequireConfig_1 = require("./addRequireConfig"); | ||
exports.addRequireConfig = addRequireConfig_1.addRequireConfig; |
@@ -11,5 +11,20 @@ /** 将相对路径转化为绝对路径 ./A ../ */ | ||
baseUrl: string, | ||
/** 分析的ast node,因为ast库没有支持ts,所以ast类型为any */ | ||
moduleID: string, | ||
/** 文件路径 */ | ||
filePath: string, | ||
/** moduleID前缀 */ | ||
prefix?: string): string; | ||
prefix?: string, | ||
/** 自定义moduleId */ | ||
alias?: aliasConf[]): string; | ||
export declare function parseJsonBase( | ||
/** 当前路径 */ | ||
baseUrl: string, | ||
/** 文件路径 */ | ||
filePath: string): string; | ||
export interface aliasConf { | ||
/** 自定义moduleID */ | ||
moduleId: string; | ||
/** 自定义module path */ | ||
path: string; | ||
/** 带上别名 */ | ||
prefix?: boolean; | ||
} |
@@ -7,3 +7,3 @@ "use strict"; | ||
* @Last Modified by: qiansc | ||
* @Last Modified time: 2019-06-05 10:45:24 | ||
* @Last Modified time: 2019-10-17 11:32:43 | ||
*/ | ||
@@ -27,11 +27,42 @@ var path_1 = require("path"); | ||
baseUrl, | ||
/** 分析的ast node,因为ast库没有支持ts,所以ast类型为any */ | ||
moduleID, | ||
/** 文件路径 */ | ||
filePath, | ||
/** moduleID前缀 */ | ||
prefix) { | ||
if (moduleID.match(/^\//) !== null && moduleID.indexOf(baseUrl) === 0) { | ||
return (prefix ? prefix + '/' : '') + moduleID.substring(baseUrl.length + 1).replace(/(\.js|\.ts)$/, ''); | ||
prefix, | ||
/** 自定义moduleId */ | ||
alias) { | ||
if (alias === void 0) { alias = []; } | ||
if (path_1.extname(filePath) === '.json') { | ||
return parseJsonBase(baseUrl, filePath); | ||
} | ||
return (prefix ? prefix + '/' : '') + moduleID.replace(/(\.js|\.ts)$/, ''); | ||
var conf; | ||
alias.forEach(function (a) { | ||
if (a.path === filePath || a.path === filePath.replace(/(\.js|\.ts)$/, '')) { | ||
conf = a; | ||
} | ||
}); | ||
if (conf !== undefined) { | ||
if (conf.prefix) { | ||
return prefix + '/' + conf.moduleId; | ||
} | ||
else { | ||
return conf.moduleId; | ||
} | ||
} | ||
if (filePath.match(/^\//) !== null && filePath.indexOf(baseUrl) === 0) { | ||
return (prefix ? prefix + '/' : '') + filePath.substring(baseUrl.length + 1).replace(/(\.js|\.ts)$/, ''); | ||
} | ||
return (prefix ? prefix + '/' : '') + filePath.replace(/(\.js|\.ts)$/, ''); | ||
} | ||
exports.parseBase = parseBase; | ||
function parseJsonBase( | ||
/** 当前路径 */ | ||
baseUrl, | ||
/** 文件路径 */ | ||
filePath) { | ||
if (filePath.indexOf(baseUrl) === 0) { | ||
return filePath.substring(baseUrl.length + 1); | ||
} | ||
return filePath; | ||
} | ||
exports.parseJsonBase = parseJsonBase; |
/// <reference types="node" /> | ||
import { aliasConf } from './moduleID'; | ||
export declare class Parser { | ||
@@ -7,8 +8,15 @@ private contents; | ||
private prefix; | ||
private alias?; | ||
private staticBaseUrl?; | ||
private cwd; | ||
private ast; | ||
constructor(contents: Buffer, filePath: string, root: string, prefix: string); | ||
private myModuleId; | ||
private parseDefine; | ||
private dependences; | ||
constructor(contents: Buffer, filePath: string, root: string, prefix: string, alias?: aliasConf[] | undefined, staticBaseUrl?: string | undefined); | ||
getContent(): Buffer; | ||
getModuleId(): string; | ||
getDependences(): string[]; | ||
isHasObj(arr: any, val: any): boolean; | ||
hook(hookOption?: HookOption): void; | ||
hook(hookOption?: HookOption): this; | ||
private parse; | ||
@@ -22,3 +30,4 @@ /** | ||
removeModuleId?: boolean; | ||
useMd5?: any; | ||
} | ||
export {}; |
@@ -7,3 +7,3 @@ "use strict"; | ||
* @Last Modified by: qiansc | ||
* @Last Modified time: 2019-06-05 10:45:51 | ||
* @Last Modified time: 2019-10-23 13:13:05 | ||
*/ | ||
@@ -13,8 +13,10 @@ var escodegen_1 = require("escodegen"); | ||
var estraverse_1 = require("estraverse"); | ||
var path_1 = require("path"); | ||
var async_analyzer_1 = require("./async-analyzer"); | ||
var fs_1 = require("fs"); | ||
var dependency_analyzer_1 = require("./dependency-analyzer"); | ||
var filter_1 = require("./filter"); | ||
var moduleID_1 = require("./moduleID"); | ||
var path_1 = require("path"); | ||
var md5File = require('md5-file'); | ||
var Parser = /** @class */ (function () { | ||
function Parser(contents, filePath, root, prefix) { | ||
function Parser(contents, filePath, root, prefix, alias, staticBaseUrl) { | ||
this.contents = contents; | ||
@@ -24,4 +26,8 @@ this.filePath = filePath; | ||
this.prefix = prefix; | ||
this.alias = alias; | ||
this.staticBaseUrl = staticBaseUrl; | ||
this.cwd = path_1.dirname(filePath); | ||
this.parse(); | ||
this.dependences = []; | ||
this.parseDefine = 0; | ||
} | ||
@@ -31,4 +37,13 @@ Parser.prototype.getContent = function () { | ||
}; | ||
Parser.prototype.getModuleId = function () { | ||
return this.myModuleId; | ||
}; | ||
Parser.prototype.getDependences = function () { | ||
return Array.from(new Set(this.dependences)); | ||
}; | ||
Parser.prototype.isHasObj = function (arr, val) { | ||
var flag = false; | ||
if (!arr || arr.length === 0) { | ||
return false; | ||
} | ||
arr.forEach(function (element) { | ||
@@ -44,2 +59,17 @@ if (JSON.stringify(element).indexOf(JSON.stringify(val)) !== -1) { | ||
if (hookOption === void 0) { hookOption = {}; } | ||
/** 生成的ModuleId md5后缀来避免其他模块引用 @molecule/toptip2_134dfas */ | ||
this.parseDefine = 0; | ||
var md5Value = ''; | ||
if (hookOption.useMd5 === true || (hookOption.useMd5 && hookOption.useMd5.useMd5)) { | ||
var exlude = hookOption.useMd5.exlude; // 要被排除的文件 | ||
if (!filter_1.include(path_1.resolve(this.filePath), exlude, this.root)) { | ||
// 不在md5排除名单中 | ||
try { | ||
md5Value = '_' + md5File.sync(this.filePath.replace('.js', '.ts')).slice(0, 7); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
} | ||
// console.log(this.contents.toString()); | ||
@@ -51,32 +81,101 @@ estraverse_1.traverse(this.ast, { | ||
if (node.callee && node.callee.name === 'define') { | ||
// 首参数是function,推入依赖数组 | ||
if (node.arguments[0].type === 'FunctionExpression') { | ||
var ele = node.arguments[0].params.map(function (item) { return ({ type: 'Literal', value: item.name }); }); | ||
node.arguments.unshift({ type: 'ArrayExpression', elements: ele }); | ||
_this.parseDefine++; | ||
if (_this.parseDefine === 1) { | ||
// 首参数是function,推入依赖数组 | ||
if (node.arguments[0].type === 'FunctionExpression') { | ||
node.arguments.unshift({ type: 'ArrayExpression', elements: [] }); | ||
} | ||
// 首参数推入moduleId => define("@molecule/toptip/main",xxxx) | ||
if (node.arguments[0].type === 'ArrayExpression') { | ||
node.arguments.unshift({ | ||
type: 'Literal', | ||
value: moduleID_1.parseBase(_this.root, _this.filePath, _this.prefix, _this.alias) + md5Value | ||
}); | ||
} | ||
else if (node.arguments[0].type === 'ObjectExpression' || node.arguments[0].type === 'Identifier') { | ||
node.arguments.unshift({ type: 'ArrayExpression', elements: [] }); | ||
node.arguments.unshift({ | ||
type: 'Literal', | ||
value: moduleID_1.parseBase(_this.root, _this.filePath, _this.prefix, _this.alias) | ||
}); | ||
} | ||
else if (node.arguments[0].type === 'Literal') { | ||
var moduleId = node.arguments[0].value; | ||
if (moduleId.split('/').pop() === path_1.basename(_this.filePath, path_1.extname(_this.filePath))) { | ||
var prefix = moduleId.match(/^\./) === null ? '' : _this.prefix; | ||
node.arguments[0].value = moduleID_1.parseBase(_this.root, _this.filePath, prefix, _this.alias); | ||
} | ||
} | ||
_this.myModuleId = node.arguments[0].value; | ||
var analyzer = new dependency_analyzer_1.DependencyAnalyzer(_this.cwd, node, _this.prefix, _this.alias, _this.root, _this.staticBaseUrl); | ||
var deps = analyzer.analysis(function (dep, requireNode, parent) { | ||
// requireNode.id.name = {type}; | ||
// console.log('analyzer', dep, requireNode); | ||
}); | ||
// 第二参数是依赖数组 => define("", ['require', 'exports', 'md5-file']) | ||
if (node.arguments[1].elements) { | ||
node.arguments[1].elements.forEach(function (element, index) { | ||
var valueString = element.value; | ||
/** depPath: 实际依赖的相对路径文件。如果是node_module就为空 */ | ||
var depPath = moduleID_1.parseAbsolute(path_1.dirname(_this.filePath), valueString + '.ts'); | ||
if (fs_1.existsSync(depPath)) { | ||
var md5 = '_' + md5File.sync(depPath).slice(0, 7); | ||
// moduleid 示例:@molecule/toptip/main_dc85e717d6352fa285bc70bc2d1d3595 | ||
var moduleid = moduleID_1.parseBase(_this.root, depPath, _this.prefix, _this.alias) + md5; | ||
node.arguments[1].elements[index].value = moduleid; | ||
} | ||
}); | ||
} | ||
if (node.arguments[1] && node.arguments[1].elements && | ||
!_this.isHasObj(node.arguments[1].elements, 'require')) { | ||
node.arguments[1].elements.unshift({ type: 'Literal', value: 'require' }); | ||
} | ||
if (node.arguments[2] && node.arguments[2].params) { | ||
if (!_this.isHasObj(node.arguments[2].params, 'require')) { | ||
node.arguments[2].params.unshift({ type: 'Identifier', name: 'require' }); | ||
} | ||
node.arguments[2].params.forEach(function (item, index) { | ||
if (item.name === 'exports') { | ||
node.arguments[1].elements[index] = { type: 'Literal', value: 'exports' }; | ||
} | ||
else if (item.name === 'module') { | ||
node.arguments[1].elements[index] = { type: 'Literal', value: 'module' }; | ||
} | ||
}); | ||
} | ||
deps.forEach(function (dep) { | ||
if (node.arguments[1] && node.arguments[1].elements && | ||
node.arguments[1].elements.map(function (e) { return e.value; }).indexOf(dep.moduleID) < 0) { | ||
node.arguments[1].elements.push({ type: 'Literal', value: dep.moduleID }); | ||
// factory函数的参数中推入依赖对应的变量名 | ||
// if (dep.name) { | ||
// node.arguments[2].params.push({ type: 'Identifier', name: dep.name }); | ||
// } | ||
_this.dependences.push(dep.moduleID); | ||
} | ||
}); | ||
if (hookOption.removeModuleId) { | ||
node.arguments.shift(); | ||
} | ||
} | ||
} | ||
if (node.callee && node.callee.name === 'require' && _this.parseDefine < 2) { | ||
var firstArg = node.arguments[0]; | ||
// 首参数是依赖数组,推入moduleId | ||
if (node.arguments[0].type === 'ArrayExpression') { | ||
node.arguments.unshift({ type: 'Literal', value: moduleID_1.parseBase(_this.root, _this.filePath, _this.prefix) }); | ||
if (firstArg.type === 'ArrayExpression') { | ||
firstArg.elements.map(function (element) { | ||
if (element.value && element.value.match(/^\.\.?\//) !== null) { | ||
element.value = moduleID_1.parseBase(_this.root, moduleID_1.parseAbsolute(_this.cwd, element.value), _this.prefix, _this.alias); | ||
element.raw = "\"" + element.value + "\""; | ||
_this.dependences.push(element.value); | ||
} | ||
}); | ||
} | ||
var analyzer = new dependency_analyzer_1.DependencyAnalyzer(_this.cwd, node, _this.root); | ||
var deps = analyzer.analysis(function (dep, requireNode, parent) { | ||
// requireNode.id.name = {type}; | ||
// console.log('analyzer', dep, requireNode); | ||
}); | ||
if (node.arguments[1] && !_this.isHasObj(node.arguments[1].elements, 'require')) { | ||
node.arguments[1].elements.push({ type: 'Literal', value: 'require' }); | ||
} | ||
if (node.arguments[2] && !_this.isHasObj(node.arguments[2].params, 'require')) { | ||
node.arguments[2].params.push({ type: 'Identifier', name: 'require' }); | ||
} | ||
deps.forEach(function (dep) { | ||
if (node.arguments[1].elements.map(function (e) { return e.value; }).indexOf(dep.moduleID) < 0) { | ||
node.arguments[1].elements.push({ type: 'Literal', value: dep.moduleID }); | ||
if (dep.name) { | ||
node.arguments[2].params.push({ type: 'Identifier', name: dep.name }); | ||
} | ||
if (firstArg.type === 'Literal') { | ||
if (firstArg.value && firstArg.value.match(/^\.\.?\//) !== null) { | ||
var baseUrl = path_1.extname(firstArg.value) !== '.json' ? _this.root : _this.staticBaseUrl || _this.root; | ||
firstArg.value = moduleID_1.parseBase(baseUrl, moduleID_1.parseAbsolute(_this.cwd, firstArg.value), _this.prefix, _this.alias); | ||
firstArg.raw = "\"" + firstArg.value + "\""; | ||
_this.dependences.push(firstArg.value); | ||
} | ||
}); | ||
if (hookOption.removeModuleId) { | ||
node.arguments.shift(); | ||
} | ||
@@ -88,7 +187,18 @@ } | ||
leave: function (node) { | ||
var aa = new async_analyzer_1.AsyncAnalyzer(_this.cwd, node, _this.root); | ||
aa.analysis(); | ||
}, | ||
if (node.type === 'CallExpression') { | ||
// 如果是define | ||
if (node.callee && node.callee.name === 'define') { | ||
_this.parseDefine--; | ||
} | ||
} | ||
// const aa = new AsyncAnalyzer( | ||
// this.cwd, | ||
// node, | ||
// this.root, | ||
// ); | ||
// aa.analysis(); | ||
} | ||
}); | ||
this.contents = escodegen_1.generate(this.ast); | ||
return this; | ||
}; | ||
@@ -95,0 +205,0 @@ Parser.prototype.parse = function () { |
{ | ||
"name": "gulp-amd-wrap", | ||
"version": "1.0.10", | ||
"version": "1.1.0", | ||
"discription": "gulp-amd-wrap", | ||
"scripts": { | ||
"preversion": "npm run lint && npm run cover", | ||
"lint": "tslint src/**/*.ts test/**/*.ts --project tsconfig.json --config tslint.json", | ||
"lint": "eslint 'src/**/*.ts'", | ||
"doc": "gulp doc", | ||
@@ -32,2 +32,3 @@ "conf": "tsc && node_modules/ts-node/dist/bin.js src/configuration/generate.ts", | ||
"devDependencies": { | ||
"eslint": "^5.16.0", | ||
"@semantic-release/changelog": "^3.0.2", | ||
@@ -43,2 +44,10 @@ "@semantic-release/commit-analyzer": "^6.1.0", | ||
"@types/underscore": "^1.8.9", | ||
"@typescript-eslint/eslint-plugin": "^2.3.0", | ||
"@typescript-eslint/parser": "^2.3.0", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-import": "^2.15.0", | ||
"eslint-plugin-jest": "^22.6.4", | ||
"eslint-plugin-node": "^9.1.0", | ||
"eslint-plugin-promise": "^4.1.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"chai": "^4.1.2", | ||
@@ -67,6 +76,6 @@ "commitizen": "^3.1.1", | ||
"ts-node": "^7.0.1", | ||
"tslint": "^5.11.0", | ||
"typedoc": "^0.12.0", | ||
"typedoc-plugin-markdown": "^1.1.15", | ||
"typescript": "^3.1.6" | ||
"typescript": "^3.1.6", | ||
"gulp-transform-cache": "*1.1.0" | ||
}, | ||
@@ -88,5 +97,9 @@ "release": { | ||
"estraverse": "^4.2.0", | ||
"md5-file": "^4.0.0", | ||
"minimatch": "^3.0.4", | ||
"readable-stream": "^3.3.0" | ||
}, | ||
"peerDependencies": { | ||
"gulp-transform-cache": "*1.1.0" | ||
}, | ||
"engines": { | ||
@@ -93,0 +106,0 @@ "node": ">= 6.0.x" |
@@ -33,5 +33,7 @@ # gulp-amd-wrap | ||
exelude: ['/exclude-**.js', '/dist/**'], | ||
moduleID: { | ||
'moduleID': 'filepath' | ||
} | ||
alias: [{ | ||
moduleId: 'debug', | ||
path: 'util/debug', | ||
prifix: false | ||
}] | ||
})).pipe( | ||
@@ -54,3 +56,3 @@ gulp.dest(`${__dirname}\/dist\/`), | ||
require(['A', './B', '/C', '@D/E'], function(a, b, c, d){ | ||
require(['A', './B', '/C', '@D/E', './util/debug'], function(a, b, c, d){ | ||
console.log(a, b, c, d); | ||
@@ -64,6 +66,6 @@ }); | ||
// After | ||
define('assert/minify-define', [ | ||
define('wiseindex/assert/minify-define', [ | ||
'require', | ||
'@scope/moduleA', | ||
'assert/moduleB' | ||
'wiseindex/assert/moduleB' | ||
], function (require, moduleA, moduleB) { | ||
@@ -74,5 +76,6 @@ 'use strict'; | ||
'A', | ||
'assert/B', | ||
'wiseindex/assert/B', | ||
'/C', | ||
'@D/E' | ||
'@D/E', | ||
'debug' | ||
], function (a, b, c, d) { | ||
@@ -79,0 +82,0 @@ console.log(a, b, c, d); |
@@ -17,3 +17,4 @@ { | ||
"no-var-requires": false, | ||
"quotemark": [true, "single"] | ||
"quotemark": [true, "single"], | ||
"ordered-imports": false | ||
// "use-input-property-decorator": true, | ||
@@ -24,3 +25,2 @@ // "use-output-property-decorator": true, | ||
// "max-classes-per-file": false, | ||
// "ordered-imports": false, | ||
// "variable-name": false, | ||
@@ -27,0 +27,0 @@ // "prefer-const": false, |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
648100
29
18528
89
8
46
1
+ Addedmd5-file@^4.0.0
+ Addedmd5-file@4.0.0(transitive)