From 8023621919ebb715d33b6c13393c4dd9277d3ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?chencheng=20=28=E4=BA=91=E8=B0=A6=29?= Date: Sun, 7 Apr 2024 16:47:42 +0800 Subject: [PATCH] feat: refine @okamjs/okam (#1024) --- docs/api.md | 24 ++++++ packages/bundler-okam/index.js | 114 +++-------------------------- packages/bundler-okam/package.json | 4 - packages/mako/package.json | 9 +++ packages/mako/src/index.ts | 93 ++++++++++++++++++++++- packages/mako/src/lessLoader.ts | 49 +++++++++++++ pnpm-lock.yaml | 106 ++++++++++++++------------- scripts/mako.js | 63 ++++++++-------- 8 files changed, 272 insertions(+), 190 deletions(-) create mode 100644 packages/mako/src/lessLoader.ts diff --git a/docs/api.md b/docs/api.md index e73ce8b3d..e8411a0e3 100644 --- a/docs/api.md +++ b/docs/api.md @@ -12,6 +12,7 @@ await build({ root: process.cwd(), config: {}, hooks: {}, + less: {}, watch: false, }: BuildOptions); ``` @@ -32,6 +33,29 @@ await build({ 详见[配置](./config.md)。 +### less + +- 类型:`Object` +- 默认值:`{}` + +less 配置。 + +比如。 + +```ts +{ + modifyVars: { + 'primary-color': '#1DA57A', + 'link-color': '#1DA57A', + }, + sourceMap: { + sourceMapFileInline: true, + outputSourceFiles: true, + }, + math: 'always', +} +``` + ### hooks - 类型: diff --git a/packages/bundler-okam/index.js b/packages/bundler-okam/index.js index 2b1cb82fd..7171b2773 100644 --- a/packages/bundler-okam/index.js +++ b/packages/bundler-okam/index.js @@ -10,56 +10,6 @@ const { createProxyMiddleware, } = require('@umijs/bundler-utils/compiled/http-proxy-middleware'); -function lessLoader(fn, opts) { - return async function (filePath) { - let pathname = ''; - try { - pathname = url.parse(filePath).pathname; - } catch (e) { - return; - } - if (pathname?.endsWith('.less')) { - const { alias, modifyVars, config, sourceMap } = opts; - const less = require('@umijs/bundler-utils/compiled/less'); - const input = fs.readFileSync(pathname, 'utf-8'); - const resolvePlugin = new (require('less-plugin-resolve'))({ - aliases: alias, - }); - const result = await less.render(input, { - filename: pathname, - javascriptEnabled: true, - math: config.lessLoader?.math, - plugins: [resolvePlugin], - modifyVars, - sourceMap, - rewriteUrls: 'all', - }); - return { content: result.css, type: 'css' }; - } else { - fn && fn(filePath); - } - }; -} - -// export for test only -exports._lessLoader = lessLoader; - -// ref: -// https://github.com/vercel/next.js/pull/51883 -function blockStdout() { - if (process.platform === 'darwin') { - // rust needs stdout to be blocking, otherwise it will throw an error (on macOS at least) when writing a lot of data (logs) to it - // see https://github.com/napi-rs/napi-rs/issues/1630 - // and https://github.com/nodejs/node/blob/main/doc/api/process.md#a-note-on-process-io - if (process.stdout._handle != null) { - process.stdout._handle.setBlocking(true); - } - if (process.stderr._handle != null) { - process.stderr._handle.setBlocking(true); - } - } -} - exports.build = async function (opts) { assert(opts, 'opts should be supplied'); const { @@ -80,21 +30,15 @@ exports.build = async function (opts) { okamConfig.moduleIdStrategy = 'hashed'; } - blockStdout(); const { build } = require('@okamjs/okam'); try { await build({ root: cwd, config: okamConfig, - hooks: { - load: lessLoader(null, { - cwd, - config: opts.config, - // NOTICE: 有个缺点是 如果 alias 配置是 mako 插件修改的 less 这边就感知到不了 - alias: okamConfig.resolve.alias, - modifyVars: opts.config.lessLoader?.modifyVars || opts.config.theme, - sourceMap: getLessSourceMapConfig(okamConfig.devtool), - }), + less: { + modifyVars: opts.config.lessLoader?.modifyVars || opts.config.theme, + sourceMap: getLessSourceMapConfig(okamConfig.devtool), + math: opts.config.lessLoader?.math, }, watch: false, }); @@ -211,7 +155,6 @@ exports.dev = async function (opts) { server.on('upgrade', wsProxy.upgrade); // okam dev - blockStdout(); const { build } = require('@okamjs/okam'); const okamConfig = await getOkamConfig(opts); okamConfig.hmr = { port: hmrPort, host: opts.host }; @@ -220,14 +163,12 @@ exports.dev = async function (opts) { await build({ root: cwd, config: okamConfig, + less: { + modifyVars: opts.config.lessLoader?.modifyVars || opts.config.theme, + sourceMap: getLessSourceMapConfig(okamConfig.devtool), + math: opts.config.lessLoader?.math, + }, hooks: { - load: lessLoader(null, { - cwd, - config: opts.config, - alias: okamConfig.resolve.alias, - modifyVars: opts.config.lessLoader?.modifyVars || opts.config.theme, - sourceMap: getLessSourceMapConfig(okamConfig.devtool), - }), generateEnd: (args) => { opts.onDevCompileDone(args); }, @@ -451,16 +392,6 @@ async function getOkamConfig(opts) { umd = webpackConfig.output.library; } - let makoConfig = {}; - const makoConfigPath = path.join(opts.cwd, 'mako.config.json'); - if (fs.existsSync(makoConfigPath)) { - try { - makoConfig = JSON.parse(fs.readFileSync(makoConfigPath, 'utf-8')); - } catch (e) { - throw new Error(`Parse mako.config.json failed: ${e.message}`); - } - } - const { alias, targets, @@ -541,7 +472,6 @@ async function getOkamConfig(opts) { // handle [string] with script type if (Array.isArray(v)) { const [url, ...members] = v; - ret[k] = { // ['antd', 'Button'] => `antd.Button` root: members.join('.'), @@ -555,7 +485,6 @@ async function getOkamConfig(opts) { // other types except boolean has been checked before // so here only ignore invalid boolean type } - return ret; }, {}); @@ -563,25 +492,7 @@ async function getOkamConfig(opts) { entry: opts.entry, output: { path: outputPath }, resolve: { - alias: { - ...alias, - ...makoConfig.resolve?.alias, - // we still need @swc/helpers - // since features like decorator or legacy browser support will - // inject helper functions in the build transform step - '@swc/helpers': path.dirname( - require.resolve('@swc/helpers/package.json'), - ), - 'node-libs-browser-okam': path.dirname( - require.resolve('node-libs-browser-okam/package.json'), - ), - 'react-refresh': path.dirname( - require.resolve('react-refresh/package.json'), - ), - 'react-error-overlay': path.dirname( - require.resolve('react-error-overlay/package.json'), - ), - }, + alias, }, mode: 'development', publicPath: runtimePublicPath ? 'runtime' : publicPath || '/', @@ -605,11 +516,6 @@ async function getOkamConfig(opts) { ...(opts.disableCopy ? { copy: [] } : { copy: ['public'].concat(copy) }), }; - if (process.env.DUMP_MAKO_CONFIG) { - const configFile = path.join(process.cwd(), 'mako.config.json'); - fs.writeFileSync(configFile, JSON.stringify(okamConfig, null, 2)); - } - return okamConfig; } diff --git a/packages/bundler-okam/package.json b/packages/bundler-okam/package.json index 1456a06e7..7e1d955f6 100644 --- a/packages/bundler-okam/package.json +++ b/packages/bundler-okam/package.json @@ -3,18 +3,14 @@ "version": "0.4.9", "dependencies": { "@okamjs/okam": "0.4.9", - "@swc/helpers": "0.5.1", "@umijs/bundler-utils": "^4.0.81", "chalk": "^4.1.2", "compression": "^1.7.4", "connect-history-api-fallback": "^2.0.0", "cors": "^2.8.5", "express": "^4.18.2", - "less-plugin-resolve": "^1.0.2", "lodash": "^4.17.21", "node-libs-browser-okam": "2.2.4", - "react-error-overlay": "6.0.9", - "react-refresh": "^0.14.0", "rimraf": "5.0.1", "webpack-5-chain": "8.0.1" }, diff --git a/packages/mako/package.json b/packages/mako/package.json index e8a42907f..68e26c6ba 100644 --- a/packages/mako/package.json +++ b/packages/mako/package.json @@ -15,7 +15,16 @@ } }, "license": "MIT", + "dependencies": { + "@swc/helpers": "0.5.1", + "less": "^4.2.0", + "less-plugin-resolve": "^1.0.2", + "react-error-overlay": "6.0.9", + "react-refresh": "^0.14.0" + }, "devDependencies": { + "@types/less": "^3.0.6", + "@types/node": "^20.12.5", "@napi-rs/cli": "^2.18.0" }, "engines": { diff --git a/packages/mako/src/index.ts b/packages/mako/src/index.ts index 9a6d3bf65..77677c42b 100644 --- a/packages/mako/src/index.ts +++ b/packages/mako/src/index.ts @@ -1 +1,92 @@ -export * from '../binding'; +import fs from 'fs'; +import path from 'path'; +import * as binding from '../binding'; +import { LessLoaderOpts, lessLoader } from './lessLoader'; + +// ref: +// https://github.com/vercel/next.js/pull/51883 +function blockStdout() { + if (process.platform === 'darwin') { + // rust needs stdout to be blocking, otherwise it will throw an error (on macOS at least) when writing a lot of data (logs) to it + // see https://github.com/napi-rs/napi-rs/issues/1630 + // and https://github.com/nodejs/node/blob/main/doc/api/process.md#a-note-on-process-io + if ((process.stdout as any)._handle != null) { + (process.stdout as any)._handle.setBlocking(true); + } + if ((process.stderr as any)._handle != null) { + (process.stderr as any)._handle.setBlocking(true); + } + } +} + +interface ExtraBuildParams { + less?: LessLoaderOpts; +} + +export async function build(params: binding.BuildParams & ExtraBuildParams) { + blockStdout(); + + params.hooks = params.hooks || {}; + params.config.resolve = params.config.resolve || {}; + params.config.resolve.alias = params.config.resolve.alias || {}; + + let makoConfig: any = {}; + const makoConfigPath = path.join(params.root, 'mako.config.json'); + if (fs.existsSync(makoConfigPath)) { + try { + makoConfig = JSON.parse(fs.readFileSync(makoConfigPath, 'utf-8')); + } catch (e: any) { + throw new Error(`Parse mako.config.json failed: ${e.message}`); + } + } + + // alias for: helpers, node-libs, react-refresh, react-error-overlay + params.config.resolve.alias = { + ...makoConfig.resolve?.alias, + ...params.config.resolve.alias, + // we still need @swc/helpers + // since features like decorator or legacy browser support will + // inject helper functions in the build transform step + '@swc/helpers': path.dirname(require.resolve('@swc/helpers/package.json')), + 'node-libs-browser-okam': path.dirname( + require.resolve('node-libs-browser-okam/package.json'), + ), + 'react-refresh': path.dirname( + require.resolve('react-refresh/package.json'), + ), + 'react-error-overlay': path.dirname( + require.resolve('react-error-overlay/package.json'), + ), + }; + + // built-in less-loader + let less = lessLoader(null, { + alias: params.config.resolve.alias!, + modifyVars: params.less?.modifyVars || {}, + math: params.less?.math, + sourceMap: params.less?.sourceMap || false, + }); + let originLoad = params.hooks.load; + // TODO: improve load binding, should support return null if not matched + // @ts-ignore + params.hooks.load = async function (filePath: string) { + let lessResult = await less(filePath); + if (lessResult) { + return lessResult; + } + if (originLoad) { + let originResult = await originLoad(filePath); + if (originResult) { + return originResult; + } + } + }; + + // support dump mako config + if (process.env.DUMP_MAKO_CONFIG) { + const configFile = path.join(params.root, 'mako.config.json'); + fs.writeFileSync(configFile, JSON.stringify(params.config, null, 2)); + } + + await binding.build(params); +} diff --git a/packages/mako/src/lessLoader.ts b/packages/mako/src/lessLoader.ts new file mode 100644 index 000000000..7c10d8054 --- /dev/null +++ b/packages/mako/src/lessLoader.ts @@ -0,0 +1,49 @@ +import fs from 'fs'; +import url from 'url'; + +export interface LessLoaderOpts { + alias: Record; + modifyVars: Record; + math: + | 'always' + | 'strict' + | 'parens-division' + | 'parens' + | 'strict-legacy' + | number + | undefined; + sourceMap: any; + implementation?: any; +} + +export function lessLoader(fn: Function | null, opts: LessLoaderOpts) { + return async function (filePath: string) { + let pathname = ''; + try { + pathname = url.parse(filePath).pathname || ''; + } catch (e) { + return; + } + if (pathname?.endsWith('.less')) { + const { alias, modifyVars, math, sourceMap } = opts; + const less = opts.implementation || require('less'); + const input = fs.readFileSync(pathname, 'utf-8'); + const resolvePlugin = new (require('less-plugin-resolve'))({ + aliases: alias, + }); + const result = await less.render(input, { + filename: pathname, + javascriptEnabled: true, + math, + plugins: [resolvePlugin], + modifyVars, + sourceMap, + rewriteUrls: 'all', + }); + return { content: result.css, type: 'css' }; + } else { + // TODO: remove this + fn && fn(filePath); + } + }; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 896c73098..48fdfe7a1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -340,9 +340,6 @@ importers: '@okamjs/okam': specifier: 0.4.9 version: link:../mako - '@swc/helpers': - specifier: 0.5.1 - version: 0.5.1 '@umijs/bundler-utils': specifier: ^4.0.81 version: 4.0.81 @@ -361,21 +358,12 @@ importers: express: specifier: ^4.18.2 version: 4.18.2 - less-plugin-resolve: - specifier: ^1.0.2 - version: 1.0.2 lodash: specifier: ^4.17.21 version: 4.17.21 node-libs-browser-okam: specifier: 2.2.4 version: 2.2.4 - react-error-overlay: - specifier: 6.0.9 - version: 6.0.9 - react-refresh: - specifier: ^0.14.0 - version: 0.14.0 rimraf: specifier: 5.0.1 version: 5.0.1 @@ -384,6 +372,22 @@ importers: version: 8.0.1 packages/mako: + dependencies: + '@swc/helpers': + specifier: 0.5.1 + version: 0.5.1 + less: + specifier: ^4.2.0 + version: 4.2.0 + less-plugin-resolve: + specifier: ^1.0.2 + version: 1.0.2 + react-error-overlay: + specifier: 6.0.9 + version: 6.0.9 + react-refresh: + specifier: ^0.14.0 + version: 0.14.0 optionalDependencies: '@okamjs/okam-darwin-arm64': specifier: 0.4.9 @@ -398,6 +402,12 @@ importers: '@napi-rs/cli': specifier: ^2.18.0 version: 2.18.0 + '@types/less': + specifier: ^3.0.6 + version: 3.0.6 + '@types/node': + specifier: ^20.12.5 + version: 20.12.5 packages: @@ -3387,7 +3397,7 @@ packages: dependencies: '@types/istanbul-lib-coverage': 2.0.4 '@types/istanbul-reports': 3.0.1 - '@types/node': 20.4.2 + '@types/node': 20.12.5 '@types/yargs': 16.0.5 chalk: 4.1.2 dev: true @@ -3399,7 +3409,7 @@ packages: '@jest/schemas': 29.4.3 '@types/istanbul-lib-coverage': 2.0.4 '@types/istanbul-reports': 3.0.1 - '@types/node': 20.4.2 + '@types/node': 20.12.5 '@types/yargs': 17.0.24 chalk: 4.1.2 dev: true @@ -3411,7 +3421,7 @@ packages: '@jest/schemas': 29.6.3 '@types/istanbul-lib-coverage': 2.0.4 '@types/istanbul-reports': 3.0.1 - '@types/node': 20.4.2 + '@types/node': 20.12.5 '@types/yargs': 17.0.24 chalk: 4.1.2 dev: true @@ -4057,7 +4067,7 @@ packages: /@swc/helpers@0.5.1: resolution: {integrity: sha512-sJ902EfIzn1Fa+qYmjdQqh8tPsoxyBz+8yBKC2HKUxyezKJFwPGOn7pv4WY6QuQW//ySQi5lJjA/ZT9sNWWNTg==} dependencies: - tslib: 2.5.2 + tslib: 2.5.0 /@tanstack/match-sorter-utils@8.8.4: resolution: {integrity: sha512-rKH8LjZiszWEvmi01NR72QWZ8m4xmXre0OOwlRGnjU01Eqz/QnN+cqpty2PJ0efHblq09+KilvyR7lsbzmXVEw==} @@ -4169,13 +4179,13 @@ packages: resolution: {integrity: sha512-fB3Zu92ucau0iQ0JMCFQE7b/dv8Ot07NI3KaZIkIUNXq82k4eBAqUaneXfleGY9JWskeS9y+u0nXMyspcuQrCg==} dependencies: '@types/connect': 3.4.38 - '@types/node': 20.4.2 + '@types/node': 20.12.5 dev: true /@types/connect@3.4.38: resolution: {integrity: sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==} dependencies: - '@types/node': 20.4.2 + '@types/node': 20.12.5 dev: true /@types/eslint-scope@3.7.4: @@ -4199,7 +4209,7 @@ packages: /@types/express-serve-static-core@4.17.41: resolution: {integrity: sha512-OaJ7XLaelTgrvlZD8/aa0vvvxZdUmlCn6MtWeB7TkiKW70BQLc9XEPpDLPdbo52ZhXUCrznlWdCHWxJWtdyajA==} dependencies: - '@types/node': 20.4.2 + '@types/node': 20.12.5 '@types/qs': 6.9.10 '@types/range-parser': 1.2.7 '@types/send': 0.17.4 @@ -4218,13 +4228,13 @@ packages: resolution: {integrity: sha512-MxObHvNl4A69ofaTRU8DFqvgzzv8s9yRtaPPm5gud9HDNvpB3GPQFvNuTWAI59B9huVGV5jXYJwbCsmBsOGYWA==} dependencies: '@types/jsonfile': 6.1.1 - '@types/node': 20.4.2 + '@types/node': 20.12.5 dev: true /@types/graceful-fs@4.1.6: resolution: {integrity: sha512-Sig0SNORX9fdW+bQuTEovKj3uHcUL6LQKbCrrqb1X7J6/ReAbhCXRAhc+SMejhLELFj2QcyuxmUooZ4bt5ReSw==} dependencies: - '@types/node': 20.4.2 + '@types/node': 20.12.5 dev: true /@types/hapi__joi@17.1.9: @@ -4295,7 +4305,11 @@ packages: /@types/jsonfile@6.1.1: resolution: {integrity: sha512-GSgiRCVeapDN+3pqA35IkQwasaCh/0YFH5dEF6S88iDvEn901DjOeH3/QPY+XYP1DFzDZPvIvfeEgk+7br5png==} dependencies: - '@types/node': 20.4.2 + '@types/node': 20.12.5 + dev: true + + /@types/less@3.0.6: + resolution: {integrity: sha512-PecSzorDGdabF57OBeQO/xFbAkYWo88g4Xvnsx7LRwqLC17I7OoKtA3bQB9uXkY6UkMWCOsA8HSVpaoitscdXw==} dev: true /@types/mime@1.3.5: @@ -4314,6 +4328,12 @@ packages: resolution: {integrity: sha512-IXl7o+R9iti9eBW4Wg2hx1xQDig183jj7YLn8F7udNceyfkbn1ZxmzZXuak20gR40D7pIkIY1kYGx5VIGbaHKA==} dev: true + /@types/node@20.12.5: + resolution: {integrity: sha512-BD+BjQ9LS/D8ST9p5uqBxghlN+S42iuNxjsUGjeZobe/ciXzk2qb1B6IXc6AnRLS+yFJRpN2IPEHMzwspfDJNw==} + dependencies: + undici-types: 5.26.5 + dev: true + /@types/node@20.4.2: resolution: {integrity: sha512-Dd0BYtWgnWJKwO1jkmTrzofjK2QXXcai0dmtzvIBhcA+RsG5h8R3xlyta0kGOZRNfL9GuRtb1knmPEhQrePCEw==} dev: true @@ -4391,7 +4411,7 @@ packages: resolution: {integrity: sha512-x2EM6TJOybec7c52BX0ZspPodMsQUd5L6PRwOunVyVUhXiBSKf3AezDL8Dgvgt5o0UfKNfuA0eMLr2wLT4AiBA==} dependencies: '@types/mime': 1.3.5 - '@types/node': 20.4.2 + '@types/node': 20.12.5 dev: true /@types/serve-static@1.15.5: @@ -4399,7 +4419,7 @@ packages: dependencies: '@types/http-errors': 2.0.4 '@types/mime': 3.0.4 - '@types/node': 20.4.2 + '@types/node': 20.12.5 dev: true /@types/stack-utils@2.0.3: @@ -6707,7 +6727,6 @@ packages: resolution: {integrity: sha512-1j20GZTsvKNkc4BY3NpMOM8tt///wY3FpIzozTOFO2ffuZcV61nojHXVKIy3WM+7ADCy5FVhdZYHYDdgTU0yJw==} dependencies: is-what: 3.14.1 - dev: true /copy-anything@3.0.5: resolution: {integrity: sha512-yCEafptTtb4bk7GLEQoM8KVJpxAfdBJYaXyzQEgQQQgYrZiDp8SJmGKlYza6CYjEDNstAdNdKA3UuoULlEbS6w==} @@ -6961,7 +6980,6 @@ packages: optional: true dependencies: ms: 2.1.3 - dev: true /debug@4.3.4(supports-color@5.5.0): resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==} @@ -7374,7 +7392,6 @@ packages: requiresBuild: true dependencies: prr: 1.0.1 - dev: true optional: true /error-ex@1.3.2: @@ -8796,7 +8813,6 @@ packages: engines: {node: '>=0.10.0'} dependencies: safer-buffer: 2.1.2 - dev: true /icss-utils@5.1.0(postcss@8.4.24): resolution: {integrity: sha512-soFhflCVWLfRNOPU3iv5Z9VUdT44xFRbzjLsEzSr5AQmgqPMTHdU3PMT1Cf1ssx8fLNJDA1juftYl+PUcv3MqA==} @@ -8827,7 +8843,6 @@ packages: engines: {node: '>=0.10.0'} hasBin: true requiresBuild: true - dev: true optional: true /immer@8.0.4: @@ -9213,7 +9228,6 @@ packages: /is-what@3.14.1: resolution: {integrity: sha512-sNxgpk9793nzSs7bA6JQJGeIuRBQhAaNGG77kzYQgMkrID+lS6SlK07K5LaptscDlSaIgH+GPFzf+d75FVxozA==} - dev: true /is-what@4.1.13: resolution: {integrity: sha512-Aoe8pT24sWzyoO0S2PTDyutGp9l7qYHyFtzYlC8hMLshyqV/minljBANT4f2hiS5OxnWvcKMiA5io+VaLMJ1oA==} @@ -9323,7 +9337,7 @@ packages: dependencies: '@jest/types': 29.5.0 '@types/graceful-fs': 4.1.6 - '@types/node': 20.4.2 + '@types/node': 20.12.5 anymatch: 3.1.3 fb-watchman: 2.0.2 graceful-fs: 4.2.11 @@ -9342,7 +9356,7 @@ packages: dependencies: '@jest/types': 29.6.3 '@types/graceful-fs': 4.1.6 - '@types/node': 20.4.2 + '@types/node': 20.12.5 anymatch: 3.1.3 fb-watchman: 2.0.2 graceful-fs: 4.2.11 @@ -9423,7 +9437,7 @@ packages: engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} dependencies: '@jest/types': 29.5.0 - '@types/node': 20.4.2 + '@types/node': 20.12.5 chalk: 4.1.2 ci-info: 3.8.0 graceful-fs: 4.2.11 @@ -9435,7 +9449,7 @@ packages: engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} dependencies: '@jest/types': 29.6.3 - '@types/node': 20.4.2 + '@types/node': 20.12.5 chalk: 4.1.2 ci-info: 3.8.0 graceful-fs: 4.2.11 @@ -9446,7 +9460,7 @@ packages: resolution: {integrity: sha512-7vuh85V5cdDofPyxn58nrPjBktZo0u9x1g8WtjQol+jZDaE+fhN+cIvTj11GndBnMnyfrUOG1sZQxCdjKh+DKg==} engines: {node: '>= 10.13.0'} dependencies: - '@types/node': 20.4.2 + '@types/node': 20.12.5 merge-stream: 2.0.0 supports-color: 8.1.1 dev: true @@ -9455,7 +9469,7 @@ packages: resolution: {integrity: sha512-GLHN/GTAAMEy5BFdvpUfzr9Dr80zQqBrh0fz1mtRMe05hqP45+HfQltu7oTBfduD0UeZs09d+maFtFYAXFWvAA==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} dependencies: - '@types/node': 20.4.2 + '@types/node': 20.12.5 jest-util: 29.5.0 merge-stream: 2.0.0 supports-color: 8.1.1 @@ -9465,7 +9479,7 @@ packages: resolution: {integrity: sha512-NcrQnevGoSp4b5kg+akIpthoAFHxPBcb5P6mYPY0fUNT+sSvmtu6jlkEle3anczUKIKEbMxFimk9oTP/tpIPgA==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} dependencies: - '@types/node': 20.4.2 + '@types/node': 20.12.5 jest-util: 29.5.0 merge-stream: 2.0.0 supports-color: 8.1.1 @@ -9475,7 +9489,7 @@ packages: resolution: {integrity: sha512-eIz2msL/EzL9UFTFFx7jBTkeZfku0yUAyZZZmJ93H2TYEiroIx2PQjEXcwYtYl8zXCxb+PAmA2hLIt/6ZEkPHw==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} dependencies: - '@types/node': 20.4.2 + '@types/node': 20.12.5 jest-util: 29.7.0 merge-stream: 2.0.0 supports-color: 8.1.1 @@ -9627,7 +9641,6 @@ packages: source-map: 0.6.1 transitivePeerDependencies: - supports-color - dev: true /levn@0.4.1: resolution: {integrity: sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==} @@ -9960,7 +9973,6 @@ packages: dependencies: pify: 4.0.1 semver: 5.7.1 - dev: true /make-dir@3.1.0: resolution: {integrity: sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==} @@ -10238,7 +10250,6 @@ packages: sax: 1.2.4 transitivePeerDependencies: - supports-color - dev: true optional: true /negotiator@0.6.3: @@ -10658,7 +10669,6 @@ packages: /parse-node-version@1.0.1: resolution: {integrity: sha512-3YHlOa/JgH6Mnpr05jP9eDG254US9ek25LyIxZlDItp2iJtwyaXQb57lBYLdT3MowkUFYEV2XXNAYIPlESvJlA==} engines: {node: '>= 0.10'} - dev: true /parseurl@1.3.3: resolution: {integrity: sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==} @@ -10769,7 +10779,6 @@ packages: /pify@4.0.1: resolution: {integrity: sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==} engines: {node: '>=6'} - dev: true /pino-abstract-transport@0.5.0: resolution: {integrity: sha512-+KAgmVeqXYbTtU2FScx1XS3kNyfZ5TrXY07V96QnUSFqo2gAqlvmaxH67Lj7SWazqsMabf+58ctdTcBgnOLUOQ==} @@ -11387,7 +11396,6 @@ packages: /prr@1.0.1: resolution: {integrity: sha512-yPw4Sng1gWghHQWj0B3ZggWUm4qVbPwPFcRG8KyxiU7J2OHFSoEHKS+EZ3fv5l1t9CyCiop6l/ZYeWbrgoQejw==} requiresBuild: true - dev: true optional: true /ps-tree@1.2.0: @@ -13244,7 +13252,6 @@ packages: /sax@1.2.4: resolution: {integrity: sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==} requiresBuild: true - dev: true optional: true /scheduler@0.22.0: @@ -13282,7 +13289,6 @@ packages: /semver@5.7.1: resolution: {integrity: sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==} hasBin: true - dev: true /semver@6.3.0: resolution: {integrity: sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==} @@ -13490,7 +13496,6 @@ packages: /source-map@0.6.1: resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==} engines: {node: '>=0.10.0'} - dev: true /source-map@0.7.4: resolution: {integrity: sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==} @@ -14132,9 +14137,6 @@ packages: /tslib@2.5.0: resolution: {integrity: sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==} - /tslib@2.5.2: - resolution: {integrity: sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA==} - /tsutils@3.21.0(typescript@5.4.3): resolution: {integrity: sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==} engines: {node: '>= 6'} @@ -14302,6 +14304,10 @@ packages: which-boxed-primitive: 1.0.2 dev: true + /undici-types@5.26.5: + resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==} + dev: true + /unfetch@5.0.0: resolution: {integrity: sha512-3xM2c89siXg0nHvlmYsQ2zkLASvVMBisZm5lF3gFDqfF2xonNStDJyMpvaOBe0a1Edxmqrf2E0HBdmy9QyZaeg==} dev: true diff --git a/scripts/mako.js b/scripts/mako.js index 36c741dd0..010e4fd3f 100755 --- a/scripts/mako.js +++ b/scripts/mako.js @@ -3,44 +3,45 @@ const path = require('path'); const fs = require('fs'); const { build } = require('@okamjs/okam'); -const { _lessLoader } = require('@alipay/umi-bundler-okam'); const cwd = process.argv[2]; -const isWatch = process.argv.includes('--watch'); -let makoConfig = {}; -const makoConfigPath = path.join(cwd, 'mako.config.json'); -if (fs.existsSync(makoConfigPath)) { - makoConfig = JSON.parse(fs.readFileSync(makoConfigPath, 'utf-8')); -} -const alias = {}; -if (makoConfig.resolve?.alias) { - Object.keys(makoConfig.resolve.alias).forEach((key) => { - alias[key] = path.join(cwd, makoConfig.resolve.alias[key]); - }); -} -const okamConfig = { - resolve: { alias }, -}; console.log('> run mako build for', cwd); -let hooks = {}; -const hooksPath = path.join(cwd, 'hooks.config.js'); -if (fs.existsSync(hooksPath)) { - hooks = require(hooksPath); -} +const config = getMakoConfig(); build({ root: cwd, - config: okamConfig, - hooks: { - ...hooks, - load: _lessLoader(null, { - cwd, - alias, - modifyVars: makoConfig.less?.theme || {}, - config: {}, - }), + config, + less: { + modifyVars: config.less?.theme || {}, }, - watch: isWatch, + hooks: getHooks(), + watch: process.argv.includes('--watch'), }).catch((e) => { console.error(e); process.exit(1); }); + +function getHooks() { + let hooks = {}; + const hooksPath = path.join(cwd, 'hooks.config.js'); + if (fs.existsSync(hooksPath)) { + hooks = require(hooksPath); + } + return hooks; +} + +function getMakoConfig() { + let makoConfig = {}; + const makoConfigPath = path.join(cwd, 'mako.config.json'); + if (fs.existsSync(makoConfigPath)) { + makoConfig = JSON.parse(fs.readFileSync(makoConfigPath, 'utf-8')); + } + makoConfig.resolve = makoConfig.resolve || {}; + makoConfig.resolve.alias = makoConfig.resolve.alias || {}; + Object.keys(makoConfig.resolve.alias).forEach((key) => { + makoConfig.resolve.alias[key] = path.join( + cwd, + makoConfig.resolve.alias[key], + ); + }); + return makoConfig; +}