@lynx-js/rspeedy
Version:
A webpack/rspack-based frontend toolchain for Lynx
378 lines (377 loc) • 18.6 kB
JavaScript
export const __webpack_ids__ = [
"src_cli_dev_ts"
];
export const __webpack_modules__ = {
"./src/cli/dev.ts": function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
dev: ()=>dev
});
var node_path__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("node:path");
var _rsbuild_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("@rsbuild/core");
var picocolors__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__("../../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js");
var picocolors__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/ __webpack_require__.n(picocolors__WEBPACK_IMPORTED_MODULE_5__);
var _create_rspeedy_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__("./src/create-rspeedy.ts");
var _exit_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__("./src/cli/exit.ts");
var _init_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__("./src/cli/init.ts");
async function dev(cwd, devOptions) {
let onBeforeRestart = [];
try {
const { rspeedyConfig, configPath, createRspeedyOptions } = await (0, _init_js__WEBPACK_IMPORTED_MODULE_4__.S)(cwd, devOptions);
const watchedFiles = [
configPath
];
if (Array.isArray(rspeedyConfig.dev?.watchFiles)) watchedFiles.push(...rspeedyConfig.dev.watchFiles.filter((item)=>'reload-server' === item.type).flatMap((item)=>item.paths));
else if (rspeedyConfig.dev?.watchFiles?.type === 'reload-server') {
const { paths } = rspeedyConfig.dev.watchFiles;
watchedFiles.push(...Array.isArray(paths) ? paths : [
paths
]);
}
await watchFiles(watchedFiles.map((filePath)=>node_path__WEBPACK_IMPORTED_MODULE_0__["default"].isAbsolute(filePath) ? filePath : node_path__WEBPACK_IMPORTED_MODULE_0__["default"].join(cwd, filePath)), async (filename)=>{
_rsbuild_core__WEBPACK_IMPORTED_MODULE_1__.logger.info(`Restart because ${picocolors__WEBPACK_IMPORTED_MODULE_5___default().yellow(filename)} is changed.\n`);
const cleanup = onBeforeRestart.map((f)=>f());
onBeforeRestart = [];
await Promise.all(cleanup);
await dev.call(this, cwd, devOptions);
});
const rspeedy = await (0, _create_rspeedy_js__WEBPACK_IMPORTED_MODULE_2__.S)(createRspeedyOptions);
const server = await rspeedy.createDevServer();
const { server: { close } } = await server.listen();
onBeforeRestart.push(close);
} catch (error) {
_rsbuild_core__WEBPACK_IMPORTED_MODULE_1__.logger.error(error);
(0, _exit_js__WEBPACK_IMPORTED_MODULE_3__.exit)(1);
}
}
async function watchFiles(files, callback) {
const chokidar = await __webpack_require__.e("vendors-node_modules_pnpm_chokidar_4_0_3_node_modules_chokidar_esm_index_js").then(__webpack_require__.bind(__webpack_require__, "../../../node_modules/.pnpm/chokidar@4.0.3/node_modules/chokidar/esm/index.js"));
const watcher = chokidar.default.watch(files, {
ignoreInitial: true,
ignorePermissionErrors: true
});
const cb = debounce((event, filePath)=>{
const startTime = Date.now();
watcher.close().then(()=>callback(filePath, startTime, event));
}, 300);
watcher.once('add', cb.bind(null, 'add'));
watcher.once('change', cb.bind(null, 'change'));
watcher.once('unlink', cb.bind(null, 'unlink'));
}
function debounce(func, wait) {
let timeoutId = null;
return (...args)=>{
if (null !== timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(()=>{
func(...args);
}, wait);
};
}
},
"./src/cli/init.ts": function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.d(__webpack_exports__, {
S: ()=>init
});
var external_node_fs_ = __webpack_require__("node:fs");
var external_node_path_ = __webpack_require__("node:path");
var external_node_url_ = __webpack_require__("node:url");
var picocolors = __webpack_require__("../../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js");
var picocolors_default = /*#__PURE__*/ __webpack_require__.n(picocolors);
var register_ = __webpack_require__("@lynx-js/rspeedy/register");
var debug = __webpack_require__("./src/debug.ts");
const resolveConfigPath = (root, customConfig)=>{
if (customConfig) {
(0, debug.fF)(`load custom config file ${customConfig} from ${root}`);
const customConfigPath = (0, external_node_path_.isAbsolute)(customConfig) ? customConfig : (0, external_node_path_.join)(root, customConfig);
if (external_node_fs_["default"].existsSync(customConfigPath)) return customConfigPath;
throw new Error(`Cannot find config file: ${picocolors_default().dim(customConfigPath)}`);
}
const CONFIG_FILES = [
'lynx.config.ts',
'lynx.config.js',
'lynx.config.mts',
'lynx.config.mjs'
];
for (const file of CONFIG_FILES){
(0, debug.fF)(`load default config file ${file} from ${root}`);
const configFile = (0, external_node_path_.join)(root, file);
if (external_node_fs_["default"].existsSync(configFile)) {
(0, debug.fF)(`default config ${configFile} found`);
return configFile;
}
}
throw new Error([
`Cannot find the default config file: ${picocolors_default().dim((0, external_node_path_.join)(root, CONFIG_FILES[0]))}.`,
`Use custom config with ${picocolors_default().green('`--config <config>`')} options.`
].join(' '));
};
async function loadConfig(loadConfigOptions) {
let { configPath } = loadConfigOptions;
if (!configPath || !(0, external_node_path_.isAbsolute)(configPath)) configPath = resolveConfigPath(loadConfigOptions.cwd ?? process.cwd(), configPath);
const specifier = (0, external_node_url_.pathToFileURL)(configPath).toString();
let unregister;
unregister = shouldUseNativeImport(configPath) ? ()=>{} : (0, register_.register)({
load: !hasNativeTSSupport(),
resolve: true
});
try {
const [exports, { validate }] = await Promise.all([
import(`${specifier}?t=${Date.now()}`),
__webpack_require__.e("src_config_validate_ts").then(__webpack_require__.bind(__webpack_require__, "./src/config/validate.ts"))
]);
const content = validate('default' in exports ? exports.default : exports, configPath);
return {
configPath,
content: 'function' == typeof content ? await content() : await content
};
} finally{
unregister();
}
}
function shouldUseNativeImport(configPath) {
return isJavaScriptPath(configPath) || isDeno();
}
function hasNativeTSSupport() {
if (isDeno()) return true;
if (process.features.typescript) return true;
if (false === process.features.typescript) return false;
const { NODE_OPTIONS } = process.env;
if (!NODE_OPTIONS) return false;
return NODE_OPTIONS.includes('--experimental-transform-types') || NODE_OPTIONS.includes('--experimental-strip-types');
}
function isJavaScriptPath(configPath) {
const ext = (0, external_node_path_.extname)(configPath);
return [
'.js',
'.mjs',
'.cjs'
].includes(ext);
}
function isDeno() {
if ('undefined' != typeof Deno || process.versions?.deno) return true;
return false;
}
async function init(cwd, options) {
const { content: rspeedyConfig, configPath } = await loadConfig({
cwd,
configPath: options.config
});
if (rspeedyConfig.performance?.buildCache) if (true === rspeedyConfig.performance.buildCache) rspeedyConfig.performance.buildCache = {
buildDependencies: [
configPath
]
};
else {
rspeedyConfig.performance.buildCache.buildDependencies ??= [];
rspeedyConfig.performance.buildCache.buildDependencies.push(configPath);
}
const createRspeedyOptions = {
cwd,
rspeedyConfig
};
if (options.noEnv) createRspeedyOptions.loadEnv = false;
else if (options.envMode) createRspeedyOptions.loadEnv = {
mode: options.envMode
};
if ('base' in options && options.base) {
rspeedyConfig.server ??= {};
rspeedyConfig.server.base = options.base;
}
if ('environment' in options && options.environment) createRspeedyOptions.environment = options.environment;
if (options.mode) rspeedyConfig.mode = options.mode;
return {
createRspeedyOptions,
configPath,
rspeedyConfig
};
}
},
"./src/create-rspeedy.ts": function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.d(__webpack_exports__, {
S: ()=>createRspeedy
});
var external_node_path_ = __webpack_require__("node:path");
var core_ = __webpack_require__("@rsbuild/core");
var debug = __webpack_require__("./src/debug.ts");
function applyDefaultRspeedyConfig(config) {
const enableChunkSplitting = config.performance?.chunkSplit?.strategy && config.performance?.chunkSplit?.strategy !== 'all-in-one';
return (0, core_.mergeRsbuildConfig)({
mode: (()=>{
if (config.mode) return config.mode;
const nodeEnv = process.env['NODE_ENV'];
return 'production' === nodeEnv || 'development' === nodeEnv ? nodeEnv : 'none';
})(),
output: {
filename: getFilename(config.output?.filename),
inlineScripts: !enableChunkSplitting
},
performance: {
profile: (0, debug.L1)() ? true : void 0
},
tools: {
rsdoctor: {
experiments: {
enableNativePlugin: true
}
}
}
}, config);
}
const DEFAULT_FILENAME = '[name].[platform].bundle';
function getFilename(filename) {
if ('string' == typeof filename) return {
bundle: filename,
template: filename
};
const finalFilename = filename?.bundle ?? filename?.template ?? DEFAULT_FILENAME;
return {
bundle: finalFilename,
template: finalFilename
};
}
const DEFAULT_ENTRY = './src/index.js';
function toRsbuildEntry(entry) {
if (void 0 === entry) {
(0, debug.fF)(`Using default entry ${DEFAULT_ENTRY}`);
return {
main: DEFAULT_ENTRY
};
}
if (Array.isArray(entry) || 'string' == typeof entry) {
(0, debug.fF)(()=>`Using single entry ${[
''
].concat(entry).join('\n - ')}`);
return {
main: entry
};
}
return Object.fromEntries(Object.entries(entry).map(([key, value])=>{
if (Array.isArray(value) || 'string' == typeof value) {
(0, debug.NW)(`Using multiple entries - ${key}`, value);
return [
key,
{
import: value
}
];
}
(0, debug.NW)(`Using multiple entries - ${key}`, value.import ?? DEFAULT_ENTRY);
if (void 0 === value.import) return [
key,
{
...value,
import: DEFAULT_ENTRY
}
];
return [
key,
value
];
}));
}
const defaultDataUriLimit = 2048;
function toRsbuildConfig(config) {
return {
dev: {
watchFiles: config.dev?.watchFiles,
writeToDisk: config.dev?.writeToDisk ?? true,
progressBar: config.dev?.progressBar ?? true
},
environments: config.environments ?? {
lynx: {}
},
mode: config.mode,
output: {
assetPrefix: config.output?.assetPrefix,
charset: 'utf8',
cleanDistPath: config.output?.cleanDistPath,
copy: config.output?.copy,
cssModules: config.output?.cssModules,
dataUriLimit: config.output?.dataUriLimit ?? defaultDataUriLimit,
distPath: config.output?.distPath,
filenameHash: config.output?.filenameHash,
inlineScripts: config.output?.inlineScripts,
legalComments: config.output?.legalComments ?? 'none',
polyfill: 'off',
sourceMap: config.output?.sourceMap
},
resolve: {
alias: config.source?.alias
},
source: {
assetsInclude: config.source?.assetsInclude,
decorators: config.source?.decorators,
define: config.source?.define,
entry: toRsbuildEntry(config.source?.entry),
exclude: config.source?.exclude,
include: config.source?.include,
preEntry: config.source?.preEntry,
transformImport: config.source?.transformImport,
tsconfigPath: config.source?.tsconfigPath
},
server: {
base: config.server?.base,
headers: config.server?.headers,
host: config.server?.host,
port: config.server?.port,
strictPort: config.server?.strictPort
},
plugins: config.plugins,
performance: {
buildCache: config.performance?.buildCache,
chunkSplit: config.performance?.chunkSplit,
profile: config.performance?.profile,
removeConsole: toRsbuildRemoveConsole(config),
printFileSize: config.performance?.printFileSize ?? true
},
tools: {
bundlerChain: config.tools?.bundlerChain,
cssExtract: config.tools?.cssExtract,
cssLoader: config.tools?.cssLoader,
htmlPlugin: false,
rspack: config.tools?.rspack,
swc: config.tools?.swc
}
};
}
function toRsbuildRemoveConsole(config) {
if (config.performance?.removeConsole === true) return [
'log',
'warn',
'error',
'info',
'debug',
'profile',
'profileEnd'
];
return config.performance?.removeConsole;
}
async function createRspeedy({ cwd = process.cwd(), rspeedyConfig = {}, loadEnv = true, environment = [], callerName = 'rspeedy' }) {
const config = applyDefaultRspeedyConfig(rspeedyConfig);
const [rspeedy, { applyDefaultPlugins }] = await Promise.all([
(0, core_.createRsbuild)({
cwd,
loadEnv,
rsbuildConfig: toRsbuildConfig(config),
environment,
callerName
}),
__webpack_require__.e("src_plugins_index_ts").then(__webpack_require__.bind(__webpack_require__, "./src/plugins/index.ts"))
]);
await applyDefaultPlugins(rspeedy, config);
const inspectConfig = rspeedy.inspectConfig.bind(rspeedy);
return Object.assign(rspeedy, {
getRspeedyConfig: ()=>config,
async inspectConfig (options) {
const result = await inspectConfig(options);
const { inspectRspeedyConfig } = await Promise.all([
__webpack_require__.e("vendors-node_modules_pnpm_javascript-stringify_2_1_0_node_modules_javascript-stringify_dist_i-562fbc"),
__webpack_require__.e("src_plugins_inspect_plugin_ts")
]).then(__webpack_require__.bind(__webpack_require__, "./src/plugins/inspect.plugin.ts"));
await inspectRspeedyConfig(rspeedyConfig, external_node_path_["default"].resolve(options.outputPath ?? rspeedy.context.distPath, '.rsbuild/rspeedy.config.js'), options.verbose ?? false);
return result;
}
});
}
}
};