From 602dbb60d36ade41809093cb82ab012a0bf8fded Mon Sep 17 00:00:00 2001 From: winixt Date: Sun, 10 Apr 2022 17:43:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=20webpack=20=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E6=97=B6=E7=9A=84=E4=BF=A1=E6=81=AF=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.config.js | 1 - package.json | 4 +-- .../src/commands/build/babelPolyfillPlugin.js | 2 +- packages/fes-build-webpack/package.json | 1 - .../plugins/commands/webpackConfig/index.js | 2 -- scripts/{build.js => build.mjs} | 34 +++++++++++-------- scripts/{compiler.js => compiler.mjs} | 8 ++--- scripts/{randomColor.js => randomColor.mjs} | 6 ++-- yarn.lock | 30 ---------------- 9 files changed, 28 insertions(+), 60 deletions(-) rename scripts/{build.js => build.mjs} (87%) rename scripts/{compiler.js => compiler.mjs} (88%) rename scripts/{randomColor.js => randomColor.mjs} (89%) diff --git a/build.config.js b/build.config.js index bca05232..0745f0b3 100644 --- a/build.config.js +++ b/build.config.js @@ -1,5 +1,4 @@ module.exports = { - // 需要编译的包 pkgs: [ 'create-fes-app', 'fes', diff --git a/package.json b/package.json index 74f5a37d..65df8b6e 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,8 @@ "packages/*" ], "scripts": { - "dev": "node scripts/build.js --watch", - "build": "node scripts/build.js", + "dev": "node scripts/build.mjs --watch", + "build": "node scripts/build.mjs", "release": "node scripts/release.mjs", "docs:dev": "vuepress dev docs --clean-cache", "docs:build": "vuepress build docs --clean-cache", diff --git a/packages/fes-build-vite/src/commands/build/babelPolyfillPlugin.js b/packages/fes-build-vite/src/commands/build/babelPolyfillPlugin.js index 1b018a39..b4291657 100644 --- a/packages/fes-build-vite/src/commands/build/babelPolyfillPlugin.js +++ b/packages/fes-build-vite/src/commands/build/babelPolyfillPlugin.js @@ -3,7 +3,7 @@ import { transformSync } from '@babel/core'; const DEFAULT_FILTER = /\.[jt]sx?$/; export default (config) => ({ - name: 'vite-pligin:babel-polyfill', + name: 'vite-plugin:babel-polyfill', transform(code, id) { const [filepath] = id.split('?'); if (DEFAULT_FILTER.test(id) || DEFAULT_FILTER.test(filepath)) { diff --git a/packages/fes-build-webpack/package.json b/packages/fes-build-webpack/package.json index d7ef6cd9..ce2ad17d 100644 --- a/packages/fes-build-webpack/package.json +++ b/packages/fes-build-webpack/package.json @@ -35,7 +35,6 @@ "@babel/preset-typescript": "^7.15.0", "@fesjs/compiler": "^2.0.5", "@fesjs/utils": "^2.0.4", - "@soda/friendly-errors-webpack-plugin": "^1.8.0", "@vue/babel-plugin-jsx": "^1.0.2", "autoprefixer": "^10.2.4", "babel-loader": "^8.2.2", diff --git a/packages/fes-build-webpack/src/plugins/commands/webpackConfig/index.js b/packages/fes-build-webpack/src/plugins/commands/webpackConfig/index.js index d847e694..1b962e1a 100644 --- a/packages/fes-build-webpack/src/plugins/commands/webpackConfig/index.js +++ b/packages/fes-build-webpack/src/plugins/commands/webpackConfig/index.js @@ -281,8 +281,6 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod // --------------- 构建输出 ---------- webpackConfig.plugin('progress').use(require.resolve('webpackbar')); - webpackConfig.plugin('friendly-errors').use(require('@soda/friendly-errors-webpack-plugin')); - // --------------- chainwebpack ----------- if (chainWebpack) { await chainWebpack(webpackConfig, { diff --git a/scripts/build.js b/scripts/build.mjs similarity index 87% rename from scripts/build.js rename to scripts/build.mjs index 4a9245d0..35955fca 100644 --- a/scripts/build.js +++ b/scripts/build.mjs @@ -1,16 +1,20 @@ +/* eslint-disable import/extensions */ // 关闭 import 规则 /* eslint import/no-extraneous-dependencies: 0 */ -const fs = require('fs'); -const fse = require('fs-extra'); -const path = require('path'); -const merge = require('deepmerge'); -const chokidar = require('chokidar'); -const chalk = require('chalk'); -const argv = require('yargs-parser')(process.argv.slice(2)); +import path from 'path'; +import fs from 'fs'; +import fse from 'fs-extra'; +import chalk from 'chalk'; +import merge from 'deepmerge'; +import chokidar from 'chokidar'; +import yargsParser from 'yargs-parser'; +import compiler from './compiler.mjs'; +import randomColor from './randomColor.mjs'; -const compiler = require('./compiler'); -const randomColor = require('./randomColor'); +import buildConfig from '../build.config.js'; + +const argv = yargsParser(process.argv.slice(2)); const ESM_OUTPUT_DIR = 'es'; const NODE_CJS_OUTPUT_DIR = 'lib'; @@ -54,16 +58,16 @@ function getOutputPath(config, pkgName) { function getGlobalConfig() { if (fs.existsSync(GLOBAL_CONFIG_PATH)) { - const userConfig = require(GLOBAL_CONFIG_PATH); - return merge(DEFAULT_CONFIG, userConfig); + return merge(DEFAULT_CONFIG, buildConfig); } return DEFAULT_CONFIG; } -function getPkgConfig(config, pkgName) { +async function getPkgConfig(config, pkgName) { const pkgConfigPath = path.join(getPkgPath(pkgName), CONFIG_FILE_NAME); if (fs.existsSync(pkgConfigPath)) { - return merge(config, require(pkgConfigPath)); + const content = await import(pkgConfigPath); + return merge(config, content.default); } return config; @@ -150,11 +154,11 @@ function watchFile(dir, outputDir, config, log) { } function compilerPkgs(pkgs, globalConfig) { - pkgs.forEach((pkgName) => { + pkgs.forEach(async (pkgName) => { const sourceCodeDir = getPkgSourcePath(pkgName); if (fs.existsSync(sourceCodeDir)) { const log = genLog(pkgName); - const config = getPkgConfig(globalConfig, pkgName); + const config = await getPkgConfig(globalConfig, pkgName); const outputDir = getOutputPath(config, pkgName); cleanBeforeCompilerResult(pkgName, log); diff --git a/scripts/compiler.js b/scripts/compiler.mjs similarity index 88% rename from scripts/compiler.js rename to scripts/compiler.mjs index 2d142b71..6c9e173a 100644 --- a/scripts/compiler.js +++ b/scripts/compiler.mjs @@ -1,10 +1,10 @@ // 关闭 import 规则 /* eslint import/no-extraneous-dependencies: 0 */ -const babel = require('@babel/core'); +import { transformSync } from '@babel/core'; function transform(code, options) { - const result = babel.transformSync(code, options); + const result = transformSync(code, options); return result.code; } @@ -39,7 +39,7 @@ function transformBrowserCode(code) { }); } -function compiler(code, config) { +export default function compiler(code, config) { if (!config.target || config.target === 'node') { return transformNodeCode(code); } @@ -48,5 +48,3 @@ function compiler(code, config) { } throw new Error(`config target error: ${config.target}, only can use 'node' and 'browser'`); } - -module.exports = compiler; diff --git a/scripts/randomColor.js b/scripts/randomColor.mjs similarity index 89% rename from scripts/randomColor.js rename to scripts/randomColor.mjs index bca8e2de..de9ad200 100644 --- a/scripts/randomColor.js +++ b/scripts/randomColor.mjs @@ -1,5 +1,5 @@ /* eslint import/no-extraneous-dependencies: 0 */ -const chalk = require('chalk'); +import chalk from 'chalk'; const colors = [ 'red', @@ -20,7 +20,7 @@ const colors = [ let index = 0; const cache = {}; -module.exports = function (pkg) { +export default function (pkg) { if (!cache[pkg]) { const color = colors[index]; const str = chalk[color].bold(pkg); @@ -32,4 +32,4 @@ module.exports = function (pkg) { } } return cache[pkg]; -}; +} diff --git a/yarn.lock b/yarn.lock index 4e680a34..9de1a354 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1898,16 +1898,6 @@ dependencies: "@sinonjs/commons" "^1.7.0" -"@soda/friendly-errors-webpack-plugin@^1.8.0": - version "1.8.1" - resolved "https://registry.npmmirror.com/@soda/friendly-errors-webpack-plugin/-/friendly-errors-webpack-plugin-1.8.1.tgz#4d4fbb1108993aaa362116247c3d18188a2c6c85" - integrity sha512-h2ooWqP8XuFqTXT+NyAFbrArzfQA7R6HTezADrvD9Re8fxMLTPPniLdqVTdDaO0eIoLaAwKT+d6w+5GeTk7Vbg== - dependencies: - chalk "^3.0.0" - error-stack-parser "^2.0.6" - string-width "^4.2.3" - strip-ansi "^6.0.1" - "@surma/rollup-plugin-off-main-thread@^2.2.3": version "2.2.3" resolved "https://registry.npmmirror.com/@surma/rollup-plugin-off-main-thread/-/rollup-plugin-off-main-thread-2.2.3.tgz#ee34985952ca21558ab0d952f00298ad2190c053" @@ -3671,14 +3661,6 @@ chalk@^2.0.0, chalk@^2.1.0, chalk@^2.4.1, chalk@^2.4.2: escape-string-regexp "^1.0.5" supports-color "^5.3.0" -chalk@^3.0.0: - version "3.0.0" - resolved "https://registry.npmmirror.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4" - integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg== - dependencies: - ansi-styles "^4.1.0" - supports-color "^7.1.0" - chalk@^4.0.0, chalk@^4.0.2, chalk@^4.1.0, chalk@^4.1.2: version "4.1.2" resolved "https://registry.npmmirror.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01" @@ -4998,13 +4980,6 @@ error-ex@^1.3.1: dependencies: is-arrayish "^0.2.1" -error-stack-parser@^2.0.6: - version "2.0.7" - resolved "https://registry.npmmirror.com/error-stack-parser/-/error-stack-parser-2.0.7.tgz#b0c6e2ce27d0495cf78ad98715e0cad1219abb57" - integrity sha512-chLOW0ZGRf4s8raLrDxa5sdkvPec5YdvwbFnqJme4rk0rFajP8mPtrDL1+I+CwrQDCjswDA5sREX7jYQDQs9vA== - dependencies: - stackframe "^1.1.1" - es-abstract@^1.19.0, es-abstract@^1.19.1: version "1.19.2" resolved "https://registry.npmmirror.com/es-abstract/-/es-abstract-1.19.2.tgz#8f7b696d8f15b167ae3640b4060670f3d054143f" @@ -10667,11 +10642,6 @@ stack-utils@^2.0.3: dependencies: escape-string-regexp "^2.0.0" -stackframe@^1.1.1: - version "1.2.1" - resolved "https://registry.npmmirror.com/stackframe/-/stackframe-1.2.1.tgz#1033a3473ee67f08e2f2fc8eba6aef4f845124e1" - integrity sha512-h88QkzREN/hy8eRdyNhhsO7RSJ5oyTqxxmmn0dzBIMUclZsjpfmrsg81vp8mjjAs2vAZ72nyWxRUwSwmh0e4xg== - static-extend@^0.1.1: version "0.1.2" resolved "https://registry.npmmirror.com/static-extend/-/static-extend-0.1.2.tgz#60809c39cbff55337226fd5e0b520f341f1fb5c6"