xhs-mp-sjs-loader 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # 构建工具
@@ -0,0 +1,12 @@
1
+ import type { Span } from '@swc/core';
2
+ export declare const SJS_HELPER = "_sjs$";
3
+ declare function sjsCompiler(code: any, onRequire: (s: string) => string): {
4
+ code: string;
5
+ map: string | undefined;
6
+ legacyCode: string;
7
+ errors: {
8
+ message: string;
9
+ span: Span;
10
+ }[];
11
+ };
12
+ export default sjsCompiler;
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("lodash"),t=require("@swc/core");const r=(e,r,s="esnext",a=!1)=>t.transformSync(e,{swcrc:!1,configFile:!1,minify:a,sourceMaps:!1,jsc:{preserveAllComments:!0,target:s,externalHelpers:!1,parser:{syntax:"ecmascript",jsx:!1,dynamicImport:!1,privateMethod:!1,functionBind:!1,classPrivateProperty:!1,exportDefaultFrom:!1,exportNamespaceFrom:!1,decorators:!1,decoratorsBeforeExport:!1,importMeta:!1}},plugin:r}),s=["getDate","getRegExp","console"],a=["Object","Boolean","Array","String","Number","Date","Map","Set","decodeURI","decodeURIComponent","encodeURI","encodeURIComponent","isNaN","isFinite","parseFloat","parseInt","Infinity","NaN","undefined","setTimeout","clearTimeout","setInterval","clearInterval","JSON","Math","module","require","exports",...s];class n{constructor(){this.defines={},this.access={}}toString(){return`defines: ${Object.keys(this.defines)} access: ${Object.keys(this.access)}`}anlysis(){for(const e in this.access)if(this.checkGlobal(e)&&!a.includes(e))return{variable:e,span:this.access[e]}}checkGlobal(e){let t=this;for(;t;){if(t.defines[e])return!1;t=t.parentScope}return!t}setAccess(e,t){this.access[e]=t}setDefines(e,t){this.defines[e]=t}setParentScope(e){this.parentScope=e}}class o{constructor(){this._scopeStack=[],this._scopeTables=[],this._stop=!1}stop(){this._stop=!0}enterScope(){this._stop||this._scopeStack.push(new n)}exitScope(){if(this._stop)return;const e=this._scopeStack.pop();e&&(this._scopeTables.push(e),e.setParentScope(this.getCurrentScope()))}getCurrentScope(){if(!this._stop)return this._scopeStack[this._scopeStack.length-1]}pushIdentifier(e){var t;this._stop||this.getCurrentScope()&&(null===(t=this.getCurrentScope())||void 0===t||t.setDefines(e.value,e.span))}pushAccess(e){if(this._stop)return;const t=this.getCurrentScope();t&&(null==t||t.setAccess(e.value,e.span),s.includes(e.value)&&(e._xhsScope=t))}getDFSScope(){return this._scopeTables}}const i="_sjs$",c=`${i}.s`,p=`${i}.g`,l=`${i}.c`;exports.SJS_HELPER=i,exports.default=function(t,a){var n;const u=[];let d="scan";const y=new o,f=[];function h(e,t){if("scan"!==d)return;const r=e.trim().split(/\s+/).length>1?e:`${e} is disallowed`;u.push({message:r,span:Object.assign({},t)})}function m(e){switch(e.type){case"RestElement":m(e.argument),"Identifier"===e.argument.type&&y.pushIdentifier(e.argument);break;case"AssignmentPattern":I(e.right),"Identifier"===e.left.type&&y.pushIdentifier(e.left);break;case"Identifier":y.pushIdentifier(e);break;case"MemberExpression":I(e);break;case"ArrayPattern":h("Destructuring assignment like const [a, b] = array; is disallowed ",e.span);break;case"ObjectPattern":h("Destructuring assignment like const {a, b} = obj; is disallowed ",e.span);break;default:h(e.type,e.span)}}function b(e){var t;(null===(t=null==e?void 0:e.decorators)||void 0===t?void 0:t.length)&&h(e.type,e.span),e.pat&&m(e.pat)}function v(e){var t,r,s,a,n,o,i,c,p,l,u;switch(e.type){case"BlockStatement":y.enterScope(),null===(t=e.stmts)||void 0===t||t.forEach(v),y.exitScope();break;case"TryStatement":e.block&&v(e.block),y.enterScope(),(null===(r=e.handler)||void 0===r?void 0:r.param)&&m(null===(s=e.handler)||void 0===s?void 0:s.param),"BlockStatement"===(null===(n=null===(a=e.handler)||void 0===a?void 0:a.body)||void 0===n?void 0:n.type)&&(null===(i=null===(o=e.handler)||void 0===o?void 0:o.body)||void 0===i||i.stmts.forEach(v)),y.exitScope(),e.finalizer&&v(e.finalizer);break;case"ForStatement":if(y.enterScope(),e.init)if("VariableDeclaration"===e.init.type)v(e.init);else I(e.init);e.test&&I(e.test),e.update&&I(e.update),"BlockStatement"===e.body.type?null===(c=e.body.stmts)||void 0===c||c.forEach(v):v(e.body),y.exitScope();break;case"SwitchStatement":I(e.discriminant),null===(p=e.cases)||void 0===p||p.forEach((e=>{var t;e.test&&I(e.test),null===(t=e.consequent)||void 0===t||t.forEach(v)}));break;case"DoWhileStatement":case"WhileStatement":I(e.test),v(e.body);break;case"IfStatement":I(e.test),v(e.consequent),e.alternate&&v(e.alternate);break;case"ExpressionStatement":I(e.expression);break;case"FunctionDeclaration":k(e),y.pushIdentifier(e.identifier),y.enterScope(),y.pushIdentifier(Object.assign(Object.assign({},e.identifier),{value:"arguments"})),e.params.forEach(b),"BlockStatement"===(null===(l=null==e?void 0:e.body)||void 0===l?void 0:l.type)&&(null===(u=e.body.stmts)||void 0===u||u.forEach(v)),y.exitScope();break;case"ReturnStatement":e.argument&&I(e.argument);break;case"VariableDeclaration":for(const t of e.declarations){if("Identifier"===t.id.type)y.pushIdentifier(t.id);else h(t.id.type,t.id.span);t.init&&I(t.init)}break;case"BreakStatement":case"DebuggerStatement":case"ContinueStatement":case"EmptyStatement":break;default:h(e.type,e.span)}}function g(e){if("Computed"===e.type){I(e.expression)}}function S(e){var t,r,s;switch(e.type){case"KeyValueProperty":g(e.key),I(e.value);break;case"GetterProperty":g(e.key),y.enterScope(),"BlockStatement"===(null===(t=e.body)||void 0===t?void 0:t.type)&&e.body.stmts.forEach(v),y.exitScope();break;case"SetterProperty":g(e.key),y.enterScope(),m(e.param),"BlockStatement"===(null===(r=e.body)||void 0===r?void 0:r.type)&&e.body.stmts.forEach(v),y.exitScope();break;case"Identifier":y.pushAccess(e),E(e);break;case"MethodProperty":g(e.key),k(e),y.enterScope(),e.params.forEach(b),"BlockStatement"===(null===(s=e.body)||void 0===s?void 0:s.type)&&e.body.stmts.forEach(v),y.exitScope();break;case"SpreadElement":I(e.arguments);break;default:h(e.type,e.span)}}function x(e){return"Computed"===e.type?(I(e.expression),e.expression):"Identifier"===e.type?{type:"StringLiteral",span:e.span,value:e.value,raw:JSON.stringify(e.value)}:"PrivateName"===e.type?{type:"NullLiteral"}:void 0}function k(e){(e.async||e.generator)&&h(e.type,e.span)}function E(e){var t;"generate"===d&&s.includes(e.value)&&(null===(t=e._xhsScope)||void 0===t?void 0:t.checkGlobal(e.value))&&(e.value=`${i}.${e.value}`)}function I(e){var t,r,s,a,n,o;switch(e.type){case"FunctionExpression":k(e),e.identifier&&y.pushIdentifier(e.identifier),y.enterScope(),e.params.forEach(b),"BlockStatement"===(null===(t=e.body)||void 0===t?void 0:t.type)&&(null===(r=e.body.stmts)||void 0===r||r.forEach(v)),y.exitScope();break;case"ArrowFunctionExpression":k(e),y.enterScope(),e.params.forEach(m),"BlockStatement"===e.body.type?null===(s=e.body.stmts)||void 0===s||s.forEach(v):I(e.body),y.exitScope();break;case"CallExpression":switch(e.callee.type){case"Import":case"Super":h(e.callee.type,e.span);break;default:if("MemberExpression"===e.callee.type&&"generate"===d){const t=e.callee,r=e;r.type="CallExpression",r.callee={type:"Identifier",span:e.span,value:l,optional:!1},I(t.object),e.arguments.forEach((e=>I(e.expression))),r.arguments=[{expression:x(t.property)},{expression:t.object},{expression:{type:"ArrayExpression",elements:e.arguments,span:e.span}}]}else"MemberExpression"===e.callee.type&&"legacyGenerate"===d&&"Identifier"===e.callee.property.type&&"constructor"===e.callee.property.value?(e.callee.property.value=`${e.callee.property.value}.name`,I(e.callee.object),e.arguments.forEach((e=>I(e.expression)))):("Identifier"===e.callee.type&&("require"===e.callee.value&&(o=e,"scan"===d&&(f.push(o),"Identifier"===o.callee.type&&(o.callee._xhsScope=y.getCurrentScope()))),E(e.callee)),I(e.callee),e.arguments.forEach((e=>I(e.expression))))}break;case"ArrayExpression":null===(a=e.elements)||void 0===a||a.forEach((e=>{e&&I(e.expression)}));break;case"ObjectExpression":e.properties.forEach(S);break;case"TemplateLiteral":e.expressions.forEach(I);break;case"UpdateExpression":if("MemberExpression"===e.argument.type&&"generate"===d){const t=e;t.type="CallExpression",t.callee={type:"Identifier",span:e.span,value:c,optional:!1},I(e.argument.object),t.arguments=[{expression:x(e.argument.property)},{expression:e.argument.object},{expression:{type:"StringLiteral",span:e.span,value:e.operator,raw:JSON.stringify(e.operator)}}],Reflect.deleteProperty(e,"prefix"),Reflect.deleteProperty(e,"operator"),Reflect.deleteProperty(e,"argument")}else I(e.argument);break;case"UnaryExpression":I(e.argument);break;case"AssignmentExpression":if("MemberExpression"===e.left.type&&"generate"===d){const t=e;t.type="CallExpression",t.callee={type:"Identifier",span:e.span,value:c,optional:!1},I(e.right),I(e.left.object),t.arguments=[{expression:x(e.left.property)},{expression:e.left.object},{expression:{type:"StringLiteral",span:e.span,value:e.operator,raw:JSON.stringify(e.operator)}},{expression:e.right}],Reflect.deleteProperty(e,"left"),Reflect.deleteProperty(e,"right"),Reflect.deleteProperty(e,"operator")}else m(e.left),I(e.right);break;case"BinaryExpression":I(e.left),I(e.right);break;case"ConditionalExpression":I(e.test),I(e.consequent),I(e.alternate);break;case"MemberExpression":if("generate"===d){const t=e;t.type="CallExpression",t.callee={type:"Identifier",span:e.span,value:p,optional:!1},I(e.object),t.arguments=[{expression:x(e.property)},{expression:e.object}],Reflect.deleteProperty(e,"object"),Reflect.deleteProperty(e,"property")}else"legacyGenerate"===d&&"Identifier"===e.property.type&&"constructor"===e.property.value?(e.property.value=`${e.property.value}.name`,I(e.object)):(I(e.object),function(e){switch(e.type){case"Computed":I(e.expression);break;case"Identifier":break;default:h(e.type,e.span)}}(e.property));break;case"ParenthesisExpression":I(e.expression);break;case"SequenceExpression":null===(n=e.expressions)||void 0===n||n.forEach(I);break;case"Identifier":y.pushAccess(e),E(e);break;case"BigIntLiteral":case"NullLiteral":case"StringLiteral":case"BooleanLiteral":case"NumericLiteral":case"ThisExpression":case"RegExpLiteral":break;default:h(e.type,e.span)}}let j;const P=r(t,(t=>{d="scan",y.enterScope(),t.body.forEach(v),y.exitScope(),y.stop();return y.getDFSScope().map((e=>e.anlysis())).filter(Boolean).forEach((e=>{e&&h(`identifier(${e.variable}) is undefined`,e.span)})),f.forEach((e=>{var t,r,s;if("Identifier"===e.callee.type&&(null===(t=e.callee._xhsScope)||void 0===t?void 0:t.checkGlobal(e.callee.value))){const t=null===(s=null===(r=e.arguments)||void 0===r?void 0:r[0])||void 0===s?void 0:s.expression;if(t&&"StringLiteral"===(null==t?void 0:t.type)&&(null==t?void 0:t.value))try{const e=null==a?void 0:a(null==t?void 0:t.value);t.value=e,t.raw=JSON.stringify(e)}catch(t){h(t.message,e.span)}else h("require() must provide static path, dynamic or empty path is disallowed",e.span)}})),u.length?(t.body=[],t):(j=e.cloneDeep(t),d="generate",t.body.forEach(v),t)}));d="legacyGenerate";const _=(null===(n=null==j?void 0:j.body)||void 0===n?void 0:n.length)?r("var magic;",(()=>(j.body.forEach(v),j)),"es2015",!0).code:"";return{code:P.code,map:P.map,legacyCode:_,errors:u}};
@@ -0,0 +1,12 @@
1
+ import type { Span } from '@swc/core';
2
+ export declare const SJS_HELPER = "_sjs$";
3
+ declare function sjsCompiler(code: any, onRequire: (s: string) => string): {
4
+ code: string;
5
+ map: string | undefined;
6
+ legacyCode: string;
7
+ errors: {
8
+ message: string;
9
+ span: Span;
10
+ }[];
11
+ };
12
+ export default sjsCompiler;
@@ -0,0 +1,33 @@
1
+ import type { Span, Identifier } from '@swc/core';
2
+ export declare const SPECIFY_VAR: string[];
3
+ declare module "@swc/core" {
4
+ interface Identifier {
5
+ _xhsScope?: Scope;
6
+ }
7
+ }
8
+ export declare class Scope {
9
+ private parentScope;
10
+ private defines;
11
+ private access;
12
+ toString(): string;
13
+ anlysis(): {
14
+ variable: string;
15
+ span: Span;
16
+ } | undefined;
17
+ checkGlobal(key: string): boolean;
18
+ setAccess(key: string, value: Span): void;
19
+ setDefines(key: string, value: Span): void;
20
+ setParentScope(scope: Scope | undefined): void;
21
+ }
22
+ export declare class ScopeManager {
23
+ private _scopeStack;
24
+ private _scopeTables;
25
+ private _stop;
26
+ stop(): void;
27
+ enterScope(): void;
28
+ exitScope(): void;
29
+ getCurrentScope(): Scope | undefined;
30
+ pushIdentifier(id: Identifier): void;
31
+ pushAccess(id: Identifier): void;
32
+ getDFSScope(): Scope[];
33
+ }
@@ -0,0 +1,2 @@
1
+ import type { JscTarget, Program } from '@swc/core';
2
+ export declare const transform: (code: string, plugin?: (m: Program) => Program, target?: JscTarget, minify?: boolean) => import("@swc/core").Output;
@@ -0,0 +1,33 @@
1
+ import type { Span, Identifier } from '@swc/core';
2
+ export declare const SPECIFY_VAR: string[];
3
+ declare module "@swc/core" {
4
+ interface Identifier {
5
+ _xhsScope?: Scope;
6
+ }
7
+ }
8
+ export declare class Scope {
9
+ private parentScope;
10
+ private defines;
11
+ private access;
12
+ toString(): string;
13
+ anlysis(): {
14
+ variable: string;
15
+ span: Span;
16
+ } | undefined;
17
+ checkGlobal(key: string): boolean;
18
+ setAccess(key: string, value: Span): void;
19
+ setDefines(key: string, value: Span): void;
20
+ setParentScope(scope: Scope | undefined): void;
21
+ }
22
+ export declare class ScopeManager {
23
+ private _scopeStack;
24
+ private _scopeTables;
25
+ private _stop;
26
+ stop(): void;
27
+ enterScope(): void;
28
+ exitScope(): void;
29
+ getCurrentScope(): Scope | undefined;
30
+ pushIdentifier(id: Identifier): void;
31
+ pushAccess(id: Identifier): void;
32
+ getDFSScope(): Scope[];
33
+ }
@@ -0,0 +1,2 @@
1
+ import type { JscTarget, Program } from '@swc/core';
2
+ export declare const transform: (code: string, plugin?: (m: Program) => Program, target?: JscTarget, minify?: boolean) => import("@swc/core").Output;
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "xhs-mp-sjs-loader",
3
+ "version": "1.11.0",
4
+ "description": "sjs loader",
5
+ "preferGlobal": true,
6
+ "category": "esm",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "publishConfig": {
10
+ "registry": "https://registry.npmjs.org/",
11
+ "access": "public"
12
+ },
13
+ "files": [
14
+ "dist/"
15
+ ],
16
+ "keywords": [
17
+ "sjs-loader"
18
+ ],
19
+ "author": "zyu1 <fuzhu@xiaohongshu.com>",
20
+ "license": "MIT",
21
+ "dependencies": {
22
+ "fs-extra": "10.0.0",
23
+ "lodash": "^4.17.21",
24
+ "querystring": "0.2.1",
25
+ "source-map": "^0.6.1"
26
+ },
27
+ "peerDependencies": {
28
+ "@swc/core": "^1.3.83"
29
+ },
30
+ "devDependencies": {
31
+ "@rollup/plugin-commonjs": "^25.0.7",
32
+ "@rollup/plugin-json": "^6.1.0",
33
+ "@swc/core": "^1.3.83",
34
+ "@types/fs-extra": "9.0.12",
35
+ "@types/node": "14",
36
+ "@xhs/eslint-plugin-xhs": "1.0.2",
37
+ "cpy-cli": "3.1.1",
38
+ "eslint": "7.27.0",
39
+ "eslint-config-prettier": "8.1.0",
40
+ "jest": "27.0.6",
41
+ "prettier": "~3.0.2",
42
+ "rimraf": "^5.0.5",
43
+ "rollup": "^4.9.6",
44
+ "rollup-plugin-alias": "^2.2.0",
45
+ "rollup-plugin-terser": "^7.0.2",
46
+ "rollup-plugin-typescript2": "^0.36.0",
47
+ "typescript": "^5.4.3",
48
+ "webpack": "^5.88.2"
49
+ },
50
+ "scripts": {
51
+ "watch": "npm run prebuild && rollup -c -w --sourcemap true",
52
+ "build": "npm run prebuild && rollup -c && tsc --emitDeclarationOnly --declaration",
53
+ "version": "formula changelog && git add .",
54
+ "clean": "rimraf ./dist",
55
+ "prebuild": "npm run clean",
56
+ "lint": "eslint . --fix",
57
+ "compile": "jest '(/tests/.*\\.spec)\\.ts$'",
58
+ "unit": "jest '(/tests/.*\\.test)\\.ts$' --collectCoverage=false",
59
+ "test": "echo skip test"
60
+ },
61
+ "readme": "# 构建工具\n"
62
+ }