Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mpflow",
"version": "0.0.1",
"version": "1.0.0-alpha.1",
"description": "enhanced cli tool for mp deveopers",
"main": "index.js",
"private": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/cli",
"version": "0.0.35",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-babel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/plugin-babel",
"version": "0.0.38",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-css/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/plugin-css",
"version": "0.0.34",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-e2e-test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/plugin-e2e-test",
"version": "0.0.34",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-slim/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/plugin-slim",
"version": "0.0.34",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/plugin-typescript",
"version": "0.0.34",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-unit-test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/plugin-unit-test",
"version": "0.0.34",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/service-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/service-core",
"version": "0.0.15",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/service",
"version": "0.0.34",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/template-miniprogram-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/template-miniprogram-plugin",
"version": "0.0.11",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/template-miniprogram/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/template-miniprogram",
"version": "0.0.12",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mpflow/test-utils",
"private": true,
"version": "0.0.8",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down
6 changes: 4 additions & 2 deletions packages/webpack-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/webpack-plugin",
"version": "0.0.29",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand Down Expand Up @@ -42,8 +42,10 @@
"webpack": "^4.43.0"
},
"dependencies": {
"@babel/runtime": "^7.25.6",
"deepmerge": "^4.3.1",
"ejs": "^3.1.10",
"webpack-chain": "^6.5.1"
"webpack-chain": "^6.5.1",
"webpack-sources": "^1.4.3"
}
}
4 changes: 3 additions & 1 deletion packages/wxml-loader/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpflow/wxml-loader",
"version": "0.0.10",
"version": "1.0.0-alpha.1",
"description": "> TODO: description",
"author": "chyizheng <chyizheng@tencent.com>",
"homepage": "",
Expand All @@ -26,6 +26,8 @@
"test-debug": "node --inspect-brk ../../node_modules/.bin/jest --config ../../jest.config.js --runInBand"
},
"dependencies": {
"@mpflow/wxml-parser": "workspace:^",
"@babel/runtime": "^7.25.6",
"fast-glob": "^3.3.2",
"loader-utils": "^2.0.4",
"schema-utils": "^2.7.1",
Expand Down
189 changes: 59 additions & 130 deletions packages/wxml-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,9 @@
import { getOptions, interpolateName, stringifyRequest } from 'loader-utils'
import { getOptions, interpolateName, stringifyRequest, urlToRequest } from 'loader-utils'
import path from 'path'
import validateOptions from 'schema-utils'
import { SourceMapGenerator, RawSourceMap } from 'source-map'
import { RawSourceMap } from 'source-map'
import { loader } from 'webpack'
import {
importPlugin,
PluginChildImportMessage,
PluginImportMessage,
PluginReplaceMessage,
pluginRunner,
} from './plugins'
import { ImportAttribute } from './plugins/import-plugin'
import * as parser from './wxml-parser'

function getImportCode(
loaderContext: loader.LoaderContext,
imports: PluginImportMessage['value'][],
esModule: boolean,
) {
let code = ''

const apiUrl = stringifyRequest(loaderContext, require.resolve('./runtime/api'))

code += esModule
? `import ___WXML_LOADER_API_IMPORT___ from ${apiUrl};\n`
: `var ___WXML_LOADER_API_IMPORT___ = require(${apiUrl})\n`

for (const item of imports) {
const { importName, url } = item
const request = path.isAbsolute(url) ? JSON.stringify(url) : stringifyRequest(loaderContext, url)

code += esModule ? `import ${importName} from ${request};\n` : `var ${importName} = require(${request});\n`
}

return code
}

function getModuleCode(
result: {
code: string
map: SourceMapGenerator | undefined
},
childImports: PluginChildImportMessage['value'][],
replacers: PluginReplaceMessage['value'][],
url: string,
esModule: boolean,
sourceMap: boolean,
) {
let content = JSON.stringify(result.code)
const sourceMapContent = sourceMap && result.map ? result.map.toString() : ' undefined'
let beforeCode = ''

beforeCode += esModule
? `var exports = ___WXML_LOADER_API_IMPORT___();\n`
: `exports = ___WXML_LOADER_API_IMPORT___();\n`

for (const item of childImports) {
const { importName } = item

beforeCode += `exports.i(${importName});\n`
}

for (const item of replacers) {
const { pattern, replacementName, target } = item

beforeCode += `var ${replacementName} = ${target};\n`

content = content.replace(pattern, () => `" + ${replacementName} + "`)
}

beforeCode += `exports.url = ${url};\n`

return `${beforeCode}\nexports.e(module.id, ${content}, ${url}, ${sourceMapContent});\n`
}

function getExportCode(esModule: boolean) {
return esModule ? 'export default exports;\n' : 'module.exports = exports;\n'
}
import { get_code } from '@mpflow/wxml-parser'

export interface Options {
context?: string
Expand All @@ -89,7 +16,40 @@ export interface Options {
importAttributes?: ImportAttribute[]
}

const wxmlLoader: loader.Loader = function wxmlLoader(content, map) {
export interface ImportAttribute {
tag: string
attribute: string
importType?: 'child' | 'inline'
}

const defaultImportAttributes: ImportAttribute[] = [
{
tag: 'import',
attribute: 'src',
importType: 'child',
},
{
tag: 'include',
attribute: 'src',
importType: 'child',
},
{
tag: 'wxs',
attribute: 'src',
},
{
tag: 'image',
attribute: 'src',
},
]

const RUNTIME_API_MODULE = '\0<MPFLOW_WXML_LOADER_RUNTIME_API_MODULE>\0'

const wxmlLoader = function wxmlLoader(
this: loader.LoaderContext,
content: string | Buffer,
_map: RawSourceMap | null,
) {
this.async()
;(async (): Promise<[string | Buffer, RawSourceMap?]> => {
const options: Options = getOptions(this) || {}
Expand Down Expand Up @@ -139,70 +99,39 @@ const wxmlLoader: loader.Loader = function wxmlLoader(content, map) {
},
)

// const sourceMap = typeof options.sourceMap === 'boolean' ? options.sourceMap : this.sourceMap
const sourceMap = false // do not generate sourceMap since wcc compiler don't recognize it
const minimize = typeof options.minimize === 'boolean' ? options.minimize : this.minimize
const minimize = typeof options.minimize === 'boolean' ? options.minimize : this.mode === 'production'

const contentStr = typeof content === 'string' ? content : content.toString('utf8')
const ast = parser.parse(this.resourcePath, contentStr)

const { messages } = await pluginRunner([
importPlugin({
resolveMustache: options.resolveMustache,
attributes: options.importAttributes,
}),
]).process(ast, {
messages: [],
fs: this.fs,
context: this.context,
})

const imports: PluginImportMessage['value'][] = []
const childImports: PluginChildImportMessage['value'][] = []
const replacers: PluginReplaceMessage['value'][] = []
for (const message of messages) {
switch (message.type) {
case 'import':
imports.push(message.value)
break
case 'child-import':
childImports.push(message.value)
break
case 'replacer':
replacers.push(message.value)
break
}
}

const result = parser.codegen(ast, {
sourceMap,
minimize,
prevMap: sourceMap ? map : undefined,
})

if (sourceMap && result.map) {
result.map.setSourceContent(this.resourcePath, contentStr)
}

const context = options.context || this.rootContext

const url = interpolateName(this, options.name || '[name].[ext]', {
const url = interpolateName(this as loader.LoaderContext, options.name || '[name].[ext]', {
context,
content,
})

const outputPath = JSON.stringify(path.posix.join(options.outputPath || '', url))
const publicPath = `__webpack_public_path__ + ${outputPath}`

const esModule = typeof options.esModule !== 'undefined' ? options.esModule : false

const importCode = getImportCode(this, imports, esModule)
const moduleCode = getModuleCode(result, childImports, replacers, publicPath, esModule, sourceMap)
const exportCode = getExportCode(esModule)

return [`${importCode}${moduleCode}${exportCode}`]
const rustStringifyRequest = (request: string) => {
return stringifyRequest(this, request === RUNTIME_API_MODULE ? require.resolve('./runtime/api') : request)
}
const rustUrlToRequest = (url: string) => urlToRequest(url, '')
return [
get_code(
this.resourcePath,
contentStr,
{
publicPath: publicPath,
esModule: options.esModule ?? false,
minimize: options.minimize ?? true,
resolveMustache: options.resolveMustache ?? false,
attributes: options.importAttributes ?? defaultImportAttributes,
},
rustStringifyRequest,
rustUrlToRequest,
),
]
})().then(
([content, sourceMap]: [string | Buffer, RawSourceMap?]) => {
([content, sourceMap]) => {
this.callback(null, content, sourceMap)
},
err => {
Expand Down
Loading