@umijs/bundler-esbuild 4.0.0-beta.9 → 4.0.0-canary.202200505.1
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.d.ts +3 -1
- package/dist/build.js +41 -36
- package/dist/cli.js +4 -19
- package/dist/index.js +5 -1
- package/dist/plugins/alias.js +9 -20
- package/dist/plugins/less.d.ts +7 -2
- package/dist/plugins/less.js +125 -17
- package/dist/plugins/style.d.ts +4 -1
- package/dist/plugins/style.js +85 -54
- package/dist/utils/getBrowserlist.d.ts +1 -0
- package/dist/utils/getBrowserlist.js +11 -0
- package/dist/utils/postcssProcess.d.ts +2 -0
- package/dist/utils/postcssProcess.js +25 -0
- package/package.json +12 -8
package/dist/build.d.ts
CHANGED
|
@@ -15,5 +15,7 @@ interface IOpts {
|
|
|
15
15
|
extraBabelPresets?: IBabelPlugin[];
|
|
16
16
|
inlineStyle?: boolean;
|
|
17
17
|
}
|
|
18
|
-
export declare function build(opts: IOpts): Promise<import("@umijs/bundler-utils/compiled/esbuild").BuildResult
|
|
18
|
+
export declare function build(opts: IOpts): Promise<import("@umijs/bundler-utils/compiled/esbuild").BuildResult & {
|
|
19
|
+
metafile: import("@umijs/bundler-utils/compiled/esbuild").Metafile;
|
|
20
|
+
}>;
|
|
19
21
|
export {};
|
package/dist/build.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -20,33 +11,47 @@ const alias_1 = __importDefault(require("./plugins/alias"));
|
|
|
20
11
|
const externals_1 = __importDefault(require("./plugins/externals"));
|
|
21
12
|
const less_1 = __importDefault(require("./plugins/less"));
|
|
22
13
|
const style_1 = require("./plugins/style");
|
|
23
|
-
function build(opts) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
14
|
+
async function build(opts) {
|
|
15
|
+
const outputPath = opts.config.outputPath || (0, path_1.join)(opts.cwd, 'dist');
|
|
16
|
+
if (opts.clean) {
|
|
17
|
+
utils_1.rimraf.sync(outputPath);
|
|
18
|
+
}
|
|
19
|
+
return await (0, esbuild_1.build)({
|
|
20
|
+
entryPoints: opts.entry,
|
|
21
|
+
bundle: true,
|
|
22
|
+
format: opts.format || 'iife',
|
|
23
|
+
logLevel: 'error',
|
|
24
|
+
// splitting: true,
|
|
25
|
+
sourcemap: opts.sourcemap,
|
|
26
|
+
outdir: outputPath,
|
|
27
|
+
metafile: true,
|
|
28
|
+
plugins: [
|
|
29
|
+
(0, less_1.default)({
|
|
30
|
+
modifyVars: opts.config.theme,
|
|
31
|
+
javascriptEnabled: true,
|
|
32
|
+
alias: opts.config.alias,
|
|
33
|
+
// ref: https://github.com/umijs/umi-next/pull/214
|
|
34
|
+
inlineStyle: opts.inlineStyle,
|
|
35
|
+
config: opts.config,
|
|
36
|
+
...opts.config.lessLoader,
|
|
37
|
+
}),
|
|
38
|
+
opts.config.alias && (0, alias_1.default)(addCwdPrefix(opts.config.alias, opts.cwd)),
|
|
39
|
+
opts.config.externals && (0, externals_1.default)(opts.config.externals),
|
|
40
|
+
(0, style_1.style)({
|
|
41
|
+
inlineStyle: opts.inlineStyle,
|
|
42
|
+
config: opts.config,
|
|
43
|
+
}),
|
|
44
|
+
].filter(Boolean),
|
|
45
|
+
define: {
|
|
46
|
+
// __dirname sham
|
|
47
|
+
__dirname: JSON.stringify('__dirname'),
|
|
48
|
+
'process.env.NODE_ENV': JSON.stringify(opts.mode || 'development'),
|
|
49
|
+
...opts.config.define,
|
|
50
|
+
},
|
|
51
|
+
loader: {
|
|
52
|
+
'.svg': 'dataurl',
|
|
53
|
+
'.ttf': 'dataurl',
|
|
54
|
+
},
|
|
50
55
|
});
|
|
51
56
|
}
|
|
52
57
|
exports.build = build;
|
package/dist/cli.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -21,7 +12,7 @@ const build_1 = require("./build");
|
|
|
21
12
|
const args = (0, utils_1.yParser)(process.argv.slice(2), {});
|
|
22
13
|
const command = args._[0];
|
|
23
14
|
const cwd = process.cwd();
|
|
24
|
-
const entry = tryPaths([
|
|
15
|
+
const entry = (0, utils_1.tryPaths)([
|
|
25
16
|
(0, path_1.join)(cwd, 'src/index.tsx'),
|
|
26
17
|
(0, path_1.join)(cwd, 'src/index.ts'),
|
|
27
18
|
(0, path_1.join)(cwd, 'index.tsx'),
|
|
@@ -38,11 +29,11 @@ if ((0, fs_1.existsSync)(configFile)) {
|
|
|
38
29
|
}
|
|
39
30
|
Object.assign(config, args);
|
|
40
31
|
if (command === 'build') {
|
|
41
|
-
(() =>
|
|
32
|
+
(async () => {
|
|
42
33
|
process.env.NODE_ENV = 'production';
|
|
43
34
|
(0, assert_1.default)(entry, `Build failed: entry not found.`);
|
|
44
35
|
try {
|
|
45
|
-
|
|
36
|
+
await (0, build_1.build)({
|
|
46
37
|
config,
|
|
47
38
|
cwd,
|
|
48
39
|
entry: {
|
|
@@ -53,7 +44,7 @@ if (command === 'build') {
|
|
|
53
44
|
catch (e) {
|
|
54
45
|
console.error(e);
|
|
55
46
|
}
|
|
56
|
-
})
|
|
47
|
+
})();
|
|
57
48
|
}
|
|
58
49
|
else {
|
|
59
50
|
error(`Unsupported command ${command}.`);
|
|
@@ -61,12 +52,6 @@ else {
|
|
|
61
52
|
function error(msg) {
|
|
62
53
|
console.error(utils_1.chalk.red(msg));
|
|
63
54
|
}
|
|
64
|
-
function tryPaths(paths) {
|
|
65
|
-
for (const path of paths) {
|
|
66
|
-
if ((0, fs_1.existsSync)(path))
|
|
67
|
-
return path;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
55
|
function getEntryKey(path) {
|
|
71
56
|
return (0, path_1.basename)(path, (0, path_1.extname)(path));
|
|
72
57
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
package/dist/plugins/alias.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -21,11 +12,9 @@ const resolver = enhanced_resolve_1.default.create({
|
|
|
21
12
|
// TODO: support exports
|
|
22
13
|
exportsFields: [],
|
|
23
14
|
});
|
|
24
|
-
function resolve(context, path) {
|
|
25
|
-
return
|
|
26
|
-
|
|
27
|
-
resolver(context, path, (err, result) => err ? reject(err) : resolve(result));
|
|
28
|
-
});
|
|
15
|
+
async function resolve(context, path) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
resolver(context, path, (err, result) => err ? reject(err) : resolve(result));
|
|
29
18
|
});
|
|
30
19
|
}
|
|
31
20
|
// https://esbuild.github.io/plugins/#resolve-callbacks
|
|
@@ -43,22 +32,22 @@ exports.default = (options = {}) => {
|
|
|
43
32
|
else {
|
|
44
33
|
filter = new RegExp(`^${key}$`);
|
|
45
34
|
}
|
|
46
|
-
onResolve({ filter: filter }, (args) =>
|
|
47
|
-
const path =
|
|
35
|
+
onResolve({ filter: filter }, async (args) => {
|
|
36
|
+
const path = await resolve(args.importer, args.path.replace(filter, value));
|
|
48
37
|
return {
|
|
49
38
|
path,
|
|
50
39
|
};
|
|
51
|
-
})
|
|
40
|
+
});
|
|
52
41
|
if (!key.endsWith('/') &&
|
|
53
42
|
(0, fs_1.existsSync)(value) &&
|
|
54
43
|
(0, fs_1.statSync)(value).isDirectory()) {
|
|
55
44
|
const filter = new RegExp(`^${addSlashAffix(key)}`);
|
|
56
|
-
onResolve({ filter }, (args) =>
|
|
57
|
-
const path =
|
|
45
|
+
onResolve({ filter }, async (args) => {
|
|
46
|
+
const path = await resolve(args.importer, args.path.replace(filter, addSlashAffix(value)));
|
|
58
47
|
return {
|
|
59
48
|
path,
|
|
60
49
|
};
|
|
61
|
-
})
|
|
50
|
+
});
|
|
62
51
|
}
|
|
63
52
|
});
|
|
64
53
|
},
|
package/dist/plugins/less.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
/// <reference types="less" />
|
|
2
1
|
import { Plugin } from '@umijs/bundler-utils/compiled/esbuild';
|
|
3
|
-
|
|
2
|
+
import { IConfig } from '../types';
|
|
3
|
+
export declare const aliasLessImportPath: (filePath: string, alias: Record<string, string>, importer: string) => Promise<string | null>;
|
|
4
|
+
declare const _default: (options?: Less.Options & {
|
|
5
|
+
alias?: Record<string, string>;
|
|
6
|
+
inlineStyle?: boolean;
|
|
7
|
+
config?: IConfig;
|
|
8
|
+
}) => Plugin;
|
|
4
9
|
export default _default;
|
package/dist/plugins/less.js
CHANGED
|
@@ -1,33 +1,141 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.aliasLessImportPath = void 0;
|
|
7
|
+
const less_1 = __importDefault(require("@umijs/bundler-utils/compiled/less"));
|
|
8
|
+
const enhanced_resolve_1 = __importDefault(require("enhanced-resolve"));
|
|
15
9
|
const fs_1 = require("fs");
|
|
16
|
-
const
|
|
10
|
+
const less_plugin_aliases_1 = __importDefault(require("less-plugin-aliases"));
|
|
17
11
|
const path_1 = __importDefault(require("path"));
|
|
12
|
+
const postcssProcess_1 = require("../utils/postcssProcess");
|
|
13
|
+
const sortByAffix_1 = require("../utils/sortByAffix");
|
|
14
|
+
const resolver = enhanced_resolve_1.default.create({
|
|
15
|
+
mainFields: ['module', 'browser', 'main'],
|
|
16
|
+
extensions: [
|
|
17
|
+
'.json',
|
|
18
|
+
'.js',
|
|
19
|
+
'.jsx',
|
|
20
|
+
'.ts',
|
|
21
|
+
'.tsx',
|
|
22
|
+
'.cjs',
|
|
23
|
+
'.mjs',
|
|
24
|
+
'.less',
|
|
25
|
+
'.css',
|
|
26
|
+
],
|
|
27
|
+
// TODO: support exports
|
|
28
|
+
exportsFields: [],
|
|
29
|
+
});
|
|
30
|
+
async function resolve(context, path) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
resolver(context, path, (err, result) => err ? reject(err) : resolve(result));
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
const aliasLessImports = async (ctx, alias, importer) => {
|
|
36
|
+
const importRegex = /@import(?:\s+\((.*)\))?\s+['"](.*)['"]/;
|
|
37
|
+
const globalImportRegex = /@import(?:\s+\((.*)\))?\s+['"](.*)['"]/g;
|
|
38
|
+
const match = ctx.match(globalImportRegex) || [];
|
|
39
|
+
for (const el of match) {
|
|
40
|
+
const [imp, _, filePath] = el.match(importRegex) || [];
|
|
41
|
+
let aliaPath = await (0, exports.aliasLessImportPath)(filePath, alias, importer);
|
|
42
|
+
if (aliaPath) {
|
|
43
|
+
ctx = ctx.replace(imp, el.replace(filePath, aliaPath));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return ctx;
|
|
47
|
+
};
|
|
48
|
+
const aliasLessImportPath = async (filePath, alias, importer) => {
|
|
49
|
+
// ~ 写法在 esbuild 中无实际意义
|
|
50
|
+
let aliaPath = filePath.startsWith('~')
|
|
51
|
+
? filePath.replace('~', '')
|
|
52
|
+
: filePath;
|
|
53
|
+
const keys = (0, sortByAffix_1.sortByAffix)({ arr: Object.keys(alias), affix: '$' });
|
|
54
|
+
for (const key of keys) {
|
|
55
|
+
const pathSegments = aliaPath.split('/');
|
|
56
|
+
if (pathSegments[0] === key) {
|
|
57
|
+
pathSegments[0] = alias[key];
|
|
58
|
+
aliaPath = pathSegments.join('/');
|
|
59
|
+
aliaPath = path_1.default.extname(aliaPath) ? aliaPath : `${aliaPath}.less`;
|
|
60
|
+
return await resolve(importer, aliaPath);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
};
|
|
65
|
+
exports.aliasLessImportPath = aliasLessImportPath;
|
|
18
66
|
exports.default = (options = {}) => {
|
|
67
|
+
const { alias, inlineStyle, config, ...lessOptions } = options;
|
|
19
68
|
return {
|
|
20
69
|
name: 'less',
|
|
21
|
-
setup({ onLoad }) {
|
|
22
|
-
|
|
23
|
-
|
|
70
|
+
setup({ onResolve, onLoad }) {
|
|
71
|
+
onResolve({ filter: /\.less$/, namespace: 'file' }, async (args) => {
|
|
72
|
+
let filePath = args.path;
|
|
73
|
+
let isMatchedAlias = false;
|
|
74
|
+
// first match alias
|
|
75
|
+
if (!!alias) {
|
|
76
|
+
const aliasMatchPath = await (0, exports.aliasLessImportPath)(filePath, alias, args.path);
|
|
77
|
+
if (aliasMatchPath) {
|
|
78
|
+
isMatchedAlias = true;
|
|
79
|
+
filePath = aliasMatchPath;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// if alias not matched, identify whether import from deps (node_modules)
|
|
83
|
+
if (!isMatchedAlias) {
|
|
84
|
+
const isImportFromDeps = !path_1.default.isAbsolute(filePath) && !filePath.startsWith('.');
|
|
85
|
+
if (isImportFromDeps) {
|
|
86
|
+
filePath = await resolve(process.cwd(), filePath);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
filePath = path_1.default.resolve(process.cwd(), path_1.default.relative(process.cwd(), args.resolveDir), args.path);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
path: filePath,
|
|
94
|
+
namespace: inlineStyle ? 'less-file' : 'file',
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
if (inlineStyle) {
|
|
98
|
+
onResolve({ filter: /\.less$/, namespace: 'less-file' }, (args) => {
|
|
99
|
+
return { path: args.path, namespace: 'less-content' };
|
|
100
|
+
});
|
|
101
|
+
onResolve({ filter: /^__style_helper__$/, namespace: 'less-file' }, (args) => ({
|
|
102
|
+
path: args.path,
|
|
103
|
+
namespace: 'style-helper',
|
|
104
|
+
sideEffects: false,
|
|
105
|
+
}));
|
|
106
|
+
onLoad({ filter: /.*/, namespace: 'less-file' }, async (args) => ({
|
|
107
|
+
contents: `
|
|
108
|
+
import { injectStyle } from "__style_helper__"
|
|
109
|
+
import css from ${JSON.stringify(args.path)}
|
|
110
|
+
injectStyle(css)
|
|
111
|
+
export default{}
|
|
112
|
+
`,
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
onLoad({ filter: /\.less$/, namespace: inlineStyle ? 'less-content' : 'file' }, async (args) => {
|
|
116
|
+
let content = await fs_1.promises.readFile(args.path, 'utf-8');
|
|
117
|
+
if (!!alias) {
|
|
118
|
+
content = await aliasLessImports(content, alias, args.path);
|
|
119
|
+
}
|
|
24
120
|
const dir = path_1.default.dirname(args.path);
|
|
25
121
|
const filename = path_1.default.basename(args.path);
|
|
26
122
|
try {
|
|
27
|
-
const result =
|
|
123
|
+
const result = await less_1.default.render(content, {
|
|
124
|
+
plugins: [
|
|
125
|
+
new less_plugin_aliases_1.default({
|
|
126
|
+
prefix: '~',
|
|
127
|
+
aliases: alias || {},
|
|
128
|
+
}),
|
|
129
|
+
],
|
|
130
|
+
filename,
|
|
131
|
+
rootpath: dir,
|
|
132
|
+
...lessOptions,
|
|
133
|
+
paths: [...(lessOptions.paths || []), dir],
|
|
134
|
+
});
|
|
135
|
+
const postcssrResult = await (0, postcssProcess_1.postcssProcess)(config, result.css, args.path);
|
|
28
136
|
return {
|
|
29
|
-
contents:
|
|
30
|
-
loader: 'css',
|
|
137
|
+
contents: postcssrResult.css,
|
|
138
|
+
loader: inlineStyle ? 'text' : 'css',
|
|
31
139
|
resolveDir: dir,
|
|
32
140
|
};
|
|
33
141
|
}
|
|
@@ -47,7 +155,7 @@ exports.default = (options = {}) => {
|
|
|
47
155
|
resolveDir: dir,
|
|
48
156
|
};
|
|
49
157
|
}
|
|
50
|
-
})
|
|
158
|
+
});
|
|
51
159
|
},
|
|
52
160
|
};
|
|
53
161
|
};
|
package/dist/plugins/style.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Charset, Plugin } from '@umijs/bundler-utils/compiled/esbuild';
|
|
2
|
+
import { IConfig } from '../types';
|
|
2
3
|
export interface StylePluginOptions {
|
|
3
4
|
/**
|
|
4
5
|
* whether to minify the css code.
|
|
@@ -10,5 +11,7 @@ export interface StylePluginOptions {
|
|
|
10
11
|
* @default 'utf8'
|
|
11
12
|
*/
|
|
12
13
|
charset?: Charset;
|
|
14
|
+
inlineStyle?: boolean;
|
|
15
|
+
config?: IConfig;
|
|
13
16
|
}
|
|
14
|
-
export declare function
|
|
17
|
+
export declare function style({ minify, charset, inlineStyle, config, }?: StylePluginOptions): Plugin;
|
package/dist/plugins/style.js
CHANGED
|
@@ -1,24 +1,17 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.
|
|
6
|
+
exports.style = void 0;
|
|
16
7
|
// ref: https://github.com/hyrious/esbuild-plugin-style/blob/main/index.ts
|
|
17
8
|
const esbuild_1 = __importDefault(require("@umijs/bundler-utils/compiled/esbuild"));
|
|
9
|
+
const utils_1 = require("@umijs/utils");
|
|
18
10
|
const fs_1 = __importDefault(require("fs"));
|
|
19
11
|
const path_1 = __importDefault(require("path"));
|
|
12
|
+
const postcssProcess_1 = require("../utils/postcssProcess");
|
|
20
13
|
// https://github.com/evanw/esbuild/issues/20#issuecomment-802269745
|
|
21
|
-
function
|
|
14
|
+
function style({ minify = true, charset = 'utf8', inlineStyle, config, } = {}) {
|
|
22
15
|
return {
|
|
23
16
|
name: 'style',
|
|
24
17
|
setup({ onResolve, onLoad }) {
|
|
@@ -29,55 +22,93 @@ function inlineStyle({ minify = true, charset = 'utf8', } = {}) {
|
|
|
29
22
|
write: false,
|
|
30
23
|
charset,
|
|
31
24
|
minify,
|
|
25
|
+
loader: {
|
|
26
|
+
'.svg': 'dataurl',
|
|
27
|
+
// file ?
|
|
28
|
+
'.ttf': 'dataurl',
|
|
29
|
+
},
|
|
32
30
|
};
|
|
33
31
|
onResolve({ filter: /\.css$/, namespace: 'file' }, (args) => {
|
|
34
|
-
const absPath = path_1.default.
|
|
35
|
-
|
|
36
|
-
const resolved = fs_1.default.existsSync(absPath)
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
32
|
+
const absPath = path_1.default.resolve(cwd, path_1.default.relative(cwd, args.resolveDir), args.path);
|
|
33
|
+
// 通过 resolve 往上找,依赖不一定在 node_modules,可能被提到根目录,并且没有 link
|
|
34
|
+
const resolved = fs_1.default.existsSync(absPath)
|
|
35
|
+
? absPath
|
|
36
|
+
: utils_1.resolve.sync(`${args.path}`, {
|
|
37
|
+
basedir: args.resolveDir,
|
|
38
|
+
});
|
|
39
|
+
return { path: resolved, namespace: inlineStyle ? 'style-stub' : '' };
|
|
41
40
|
});
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
41
|
+
if (inlineStyle) {
|
|
42
|
+
onResolve({ filter: /\.css$/, namespace: 'style-stub' }, (args) => {
|
|
43
|
+
return { path: args.path, namespace: 'style-content' };
|
|
44
|
+
});
|
|
45
|
+
onResolve({ filter: /^__style_helper__$/, namespace: 'style-stub' }, (args) => ({
|
|
46
|
+
path: args.path,
|
|
47
|
+
namespace: 'style-helper',
|
|
48
|
+
sideEffects: false,
|
|
49
|
+
}));
|
|
50
|
+
onLoad({ filter: /.*/, namespace: 'style-helper' }, async () => ({
|
|
49
51
|
contents: `
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
export function injectStyle(text) {
|
|
53
|
+
if (typeof document !== 'undefined') {
|
|
54
|
+
var style = document.createElement('style')
|
|
55
|
+
var node = document.createTextNode(text)
|
|
56
|
+
style.appendChild(node)
|
|
57
|
+
document.head.appendChild(style)
|
|
58
|
+
}
|
|
56
59
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
})
|
|
60
|
-
}));
|
|
61
|
-
onLoad({ filter: /.*/, namespace: 'style-stub' }, (args) => __awaiter(this, void 0, void 0, function* () {
|
|
62
|
-
return ({
|
|
60
|
+
`,
|
|
61
|
+
}));
|
|
62
|
+
onLoad({ filter: /.*/, namespace: 'style-stub' }, async (args) => ({
|
|
63
63
|
contents: `
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
onLoad({
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
64
|
+
import { injectStyle } from "__style_helper__"
|
|
65
|
+
import css from ${JSON.stringify(args.path)}
|
|
66
|
+
injectStyle(css)
|
|
67
|
+
`,
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
onLoad({
|
|
71
|
+
filter: inlineStyle ? /.*/ : /\.css$/,
|
|
72
|
+
namespace: inlineStyle ? 'style-content' : 'file',
|
|
73
|
+
}, async (args) => {
|
|
74
|
+
const options = { entryPoints: [args.path], ...opt };
|
|
75
|
+
const { errors, warnings, outputFiles } = await esbuild_1.default.build(options);
|
|
76
|
+
if (errors.length > 0) {
|
|
77
|
+
return {
|
|
78
|
+
errors,
|
|
79
|
+
warnings,
|
|
80
|
+
contents: outputFiles[0].text,
|
|
81
|
+
loader: 'text',
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const dir = path_1.default.dirname(args.path);
|
|
85
|
+
try {
|
|
86
|
+
const result = await (0, postcssProcess_1.postcssProcess)(config, outputFiles[0].text, args.path);
|
|
87
|
+
return {
|
|
88
|
+
errors,
|
|
89
|
+
warnings,
|
|
90
|
+
contents: result.css,
|
|
91
|
+
loader: inlineStyle ? 'text' : 'css',
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
return {
|
|
96
|
+
errors: [
|
|
97
|
+
{
|
|
98
|
+
text: error.message,
|
|
99
|
+
location: {
|
|
100
|
+
namespace: 'file',
|
|
101
|
+
file: error.filename,
|
|
102
|
+
line: error.line,
|
|
103
|
+
column: error.column,
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
resolveDir: dir,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
});
|
|
80
111
|
},
|
|
81
112
|
};
|
|
82
113
|
}
|
|
83
|
-
exports.
|
|
114
|
+
exports.style = style;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getBrowserlist(targets: Record<string, string | boolean>): string | true | string[];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBrowserlist = void 0;
|
|
4
|
+
// refer: https://github.com/umijs/umi-next/blob/867e0c196296efbbdb95203cca35db2fa639808b/packages/bundler-webpack/src/utils/browsersList.ts#L5
|
|
5
|
+
function getBrowserlist(targets) {
|
|
6
|
+
return (targets.browsers ||
|
|
7
|
+
Object.keys(targets).map((key) => {
|
|
8
|
+
return `${key} >= ${targets[key] === true ? '0' : targets[key]}`;
|
|
9
|
+
}));
|
|
10
|
+
}
|
|
11
|
+
exports.getBrowserlist = getBrowserlist;
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
exports.postcssProcess = void 0;
|
|
7
|
+
const postcss_1 = __importDefault(require("postcss"));
|
|
8
|
+
const getBrowserlist_1 = require("./getBrowserlist");
|
|
9
|
+
async function postcssProcess(config, css, path) {
|
|
10
|
+
return await (0, postcss_1.default)([
|
|
11
|
+
require('postcss-flexbugs-fixes'),
|
|
12
|
+
require('postcss-preset-env')({
|
|
13
|
+
browsers: (0, getBrowserlist_1.getBrowserlist)((config === null || config === void 0 ? void 0 : config.targets) || {}),
|
|
14
|
+
autoprefixer: {
|
|
15
|
+
flexbox: 'no-2009',
|
|
16
|
+
...config === null || config === void 0 ? void 0 : config.autoprefixer,
|
|
17
|
+
},
|
|
18
|
+
stage: 3,
|
|
19
|
+
}),
|
|
20
|
+
].concat((config === null || config === void 0 ? void 0 : config.extraPostCSSPlugins) || [])).process(css, {
|
|
21
|
+
from: path,
|
|
22
|
+
to: path,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
exports.postcssProcess = postcssProcess;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umijs/bundler-esbuild",
|
|
3
|
-
"version": "4.0.0-
|
|
3
|
+
"version": "4.0.0-canary.202200505.1",
|
|
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",
|
|
@@ -19,17 +19,21 @@
|
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
21
|
"build": "pnpm tsc",
|
|
22
|
-
"build:deps": "
|
|
23
|
-
"dev": "pnpm build -- --watch"
|
|
22
|
+
"build:deps": "umi-scripts bundleDeps",
|
|
23
|
+
"dev": "pnpm build -- --watch",
|
|
24
|
+
"test": "umi-scripts jest-turbo"
|
|
24
25
|
},
|
|
25
26
|
"dependencies": {
|
|
26
|
-
"@umijs/bundler-utils": "4.0.0-
|
|
27
|
-
"@umijs/utils": "4.0.0-
|
|
28
|
-
"enhanced-resolve": "5.
|
|
29
|
-
"less": "
|
|
27
|
+
"@umijs/bundler-utils": "4.0.0-canary.202200505.1",
|
|
28
|
+
"@umijs/utils": "4.0.0-canary.202200505.1",
|
|
29
|
+
"enhanced-resolve": "5.9.2",
|
|
30
|
+
"less-plugin-aliases": "^1.0.3",
|
|
31
|
+
"postcss": "^8.4.12",
|
|
32
|
+
"postcss-flexbugs-fixes": "5.0.2",
|
|
33
|
+
"postcss-preset-env": "7.4.3"
|
|
30
34
|
},
|
|
31
35
|
"devDependencies": {
|
|
32
|
-
"@
|
|
36
|
+
"@alitajs/postcss-plugin-px2rem": "^0.0.1"
|
|
33
37
|
},
|
|
34
38
|
"publishConfig": {
|
|
35
39
|
"access": "public"
|