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

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
@@ -38,15 +38,20 @@ function build(opts) {
38
38
  plugins: [
39
39
  (0, less_1.default)(Object.assign({ modifyVars: opts.config.theme, javascriptEnabled: true, alias: opts.config.alias,
40
40
  // ref: https://github.com/umijs/umi-next/pull/214
41
- inlineStyle: opts.inlineStyle }, opts.config.lessLoader)),
41
+ inlineStyle: opts.inlineStyle, config: opts.config }, opts.config.lessLoader)),
42
42
  opts.config.alias && (0, alias_1.default)(addCwdPrefix(opts.config.alias, opts.cwd)),
43
43
  opts.config.externals && (0, externals_1.default)(opts.config.externals),
44
- opts.inlineStyle && (0, style_1.inlineStyle)(),
44
+ (0, style_1.style)({
45
+ inlineStyle: opts.inlineStyle,
46
+ config: opts.config,
47
+ }),
45
48
  ].filter(Boolean),
46
- define: {
49
+ define: Object.assign({
47
50
  // __dirname sham
48
- __dirname: JSON.stringify('__dirname'),
49
- 'process.env.NODE_ENV': JSON.stringify(opts.mode || 'development'),
51
+ __dirname: JSON.stringify('__dirname'), 'process.env.NODE_ENV': JSON.stringify(opts.mode || 'development') }, opts.config.define),
52
+ loader: {
53
+ '.svg': 'dataurl',
54
+ '.ttf': 'dataurl',
50
55
  },
51
56
  });
52
57
  });
package/dist/cli.js CHANGED
@@ -21,7 +21,7 @@ const build_1 = require("./build");
21
21
  const args = (0, utils_1.yParser)(process.argv.slice(2), {});
22
22
  const command = args._[0];
23
23
  const cwd = process.cwd();
24
- const entry = tryPaths([
24
+ const entry = (0, utils_1.tryPaths)([
25
25
  (0, path_1.join)(cwd, 'src/index.tsx'),
26
26
  (0, path_1.join)(cwd, 'src/index.ts'),
27
27
  (0, path_1.join)(cwd, 'index.tsx'),
@@ -61,12 +61,6 @@ else {
61
61
  function error(msg) {
62
62
  console.error(utils_1.chalk.red(msg));
63
63
  }
64
- function tryPaths(paths) {
65
- for (const path of paths) {
66
- if ((0, fs_1.existsSync)(path))
67
- return path;
68
- }
69
- }
70
64
  function getEntryKey(path) {
71
65
  return (0, path_1.basename)(path, (0, path_1.extname)(path));
72
66
  }
@@ -1,7 +1,9 @@
1
1
  /// <reference types="less" />
2
2
  import { Plugin } from '@umijs/bundler-utils/compiled/esbuild';
3
+ import { IConfig } from '../types';
3
4
  declare const _default: (options?: Less.Options & {
4
5
  alias?: Record<string, string>;
5
6
  inlineStyle?: boolean;
7
+ config?: IConfig;
6
8
  }) => Plugin;
7
9
  export default _default;
@@ -28,6 +28,7 @@ const fs_1 = require("fs");
28
28
  const less_1 = __importDefault(require("less"));
29
29
  const less_plugin_aliases_1 = __importDefault(require("less-plugin-aliases"));
30
30
  const path_1 = __importDefault(require("path"));
31
+ const postcssProcess_1 = require("../utils/postcssProcess");
31
32
  const sortByAffix_1 = require("../utils/sortByAffix");
32
33
  const resolver = enhanced_resolve_1.default.create({
33
34
  mainFields: ['module', 'browser', 'main'],
@@ -83,7 +84,7 @@ const aliasLessImportPath = (filePath, alias, importer) => __awaiter(void 0, voi
83
84
  return null;
84
85
  });
85
86
  exports.default = (options = {}) => {
86
- const { alias, inlineStyle } = options, lessOptions = __rest(options, ["alias", "inlineStyle"]);
87
+ const { alias, inlineStyle, config } = options, lessOptions = __rest(options, ["alias", "inlineStyle", "config"]);
87
88
  return {
88
89
  name: 'less',
89
90
  setup({ onResolve, onLoad }) {
@@ -118,6 +119,7 @@ exports.default = (options = {}) => {
118
119
  import { injectStyle } from "__style_helper__"
119
120
  import css from ${JSON.stringify(args.path)}
120
121
  injectStyle(css)
122
+ export default{}
121
123
  `,
122
124
  });
123
125
  }));
@@ -136,8 +138,9 @@ exports.default = (options = {}) => {
136
138
  aliases: alias || {},
137
139
  }),
138
140
  ], filename, rootpath: dir }, lessOptions), { paths: [...(lessOptions.paths || []), dir] }));
141
+ const postcssrResult = yield (0, postcssProcess_1.postcssProcess)(config, result.css, args.path);
139
142
  return {
140
- contents: result.css,
143
+ contents: postcssrResult.css,
141
144
  loader: inlineStyle ? 'text' : 'css',
142
145
  resolveDir: dir,
143
146
  };
@@ -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 inlineStyle({ minify, charset, }?: StylePluginOptions): Plugin;
17
+ export declare function style({ minify, charset, inlineStyle, config, }?: StylePluginOptions): Plugin;
@@ -12,13 +12,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.inlineStyle = void 0;
15
+ exports.style = void 0;
16
16
  // ref: https://github.com/hyrious/esbuild-plugin-style/blob/main/index.ts
17
17
  const esbuild_1 = __importDefault(require("@umijs/bundler-utils/compiled/esbuild"));
18
+ const utils_1 = require("@umijs/utils");
18
19
  const fs_1 = __importDefault(require("fs"));
19
20
  const path_1 = __importDefault(require("path"));
21
+ const postcssProcess_1 = require("../utils/postcssProcess");
20
22
  // https://github.com/evanw/esbuild/issues/20#issuecomment-802269745
21
- function inlineStyle({ minify = true, charset = 'utf8', } = {}) {
23
+ function style({ minify = true, charset = 'utf8', inlineStyle, config, } = {}) {
22
24
  return {
23
25
  name: 'style',
24
26
  setup({ onResolve, onLoad }) {
@@ -29,55 +31,97 @@ function inlineStyle({ minify = true, charset = 'utf8', } = {}) {
29
31
  write: false,
30
32
  charset,
31
33
  minify,
34
+ loader: {
35
+ '.svg': 'dataurl',
36
+ // file ?
37
+ '.ttf': 'dataurl',
38
+ },
32
39
  };
33
40
  onResolve({ filter: /\.css$/, namespace: 'file' }, (args) => {
34
- const absPath = path_1.default.join(args.resolveDir, args.path);
35
- const relPath = path_1.default.relative(cwd, absPath);
36
- const resolved = fs_1.default.existsSync(absPath) ? relPath : args.path;
37
- return { path: resolved, namespace: 'style-stub' };
41
+ const absPath = path_1.default.resolve(cwd, path_1.default.relative(cwd, args.resolveDir), args.path);
42
+ // 通过 resolve 往上找,依赖不一定在 node_modules,可能被提到根目录,并且没有 link
43
+ const resolved = fs_1.default.existsSync(absPath)
44
+ ? absPath
45
+ : utils_1.resolve.sync(`${args.path}`, {
46
+ basedir: args.resolveDir,
47
+ });
48
+ return { path: resolved, namespace: inlineStyle ? 'style-stub' : '' };
38
49
  });
39
- onResolve({ filter: /\.css$/, namespace: 'style-stub' }, (args) => {
40
- return { path: args.path, namespace: 'style-content' };
41
- });
42
- onResolve({ filter: /^__style_helper__$/, namespace: 'style-stub' }, (args) => ({
43
- path: args.path,
44
- namespace: 'style-helper',
45
- sideEffects: false,
46
- }));
47
- onLoad({ filter: /.*/, namespace: 'style-helper' }, () => __awaiter(this, void 0, void 0, function* () {
48
- return ({
49
- contents: `
50
- export function injectStyle(text) {
51
- if (typeof document !== 'undefined') {
52
- var style = document.createElement('style')
53
- var node = document.createTextNode(text)
54
- style.appendChild(node)
55
- document.head.appendChild(style)
56
- }
57
- }
58
- `,
59
- });
60
- }));
61
- onLoad({ filter: /.*/, namespace: 'style-stub' }, (args) => __awaiter(this, void 0, void 0, function* () {
62
- return ({
63
- contents: `
64
- import { injectStyle } from "__style_helper__"
65
- import css from ${JSON.stringify(args.path)}
66
- injectStyle(css)
67
- `,
50
+ if (inlineStyle) {
51
+ onResolve({ filter: /\.css$/, namespace: 'style-stub' }, (args) => {
52
+ return { path: args.path, namespace: 'style-content' };
68
53
  });
69
- }));
70
- onLoad({ filter: /.*/, namespace: 'style-content' }, (args) => __awaiter(this, void 0, void 0, function* () {
54
+ onResolve({ filter: /^__style_helper__$/, namespace: 'style-stub' }, (args) => ({
55
+ path: args.path,
56
+ namespace: 'style-helper',
57
+ sideEffects: false,
58
+ }));
59
+ onLoad({ filter: /.*/, namespace: 'style-helper' }, () => __awaiter(this, void 0, void 0, function* () {
60
+ return ({
61
+ contents: `
62
+ export function injectStyle(text) {
63
+ if (typeof document !== 'undefined') {
64
+ var style = document.createElement('style')
65
+ var node = document.createTextNode(text)
66
+ style.appendChild(node)
67
+ document.head.appendChild(style)
68
+ }
69
+ }
70
+ `,
71
+ });
72
+ }));
73
+ onLoad({ filter: /.*/, namespace: 'style-stub' }, (args) => __awaiter(this, void 0, void 0, function* () {
74
+ return ({
75
+ contents: `
76
+ import { injectStyle } from "__style_helper__"
77
+ import css from ${JSON.stringify(args.path)}
78
+ injectStyle(css)
79
+ `,
80
+ });
81
+ }));
82
+ }
83
+ onLoad({
84
+ filter: inlineStyle ? /.*/ : /\.css$/,
85
+ namespace: inlineStyle ? 'style-content' : 'file',
86
+ }, (args) => __awaiter(this, void 0, void 0, function* () {
71
87
  const options = Object.assign({ entryPoints: [args.path] }, opt);
72
88
  const { errors, warnings, outputFiles } = yield esbuild_1.default.build(options);
73
- return {
74
- errors,
75
- warnings,
76
- contents: outputFiles[0].text,
77
- loader: 'text',
78
- };
89
+ if (errors.length > 0) {
90
+ return {
91
+ errors,
92
+ warnings,
93
+ contents: outputFiles[0].text,
94
+ loader: 'text',
95
+ };
96
+ }
97
+ const dir = path_1.default.dirname(args.path);
98
+ try {
99
+ const result = yield (0, postcssProcess_1.postcssProcess)(config, outputFiles[0].text, args.path);
100
+ return {
101
+ errors,
102
+ warnings,
103
+ contents: result.css,
104
+ loader: inlineStyle ? 'text' : 'css',
105
+ };
106
+ }
107
+ catch (error) {
108
+ return {
109
+ errors: [
110
+ {
111
+ text: error.message,
112
+ location: {
113
+ namespace: 'file',
114
+ file: error.filename,
115
+ line: error.line,
116
+ column: error.column,
117
+ },
118
+ },
119
+ ],
120
+ resolveDir: dir,
121
+ };
122
+ }
79
123
  }));
80
124
  },
81
125
  };
82
126
  }
83
- exports.inlineStyle = inlineStyle;
127
+ 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,2 @@
1
+ import { IConfig } from '../types';
2
+ export declare function postcssProcess(config: IConfig, css: string, path: string): Promise<import("postcss").Result>;
@@ -0,0 +1,33 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.postcssProcess = void 0;
16
+ const postcss_1 = __importDefault(require("postcss"));
17
+ const getBrowserlist_1 = require("./getBrowserlist");
18
+ function postcssProcess(config, css, path) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ return yield (0, postcss_1.default)([
21
+ require('postcss-flexbugs-fixes'),
22
+ require('postcss-preset-env')({
23
+ browsers: (0, getBrowserlist_1.getBrowserlist)((config === null || config === void 0 ? void 0 : config.targets) || {}),
24
+ autoprefixer: Object.assign({ flexbox: 'no-2009' }, config === null || config === void 0 ? void 0 : config.autoprefixer),
25
+ stage: 3,
26
+ }),
27
+ ].concat((config === null || config === void 0 ? void 0 : config.extraPostCSSPlugins) || [])).process(css, {
28
+ from: path,
29
+ to: path,
30
+ });
31
+ });
32
+ }
33
+ exports.postcssProcess = postcssProcess;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/bundler-esbuild",
3
- "version": "4.0.0-beta.13",
3
+ "version": "4.0.0-beta.17",
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,13 +23,17 @@
23
23
  "dev": "pnpm build -- --watch"
24
24
  },
25
25
  "dependencies": {
26
- "@umijs/bundler-utils": "4.0.0-beta.13",
27
- "@umijs/utils": "4.0.0-beta.13",
26
+ "@umijs/bundler-utils": "4.0.0-beta.17",
27
+ "@umijs/utils": "4.0.0-beta.17",
28
28
  "enhanced-resolve": "5.8.3",
29
29
  "less": "4.1.2",
30
- "less-plugin-aliases": "^1.0.3"
30
+ "less-plugin-aliases": "^1.0.3",
31
+ "postcss": "^8.4.4",
32
+ "postcss-flexbugs-fixes": "5.0.2",
33
+ "postcss-preset-env": "7.0.1"
31
34
  },
32
35
  "devDependencies": {
36
+ "@alitajs/postcss-plugin-px2rem": "^0.0.1",
33
37
  "@types/less": "^3.0.3"
34
38
  },
35
39
  "publishConfig": {