lvyjs 0.2.15 → 0.2.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/bin/index.js +3 -11
- package/lib/config.d.ts +6 -6
- package/lib/config.js +15 -15
- package/lib/content.js +48 -0
- package/lib/index.d.ts +11 -3
- package/lib/index.js +56 -58
- package/lib/loader.d.ts +25 -0
- package/lib/loader.js +74 -0
- package/lib/main.js +31 -29
- package/lib/postcss.js +170 -172
- package/lib/rullup/index.d.ts +3 -3
- package/lib/rullup/index.js +115 -119
- package/lib/rullup/plugins/loader-css.js +44 -46
- package/lib/rullup/plugins/loader-files.js +30 -30
- package/lib/rullup/utils/files.js +20 -19
- package/lib/store.d.ts +66 -48
- package/lib/store.js +25 -26
- package/lib/typing.d.ts +3 -3
- package/package.json +1 -1
package/bin/index.js
CHANGED
|
@@ -3,23 +3,15 @@
|
|
|
3
3
|
import { fork } from 'child_process'
|
|
4
4
|
import { join, dirname, relative } from 'path'
|
|
5
5
|
import { fileURLToPath } from 'node:url'
|
|
6
|
-
import {
|
|
6
|
+
import { createRequire } from 'node:module'
|
|
7
7
|
const args = [...process.argv.slice(2)]
|
|
8
8
|
const currentFilePath = fileURLToPath(import.meta.url)
|
|
9
9
|
const currentDirPath = dirname(currentFilePath)
|
|
10
10
|
const pkgFilr = join(currentDirPath, '../package.json')
|
|
11
11
|
const jsFile = join(currentDirPath, '../lib/index.js')
|
|
12
12
|
const jsdir = relative(process.cwd(), jsFile)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
tsxDir = join(currentDirPath, '../node_modules/tsx/dist/cli.mjs')
|
|
16
|
-
}
|
|
17
|
-
if (!existsSync(tsxDir)) {
|
|
18
|
-
tsxDir = join(process.cwd(), 'node_modules/tsx/dist/cli.mjs')
|
|
19
|
-
}
|
|
20
|
-
if (!existsSync(tsxDir)) {
|
|
21
|
-
new Error('tsx not found')
|
|
22
|
-
}
|
|
13
|
+
const require = createRequire(import.meta.url)
|
|
14
|
+
const tsxDir = require.resolve('tsx/cli')
|
|
23
15
|
if (args.includes('build')) {
|
|
24
16
|
const argsx = args.filter(arg => arg !== 'build')
|
|
25
17
|
const msg = fork(tsxDir, [jsdir, '--lvy-build', ...argsx], {
|
package/lib/config.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
declare const assetsRegExp: RegExp
|
|
2
|
-
declare const stylesRegExp: RegExp
|
|
1
|
+
declare const assetsRegExp: RegExp
|
|
2
|
+
declare const stylesRegExp: RegExp
|
|
3
3
|
/**
|
|
4
4
|
*
|
|
5
5
|
* @param val
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
|
-
declare const createAlias: (val: any) => {}
|
|
9
|
-
declare const isWin32: () => boolean
|
|
10
|
-
declare const convertPath: (inputPath: string) => string
|
|
8
|
+
declare const createAlias: (val: any) => {}
|
|
9
|
+
declare const isWin32: () => boolean
|
|
10
|
+
declare const convertPath: (inputPath: string) => string
|
|
11
11
|
|
|
12
|
-
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp }
|
|
12
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp }
|
package/lib/config.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
const assetsRegExp = /\.(png|jpg|jpeg|gif|svg|webp|ico)
|
|
2
|
-
const stylesRegExp = /\.(css|scss|less|sass|less)
|
|
1
|
+
const assetsRegExp = /\.(png|jpg|jpeg|gif|svg|webp|ico)$/
|
|
2
|
+
const stylesRegExp = /\.(css|scss|less|sass|less)$/
|
|
3
3
|
/**
|
|
4
4
|
*
|
|
5
5
|
* @param val
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
8
|
const createAlias = val => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
9
|
+
const alias = {}
|
|
10
|
+
// 遍历 entries 数组
|
|
11
|
+
val.entries.forEach(entry => {
|
|
12
|
+
alias[entry.find] = entry.replacement
|
|
13
|
+
})
|
|
14
|
+
return alias
|
|
15
|
+
}
|
|
16
16
|
const isWin32 = () => {
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
const convertPath =
|
|
20
|
-
|
|
21
|
-
}
|
|
17
|
+
return ['win32'].includes(process.platform)
|
|
18
|
+
}
|
|
19
|
+
const convertPath = inputPath => {
|
|
20
|
+
return isWin32() ? inputPath.replace(/\\/g, '/') : inputPath
|
|
21
|
+
}
|
|
22
22
|
|
|
23
|
-
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp }
|
|
23
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp }
|
package/lib/content.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { join } from 'path'
|
|
2
|
+
import crypto from 'node:crypto'
|
|
3
|
+
import { convertPath } from './config.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 生成模块内容
|
|
7
|
+
* @param {string} relativePath 相对路径
|
|
8
|
+
*/
|
|
9
|
+
const generateModuleContent = relativePath => {
|
|
10
|
+
const contents = [
|
|
11
|
+
`const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
|
|
12
|
+
`const fileUrl = import.meta.resolve('${convertPath(relativePath)}').replace(reg, '');`,
|
|
13
|
+
'export default fileUrl;'
|
|
14
|
+
].join('\n')
|
|
15
|
+
return contents
|
|
16
|
+
}
|
|
17
|
+
const getRandomName = str => {
|
|
18
|
+
// 使用 MD5 算法创建哈希对象
|
|
19
|
+
const hash = crypto.createHash('md5')
|
|
20
|
+
// 更新哈希对象内容
|
|
21
|
+
hash.update(str)
|
|
22
|
+
return hash.digest('hex')
|
|
23
|
+
}
|
|
24
|
+
const chache = {}
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @param fileUrl
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
const generateCSSModuleContent = pathURL => {
|
|
31
|
+
const fileName = getRandomName(pathURL)
|
|
32
|
+
const outputFileURL = convertPath(
|
|
33
|
+
join(process.cwd(), 'node_modules', 'lvyjs', 'assets', `${fileName}.css`)
|
|
34
|
+
)
|
|
35
|
+
if (!chache[pathURL]) {
|
|
36
|
+
global.lvyWorkerProt.postMessage({
|
|
37
|
+
type: 'CSS_MODULE_GENERATED',
|
|
38
|
+
payload: {
|
|
39
|
+
from: pathURL,
|
|
40
|
+
to: outputFileURL
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
chache[pathURL] = true
|
|
44
|
+
}
|
|
45
|
+
return `export default "${outputFileURL}";`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { generateCSSModuleContent, generateModuleContent }
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export {
|
|
2
|
+
Options,
|
|
3
|
+
PluginsCallBack,
|
|
4
|
+
PluginsOptions,
|
|
5
|
+
PluginsValue,
|
|
6
|
+
defineConfig,
|
|
7
|
+
getOptions,
|
|
8
|
+
initConfig
|
|
9
|
+
} from './store.js'
|
|
10
|
+
export { buildAndRun, buildJS } from './rullup/index.js'
|
|
11
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js'
|
package/lib/index.js
CHANGED
|
@@ -1,73 +1,71 @@
|
|
|
1
|
-
import { buildAndRun } from './rullup/index.js'
|
|
2
|
-
export { buildJS } from './rullup/index.js'
|
|
3
|
-
import { initConfig } from './store.js'
|
|
4
|
-
export { defineConfig, getOptions } from './store.js'
|
|
5
|
-
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js'
|
|
1
|
+
import { buildAndRun } from './rullup/index.js'
|
|
2
|
+
export { buildJS } from './rullup/index.js'
|
|
3
|
+
import { initConfig } from './store.js'
|
|
4
|
+
export { defineConfig, getOptions } from './store.js'
|
|
5
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @param input
|
|
9
9
|
*/
|
|
10
10
|
const onDev = async () => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
11
|
+
const apps = []
|
|
12
|
+
if (Array.isArray(global.lvyConfig?.plugins)) {
|
|
13
|
+
// 修改config
|
|
14
|
+
for (const plugin of global.lvyConfig.plugins) {
|
|
15
|
+
if (!plugin) {
|
|
16
|
+
continue
|
|
17
|
+
}
|
|
18
|
+
await apps.push(plugin(global.lvyConfig))
|
|
20
19
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (typeof app == 'function') {
|
|
29
|
-
await app(global.lvyConfig);
|
|
30
|
-
}
|
|
31
|
-
else if (typeof app.load == 'function') {
|
|
32
|
-
app.load(global.lvyConfig);
|
|
33
|
-
}
|
|
20
|
+
}
|
|
21
|
+
// 执行loader
|
|
22
|
+
await import('./main.js')
|
|
23
|
+
//
|
|
24
|
+
for (const app of apps) {
|
|
25
|
+
if (!app) {
|
|
26
|
+
continue
|
|
34
27
|
}
|
|
35
|
-
|
|
28
|
+
if (typeof app == 'function') {
|
|
29
|
+
await app(global.lvyConfig)
|
|
30
|
+
} else if (typeof app.load == 'function') {
|
|
31
|
+
app.load(global.lvyConfig)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
36
35
|
/**
|
|
37
36
|
* @param input
|
|
38
37
|
*/
|
|
39
38
|
const onBuild = async () => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
//
|
|
51
|
-
for (const app of apps) {
|
|
52
|
-
if (!app) {
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
if (typeof app != 'function' && typeof app.build == 'function') {
|
|
56
|
-
await app.build(global.lvyConfig);
|
|
57
|
-
}
|
|
39
|
+
const apps = []
|
|
40
|
+
if (Array.isArray(global.lvyConfig?.plugins)) {
|
|
41
|
+
// 修改config
|
|
42
|
+
for (const plugin of global.lvyConfig.plugins) {
|
|
43
|
+
if (!plugin) {
|
|
44
|
+
continue
|
|
45
|
+
}
|
|
46
|
+
await apps.push(plugin(global.lvyConfig))
|
|
58
47
|
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
48
|
+
}
|
|
49
|
+
//
|
|
50
|
+
for (const app of apps) {
|
|
51
|
+
if (!app) {
|
|
52
|
+
continue
|
|
64
53
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
await onBuild();
|
|
68
|
-
buildAndRun();
|
|
54
|
+
if (typeof app != 'function' && typeof app.build == 'function') {
|
|
55
|
+
await app.build(global.lvyConfig)
|
|
69
56
|
}
|
|
70
|
-
}
|
|
71
|
-
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const main = async () => {
|
|
60
|
+
if (process.argv.includes('--lvy-dev')) {
|
|
61
|
+
await initConfig()
|
|
62
|
+
await onDev()
|
|
63
|
+
} else if (process.argv.includes('--lvy-build')) {
|
|
64
|
+
await initConfig()
|
|
65
|
+
await onBuild()
|
|
66
|
+
buildAndRun()
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
main()
|
|
72
70
|
|
|
73
|
-
export { buildAndRun, initConfig }
|
|
71
|
+
export { buildAndRun, initConfig }
|
package/lib/loader.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { MessagePort } from 'worker_threads'
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
var lvyWorkerProt: MessagePort
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* @param param0
|
|
8
|
+
*/
|
|
9
|
+
declare function initialize({ port, lvyConfig }: { port: any; lvyConfig: any }): Promise<void>
|
|
10
|
+
/**
|
|
11
|
+
* @param specifier
|
|
12
|
+
* @param context
|
|
13
|
+
* @param nextResolve
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
declare function resolve(specifier: any, context: any, nextResolve: any): Promise<any>
|
|
17
|
+
/**
|
|
18
|
+
* @param url
|
|
19
|
+
* @param context
|
|
20
|
+
* @param defaultLoad
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
declare const load: (url: any, context: any, nextLoad: any) => any
|
|
24
|
+
|
|
25
|
+
export { initialize, load, resolve }
|
package/lib/loader.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { generateModuleContent, generateCSSModuleContent } from './content.js'
|
|
2
|
+
import { isWin32, convertPath } from './config.js'
|
|
3
|
+
|
|
4
|
+
const reg = isWin32() ? /^file:\/\/\// : /^file:\/\//
|
|
5
|
+
const baseURL = isWin32() ? 'file:///' : 'file://'
|
|
6
|
+
const nodeReg = /(node_modules|node_|node:)/
|
|
7
|
+
/**
|
|
8
|
+
* @param param0
|
|
9
|
+
*/
|
|
10
|
+
async function initialize({ port, lvyConfig }) {
|
|
11
|
+
global.lvyConfig = lvyConfig
|
|
12
|
+
global.lvyWorkerProt = port
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* @param specifier
|
|
16
|
+
* @param context
|
|
17
|
+
* @param nextResolve
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
async function resolve(specifier, context, nextResolve) {
|
|
21
|
+
if (!global.lvyConfig?.alias) {
|
|
22
|
+
global.lvyConfig.alias = {}
|
|
23
|
+
}
|
|
24
|
+
if (global.lvyConfig.alias?.entries) {
|
|
25
|
+
for (const { find, replacement } of global.lvyConfig.alias?.entries) {
|
|
26
|
+
if (specifier.startsWith(find)) {
|
|
27
|
+
const parentURL = `${baseURL}${convertPath(specifier.replace(find, replacement))}`
|
|
28
|
+
return nextResolve(specifier, {
|
|
29
|
+
...context,
|
|
30
|
+
parentURL: parentURL
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return nextResolve(specifier, context)
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @param url
|
|
39
|
+
* @param context
|
|
40
|
+
* @param defaultLoad
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
const load = (url, context, nextLoad) => {
|
|
44
|
+
if (nodeReg.test(url)) {
|
|
45
|
+
return nextLoad(url, context)
|
|
46
|
+
}
|
|
47
|
+
const urls = url.split('?')
|
|
48
|
+
const baseURL = urls[0]
|
|
49
|
+
// 得到静态资源。转为普通的文件引用。
|
|
50
|
+
if (!global.lvyConfig.assets) {
|
|
51
|
+
global.lvyConfig.assets = {}
|
|
52
|
+
}
|
|
53
|
+
// 匹配静态资源
|
|
54
|
+
if (global.lvyConfig.assets?.filter && global.lvyConfig.assets?.filter.test(baseURL)) {
|
|
55
|
+
const code = generateModuleContent(baseURL.replace(reg, ''))
|
|
56
|
+
return nextLoad(baseURL, {
|
|
57
|
+
format: 'module',
|
|
58
|
+
source: code
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
if (!global.lvyConfig.styles) {
|
|
62
|
+
global.lvyConfig.styles = {}
|
|
63
|
+
}
|
|
64
|
+
if (global.lvyConfig.styles?.filter && global.lvyConfig.styles?.filter.test(baseURL)) {
|
|
65
|
+
const code = generateCSSModuleContent(baseURL.replace(reg, ''))
|
|
66
|
+
return nextLoad(baseURL, {
|
|
67
|
+
format: 'module',
|
|
68
|
+
source: code
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
return nextLoad(url, context)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { initialize, load, resolve }
|
package/lib/main.js
CHANGED
|
@@ -1,36 +1,38 @@
|
|
|
1
|
-
import module from 'node:module'
|
|
2
|
-
import { MessageChannel } from 'node:worker_threads'
|
|
3
|
-
import { postCSS } from './postcss.js'
|
|
4
|
-
import { stylesRegExp, assetsRegExp } from './config.js'
|
|
1
|
+
import module from 'node:module'
|
|
2
|
+
import { MessageChannel } from 'node:worker_threads'
|
|
3
|
+
import { postCSS } from './postcss.js'
|
|
4
|
+
import { stylesRegExp, assetsRegExp } from './config.js'
|
|
5
5
|
|
|
6
6
|
if (!module.register) {
|
|
7
|
-
|
|
7
|
+
throw new Error(
|
|
8
|
+
`This version of Node.js (${process.version}) does not support module.register(). Please upgrade to Node v18.19 or v20.6 and above.`
|
|
9
|
+
)
|
|
8
10
|
}
|
|
9
|
-
const { port1, port2 } = new MessageChannel()
|
|
10
|
-
const cache = {}
|
|
11
|
+
const { port1, port2 } = new MessageChannel()
|
|
12
|
+
const cache = {}
|
|
11
13
|
port1.on('message', msg => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
14
|
+
if (msg.type == 'CSS_MODULE_GENERATED') {
|
|
15
|
+
const { from, to } = msg.payload
|
|
16
|
+
if (!cache[from]) {
|
|
17
|
+
postCSS(from, to)
|
|
18
|
+
cache[from] = true
|
|
18
19
|
}
|
|
19
|
-
}
|
|
20
|
+
}
|
|
21
|
+
})
|
|
20
22
|
// port1.unref()
|
|
21
23
|
module.register('./loader.js', {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
})
|
|
24
|
+
parentURL: import.meta.url,
|
|
25
|
+
data: {
|
|
26
|
+
port: port2,
|
|
27
|
+
lvyConfig: {
|
|
28
|
+
alias: global.lvyConfig?.alias,
|
|
29
|
+
assets: global.lvyConfig?.assets ?? {
|
|
30
|
+
filter: assetsRegExp
|
|
31
|
+
},
|
|
32
|
+
styles: global.lvyConfig?.styles ?? {
|
|
33
|
+
filter: stylesRegExp
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
transferList: [port2]
|
|
38
|
+
})
|