@utoo/web 1.2.0-rc.1 → 1.2.0-rc.11

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 (58) hide show
  1. package/README.md +55 -0
  2. package/esm/{aa0ad15d2a587c43406f.wasm → 7a09b7ab69b3abceced7.wasm} +0 -0
  3. package/esm/index.d.ts +3 -2
  4. package/esm/index.js +3 -2
  5. package/esm/loaderWorker.js +1 -1
  6. package/esm/loaderWorker.js.LICENSE.txt +0 -21
  7. package/esm/{forkedProject.d.ts → project/ForkedProject.d.ts} +5 -3
  8. package/esm/{forkedProject.js → project/ForkedProject.js} +8 -0
  9. package/esm/{internalProject.d.ts → project/InternalProject.d.ts} +5 -4
  10. package/esm/project/InternalProject.js +134 -0
  11. package/esm/{project.d.ts → project/Project.d.ts} +3 -1
  12. package/esm/{project.js → project/Project.js} +19 -6
  13. package/esm/serviceWorker.js +4 -3
  14. package/esm/{type.d.ts → types.d.ts} +43 -0
  15. package/esm/types.js +56 -0
  16. package/esm/utils/sabcom.d.ts +61 -0
  17. package/esm/utils/sabcom.js +203 -0
  18. package/esm/utoo/index.d.ts +73 -38
  19. package/esm/utoo/index.js +167 -143
  20. package/esm/utoo/index_bg.wasm +0 -0
  21. package/esm/webpackLoaders/{worker/cjs.js → cjs.js} +65 -41
  22. package/esm/webpackLoaders/loaderWorkerPool.d.ts +2 -0
  23. package/esm/webpackLoaders/loaderWorkerPool.js +90 -0
  24. package/esm/webpackLoaders/polyfills/fsPolyfill.d.ts +78 -0
  25. package/esm/webpackLoaders/{worker/polyfills → polyfills}/fsPolyfill.js +154 -191
  26. package/esm/webpackLoaders/polyfills/fsPromisesPolyfill.d.ts +26 -0
  27. package/esm/webpackLoaders/polyfills/fsPromisesPolyfill.js +112 -0
  28. package/esm/webpackLoaders/{worker/polyfills → polyfills}/nodePolyFills.d.ts +0 -2
  29. package/esm/webpackLoaders/{worker/polyfills → polyfills}/nodePolyFills.js +4 -3
  30. package/esm/webpackLoaders/{worker/type.d.ts → types.d.ts} +1 -0
  31. package/esm/webpackLoaders/{worker/index.js → worker.js} +8 -3
  32. package/esm/worker.js +2 -2
  33. package/package.json +14 -13
  34. package/esm/internalProject.js +0 -110
  35. package/esm/loaderWorkerPool.d.ts +0 -3
  36. package/esm/loaderWorkerPool.js +0 -125
  37. package/esm/sabcom.d.ts +0 -31
  38. package/esm/sabcom.js +0 -71
  39. package/esm/type.js +0 -12
  40. package/esm/webpackLoaders/loaders/less-loader/index.d.ts +0 -3
  41. package/esm/webpackLoaders/loaders/less-loader/index.js +0 -103
  42. package/esm/webpackLoaders/loaders/less-loader/options.json +0 -67
  43. package/esm/webpackLoaders/loaders/less-loader/utils.d.ts +0 -14
  44. package/esm/webpackLoaders/loaders/less-loader/utils.js +0 -217
  45. package/esm/webpackLoaders/worker/polyfills/fastGlobPolyfill.d.ts +0 -2
  46. package/esm/webpackLoaders/worker/polyfills/fastGlobPolyfill.js +0 -48
  47. package/esm/webpackLoaders/worker/polyfills/fsPolyfill.d.ts +0 -124
  48. package/esm/webpackLoaders/worker/polyfills/fsPromisesPolyfill.d.ts +0 -9
  49. package/esm/webpackLoaders/worker/polyfills/fsPromisesPolyfill.js +0 -9
  50. /package/esm/{installServiceWorker.d.ts → utils/installServiceWorker.d.ts} +0 -0
  51. /package/esm/{installServiceWorker.js → utils/installServiceWorker.js} +0 -0
  52. /package/esm/{message.d.ts → utils/message.d.ts} +0 -0
  53. /package/esm/{message.js → utils/message.js} +0 -0
  54. /package/esm/webpackLoaders/{worker/cjs.d.ts → cjs.d.ts} +0 -0
  55. /package/esm/webpackLoaders/{worker/polyfills → polyfills}/workerThreadsPolyfill.d.ts +0 -0
  56. /package/esm/webpackLoaders/{worker/polyfills → polyfills}/workerThreadsPolyfill.js +0 -0
  57. /package/esm/webpackLoaders/{worker/type.js → types.js} +0 -0
  58. /package/esm/webpackLoaders/{worker/index.d.ts → worker.d.ts} +0 -0
package/package.json CHANGED
@@ -1,37 +1,38 @@
1
1
  {
2
2
  "name": "@utoo/web",
3
- "version": "1.2.0-rc.1",
3
+ "version": "1.2.0-rc.11",
4
4
  "module": "esm/index.js",
5
5
  "types": "esm/index.d.ts",
6
6
  "files": [
7
- "esm/*"
7
+ "esm"
8
8
  ],
9
9
  "scripts": {
10
- "install-toolchain": "cargo install wasm-bindgen-cli@0.2.104 && brew install binaryen",
10
+ "install-toolchain": "cargo install wasm-bindgen-cli@0.2.104 && brew install binaryen llvm",
11
11
  "build-wasm": "cargo build -p utoo-wasm --target wasm32-unknown-unknown -Z build-std=panic_abort,std",
12
12
  "build-wasm:pm": "cargo build -p utoo-wasm --target wasm32-unknown-unknown --no-default-features -Z build-std=panic_abort,std",
13
13
  "bindgen-dev": "wasm-bindgen ../../target/wasm32-unknown-unknown/wasm-dev/utoo_wasm.wasm --out-dir src/utoo --out-name index --target web --debug --keep-debug --no-demangle",
14
14
  "bindgen-build": "wasm-bindgen ../../target/wasm32-unknown-unknown/release/utoo_wasm.wasm --out-dir src/utoo --out-name index --target web",
15
15
  "bindgen-build:local": "wasm-bindgen ../../target/wasm32-unknown-unknown/release-local/utoo_wasm.wasm --out-dir src/utoo --out-name index --target web",
16
- "pretsc": "npm run build --workspace @utoo/pack-shared",
17
- "tsc": "rm -rf esm && tsc -p ./tsconfig.json",
18
- "dev": "npm run build-wasm -- --profile wasm-dev && npm run bindgen-dev && npm run tsc && cp src/utoo/index_bg.wasm esm/utoo",
19
- "dev:pm": "npm run build-wasm:pm -- --profile wasm-dev && npm run bindgen-dev && npm run tsc && cp src/utoo/index_bg.wasm esm/utoo",
16
+ "tsc": "rm -rf esm && tsc -p ./tsconfig.build.json",
17
+ "copy-wasm": "cp src/utoo/index_bg.wasm esm/utoo",
18
+ "build:shared": "npx turbo run build --filter=@utoo/pack-shared",
19
+ "dev": "npm run build-wasm -- --profile wasm-dev && npm run bindgen-dev && npx turbo run tsc --filter=@utoo/web && npm run build-loaderWorker && npm run copy-wasm",
20
+ "dev:pm": "npm run build-wasm:pm -- --profile wasm-dev && npm run bindgen-dev && npm run tsc && npm run copy-wasm",
20
21
  "wasm-opt": "wasm-opt src/utoo/index_bg.wasm -o esm/utoo/index_bg.wasm --enable-threads --enable-bulk-memory --enable-nontrapping-float-to-int -Oz",
21
22
  "build": "npm run build-wasm -- --release && npm run bindgen-build && npm run tsc && npm run build-loaderWorker && npm run wasm-opt",
22
- "build:local": "npm run build-wasm -- --profile release-local && npm run bindgen-build:local && npm run tsc && npm run build-loaderWorker && cp src/utoo/index_bg.wasm esm/utoo",
23
- "build-loaderWorker": "node cli/umd.js -e ./src/webpackLoaders/worker/index.ts -o ./esm/loaderWorker.js -t webworker",
24
- "prepublishOnly": "npm run build"
23
+ "build:local": "CC=$(brew --prefix llvm)/bin/clang CXX=$(brew --prefix llvm)/bin/clang++ npm run build-wasm -- --profile release-local && npm run bindgen-build:local && npm run build:shared && npm run tsc && npm run build-loaderWorker && npm run copy-wasm",
24
+ "build-loaderWorker": "node cli/umd.js -e ./src/webpackLoaders/worker.ts -o ./esm/loaderWorker.js -t webworker",
25
+ "clean": "rm -rf esm src/utoo",
26
+ "prepublishOnly": "turbo run build --filter=@utoo/web"
25
27
  },
26
28
  "dependencies": {
27
- "@utoo/pack-shared": "^0.0.7",
29
+ "@utoo/pack-shared": "^0.0.8",
28
30
  "comlink": "^4.4.2",
29
- "micromatch": "^4.0.8",
30
31
  "systemjs": "^6.15.1"
31
32
  },
32
33
  "devDependencies": {
33
- "@types/micromatch": "^4.0.8",
34
34
  "@types/systemjs": "^6.15.1",
35
+ "binaryen": "^125.0.0",
35
36
  "node-stdlib-browser": "^1.3.1",
36
37
  "ts-loader": "^9.5.2",
37
38
  "typescript": "^5.8.3",
@@ -1,110 +0,0 @@
1
- import { runLoaderWorkerPool } from "./loaderWorkerPool";
2
- import initWasm, { init_log_filter, Project as ProjectInternal, } from "./utoo";
3
- class InternalEndpoint {
4
- constructor() {
5
- this.loaderWorkerPoolInitialized = false;
6
- }
7
- // This should be called only once
8
- async mount(opt) {
9
- var _a;
10
- this.options = opt;
11
- const { cwd, wasmUrl, threadWorkerUrl, logFilter } = opt;
12
- (_a = this.wasmInit) !== null && _a !== void 0 ? _a : (this.wasmInit = initWasm(wasmUrl));
13
- await this.wasmInit;
14
- // Initialize log filter after wasm init
15
- const filter = logFilter || "pack_core=info,pack_api=info,utoo_wasm=info";
16
- init_log_filter(filter);
17
- this.projectInternal = new ProjectInternal(cwd, threadWorkerUrl);
18
- return;
19
- }
20
- async install(packageLock, maxConcurrentDownloads) {
21
- await this.wasmInit;
22
- await this.projectInternal.install(packageLock, maxConcurrentDownloads);
23
- return;
24
- }
25
- async build() {
26
- var _a, _b;
27
- const binding = await this.wasmInit;
28
- if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.loaderWorkerUrl) && !this.loaderWorkerPoolInitialized) {
29
- runLoaderWorkerPool(binding, this.options.cwd, this.projectInternal, this.options.loaderWorkerUrl, (_b = this.options) === null || _b === void 0 ? void 0 : _b.loadersImportMap);
30
- this.loaderWorkerPoolInitialized = true;
31
- }
32
- return await this.projectInternal.build();
33
- }
34
- async readFile(path, encoding) {
35
- await this.wasmInit;
36
- let ret;
37
- if (encoding === "utf8") {
38
- ret = await this.projectInternal.readToString(path);
39
- }
40
- else {
41
- ret = await this.projectInternal.read(path);
42
- }
43
- return ret;
44
- }
45
- async writeFile(path, content, _encoding) {
46
- await this.wasmInit;
47
- if (typeof content === "string") {
48
- return await this.projectInternal.writeString(path, content);
49
- }
50
- else {
51
- return await this.projectInternal.write(path, content);
52
- }
53
- }
54
- async copyFile(src, dst) {
55
- await this.wasmInit;
56
- return await this.projectInternal.copyFile(src, dst);
57
- }
58
- async readdir(path, options) {
59
- await this.wasmInit;
60
- const dirEntries = (options === null || options === void 0 ? void 0 : options.recursive)
61
- ? await this.projectInternal.readDir(path)
62
- : // TODO: support recursive readDirAll
63
- await this.projectInternal.readDir(path);
64
- const rawDirents = dirEntries.map((e) => {
65
- const dir = e.toJSON();
66
- return {
67
- name: dir.name,
68
- type: dir.type,
69
- };
70
- });
71
- // WARN: This is a hack, functions can not be structurally cloned
72
- return rawDirents;
73
- }
74
- async mkdir(path, options) {
75
- await this.wasmInit;
76
- if (options === null || options === void 0 ? void 0 : options.recursive) {
77
- return await this.projectInternal.createDirAll(path);
78
- }
79
- else {
80
- return await this.projectInternal.createDir(path);
81
- }
82
- }
83
- async rm(path, options) {
84
- await this.wasmInit;
85
- let metadata = (await this.projectInternal.metadata(path)).toJSON();
86
- switch (metadata.type) {
87
- case "file":
88
- return await this.projectInternal.removeFile(path);
89
- case "directory":
90
- return await this.projectInternal.removeDir(path, !!(options === null || options === void 0 ? void 0 : options.recursive));
91
- default:
92
- // nothing to remove now
93
- break;
94
- }
95
- }
96
- async rmdir(path, options) {
97
- await this.wasmInit;
98
- return await this.projectInternal.removeDir(path, !!(options === null || options === void 0 ? void 0 : options.recursive));
99
- }
100
- async gzip(files) {
101
- await this.wasmInit;
102
- return await this.projectInternal.gzip(files);
103
- }
104
- async sigMd5(content) {
105
- await this.wasmInit;
106
- return await this.projectInternal.sigMd5(content);
107
- }
108
- }
109
- const internalEndpoint = new InternalEndpoint();
110
- export { internalEndpoint };
@@ -1,3 +0,0 @@
1
- import { Binding } from "./type";
2
- import { Project as ProjectInternal } from "./utoo";
3
- export declare const runLoaderWorkerPool: (binding: Binding, projectCwd: string, projectInternal: ProjectInternal, workerUrl: string, loadersImportMap?: Record<string, string>) => Promise<void>;
@@ -1,125 +0,0 @@
1
- import { SAB_OP_COPY_FILE, SAB_OP_MKDIR, SAB_OP_READ_DIR, SAB_OP_READ_FILE, SAB_OP_RM, SAB_OP_RMDIR, SAB_OP_WRITE_FILE, SabComHost, } from "./sabcom";
2
- import initWasm, { registerWorkerScheduler, workerCreated, } from "./utoo";
3
- let nextWorkerId = 0;
4
- const loaderWorkers = {};
5
- export const runLoaderWorkerPool = async (binding, projectCwd, projectInternal, workerUrl, loadersImportMap) => {
6
- registerWorkerScheduler(async (creation) => {
7
- const { options: { filename, cwd }, } = creation;
8
- nextWorkerId += 1;
9
- const workerId = nextWorkerId;
10
- const sab = new SharedArrayBuffer(1024 * 1024 * 10); // 10MB
11
- const sabHost = new SabComHost(sab);
12
- const worker = new Worker(workerUrl, { name: filename });
13
- worker.onmessage = async (event) => {
14
- if (event.data === "sab_request") {
15
- const { op, data: path } = sabHost.readRequest();
16
- try {
17
- if (op === SAB_OP_READ_FILE) {
18
- const bytes = await projectInternal.read(path);
19
- sabHost.writeResponse(bytes);
20
- }
21
- else if (op === SAB_OP_READ_DIR) {
22
- const entries = await projectInternal.readDir(path);
23
- sabHost.writeResponse(JSON.stringify(entries.map((e) => e.toJSON())));
24
- }
25
- else if (op === SAB_OP_WRITE_FILE) {
26
- const { content, encoding } = JSON.parse(path);
27
- const filePath = content.path;
28
- const fileContent = content.data;
29
- // TODO: handle binary content (base64?)
30
- await projectInternal.writeString(filePath, fileContent);
31
- sabHost.writeResponse("ok");
32
- }
33
- else if (op === SAB_OP_MKDIR) {
34
- const { path: dirPath, recursive } = JSON.parse(path);
35
- if (recursive) {
36
- await projectInternal.createDirAll(dirPath);
37
- }
38
- else {
39
- await projectInternal.createDir(dirPath);
40
- }
41
- sabHost.writeResponse("ok");
42
- }
43
- else if (op === SAB_OP_RM) {
44
- const { path: rmPath, recursive } = JSON.parse(path);
45
- // Mimic internalProject.rm logic
46
- const metadata = await projectInternal.metadata(rmPath);
47
- const type = metadata.toJSON().type;
48
- if (type === "file") {
49
- await projectInternal.removeFile(rmPath);
50
- }
51
- else if (type === "directory") {
52
- await projectInternal.removeDir(rmPath, !!recursive);
53
- }
54
- sabHost.writeResponse("ok");
55
- }
56
- else if (op === SAB_OP_RMDIR) {
57
- const { path: rmPath, recursive } = JSON.parse(path);
58
- await projectInternal.removeDir(rmPath, !!recursive);
59
- sabHost.writeResponse("ok");
60
- }
61
- else if (op === SAB_OP_COPY_FILE) {
62
- const { src, dst } = JSON.parse(path);
63
- await projectInternal.copyFile(src, dst);
64
- sabHost.writeResponse("ok");
65
- }
66
- else {
67
- sabHost.writeError("Unknown op");
68
- }
69
- }
70
- catch (e) {
71
- sabHost.writeError(e.message);
72
- }
73
- }
74
- };
75
- let finalCwd = cwd;
76
- let finalFilename = filename;
77
- if (projectCwd) {
78
- const sep = "/";
79
- const pCwd = projectCwd.endsWith(sep)
80
- ? projectCwd.slice(0, -1)
81
- : projectCwd;
82
- let cCwd = cwd.startsWith(sep) ? cwd.slice(1) : cwd;
83
- if (cCwd === "." || cCwd === "./") {
84
- cCwd = "";
85
- }
86
- finalCwd = cCwd ? `${pCwd}${sep}${cCwd}` : pCwd;
87
- if (!filename.startsWith("/")) {
88
- let fName = filename;
89
- if (fName.startsWith("./"))
90
- fName = fName.slice(2);
91
- finalFilename = `${pCwd}${sep}${fName}`;
92
- }
93
- }
94
- worker.postMessage([
95
- // @ts-ignore
96
- initWasm.__wbindgen_wasm_module,
97
- binding.memory,
98
- {
99
- workerData: {
100
- cwd: finalCwd,
101
- workerId: workerId,
102
- },
103
- loaderAssets: {
104
- importMaps: { ...loadersImportMap },
105
- entrypoint: finalFilename,
106
- },
107
- sab,
108
- },
109
- ]);
110
- const workers = loaderWorkers[filename] || (loaderWorkers[filename] = new Map());
111
- workers.set(workerId, worker);
112
- workerCreated(workerId);
113
- }, (termination) => {
114
- const { workerId, options } = termination;
115
- const entrypoint = options.filename;
116
- const workers = loaderWorkers[entrypoint];
117
- if (workers) {
118
- const worker = workers.get(workerId);
119
- if (worker) {
120
- worker.terminate();
121
- workers.delete(workerId);
122
- }
123
- }
124
- });
125
- };
package/esm/sabcom.d.ts DELETED
@@ -1,31 +0,0 @@
1
- export declare const SAB_STATE_IDLE = 0;
2
- export declare const SAB_STATE_REQUEST = 1;
3
- export declare const SAB_STATE_RESPONSE = 2;
4
- export declare const SAB_STATE_ERROR = 3;
5
- export declare const SAB_OP_READ_FILE = 1;
6
- export declare const SAB_OP_READ_DIR = 2;
7
- export declare const SAB_OP_WRITE_FILE = 3;
8
- export declare const SAB_OP_MKDIR = 4;
9
- export declare const SAB_OP_RM = 5;
10
- export declare const SAB_OP_RMDIR = 6;
11
- export declare const SAB_OP_COPY_FILE = 7;
12
- export declare class SabComHost {
13
- private sab;
14
- private int32;
15
- private uint8;
16
- constructor(sab: SharedArrayBuffer);
17
- readRequest(): {
18
- op: number;
19
- data: string;
20
- };
21
- writeResponse(data: Uint8Array | string): void;
22
- writeError(message: string): void;
23
- }
24
- export declare class SabComClient {
25
- private sab;
26
- private notifyHost;
27
- private int32;
28
- private uint8;
29
- constructor(sab: SharedArrayBuffer, notifyHost: () => void);
30
- call(op: number, data: string): Uint8Array<ArrayBuffer>;
31
- }
package/esm/sabcom.js DELETED
@@ -1,71 +0,0 @@
1
- export const SAB_STATE_IDLE = 0;
2
- export const SAB_STATE_REQUEST = 1;
3
- export const SAB_STATE_RESPONSE = 2;
4
- export const SAB_STATE_ERROR = 3;
5
- export const SAB_OP_READ_FILE = 1;
6
- export const SAB_OP_READ_DIR = 2;
7
- export const SAB_OP_WRITE_FILE = 3;
8
- export const SAB_OP_MKDIR = 4;
9
- export const SAB_OP_RM = 5;
10
- export const SAB_OP_RMDIR = 6;
11
- export const SAB_OP_COPY_FILE = 7;
12
- // Layout:
13
- // 0: State (Int32)
14
- // 1: Op (Int32)
15
- // 2: Data Length (Int32)
16
- // 12...: Data (Uint8) - Start at byte 12 (3 * 4 bytes)
17
- export class SabComHost {
18
- constructor(sab) {
19
- this.sab = sab;
20
- this.int32 = new Int32Array(sab);
21
- this.uint8 = new Uint8Array(sab);
22
- }
23
- readRequest() {
24
- const op = this.int32[1];
25
- const len = this.int32[2];
26
- const data = new TextDecoder().decode(this.uint8.slice(12, 12 + len));
27
- return { op, data };
28
- }
29
- writeResponse(data) {
30
- if (typeof data === "string") {
31
- data = new TextEncoder().encode(data);
32
- }
33
- // TODO: Check size overflow
34
- this.int32[2] = data.length;
35
- this.uint8.set(data, 12);
36
- Atomics.store(this.int32, 0, SAB_STATE_RESPONSE);
37
- Atomics.notify(this.int32, 0);
38
- }
39
- writeError(message) {
40
- const data = new TextEncoder().encode(message);
41
- this.int32[2] = data.length;
42
- this.uint8.set(data, 12);
43
- Atomics.store(this.int32, 0, SAB_STATE_ERROR);
44
- Atomics.notify(this.int32, 0);
45
- }
46
- }
47
- export class SabComClient {
48
- constructor(sab, notifyHost) {
49
- this.sab = sab;
50
- this.notifyHost = notifyHost;
51
- this.int32 = new Int32Array(sab);
52
- this.uint8 = new Uint8Array(sab);
53
- }
54
- call(op, data) {
55
- const encoded = new TextEncoder().encode(data);
56
- this.int32[1] = op;
57
- this.int32[2] = encoded.length;
58
- this.uint8.set(encoded, 12);
59
- Atomics.store(this.int32, 0, SAB_STATE_REQUEST);
60
- this.notifyHost();
61
- Atomics.wait(this.int32, 0, SAB_STATE_REQUEST);
62
- const state = Atomics.load(this.int32, 0);
63
- if (state === SAB_STATE_ERROR) {
64
- const len = this.int32[2];
65
- const msg = new TextDecoder().decode(this.uint8.slice(12, 12 + len));
66
- throw new Error(msg);
67
- }
68
- const len = this.int32[2];
69
- return this.uint8.slice(12, 12 + len);
70
- }
71
- }
package/esm/type.js DELETED
@@ -1,12 +0,0 @@
1
- export class Dirent {
2
- constructor(rawDirent) {
3
- this.rawDirent = rawDirent;
4
- this.name = this.rawDirent.name;
5
- }
6
- isDirectory() {
7
- return this.rawDirent.type === "directory";
8
- }
9
- isFile() {
10
- return this.rawDirent.type === "file";
11
- }
12
- }
@@ -1,3 +0,0 @@
1
- export const __esModule: boolean;
2
- export default lessLoader;
3
- declare function lessLoader(source: any): Promise<void>;
@@ -1,103 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- exports.default = void 0;
6
- var _path = _interopRequireDefault(require("path"));
7
- var _options = _interopRequireDefault(require("./options.json"));
8
- var _utils = require("./utils");
9
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
- async function lessLoader(source) {
11
- const options = this.getOptions(_options.default);
12
- const callback = this.async();
13
- let implementation;
14
- try {
15
- implementation = (0, _utils.getLessImplementation)(this, options.implementation);
16
- }
17
- catch (error) {
18
- callback(error);
19
- return;
20
- }
21
- if (!implementation) {
22
- callback(new Error(`The Less implementation "${options.implementation}" not found`));
23
- return;
24
- }
25
- const lessOptions = (0, _utils.getLessOptions)(this, options, implementation);
26
- const useSourceMap = typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
27
- if (useSourceMap) {
28
- lessOptions.sourceMap = {
29
- outputSourceFiles: true
30
- };
31
- }
32
- let data = source;
33
- if (typeof options.additionalData !== "undefined") {
34
- data = typeof options.additionalData === "function" ? `${await options.additionalData(data, this)}` : `${options.additionalData}\n${data}`;
35
- }
36
- const logger = this.getLogger("less-loader");
37
- const loaderContext = this;
38
- const loggerListener = {
39
- error(message) {
40
- // TODO enable by default in the next major release
41
- if (options.lessLogAsWarnOrErr) {
42
- loaderContext.emitError(new Error(message));
43
- }
44
- else {
45
- logger.error(message);
46
- }
47
- },
48
- warn(message) {
49
- // TODO enable by default in the next major release
50
- if (options.lessLogAsWarnOrErr) {
51
- loaderContext.emitWarning(new Error(message));
52
- }
53
- else {
54
- logger.warn(message);
55
- }
56
- },
57
- info(message) {
58
- logger.log(message);
59
- },
60
- debug(message) {
61
- logger.debug(message);
62
- }
63
- };
64
- implementation.logger.addListener(loggerListener);
65
- let result;
66
- try {
67
- result = await implementation.render(data, lessOptions);
68
- }
69
- catch (error) {
70
- if (error.filename) {
71
- // `less` returns forward slashes on windows when `webpack` resolver return an absolute windows path in `WebpackFileManager`
72
- // Ref: https://github.com/webpack-contrib/less-loader/issues/357
73
- this.addDependency(_path.default.normalize(error.filename));
74
- }
75
- callback((0, _utils.errorFactory)(error));
76
- return;
77
- }
78
- finally {
79
- // Fix memory leaks in `less`
80
- implementation.logger.removeListener(loggerListener);
81
- delete lessOptions.pluginManager.webpackLoaderContext;
82
- delete lessOptions.pluginManager;
83
- }
84
- const { css, imports } = result;
85
- imports.forEach(item => {
86
- if ((0, _utils.isUnsupportedUrl)(item)) {
87
- return;
88
- }
89
- // `less` return forward slashes on windows when `webpack` resolver return an absolute windows path in `WebpackFileManager`
90
- // Ref: https://github.com/webpack-contrib/less-loader/issues/357
91
- const normalizedItem = _path.default.normalize(item);
92
- // Custom `importer` can return only `contents` so item will be relative
93
- if (_path.default.isAbsolute(normalizedItem)) {
94
- this.addDependency(normalizedItem);
95
- }
96
- });
97
- let map = typeof result.map === "string" ? JSON.parse(result.map) : result.map;
98
- if (map && useSourceMap) {
99
- map = (0, _utils.normalizeSourceMap)(map, this.rootContext);
100
- }
101
- callback(null, css, map);
102
- }
103
- var _default = exports.default = lessLoader;
@@ -1,67 +0,0 @@
1
- {
2
- "title": "Less Loader options",
3
- "type": "object",
4
- "properties": {
5
- "lessOptions": {
6
- "description": "Options to pass through to `Less`.",
7
- "link": "https://github.com/webpack-contrib/less-loader#lessoptions",
8
- "anyOf": [
9
- {
10
- "type": "object",
11
- "additionalProperties": true
12
- },
13
- {
14
- "instanceof": "Function"
15
- }
16
- ]
17
- },
18
- "additionalData": {
19
- "description": "Prepends/Appends `Less` code to the actual entry file.",
20
- "link": "https://github.com/webpack-contrib/less-loader#additionalData",
21
- "anyOf": [
22
- {
23
- "type": "string"
24
- },
25
- {
26
- "instanceof": "Function"
27
- }
28
- ]
29
- },
30
- "sourceMap": {
31
- "description": "Enables/Disables generation of source maps.",
32
- "link": "https://github.com/webpack-contrib/less-loader#sourcemap",
33
- "type": "boolean"
34
- },
35
- "webpackImporter": {
36
- "description": "Enables/Disables default `webpack` importer.",
37
- "link": "https://github.com/webpack-contrib/less-loader#webpackimporter",
38
- "anyOf": [
39
- {
40
- "type": "boolean"
41
- },
42
- {
43
- "type": "string",
44
- "enum": ["only"]
45
- }
46
- ]
47
- },
48
- "implementation": {
49
- "description": "The implementation of the `Less` to be used.",
50
- "link": "https://github.com/webpack-contrib/less-loader#implementation",
51
- "anyOf": [
52
- {
53
- "type": "string"
54
- },
55
- {
56
- "type": "object"
57
- }
58
- ]
59
- },
60
- "lessLogAsWarnOrErr": {
61
- "description": "Less warnings and errors will be webpack warnings or errors.",
62
- "link": "https://github.com/webpack-contrib/less-loader#lesslogaswarnorerr",
63
- "type": "boolean"
64
- }
65
- },
66
- "additionalProperties": false
67
- }
@@ -1,14 +0,0 @@
1
- export const __esModule: boolean;
2
- export function errorFactory(error: any): Error;
3
- export function getLessImplementation(loaderContext: any, implementation: any): any;
4
- /**
5
- * Get the `less` options from the loader context and normalizes its values
6
- *
7
- * @param {object} loaderContext
8
- * @param {object} loaderOptions
9
- * @param {object} implementation
10
- * @returns {Object}
11
- */
12
- export function getLessOptions(loaderContext: object, loaderOptions: object, implementation: object): Object;
13
- export function isUnsupportedUrl(url: any): boolean;
14
- export function normalizeSourceMap(map: any): any;