@umijs/bundler-esbuild 4.0.0-beta.12 → 4.0.0-beta.13

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/dist/build.js CHANGED
@@ -36,7 +36,9 @@ function build(opts) {
36
36
  outdir: outputPath,
37
37
  metafile: true,
38
38
  plugins: [
39
- (0, less_1.default)(Object.assign({ modifyVars: opts.config.theme, javascriptEnabled: true }, opts.config.lessLoader)),
39
+ (0, less_1.default)(Object.assign({ modifyVars: opts.config.theme, javascriptEnabled: true, alias: opts.config.alias,
40
+ // ref: https://github.com/umijs/umi-next/pull/214
41
+ inlineStyle: opts.inlineStyle }, opts.config.lessLoader)),
40
42
  opts.config.alias && (0, alias_1.default)(addCwdPrefix(opts.config.alias, opts.cwd)),
41
43
  opts.config.externals && (0, externals_1.default)(opts.config.externals),
42
44
  opts.inlineStyle && (0, style_1.inlineStyle)(),
@@ -1,4 +1,7 @@
1
1
  /// <reference types="less" />
2
2
  import { Plugin } from '@umijs/bundler-utils/compiled/esbuild';
3
- declare const _default: (options?: Less.Options) => Plugin;
3
+ declare const _default: (options?: Less.Options & {
4
+ alias?: Record<string, string>;
5
+ inlineStyle?: boolean;
6
+ }) => Plugin;
4
7
  export default _default;
@@ -8,26 +8,137 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
11
22
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
24
  };
14
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
+ const enhanced_resolve_1 = __importDefault(require("enhanced-resolve"));
15
27
  const fs_1 = require("fs");
16
28
  const less_1 = __importDefault(require("less"));
29
+ const less_plugin_aliases_1 = __importDefault(require("less-plugin-aliases"));
17
30
  const path_1 = __importDefault(require("path"));
31
+ const sortByAffix_1 = require("../utils/sortByAffix");
32
+ const resolver = enhanced_resolve_1.default.create({
33
+ mainFields: ['module', 'browser', 'main'],
34
+ extensions: [
35
+ '.json',
36
+ '.js',
37
+ '.jsx',
38
+ '.ts',
39
+ '.tsx',
40
+ '.cjs',
41
+ '.mjs',
42
+ '.less',
43
+ '.css',
44
+ ],
45
+ // TODO: support exports
46
+ exportsFields: [],
47
+ });
48
+ function resolve(context, path) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ return new Promise((resolve, reject) => {
51
+ resolver(context, path, (err, result) => err ? reject(err) : resolve(result));
52
+ });
53
+ });
54
+ }
55
+ const aliasLessImports = (ctx, alias, importer) => __awaiter(void 0, void 0, void 0, function* () {
56
+ const importRegex = /@import(?:\s+\((.*)\))?\s+['"](.*)['"]/;
57
+ const globalImportRegex = /@import(?:\s+\((.*)\))?\s+['"](.*)['"]/g;
58
+ const match = ctx.match(globalImportRegex) || [];
59
+ for (const el of match) {
60
+ const [imp, _, filePath] = el.match(importRegex) || [];
61
+ let aliaPath = yield aliasLessImportPath(filePath, alias, importer);
62
+ if (aliaPath) {
63
+ ctx = ctx.replace(imp, el.replace(filePath, aliaPath));
64
+ }
65
+ }
66
+ return ctx;
67
+ });
68
+ const aliasLessImportPath = (filePath, alias, importer) => __awaiter(void 0, void 0, void 0, function* () {
69
+ // ~ 写法在 esbuild 中无实际意义
70
+ let aliaPath = filePath.startsWith('~')
71
+ ? filePath.replace('~', '')
72
+ : filePath;
73
+ const keys = (0, sortByAffix_1.sortByAffix)({ arr: Object.keys(alias), affix: '$' });
74
+ for (const key of keys) {
75
+ const value = alias[key];
76
+ const filter = new RegExp(`^${key}`);
77
+ if (filter.test(aliaPath)) {
78
+ aliaPath = aliaPath.replace(filter, value);
79
+ aliaPath = path_1.default.extname(aliaPath) ? aliaPath : `${aliaPath}.less`;
80
+ return yield resolve(importer, aliaPath);
81
+ }
82
+ }
83
+ return null;
84
+ });
18
85
  exports.default = (options = {}) => {
86
+ const { alias, inlineStyle } = options, lessOptions = __rest(options, ["alias", "inlineStyle"]);
19
87
  return {
20
88
  name: 'less',
21
- setup({ onLoad }) {
22
- onLoad({ filter: /\.less$/, namespace: 'file' }, (args) => __awaiter(this, void 0, void 0, function* () {
23
- const content = yield fs_1.promises.readFile(args.path, 'utf-8');
89
+ setup({ onResolve, onLoad }) {
90
+ onResolve({ filter: /\.less$/, namespace: 'file' }, (args) => __awaiter(this, void 0, void 0, function* () {
91
+ let filePath = args.path;
92
+ if (!!alias) {
93
+ filePath =
94
+ (yield aliasLessImportPath(filePath, alias, args.path)) ||
95
+ path_1.default.resolve(process.cwd(), path_1.default.relative(process.cwd(), args.resolveDir), args.path);
96
+ }
97
+ else {
98
+ //没有别名也要对路径进行处理
99
+ filePath = path_1.default.resolve(process.cwd(), path_1.default.relative(process.cwd(), args.resolveDir), args.path);
100
+ }
101
+ return {
102
+ path: filePath,
103
+ namespace: inlineStyle ? 'less-file' : 'file',
104
+ };
105
+ }));
106
+ if (inlineStyle) {
107
+ onResolve({ filter: /\.less$/, namespace: 'less-file' }, (args) => {
108
+ return { path: args.path, namespace: 'less-content' };
109
+ });
110
+ onResolve({ filter: /^__style_helper__$/, namespace: 'less-file' }, (args) => ({
111
+ path: args.path,
112
+ namespace: 'style-helper',
113
+ sideEffects: false,
114
+ }));
115
+ onLoad({ filter: /.*/, namespace: 'less-file' }, (args) => __awaiter(this, void 0, void 0, function* () {
116
+ return ({
117
+ contents: `
118
+ import { injectStyle } from "__style_helper__"
119
+ import css from ${JSON.stringify(args.path)}
120
+ injectStyle(css)
121
+ `,
122
+ });
123
+ }));
124
+ }
125
+ onLoad({ filter: /\.less$/, namespace: inlineStyle ? 'less-content' : 'file' }, (args) => __awaiter(this, void 0, void 0, function* () {
126
+ let content = yield fs_1.promises.readFile(args.path, 'utf-8');
127
+ if (!!alias) {
128
+ content = yield aliasLessImports(content, alias, args.path);
129
+ }
24
130
  const dir = path_1.default.dirname(args.path);
25
131
  const filename = path_1.default.basename(args.path);
26
132
  try {
27
- const result = yield less_1.default.render(content, Object.assign(Object.assign({ filename, rootpath: dir }, options), { paths: [...(options.paths || []), dir] }));
133
+ const result = yield less_1.default.render(content, Object.assign(Object.assign({ plugins: [
134
+ new less_plugin_aliases_1.default({
135
+ prefix: '~',
136
+ aliases: alias || {},
137
+ }),
138
+ ], filename, rootpath: dir }, lessOptions), { paths: [...(lessOptions.paths || []), dir] }));
28
139
  return {
29
140
  contents: result.css,
30
- loader: 'css',
141
+ loader: inlineStyle ? 'text' : 'css',
31
142
  resolveDir: dir,
32
143
  };
33
144
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/bundler-esbuild",
3
- "version": "4.0.0-beta.12",
3
+ "version": "4.0.0-beta.13",
4
4
  "description": "@umijs/bundler-esbuild",
5
5
  "homepage": "https://github.com/umijs/umi-next/tree/master/packages/bundler-esbuild#readme",
6
6
  "bugs": "https://github.com/umijs/umi-next/issues",
@@ -23,10 +23,11 @@
23
23
  "dev": "pnpm build -- --watch"
24
24
  },
25
25
  "dependencies": {
26
- "@umijs/bundler-utils": "4.0.0-beta.12",
27
- "@umijs/utils": "4.0.0-beta.12",
26
+ "@umijs/bundler-utils": "4.0.0-beta.13",
27
+ "@umijs/utils": "4.0.0-beta.13",
28
28
  "enhanced-resolve": "5.8.3",
29
- "less": "4.1.2"
29
+ "less": "4.1.2",
30
+ "less-plugin-aliases": "^1.0.3"
30
31
  },
31
32
  "devDependencies": {
32
33
  "@types/less": "^3.0.3"