@moneko/core 3.2.1-beta.0 → 3.2.1-beta.2

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/lib/swcrc.js CHANGED
@@ -1,2 +1,100 @@
1
- import{merge as e}from"webpack-merge";import{CONFIG as o}from"./config.js";import r from"./paths.js";import s from"./polyfills.js";import{isReact as t,isSolid as n,jsxImportSource as c}from"./process-env.js";export const swcMinifyOption={compress:{top_retain:[],keep_infinity:!0,global_defs:{"@alert":"console.log"},pure_funcs:["console.log","console.warn","console.error","console.info"],toplevel:!1,drop_console:!0,drop_debugger:!0,module:!1,ecma:2015,ie8:!1,keep_classnames:void 0,keep_fnames:!1},mangle:!0};export default((a=!1)=>{let l={module:{type:"es6",resolveFully:!0},jsc:{parser:{syntax:"typescript",tsx:!0,decorators:!0,dynamicImport:!0},loose:!0,target:"es2017",externalHelpers:!0,transform:{legacyDecorator:!0,decoratorMetadata:!0,react:{runtime:"automatic",throwIfNamespace:!0,refresh:t&&a,development:a,importSource:c},optimizer:{simplify:!1}},experimental:{emitAssertForImportAttributes:!0,cacheRoot:r.swcCachePath,plugins:[["swc-plugin-another-transform-imports",function(e){for(let o=0,r=Object.keys(e),s=r.length;o<s;o++)e[r[o]].transform=`${r[o]}/${e[r[o]].transform}`,e[r[o]].style&&(e[r[o]].style=`${r[o]}/${e[r[o]].style}`);return e}(o.importOnDemand||{})],n&&["@moneko/jsx-dom-expressions",o.jsxDomExpressions||{}]].filter(Boolean)}},sourceMaps:!0,parseMap:!0};return(o.polyfill&&(l.jsc.target=void 0,// swc polyfill,会复用 babel 链路,但效率比 babel 低
2
- l.env={include:s(),mode:"usage",coreJs:"3.32.2"}),o.swcrc)?e(l,"function"==typeof o.swcrc?o.swcrc(a):o.swcrc):l});
1
+ import { merge } from 'webpack-merge';
2
+ import { CONFIG } from './config.js';
3
+ import paths from './paths.js';
4
+ import polyfills from './polyfills.js';
5
+ import { isReact, isSolid, jsxImportSource } from './process-env.js';
6
+ export const swcMinifyOption = {
7
+ compress: {
8
+ top_retain: [],
9
+ keep_infinity: true,
10
+ global_defs: {
11
+ '@alert': 'console.log'
12
+ },
13
+ pure_funcs: [
14
+ 'console.log',
15
+ 'console.warn',
16
+ 'console.error',
17
+ 'console.info'
18
+ ],
19
+ toplevel: false,
20
+ drop_console: true,
21
+ drop_debugger: true,
22
+ module: false,
23
+ ecma: 2015,
24
+ ie8: false,
25
+ keep_classnames: void 0,
26
+ keep_fnames: false
27
+ },
28
+ mangle: true
29
+ };
30
+ function transformConfigs(config) {
31
+ for(let i = 0, ks = Object.keys(config), l = ks.length; i < l; i++){
32
+ config[ks[i]].transform = `${ks[i]}/${config[ks[i]].transform}`;
33
+ if (config[ks[i]].style) {
34
+ config[ks[i]].style = `${ks[i]}/${config[ks[i]].style}`;
35
+ }
36
+ }
37
+ return config;
38
+ }
39
+ export default ((isDev = false)=>{
40
+ const swcOption = {
41
+ module: {
42
+ type: 'es6',
43
+ resolveFully: true
44
+ },
45
+ jsc: {
46
+ parser: {
47
+ syntax: 'typescript',
48
+ tsx: true,
49
+ decorators: true,
50
+ dynamicImport: true
51
+ },
52
+ loose: true,
53
+ target: 'es2017',
54
+ externalHelpers: true,
55
+ transform: {
56
+ legacyDecorator: true,
57
+ decoratorMetadata: true,
58
+ react: {
59
+ runtime: 'automatic',
60
+ throwIfNamespace: true,
61
+ refresh: isReact && isDev,
62
+ development: isDev,
63
+ importSource: jsxImportSource
64
+ },
65
+ optimizer: {
66
+ simplify: false
67
+ }
68
+ },
69
+ experimental: {
70
+ emitAssertForImportAttributes: true,
71
+ cacheRoot: paths.swcCachePath,
72
+ plugins: [
73
+ [
74
+ 'swc-plugin-another-transform-imports',
75
+ transformConfigs(CONFIG.importOnDemand || {})
76
+ ],
77
+ isSolid && [
78
+ '@moneko/jsx-dom-expressions',
79
+ CONFIG.jsxDomExpressions || {}
80
+ ]
81
+ ].filter(Boolean)
82
+ }
83
+ },
84
+ sourceMaps: true,
85
+ parseMap: true
86
+ };
87
+ if (CONFIG.polyfill) {
88
+ swcOption.jsc.target = void 0;
89
+ // swc polyfill,会复用 babel 链路,但效率比 babel 低
90
+ swcOption.env = {
91
+ include: polyfills(),
92
+ mode: 'usage',
93
+ coreJs: '3.32.2'
94
+ };
95
+ }
96
+ if (CONFIG.swcrc) {
97
+ return merge(swcOption, typeof CONFIG.swcrc === 'function' ? CONFIG.swcrc(isDev) : CONFIG.swcrc);
98
+ }
99
+ return swcOption;
100
+ });
@@ -1,2 +1,26 @@
1
- import t from"fs";import r from"ts-import-plugin";import o from"webpack-merge";import{CONFIG as e}from"./config.js";import{isFunction as s,readConf as m,resolveProgramPath as a}from"./utils.js";let i=[];Array.isArray(e.importOnDemand)&&Object.assign(i,e.importOnDemand.map(t=>r(t)));let n=[...i],p={transpileOnly:!0,getCustomTransformers:()=>({before:n})};try{let r=a("tsloader.config.ts");t.accessSync(r,t.constants.R_OK);let e=(await m(r,"tsloader.config")).default;p=o(p,s(e)?e(process):e);// eslint-disable-next-line no-empty
2
- }catch(t){}export default p;
1
+ import fs from 'fs';
2
+ import transformerFactory from 'ts-import-plugin';
3
+ import merge from 'webpack-merge';
4
+ import { CONFIG } from './config.js';
5
+ import { isFunction, readConf, resolveProgramPath } from './utils.js';
6
+ const importOnDemand = [];
7
+ if (Array.isArray(CONFIG.importOnDemand)) {
8
+ Object.assign(importOnDemand, CONFIG.importOnDemand.map((item)=>transformerFactory(item)));
9
+ }
10
+ const beforeTransformers = [
11
+ ...importOnDemand
12
+ ];
13
+ let tsLoaderConfig = {
14
+ transpileOnly: true,
15
+ getCustomTransformers: ()=>({
16
+ before: beforeTransformers
17
+ })
18
+ };
19
+ try {
20
+ const customTsloaderConfigPath = resolveProgramPath('tsloader.config.ts');
21
+ fs.accessSync(customTsloaderConfigPath, fs.constants.R_OK);
22
+ const customTsloaderConfig = (await readConf(customTsloaderConfigPath, 'tsloader.config')).default;
23
+ tsLoaderConfig = merge(tsLoaderConfig, isFunction(customTsloaderConfig) ? customTsloaderConfig(process) : customTsloaderConfig);
24
+ // eslint-disable-next-line no-empty
25
+ } catch (error) {}
26
+ export default tsLoaderConfig;
package/lib/utils.js CHANGED
@@ -1,15 +1,79 @@
1
- import{readFileSync as t,writeFileSync as e}from"fs";import{resolve as o}from"path";import{transformFileSync as r}from"@swc/core";import n from"./paths.js";import c from"./resolver-sync.js";let p=/export\s+(?:async\s+)?(?:function|const)\s+(\w+)/g;/** 获取导出标识
1
+ import { readFileSync, writeFileSync } from 'fs';
2
+ import { resolve } from 'path';
3
+ import { transformFileSync } from '@swc/core';
4
+ import paths from './paths.js';
5
+ import resolverSync from './resolver-sync.js';
6
+ const exportPattern = /export\s+(?:async\s+)?(?:function|const)\s+(\w+)/g;
7
+ /** 获取导出标识
2
8
  * @param {string} path 文件地址
3
9
  * @returns {string[]} 包含的标识
4
- */export function getExportTokens(e){let o;let r=t(e,{encoding:"utf-8"}),n=[];for(;o=p.exec(r);)n.includes(o[1])||n.push(o[1]);return n}let s={inputSourceMap:!1,sourceMaps:!1,module:{type:"es6"},jsc:{parser:{syntax:"typescript"},loose:!1}};export function tfc(t){return r(t,s).code||"{}"}export function readConf(t,o){let r=`${n.cachePath}/${o}.mjs`;return e(r,tfc(t),"utf-8"),import(r)}export function toUpperCaseString(t){return t?.replace(/\b\w/g,t=>t.toUpperCase()).replace(/\./g," ")}/** 位于项目根目录下的位置
10
+ */ export function getExportTokens(path) {
11
+ const code = readFileSync(path, {
12
+ encoding: 'utf-8'
13
+ });
14
+ const tokens = [];
15
+ let match;
16
+ while(match = exportPattern.exec(code)){
17
+ if (!tokens.includes(match[1])) {
18
+ tokens.push(match[1]);
19
+ }
20
+ }
21
+ return tokens;
22
+ }
23
+ const swcOption = {
24
+ inputSourceMap: false,
25
+ sourceMaps: false,
26
+ module: {
27
+ type: 'es6'
28
+ },
29
+ jsc: {
30
+ parser: {
31
+ syntax: 'typescript'
32
+ },
33
+ loose: false
34
+ }
35
+ };
36
+ export function tfc(filepath) {
37
+ return transformFileSync(filepath, swcOption).code || '{}';
38
+ }
39
+ export function readConf(src, name) {
40
+ const cacheFile = `${paths.cachePath}/${name}.mjs`;
41
+ writeFileSync(cacheFile, tfc(src), 'utf-8');
42
+ return import(cacheFile);
43
+ }
44
+ export function toUpperCaseString(string) {
45
+ return string?.replace(/\b\w/g, (th)=>th.toUpperCase()).replace(/\./g, ' ');
46
+ }
47
+ /** 位于项目根目录下的位置
5
48
  * @param {string} src 路径
6
49
  * @returns {string} 位于项目根目录下的位置
7
- */export function resolveProgramPath(t){return o(n.programPath,`./${t}`)}/** 位于项目根目录node_modules下的位置
50
+ */ export function resolveProgramPath(src) {
51
+ return resolve(paths.programPath, `./${src}`);
52
+ }
53
+ /** 位于项目根目录node_modules下的位置
8
54
  * @param {string} src 路径
9
55
  * @returns {string} 位于项目根目录node_modules下的位置
10
- */export const resolveNodeModulesPath=t=>resolveProgramPath(`node_modules/${t}`);/** 获取模块真实入口位置
56
+ */ export const resolveNodeModulesPath = (src)=>{
57
+ return resolveProgramPath(`node_modules/${src}`);
58
+ };
59
+ /** 获取模块真实入口位置
11
60
  * @param {string} url 路径
12
61
  * @returns {string} 模块真实入口路径
13
- */export const realResolve=t=>c.resolveSync({},n.programPath,t)||t;export function isObject(t){let e=typeof t;return null!==t&&("object"==e||"function"==e)}// eslint-disable-next-line no-unused-vars
14
- export function isFunction(t){if(!isObject(t))return!1;let e=Object.prototype.toString.call(t);return"[object Function]"==e||"[object AsyncFunction]"==e||"[object GeneratorFunction]"==e||"[object Proxy]"==e}export function empty(){// empty void
15
- }
62
+ */ export const realResolve = (url)=>resolverSync.resolveSync({}, paths.programPath, url) || url;
63
+ const funcTag = '[object Function]';
64
+ const asyncTag = '[object AsyncFunction]';
65
+ const genTag = '[object GeneratorFunction]';
66
+ const proxyTag = '[object Proxy]';
67
+ export function isObject(target) {
68
+ const type = typeof target;
69
+ return target !== null && (type == 'object' || type == 'function');
70
+ }
71
+ // eslint-disable-next-line no-unused-vars
72
+ export function isFunction(target) {
73
+ if (!isObject(target)) return false;
74
+ const tagType = Object.prototype.toString.call(target);
75
+ return tagType == funcTag || tagType == asyncTag || tagType == genTag || tagType == proxyTag;
76
+ }
77
+ export function empty() {
78
+ // empty void
79
+ }
@@ -1 +1,49 @@
1
- import o from"webpack-virtual-modules";import{docs as t}from"./docs.js";import{locales as e,localesModuleName as s}from"./locales.js";import{isLibrary as i}from"./process-env.js";import{exampleModuleName as p,examples as r,route as a,routesModuleName as l}from"./routes.js";import{resolveNodeModulesPath as n}from"./utils.js";export default class{constructor(o){this.options=o,this.hasTapped=!1}apply(f){let h=new o({[n(l)]:a.getData(l),[n(p)]:"export default []",[n(s)]:e.getData(s)});function c(o,t){h.writeModule(n(o),t||"")}h.apply(f),a.on("change",c),e.on("change",c),i&&(r.on("change",c),t.on("change",c)),f.hooks.compilation.tap("VirtualModulePlugin",()=>{if(!this.hasTapped){for(let o in this.options)if(Object.prototype.hasOwnProperty.call(this.options,o)){let t=this.options[o];c(o,"string"==typeof t?t:`export default ${JSON.stringify(t)}`)}for(let o of r)c(...o);for(let o of t)c(...o);this.hasTapped=!0}})}}
1
+ import VirtualModulesPlugin from 'webpack-virtual-modules';
2
+ import { docs } from './docs.js';
3
+ import { locales, localesModuleName } from './locales.js';
4
+ import { isLibrary } from './process-env.js';
5
+ import { exampleModuleName, examples, route, routesModuleName } from './routes.js';
6
+ import { resolveNodeModulesPath } from './utils.js';
7
+ class VirtualModulePlugin {
8
+ options;
9
+ hasTapped;
10
+ constructor(options){
11
+ this.options = options;
12
+ this.hasTapped = false;
13
+ }
14
+ apply(compiler) {
15
+ const vm = new VirtualModulesPlugin({
16
+ [resolveNodeModulesPath(routesModuleName)]: route.getData(routesModuleName),
17
+ [resolveNodeModulesPath(exampleModuleName)]: 'export default []',
18
+ [resolveNodeModulesPath(localesModuleName)]: locales.getData(localesModuleName)
19
+ });
20
+ vm.apply(compiler);
21
+ function modify(key, value) {
22
+ vm.writeModule(resolveNodeModulesPath(key), value || '');
23
+ }
24
+ route.on('change', modify);
25
+ locales.on('change', modify);
26
+ if (isLibrary) {
27
+ examples.on('change', modify);
28
+ docs.on('change', modify);
29
+ }
30
+ compiler.hooks.compilation.tap('VirtualModulePlugin', ()=>{
31
+ if (!this.hasTapped) {
32
+ for(const key in this.options){
33
+ if (Object.prototype.hasOwnProperty.call(this.options, key)) {
34
+ const value = this.options[key];
35
+ modify(key, typeof value === 'string' ? value : `export default ${JSON.stringify(value)}`);
36
+ }
37
+ }
38
+ for (const item of examples){
39
+ modify(...item);
40
+ }
41
+ for (const item of docs){
42
+ modify(...item);
43
+ }
44
+ this.hasTapped = true;
45
+ }
46
+ });
47
+ }
48
+ }
49
+ export default VirtualModulePlugin;
@@ -1 +1,37 @@
1
- import{accessSync as p,constants as o}from"fs";import{join as r}from"path";import e from"./app-entry.js";import s from"./app.js";import{CONFIG as m}from"./config.js";import{coverage as t}from"./coverage.js";import a from"./fallback.js";import c from"./merge-router.js";import f from"./normalize-css.js";import i from"./prefix-router.js";import{FRAMEWORKNAME as l,isLibrary as n,isReact as u}from"./process-env.js";import{resolveProgramPath as x}from"./utils.js";let j={...m.virtualModule,"@app":s,"@app/fallback":a,"@app/coverage":t,"@app/normalize/index.css":f,"@app/prefix-router":i,"@app/suspense/index.tsx":`import ${u?"React,":""}{Suspense,lazy} from "${l}";import Fallback from '@app/fallback';${n?'import scope from "@app/mdx-scope";':""}function SuspenseComp(props) {const Lazy = lazy(props.comp);return (<Suspense fallback={Fallback && <Fallback />}><Lazy ${n?"components={scope}":""}/></Suspense>);}export default SuspenseComp;`,"@app/entry":e,"@app/merge-router":c},d={};if(n)try{let e=r(x("site"),"./mdx-scope.ts");p(e,o.R_OK),d='import scope from "@/mdx-scope";export default scope;'}catch(p){d={}}Object.assign(j,{"@app/mdx-scope":d});export default j;
1
+ import { accessSync, constants } from 'fs';
2
+ import { join } from 'path';
3
+ import appEntry from './app-entry.js';
4
+ import app from './app.js';
5
+ import { CONFIG } from './config.js';
6
+ import { coverage } from './coverage.js';
7
+ import fallback from './fallback.js';
8
+ import mergeRouter from './merge-router.js';
9
+ import normalizeCss from './normalize-css.js';
10
+ import prefixRouter from './prefix-router.js';
11
+ import { FRAMEWORKNAME, isLibrary, isReact } from './process-env.js';
12
+ import { resolveProgramPath } from './utils.js';
13
+ const virtualModules = {
14
+ ...CONFIG.virtualModule,
15
+ '@app': app,
16
+ '@app/fallback': fallback,
17
+ '@app/coverage': coverage,
18
+ '@app/normalize/index.css': normalizeCss,
19
+ '@app/prefix-router': prefixRouter,
20
+ '@app/suspense/index.tsx': `import ${isReact ? 'React,' : ''}{Suspense,lazy} from "${FRAMEWORKNAME}";import Fallback from '@app/fallback';${isLibrary ? 'import scope from "@app/mdx-scope";' : ''}function SuspenseComp(props) {const Lazy = lazy(props.comp);return (<Suspense fallback={Fallback && <Fallback />}><Lazy ${isLibrary ? 'components={scope}' : ''}/></Suspense>);}export default SuspenseComp;`,
21
+ '@app/entry': appEntry,
22
+ '@app/merge-router': mergeRouter
23
+ };
24
+ let mdxScope = {};
25
+ if (isLibrary) {
26
+ try {
27
+ const appEntryPath = join(resolveProgramPath('site'), './mdx-scope.ts');
28
+ accessSync(appEntryPath, constants.R_OK);
29
+ mdxScope = 'import scope from "@/mdx-scope";export default scope;';
30
+ } catch (error) {
31
+ mdxScope = {};
32
+ }
33
+ }
34
+ Object.assign(virtualModules, {
35
+ '@app/mdx-scope': mdxScope
36
+ });
37
+ export default virtualModules;
package/lib/yarn-argv.js CHANGED
@@ -1 +1,9 @@
1
- let e=JSON.parse(process.env.npm_config_argv||"{}")?.original,a={};e?.forEach(e=>{let r=e.split("=");Object.assign(a,{[r[0]]:r[1]||!0})});export default a;
1
+ const original = JSON.parse(process.env.npm_config_argv || '{}')?.original;
2
+ const yarnArgv = {};
3
+ original?.forEach((o)=>{
4
+ const m = o.split('=');
5
+ Object.assign(yarnArgv, {
6
+ [m[0]]: m[1] || true
7
+ });
8
+ });
9
+ export default yarnArgv;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@moneko/core",
3
- "version": "3.2.1-beta.0",
3
+ "version": "3.2.1-beta.2",
4
4
  "description": "core",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
- "build": "swc src -d lib -C module.type=nodenext -C jsc.target=esnext -C jsc.loose=true -C jsc.minify.mangle=true -C jsc.minify.compress=true -C minify=true -D",
8
+ "build": "swc src -d lib -C module.type=nodenext -C jsc.target=esnext -C jsc.loose=false -C jsc.minify.mangle=false -C jsc.minify.compress=false -C minify=false -D",
9
9
  "prebuild": "rm -rf ./lib && tsc"
10
10
  },
11
11
  "keywords": [],
@@ -26,8 +26,12 @@
26
26
  "css-minimizer-webpack-plugin": "5.0.1",
27
27
  "external-remotes-plugin": "1.0.0",
28
28
  "html-webpack-plugin": "5.5.3",
29
+ "less": "4.2.0",
30
+ "less-loader": "11.1.3",
29
31
  "mini-css-extract-plugin": "2.7.6",
30
32
  "mini-svg-data-uri": "1.4.4",
33
+ "sass": "1.69.5",
34
+ "sass-loader": "13.3.2",
31
35
  "style-loader": "3.3.3",
32
36
  "style-resources-loader": "1.5.0",
33
37
  "swc-loader": "0.2.3",
@@ -30,11 +30,10 @@ export type HtmlWebpackOption = HtmlWebpackPluginOptions & {
30
30
  export type CssMinify = 'swc' | 'cssnano';
31
31
 
32
32
  export type RulesInclude = {
33
- less?: string[];
34
33
  css?: string[];
35
34
  js?: string[];
36
35
  media?: string[];
37
- fonts?: string[];
36
+ font?: string[];
38
37
  wasm?: string[];
39
38
  };
40
39
  export interface Theme {