xhs-mp-compiler-cli 1.4.0-beta.2 → 1.4.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.
Files changed (45) hide show
  1. package/dist/compiler.d.ts +13 -15
  2. package/dist/compiler.js +40 -96
  3. package/dist/{compilerImpl.js → compilerCP.js} +42 -29
  4. package/dist/index.d.ts +4 -3
  5. package/dist/index.js +5 -5
  6. package/dist/packs/mp-pack/index.js +1 -0
  7. package/dist/packs/webpack/dev-server/index.js +0 -1
  8. package/dist/packs/webpack/index.d.ts +1 -1
  9. package/dist/packs/webpack/index.js +4 -3
  10. package/dist/packs/webpack/webpack.d.ts +1 -1
  11. package/dist/packs/webpack/webpack.js +2 -6
  12. package/dist/presets/configs/miniprogram/render/index.d.ts +3 -2
  13. package/dist/presets/configs/miniprogram/render/index.js +2 -2
  14. package/dist/presets/configs/miniprogram/service/index.js +0 -1
  15. package/dist/utils/apm/index.js +30 -13
  16. package/package.json +12 -26
  17. package/dist/compilerImpl.d.ts +0 -1
  18. package/dist/compilerImplChildProcess.js +0 -33
  19. package/dist/compilerImplThreadWorker.d.ts +0 -1
  20. package/dist/compilerImplThreadWorker.js +0 -30
  21. package/dist/compilerImplWebWorker.d.ts +0 -6
  22. package/dist/compilerImplWebWorker.js +0 -25
  23. package/dist/dev-server/compatibleAPI.d.ts +0 -25
  24. package/dist/dev-server/compatibleAPI.js +0 -56
  25. package/dist/dev-server/escapeHtml.d.ts +0 -5
  26. package/dist/dev-server/escapeHtml.js +0 -50
  27. package/dist/dev-server/etag.d.ts +0 -10
  28. package/dist/dev-server/etag.js +0 -69
  29. package/dist/dev-server/getFilenameFromUrl.d.ts +0 -9
  30. package/dist/dev-server/getFilenameFromUrl.js +0 -102
  31. package/dist/dev-server/getPaths.d.ts +0 -6
  32. package/dist/dev-server/getPaths.js +0 -20
  33. package/dist/dev-server/index.d.ts +0 -98
  34. package/dist/dev-server/index.js +0 -90
  35. package/dist/dev-server/memorize.d.ts +0 -7
  36. package/dist/dev-server/memorize.js +0 -23
  37. package/dist/dev-server/middleware.d.ts +0 -2
  38. package/dist/dev-server/middleware.js +0 -477
  39. package/dist/dev-server/ready.d.ts +0 -4
  40. package/dist/dev-server/ready.js +0 -11
  41. package/dist/sharedFs.d.ts +0 -2
  42. package/dist/sharedFs.js +0 -4
  43. package/dist/utils/workerPool.d.ts +0 -29
  44. package/dist/utils/workerPool.js +0 -115
  45. /package/dist/{compilerImplChildProcess.d.ts → compilerCP.d.ts} +0 -0
@@ -5,16 +5,20 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
7
  };
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
8
11
  Object.defineProperty(exports, "__esModule", { value: true });
9
12
  exports.createReporter = void 0;
13
+ const path_1 = __importDefault(require("path"));
10
14
  const xhs_mp_utils_1 = require("xhs-mp-utils");
15
+ const fs_extra_1 = require("fs-extra");
11
16
  const pkg = require('../../../package.json');
12
17
  function debug(...data) {
13
- return;
14
18
  // return process.send?.(JSON.stringify({ method: 'debugger', data }))
15
19
  }
16
20
  const cacheMap = {};
17
- function once(target, key, descriptor) {
21
+ function onceByParams(target, key, descriptor) {
18
22
  const originalMethod = descriptor.value;
19
23
  descriptor.value = function (...args) {
20
24
  try {
@@ -87,22 +91,22 @@ class Reporter {
87
91
  }
88
92
  }
89
93
  __decorate([
90
- once
94
+ onceByParams
91
95
  ], Reporter.prototype, "reportThirdFramework", null);
92
96
  __decorate([
93
- once
97
+ onceByParams
94
98
  ], Reporter.prototype, "reportMlFeature", null);
95
99
  __decorate([
96
- once
100
+ onceByParams
97
101
  ], Reporter.prototype, "reportMlWebComponent", null);
98
102
  __decorate([
99
- once
103
+ onceByParams
100
104
  ], Reporter.prototype, "reportJSONComponent", null);
101
105
  __decorate([
102
- once
106
+ onceByParams
103
107
  ], Reporter.prototype, "reportStyleIsolation", null);
104
108
  __decorate([
105
- once
109
+ onceByParams
106
110
  ], Reporter.prototype, "reportJSONApp", null);
107
111
  function isObject(t) {
108
112
  return t != null && typeof t === 'object';
@@ -120,18 +124,20 @@ function createReporter(project, option) {
120
124
  var _a, _b, _c;
121
125
  try {
122
126
  const app_id = option === null || option === void 0 ? void 0 : option.upload_app_id;
123
- if (!app_id) {
127
+ // 上传时有version
128
+ const version = (_a = option === null || option === void 0 ? void 0 : option.pkgInfo) === null || _a === void 0 ? void 0 : _a.version;
129
+ if (!app_id || !version) {
124
130
  return;
125
131
  }
126
132
  const apm = new xhs_mp_utils_1.APM(`mp-compiler-${pkg.version}`);
127
- (_a = apm === null || apm === void 0 ? void 0 : apm.setContext) === null || _a === void 0 ? void 0 : _a.call(apm, app_id, {
133
+ (_b = apm === null || apm === void 0 ? void 0 : apm.setContext) === null || _b === void 0 ? void 0 : _b.call(apm, app_id, {
128
134
  app_id,
129
135
  app_name: app_id,
130
136
  third_name: app_id,
131
137
  page_key: '0',
132
138
  app_env: 0,
133
139
  app_type: 0,
134
- bundle_version: ((_b = option === null || option === void 0 ? void 0 : option.pkgInfo) === null || _b === void 0 ? void 0 : _b.version) || '',
140
+ bundle_version: version,
135
141
  base_version: ((_c = project.projectJsonContent) === null || _c === void 0 ? void 0 : _c.libVersion) || ''
136
142
  });
137
143
  const reporter = new Reporter(apm);
@@ -170,8 +176,19 @@ function createReporter(project, option) {
170
176
  }
171
177
  }
172
178
  }
173
- // 延时上报
174
- setTimeout(reportJSON);
179
+ reportJSON();
180
+ // 统计taro版本
181
+ const vendor = path_1.default.join(project.miniprogramDir, 'vendors.js.LICENSE.txt');
182
+ const taro = path_1.default.join(project.miniprogramDir, 'taro.js');
183
+ if ((0, fs_extra_1.pathExistsSync)(taro) && (0, fs_extra_1.pathExistsSync)(vendor)) {
184
+ const txt = (0, fs_extra_1.readFileSync)(vendor, { encoding: 'utf-8' });
185
+ if (txt.includes('React') || txt.includes('react')) {
186
+ reporter.reportThirdFramework(xhs_mp_utils_1.FeatureReporter.ThirdFramework.taroReact);
187
+ }
188
+ else if (txt.includes('@vue') || txt.includes('Vue')) {
189
+ reporter.reportThirdFramework(xhs_mp_utils_1.FeatureReporter.ThirdFramework.taroVue);
190
+ }
191
+ }
175
192
  return reporter;
176
193
  }
177
194
  catch (error) {
package/package.json CHANGED
@@ -1,24 +1,11 @@
1
1
  {
2
2
  "name": "xhs-mp-compiler-cli",
3
- "version": "1.4.0-beta.2",
3
+ "version": "1.4.0",
4
4
  "description": "xhs mp command tool.",
5
5
  "preferGlobal": true,
6
6
  "category": "esm",
7
7
  "main": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",
9
- "exports": {
10
- ".": {
11
- "require": "./dist/index.js",
12
- "import": "./dist/index.js",
13
- "types": "./dist/index.d.ts"
14
- },
15
- "./sharedFs": {
16
- "require": "./dist/sharedFs.js",
17
- "import": "./dist/sharedFs.js",
18
- "types": "./dist/sharedFs.d.ts"
19
- },
20
- "./*.map": "./dist/*.map"
21
- },
22
9
  "publishConfig": {
23
10
  "registry": "https://registry.npmjs.org/",
24
11
  "access": "public"
@@ -33,6 +20,11 @@
33
20
  "author": "zyu1 <fuzhu@xiaohongshu.com>",
34
21
  "license": "MIT",
35
22
  "dependencies": {
23
+ "archiver": "^7.0.1",
24
+ "debug": "^4.3.4",
25
+ "globby": "^11.1.0",
26
+ "query-string": "^8.1.0",
27
+ "yauzl": "^2.10.0",
36
28
  "@babel/core": "^7.12.10",
37
29
  "@babel/generator": "^7.12.10",
38
30
  "@babel/parser": "^7.12.10",
@@ -46,11 +38,11 @@
46
38
  "@swc/core": "^1.3.74",
47
39
  "@swc/helpers": "^0.5.1",
48
40
  "@vue/babel-preset-jsx": "^1.2.4",
49
- "archiver": "^7.0.1",
50
41
  "babel-loader": "^8.2.2",
51
42
  "babel-plugin-istanbul": "^6.0.0",
52
43
  "babel-preset-minify": "^0.4.3",
53
44
  "clean-webpack-plugin": "^3.0.0",
45
+ "uuid": "^9.0.1",
54
46
  "clone": "^2.1.2",
55
47
  "commander": "^8.0.0",
56
48
  "copy-webpack-plugin": "^9.0.1",
@@ -58,13 +50,11 @@
58
50
  "css-loader": "^5.0.2",
59
51
  "css-minimizer-webpack-plugin": "^1.2.0",
60
52
  "cssnano": "4.1.10",
61
- "debug": "^4.3.4",
62
53
  "enhanced-resolve": "^5.8.3",
63
54
  "fork-ts-checker-webpack-plugin": "^6.1.0",
64
55
  "friendly-errors-webpack-plugin": "^1.7.0",
65
56
  "fs-extra": "^10.0.0",
66
57
  "glob": "^8.0.1",
67
- "globby": "^11.1.0",
68
58
  "htmlparser2": "^7.1.2",
69
59
  "lodash": "^4.17.21",
70
60
  "mini-css-extract-plugin": "^1.6.2",
@@ -77,7 +67,6 @@
77
67
  "postcss-prefix-selector": "^1.10.0",
78
68
  "postcss-preset-env": "^6.7.0",
79
69
  "postcss-pxtorem": "^5.1.1",
80
- "query-string": "^8.1.0",
81
70
  "querystring": "^0.2.1",
82
71
  "source-map": "^0.6.1",
83
72
  "speed-measure-webpack-plugin": "^1.5.0",
@@ -86,7 +75,6 @@
86
75
  "tapable": "^2.2.1",
87
76
  "terser-webpack-plugin": "^5.2.4",
88
77
  "typescript": "^5.1.6",
89
- "uuid": "^9.0.1",
90
78
  "vue": "^3.2.26",
91
79
  "vue-loader": "^16.4.1",
92
80
  "vue-style-loader": "^4.1.2",
@@ -94,13 +82,11 @@
94
82
  "webpack-bundle-analyzer": "^4.4.0",
95
83
  "webpack-chain": "^6.5.1",
96
84
  "webpack-sources": "^3.2.2",
97
- "xhs-mp-compiler-ml-loader": "1.4.0-beta.2",
98
- "xhs-mp-compiler-utils": "1.3.0-beta.2",
99
- "xhs-mp-pack": "^1.3.0-beta.2",
100
- "xhs-mp-project": "^1.3.0-beta.2",
101
- "xhs-mp-utils": "^1.4.0-beta.2",
102
- "xhs-mp-shared-fs": "^1.1.0-beta.2",
103
- "yauzl": "^2.10.0"
85
+ "xhs-mp-compiler-ml-loader": "1.4.0",
86
+ "xhs-mp-compiler-utils": "1.2.5",
87
+ "xhs-mp-pack": "^1.2.5",
88
+ "xhs-mp-project": "^1.2.5",
89
+ "xhs-mp-utils": "^1.4.0"
104
90
  },
105
91
  "devDependencies": {
106
92
  "@types/babel__generator": "7.6.3",
@@ -1 +0,0 @@
1
- export declare function runCompilerImpl(initData: any, onMessage: any, _sendToMaster: any): void;
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const compilerImpl_1 = require("./compilerImpl");
4
- let initData = JSON.parse(process.argv[2]);
5
- /* 发送消息给父进程 */
6
- const sendToMaster = (method, data = {}) => {
7
- var _a;
8
- (_a = process.send) === null || _a === void 0 ? void 0 : _a.call(process, JSON.stringify(Object.assign({ method }, data)));
9
- };
10
- function onMessage(handler) {
11
- process.on('message', (message) => {
12
- let objData = {};
13
- try {
14
- objData = JSON.parse(message);
15
- }
16
- catch (error) {
17
- // do something
18
- }
19
- handler(message);
20
- });
21
- }
22
- process.on('uncaughtException', error => {
23
- console.error(error);
24
- sendToMaster('emitCompileStats', {
25
- stats: {
26
- errors: [error.message]
27
- }
28
- });
29
- });
30
- process.on('disconnect', () => {
31
- process.exit(-1);
32
- });
33
- (0, compilerImpl_1.runCompilerImpl)(initData, onMessage, sendToMaster);
@@ -1 +0,0 @@
1
- export {};
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const compilerImpl_1 = require("./compilerImpl");
4
- const { workerData, parentPort } = require('worker_threads');
5
- const initData = workerData || {};
6
- /* 发送消息主线程 */
7
- const sendToMaster = (method, data = {}) => {
8
- parentPort.postMessage(JSON.stringify(Object.assign({ method }, data)));
9
- };
10
- const onMessage = handler => {
11
- parentPort.on('message', (message) => {
12
- let objData = {};
13
- try {
14
- objData = JSON.parse(message);
15
- }
16
- catch (error) {
17
- // do something
18
- }
19
- handler(objData);
20
- });
21
- };
22
- parentPort.on('uncaughtException', error => {
23
- console.error(error);
24
- sendToMaster('emitCompileStats', {
25
- stats: {
26
- errors: [error.message]
27
- }
28
- });
29
- });
30
- (0, compilerImpl_1.runCompilerImpl)(initData, onMessage, sendToMaster);
@@ -1,6 +0,0 @@
1
- declare const runCompilerImpl: any;
2
- declare const createCompiler: any;
3
- declare let globalConfig: any;
4
- declare let projectCompiler: any;
5
- declare const sendToMaster: (method: any, data?: {}) => void;
6
- declare const onMessage: (handler: any) => void;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- const { runCompilerImpl } = require('./compilerImpl');
3
- const { createCompiler } = require('./packs');
4
- let globalConfig = {};
5
- let projectCompiler;
6
- /* 发送消息主线程 */
7
- const sendToMaster = (method, data = {}) => {
8
- // @ts-ignore
9
- postMessage(JSON.stringify(Object.assign({ method }, data)));
10
- };
11
- const onMessage = handler => {
12
- // @ts-ignore
13
- onmessage = (e) => {
14
- const message = e.data;
15
- let objData = {};
16
- try {
17
- objData = JSON.parse(message);
18
- }
19
- catch (error) {
20
- // do something
21
- }
22
- handler(objData);
23
- };
24
- };
25
- runCompilerImpl(null, onMessage, sendToMaster);
@@ -1,25 +0,0 @@
1
- /// <reference types="node/http.js" />
2
- /// <reference types="node/fs.js" />
3
- import { Response } from "./index.js";
4
- import { ReadStream, createReadStream as fsCreateReadStream } from "fs";
5
- import { ServerResponse } from "http";
6
- export interface ExpectedResponse {
7
- status?: (status: number) => void;
8
- send?: (data: any) => void;
9
- pipeInto?: (data: any) => void;
10
- }
11
- declare function setStatusCode(res: ServerResponse & ExpectedResponse, code: number): void;
12
- declare function pipe(res: ServerResponse & ExpectedResponse, bufferOrStream: ReadStream): void;
13
- declare function send(res: Response, bufferOrStream: string | Buffer): void;
14
- interface OutputFileSystem {
15
- createReadStream?: typeof fsCreateReadStream;
16
- readFileSync?: (path: string | Buffer, options?: {
17
- encoding?: null;
18
- flag?: string;
19
- }) => Buffer;
20
- }
21
- declare function createReadStreamOrReadFileSync(filename: string, outputFileSystem: OutputFileSystem, start: number, end: number): {
22
- bufferOrStream: Buffer | ReadStream;
23
- byteLength: number;
24
- };
25
- export { setStatusCode, send, pipe, createReadStreamOrReadFileSync };
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createReadStreamOrReadFileSync = exports.pipe = exports.send = exports.setStatusCode = void 0;
4
- // Function to set status code
5
- function setStatusCode(res, code) {
6
- // Pseudo API
7
- if (typeof res.status === "function") {
8
- res.status(code);
9
- return;
10
- }
11
- // Node.js API
12
- res.statusCode = code;
13
- }
14
- exports.setStatusCode = setStatusCode;
15
- // Function to pipe data to response
16
- function pipe(res, bufferOrStream) {
17
- // Pseudo API and Koa API
18
- if (typeof res.pipeInto === "function") {
19
- res.pipeInto(bufferOrStream);
20
- return;
21
- }
22
- // Node.js API and Express API and Hapi API
23
- bufferOrStream.pipe(res);
24
- }
25
- exports.pipe = pipe;
26
- // Function to send data to response
27
- function send(res, bufferOrStream) {
28
- // Pseudo API and Express API and Koa API
29
- if (typeof res.send === "function") {
30
- res.send(bufferOrStream);
31
- return;
32
- }
33
- res.end(bufferOrStream);
34
- }
35
- exports.send = send;
36
- // Function to create read stream or read file synchronously
37
- function createReadStreamOrReadFileSync(filename, outputFileSystem, start, end) {
38
- let bufferOrStream;
39
- let byteLength;
40
- // Stream logic
41
- const isFsSupportsStream = typeof outputFileSystem.createReadStream === "function";
42
- if (isFsSupportsStream) {
43
- bufferOrStream = outputFileSystem.createReadStream(filename, {
44
- start,
45
- end,
46
- });
47
- // Handle files with zero bytes
48
- byteLength = end === 0 ? 0 : end - start + 1;
49
- }
50
- else {
51
- bufferOrStream = outputFileSystem.readFileSync(filename);
52
- byteLength = bufferOrStream.byteLength;
53
- }
54
- return { bufferOrStream, byteLength };
55
- }
56
- exports.createReadStreamOrReadFileSync = createReadStreamOrReadFileSync;
@@ -1,5 +0,0 @@
1
- /**
2
- * Escapes a string for use in HTML.
3
- */
4
- declare function escapeHtml(string: string): string;
5
- export default escapeHtml;
@@ -1,50 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const matchHtmlRegExp = /["'&<>]/;
4
- /**
5
- * Escapes a string for use in HTML.
6
- */
7
- function escapeHtml(string) {
8
- const str = `${string}`;
9
- const match = matchHtmlRegExp.exec(str);
10
- if (!match) {
11
- return str;
12
- }
13
- let escape;
14
- let html = "";
15
- let index = 0;
16
- let lastIndex = 0;
17
- for ({ index } = match; index < str.length; index++) {
18
- switch (str.charCodeAt(index)) {
19
- // "
20
- case 34:
21
- escape = "&quot;";
22
- break;
23
- // &
24
- case 38:
25
- escape = "&amp;";
26
- break;
27
- // '
28
- case 39:
29
- escape = "&#39;";
30
- break;
31
- // <
32
- case 60:
33
- escape = "&lt;";
34
- break;
35
- // >
36
- case 62:
37
- escape = "&gt;";
38
- break;
39
- default:
40
- continue;
41
- }
42
- if (lastIndex !== index) {
43
- html += str.substring(lastIndex, index);
44
- }
45
- lastIndex = index + 1;
46
- html += escape;
47
- }
48
- return lastIndex !== index ? html + str.substring(lastIndex, index) : html;
49
- }
50
- exports.default = escapeHtml;
@@ -1,10 +0,0 @@
1
- /// <reference types="node/fs" />
2
- import { Stats, ReadStream } from 'fs';
3
- /**
4
- * Create a simple ETag.
5
- */
6
- declare function etag(entity: Buffer | ReadStream | Stats): Promise<{
7
- hash: string;
8
- buffer?: Buffer;
9
- }>;
10
- export default etag;
@@ -1,69 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const crypto_1 = require("crypto");
13
- /**
14
- * Generate a tag for a stat.
15
- */
16
- function statTag(stat) {
17
- const mtime = stat.mtime.getTime().toString(16);
18
- const size = stat.size.toString(16);
19
- return { hash: `W/"${size}-${mtime}"` };
20
- }
21
- /**
22
- * Generate an entity tag.
23
- */
24
- function entityTag(entity) {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- const sha1 = (0, crypto_1.createHash)('sha1');
27
- if (!Buffer.isBuffer(entity)) {
28
- let byteLength = 0;
29
- const buffers = [];
30
- yield new Promise((resolve, reject) => {
31
- entity
32
- .on('data', (chunk) => {
33
- sha1.update(chunk);
34
- buffers.push(chunk);
35
- byteLength += chunk.byteLength;
36
- })
37
- .on('end', () => {
38
- resolve(sha1);
39
- })
40
- .on('error', reject);
41
- });
42
- return {
43
- buffer: Buffer.concat(buffers),
44
- hash: `"${byteLength.toString(16)}-${sha1.digest('base64').substring(0, 27)}"`,
45
- };
46
- }
47
- if (entity.byteLength === 0) {
48
- // Fast-path empty
49
- return { hash: '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"' };
50
- }
51
- // Compute hash of entity
52
- const hash = sha1.update(entity).digest('base64').substring(0, 27);
53
- // Compute length of entity
54
- const { byteLength } = entity;
55
- return { hash: `"${byteLength.toString(16)}-${hash}"` };
56
- });
57
- }
58
- /**
59
- * Create a simple ETag.
60
- */
61
- function etag(entity) {
62
- return __awaiter(this, void 0, void 0, function* () {
63
- const isStrong = Buffer.isBuffer(entity) || entity.pipe !== undefined;
64
- return isStrong
65
- ? entityTag(entity)
66
- : statTag(entity);
67
- });
68
- }
69
- exports.default = etag;
@@ -1,9 +0,0 @@
1
- /// <reference types="node/fs" />
2
- import { Stats } from 'fs';
3
- import { FilledContext } from '.';
4
- export interface Extra {
5
- stats?: Stats;
6
- errorCode?: number;
7
- }
8
- declare function getFilenameFromUrl(context: FilledContext, url: string, extra?: Extra): string | undefined;
9
- export default getFilenameFromUrl;
@@ -1,102 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const path_1 = __importDefault(require("path"));
7
- const url_1 = require("url");
8
- const querystring_1 = __importDefault(require("querystring"));
9
- const fs_1 = require("fs");
10
- const memorize_1 = __importDefault(require("./memorize"));
11
- const getPaths_1 = __importDefault(require("./getPaths"));
12
- // eslint-disable-next-line no-undefined
13
- const memoizedParse = (0, memorize_1.default)(url_1.parse, undefined, (value) => {
14
- if (value.pathname) {
15
- // eslint-disable-next-line no-param-reassign
16
- value.pathname = decode(value.pathname);
17
- }
18
- return value;
19
- });
20
- const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
21
- /**
22
- * decodeURIComponent.
23
- *
24
- * Allows V8 to only deoptimize this fn instead of all of send().
25
- */
26
- function decode(input) {
27
- return querystring_1.default.unescape(input);
28
- }
29
- // TODO refactor me in the next major release, this function should return `{ filename, stats, error }`
30
- // TODO fix redirect logic when `/` at the end, like https://github.com/pillarjs/send/blob/master/index.js#L586
31
- function getFilenameFromUrl(context, url, extra = {}) {
32
- const { options } = context;
33
- const paths = (0, getPaths_1.default)(context);
34
- let foundFilename;
35
- let urlObject;
36
- try {
37
- // The `url` property of the `request` is contains only `pathname`, `search` and `hash`
38
- urlObject = memoizedParse(url, false, true);
39
- }
40
- catch (_ignoreError) {
41
- return;
42
- }
43
- for (const { publicPath, outputPath } of paths) {
44
- let filename;
45
- let publicPathObject;
46
- try {
47
- publicPathObject = memoizedParse(publicPath !== "auto" && publicPath ? publicPath : "/", false, true);
48
- }
49
- catch (_ignoreError) {
50
- continue;
51
- }
52
- const { pathname } = urlObject;
53
- const { pathname: publicPathPathname } = publicPathObject;
54
- if (pathname && pathname.startsWith(publicPathPathname)) {
55
- // Null byte(s)
56
- if (pathname.includes("\0")) {
57
- extra.errorCode = 400;
58
- return;
59
- }
60
- // ".." is malicious
61
- if (UP_PATH_REGEXP.test(path_1.default.normalize(`./${pathname}`))) {
62
- extra.errorCode = 403;
63
- return;
64
- }
65
- // Strip the `pathname` property from the `publicPath` option from the start of requested url
66
- // `/complex/foo.js` => `foo.js`
67
- // and add outputPath
68
- // `foo.js` => `/home/user/my-project/dist/foo.js`
69
- filename = path_1.default.join(outputPath, pathname.slice(publicPathPathname.length));
70
- try {
71
- extra.stats = (0, fs_1.statSync)(filename);
72
- }
73
- catch (_ignoreError) {
74
- continue;
75
- }
76
- if (extra.stats.isFile()) {
77
- foundFilename = filename;
78
- break;
79
- }
80
- else if (extra.stats.isDirectory() &&
81
- (typeof options.index === "undefined" || options.index)) {
82
- const indexValue = typeof options.index === "undefined" ||
83
- typeof options.index === "boolean"
84
- ? "index.html"
85
- : options.index;
86
- filename = path_1.default.join(filename, indexValue);
87
- try {
88
- extra.stats = (0, fs_1.statSync)(filename);
89
- }
90
- catch (__ignoreError) {
91
- continue;
92
- }
93
- if (extra.stats.isFile()) {
94
- foundFilename = filename;
95
- break;
96
- }
97
- }
98
- }
99
- }
100
- return foundFilename;
101
- }
102
- exports.default = getFilenameFromUrl;
@@ -1,6 +0,0 @@
1
- import { FilledContext } from ".";
2
- declare function getPaths(context: FilledContext): {
3
- outputPath: string;
4
- publicPath: string;
5
- }[];
6
- export = getPaths;
@@ -1,20 +0,0 @@
1
- "use strict";
2
- function getPaths(context) {
3
- const { stats, options } = context;
4
- const childStats = stats.stats
5
- ? stats.stats
6
- : [stats];
7
- const publicPaths = [];
8
- for (const { compilation } of childStats) {
9
- // The `output.path` is always present and always absolute
10
- const outputPath = compilation.getPath(compilation.outputOptions.path || "");
11
- const publicPath = options.publicPath
12
- ? compilation.getPath(options.publicPath)
13
- : compilation.outputOptions.publicPath
14
- ? compilation.getPath(compilation.outputOptions.publicPath)
15
- : "";
16
- publicPaths.push({ outputPath, publicPath });
17
- }
18
- return publicPaths;
19
- }
20
- module.exports = getPaths;