@mpxjs/webpack-plugin 2.6.114-alpha.4 → 2.6.114-alpha.7
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/{dependency → dependencies}/AddEntryDependency.js +0 -0
- package/lib/dependencies/AppEntryDependency.js +58 -0
- package/lib/dependencies/CommonJsAsyncDependency.js +51 -0
- package/lib/dependencies/CommonJsVariableDependency.js +81 -0
- package/lib/dependencies/DynamicEntryDependency.js +171 -0
- package/lib/dependencies/FlagPluginDependency.js +24 -0
- package/lib/dependencies/InjectDependency.js +43 -0
- package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
- package/lib/dependencies/RecordIndependentDependency.js +44 -0
- package/lib/dependencies/RecordResourceMapDependency.js +62 -0
- package/lib/dependencies/RemoveEntryDependency.js +40 -0
- package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
- package/lib/dependencies/ResolveDependency.js +88 -0
- package/lib/extractor.js +82 -178
- package/lib/file-loader.js +7 -19
- package/lib/helpers.js +39 -334
- package/lib/independent-loader.js +52 -0
- package/lib/index.js +864 -541
- package/lib/json-compiler/helper.js +156 -0
- package/lib/json-compiler/index.js +242 -451
- package/lib/json-compiler/plugin.js +150 -0
- package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
- package/lib/loader.js +137 -240
- package/lib/native-loader.js +71 -133
- package/lib/parser.js +1 -2
- package/lib/partial-compile/index.js +35 -0
- package/lib/platform/json/wx/index.js +1 -1
- package/lib/platform/template/normalize-component-rules.js +2 -3
- package/lib/platform/template/wx/index.js +30 -4
- package/lib/record-loader.js +11 -0
- package/lib/{path-loader.js → resolve-loader.js} +0 -0
- package/lib/resolver/AddEnvPlugin.js +4 -3
- package/lib/resolver/AddModePlugin.js +4 -3
- package/lib/resolver/FixDescriptionInfoPlugin.js +28 -0
- package/lib/resolver/PackageEntryPlugin.js +23 -36
- package/lib/runtime/base.styl +5 -0
- package/lib/runtime/components/tenon/tenon-button.vue +1 -1
- package/lib/runtime/components/tenon/tenon-image.vue +1 -1
- package/lib/runtime/components/tenon/tenon-input.vue +1 -1
- package/lib/runtime/components/tenon/tenon-switch.vue +1 -1
- package/lib/runtime/components/tenon/tenon-text-area.vue +1 -1
- package/lib/runtime/components/tenon/tenon-text.vue +1 -1
- package/lib/runtime/components/web/mpx-image.vue +20 -5
- package/lib/runtime/components/web/mpx-movable-view.vue +6 -2
- package/lib/runtime/components/web/mpx-swiper.vue +18 -3
- package/lib/runtime/i18n.wxs +31 -11
- package/lib/runtime/optionProcessor.js +48 -3
- package/lib/selector.js +29 -10
- package/lib/style-compiler/index.js +15 -23
- package/lib/style-compiler/load-postcss-config.js +3 -1
- package/lib/style-compiler/plugins/conditional-strip.js +68 -65
- package/lib/style-compiler/plugins/rpx.js +43 -37
- package/lib/style-compiler/plugins/scope-id.js +79 -72
- package/lib/style-compiler/plugins/trans-special.js +25 -18
- package/lib/style-compiler/plugins/trim.js +13 -7
- package/lib/style-compiler/plugins/vw.js +22 -16
- package/lib/template-compiler/compiler.js +95 -197
- package/lib/template-compiler/index.js +52 -139
- package/lib/template-compiler/trans-dynamic-class-expr.js +18 -13
- package/lib/tenon/index.js +1 -3
- package/lib/tenon/processScript.js +52 -40
- package/lib/url-loader.js +11 -29
- package/lib/utils/add-query.js +1 -1
- package/lib/utils/const.js +10 -0
- package/lib/utils/emit-file.js +10 -0
- package/lib/utils/eval-json-js.js +31 -0
- package/lib/utils/get-entry-name.js +13 -0
- package/lib/utils/get-json-content.js +42 -0
- package/lib/utils/is-url-request.js +10 -1
- package/lib/utils/match-condition.js +4 -1
- package/lib/utils/normalize.js +4 -15
- package/lib/utils/parse-request.js +3 -3
- package/lib/utils/resolve.js +13 -0
- package/lib/utils/set.js +47 -0
- package/lib/utils/stringify-loaders-resource.js +25 -0
- package/lib/utils/stringify-query.js +4 -0
- package/lib/web/processJSON.js +113 -144
- package/lib/web/processScript.js +47 -34
- package/lib/web/processTemplate.js +57 -40
- package/lib/wxml/{wxml-loader.js → loader.js} +21 -62
- package/lib/wxs/WxsModuleIdsPlugin.js +29 -0
- package/lib/wxs/WxsParserPlugin.js +2 -2
- package/lib/wxs/WxsPlugin.js +4 -8
- package/lib/wxs/WxsTemplatePlugin.js +46 -92
- package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +5 -4
- package/lib/wxs/loader.js +142 -0
- package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +20 -5
- package/lib/wxss/loader.js +31 -43
- package/lib/wxss/localsLoader.js +1 -5
- package/lib/wxss/processCss.js +107 -103
- package/package.json +18 -20
- package/lib/built-in-loader.js +0 -49
- package/lib/content-loader.js +0 -13
- package/lib/dependency/ChildCompileDependency.js +0 -24
- package/lib/dependency/InjectDependency.js +0 -26
- package/lib/dependency/RemovedModuleDependency.js +0 -23
- package/lib/dependency/ResolveDependency.js +0 -53
- package/lib/plugin-loader.js +0 -287
- package/lib/staticConfig.js +0 -4
- package/lib/utils/get-main-compilation.js +0 -6
- package/lib/utils/read-json-for-src.js +0 -34
- package/lib/utils/try-require.js +0 -16
- package/lib/wxs/wxs-loader.js +0 -117
- package/lib/wxss/getImportPrefix.js +0 -30
|
@@ -1,104 +1,58 @@
|
|
|
1
|
-
const Template = require('webpack/lib/Template')
|
|
2
1
|
const config = require('../config')
|
|
3
|
-
const { ConcatSource } = require('webpack
|
|
2
|
+
const { ConcatSource } = require('webpack').sources
|
|
3
|
+
const JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin')
|
|
4
4
|
|
|
5
|
-
module.exports = class
|
|
5
|
+
module.exports = class WxsTemplatePlugin {
|
|
6
6
|
constructor (options = { mode: 'wx' }) {
|
|
7
7
|
this.options = options
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
apply (
|
|
11
|
-
|
|
12
|
-
return Template.asString([
|
|
13
|
-
'// Check if module is in cache',
|
|
14
|
-
'if(installedModules[moduleId]) {',
|
|
15
|
-
Template.indent('return installedModules[moduleId].exports;'),
|
|
16
|
-
'}',
|
|
17
|
-
'// Create a new module (and put it into the cache)',
|
|
18
|
-
'var module = installedModules[moduleId] = {',
|
|
19
|
-
Template.indent(mainTemplate.hooks.moduleObj.call('', chunk, hash, 'moduleId')),
|
|
20
|
-
'};',
|
|
21
|
-
'',
|
|
22
|
-
Template.asString(
|
|
23
|
-
[
|
|
24
|
-
'// Execute the module function',
|
|
25
|
-
'// wxs连call都不支持我也是服气...',
|
|
26
|
-
`modules[moduleId](module, module.exports, ${mainTemplate.renderRequireFunctionForModule(
|
|
27
|
-
hash,
|
|
28
|
-
chunk,
|
|
29
|
-
'moduleId'
|
|
30
|
-
)});`
|
|
31
|
-
]
|
|
32
|
-
),
|
|
33
|
-
'',
|
|
34
|
-
'// Flag the module as loaded',
|
|
35
|
-
'module.l = true;',
|
|
36
|
-
'',
|
|
37
|
-
'// Return the exports of the module',
|
|
38
|
-
'return module.exports;'
|
|
39
|
-
])
|
|
40
|
-
})
|
|
41
|
-
mainTemplate.hooks.requireExtensions.tap(
|
|
42
|
-
'WxsMainTemplatePlugin',
|
|
43
|
-
() => {
|
|
44
|
-
return Template.asString([
|
|
45
|
-
'// define harmony function exports',
|
|
46
|
-
`${mainTemplate.requireFn}.d = function(exports, name, getter) {`,
|
|
47
|
-
Template.indent([
|
|
48
|
-
'exports[name] = getter();'
|
|
49
|
-
]),
|
|
50
|
-
'};',
|
|
51
|
-
'',
|
|
52
|
-
'// define __esModule on exports',
|
|
53
|
-
`${mainTemplate.requireFn}.r = function(exports) {`,
|
|
54
|
-
Template.indent([
|
|
55
|
-
'exports.__esModule = true;'
|
|
56
|
-
]),
|
|
57
|
-
'};',
|
|
58
|
-
'',
|
|
59
|
-
'// getDefaultExport function for compatibility with non-harmony modules',
|
|
60
|
-
mainTemplate.requireFn + '.n = function(module) {',
|
|
61
|
-
Template.indent([
|
|
62
|
-
'var getter = module && module.__esModule ?',
|
|
63
|
-
Template.indent([
|
|
64
|
-
'function getDefault() { return module["default"]; } :',
|
|
65
|
-
'function getModuleExports() { return module; };'
|
|
66
|
-
]),
|
|
67
|
-
`getter.a = getter();`,
|
|
68
|
-
'return getter;'
|
|
69
|
-
]),
|
|
70
|
-
'};'
|
|
71
|
-
])
|
|
72
|
-
}
|
|
73
|
-
)
|
|
74
|
-
mainTemplate.hooks.renderWithEntry.tap(
|
|
75
|
-
'WxsMainTemplatePlugin',
|
|
76
|
-
(source, chunk, hash) => {
|
|
77
|
-
const prefix = config[this.options.mode].wxs.templatePrefix
|
|
78
|
-
return new ConcatSource(prefix, source)
|
|
79
|
-
}
|
|
80
|
-
)
|
|
10
|
+
apply (compilation) {
|
|
11
|
+
const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation)
|
|
81
12
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (chunk.entryModule) {
|
|
86
|
-
buf.push('// Load entry module and return exports')
|
|
87
|
-
buf.push(
|
|
88
|
-
`var entryExports = ${mainTemplate.renderRequireFunctionForModule(
|
|
89
|
-
hash,
|
|
90
|
-
chunk,
|
|
91
|
-
JSON.stringify(chunk.entryModule.id)
|
|
92
|
-
)}(${mainTemplate.requireFn}.s = ${JSON.stringify(chunk.entryModule.id)});`
|
|
93
|
-
)
|
|
94
|
-
buf.push('return entryExports && entryExports.__esModule? entryExports["default"] : entryExports;')
|
|
95
|
-
}
|
|
96
|
-
return Template.asString(buf)
|
|
13
|
+
hooks.renderStartup.tap('WxsTemplatePlugin', (source) => {
|
|
14
|
+
const postfix = 'return __webpack_exports__ && __webpack_exports__.__esModule? __webpack_exports__["default"] : __webpack_exports__;\n'
|
|
15
|
+
return new ConcatSource(source, postfix)
|
|
97
16
|
})
|
|
98
17
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
18
|
+
hooks.render.tap('WxsTemplatePlugin', (source) => {
|
|
19
|
+
const prefix = config[this.options.mode].wxs.templatePrefix
|
|
20
|
+
return new ConcatSource(prefix, source)
|
|
102
21
|
})
|
|
22
|
+
|
|
23
|
+
// todo webpack5的新的代码生成模式下完美支持.d.r.n的成本较高,暂不处理,wxs暂时只支持wx源码形式
|
|
24
|
+
// mainTemplate.hooks.requireExtensions.tap(
|
|
25
|
+
// 'WxsMainTemplatePlugin',
|
|
26
|
+
// () => {
|
|
27
|
+
// return Template.asString([
|
|
28
|
+
// '// define harmony function exports',
|
|
29
|
+
// `${mainTemplate.requireFn}.d = function(exports, name, getter) {`,
|
|
30
|
+
// Template.indent([
|
|
31
|
+
// 'exports[name] = getter();'
|
|
32
|
+
// ]),
|
|
33
|
+
// '};',
|
|
34
|
+
// '',
|
|
35
|
+
// '// define __esModule on exports',
|
|
36
|
+
// `${mainTemplate.requireFn}.r = function(exports) {`,
|
|
37
|
+
// Template.indent([
|
|
38
|
+
// 'exports.__esModule = true;'
|
|
39
|
+
// ]),
|
|
40
|
+
// '};',
|
|
41
|
+
// '',
|
|
42
|
+
// '// getDefaultExport function for compatibility with non-harmony modules',
|
|
43
|
+
// mainTemplate.requireFn + '.n = function(module) {',
|
|
44
|
+
// Template.indent([
|
|
45
|
+
// 'var getter = module && module.__esModule ?',
|
|
46
|
+
// Template.indent([
|
|
47
|
+
// 'function getDefault() { return module["default"]; } :',
|
|
48
|
+
// 'function getModuleExports() { return module; };'
|
|
49
|
+
// ]),
|
|
50
|
+
// `getter.a = getter();`,
|
|
51
|
+
// 'return getter;'
|
|
52
|
+
// ]),
|
|
53
|
+
// '};'
|
|
54
|
+
// ])
|
|
55
|
+
// }
|
|
56
|
+
// )
|
|
103
57
|
}
|
|
104
58
|
}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
// 该loader用于将用户定义的messages注入到i18n.wxs中
|
|
2
|
-
const getMainCompilation = require('../utils/get-main-compilation')
|
|
3
2
|
const loaderUtils = require('loader-utils')
|
|
4
3
|
|
|
5
4
|
module.exports = function (content) {
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
let prefix = 'var __mpx_messages__, __mpx_datetime_formats__, __mpx_number_formats__\n'
|
|
5
|
+
const i18n = this.getMpx().i18n
|
|
6
|
+
let prefix = 'var __mpx_messages__, __mpx_datetime_formats__, __mpx_number_formats__, __mpx_locale__\n'
|
|
9
7
|
if (i18n) {
|
|
10
8
|
if (i18n.messages) {
|
|
11
9
|
prefix += `__mpx_messages__ = ${JSON.stringify(i18n.messages)}\n`
|
|
@@ -22,6 +20,9 @@ module.exports = function (content) {
|
|
|
22
20
|
} else if (i18n.numberFormatsPath) {
|
|
23
21
|
prefix += `__mpx_number_formats__ = require(${loaderUtils.stringifyRequest(this, i18n.numberFormatsPath)})\n`
|
|
24
22
|
}
|
|
23
|
+
if (i18n.locale) {
|
|
24
|
+
prefix += `__mpx_locale__ = ${JSON.stringify(i18n.locale)}\n`
|
|
25
|
+
}
|
|
25
26
|
}
|
|
26
27
|
content = prefix + content
|
|
27
28
|
return content
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin')
|
|
2
|
+
const EntryPlugin = require('webpack/lib/EntryPlugin')
|
|
3
|
+
const LazySet = require('webpack/lib/util/LazySet')
|
|
4
|
+
const LimitChunkCountPlugin = require('webpack/lib/optimize/LimitChunkCountPlugin')
|
|
5
|
+
const FlagEntryExportAsUsedPlugin = require('webpack/lib/FlagEntryExportAsUsedPlugin')
|
|
6
|
+
const path = require('path')
|
|
7
|
+
const WxsPlugin = require('./WxsPlugin')
|
|
8
|
+
const RecordResourceMapDependency = require('../dependencies/RecordResourceMapDependency')
|
|
9
|
+
const parseRequest = require('../utils/parse-request')
|
|
10
|
+
const toPosix = require('../utils/to-posix')
|
|
11
|
+
const fixRelative = require('../utils/fix-relative')
|
|
12
|
+
const addQuery = require('../utils/add-query')
|
|
13
|
+
const config = require('../config')
|
|
14
|
+
|
|
15
|
+
module.exports = content => content
|
|
16
|
+
|
|
17
|
+
module.exports.pitch = function (remainingRequest) {
|
|
18
|
+
const nativeCallback = this.async()
|
|
19
|
+
const moduleGraph = this._compilation.moduleGraph
|
|
20
|
+
const mpx = this.getMpx()
|
|
21
|
+
const mode = mpx.mode
|
|
22
|
+
const getOutputPath = mpx.getOutputPath
|
|
23
|
+
let { resourcePath, queryObj } = parseRequest(this.resource)
|
|
24
|
+
const issuer = moduleGraph.getIssuer(this._module)
|
|
25
|
+
const { resourcePath: issuerResourcePath, queryObj: issuerQueryObj } = parseRequest(queryObj.issuerResource || issuer.resource)
|
|
26
|
+
const issuerPackageName = issuerQueryObj.packageRoot || 'main'
|
|
27
|
+
const pagesMap = mpx.pagesMap
|
|
28
|
+
const componentsMap = mpx.componentsMap[issuerPackageName]
|
|
29
|
+
const staticResourcesMap = mpx.staticResourcesMap[issuerPackageName]
|
|
30
|
+
const issuerName = pagesMap[issuerResourcePath] || componentsMap[issuerResourcePath] || staticResourcesMap[issuerResourcePath]
|
|
31
|
+
const issuerDir = path.dirname(issuerName)
|
|
32
|
+
|
|
33
|
+
const getName = (raw) => {
|
|
34
|
+
const match = /^(.*?)(\.[^.]*)?$/.exec(raw)
|
|
35
|
+
return match[1]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const wxsModule = queryObj.wxsModule
|
|
39
|
+
if (wxsModule) {
|
|
40
|
+
resourcePath = `${resourcePath}~${wxsModule}`
|
|
41
|
+
}
|
|
42
|
+
const packageRoot = queryObj.packageRoot || ''
|
|
43
|
+
const ext = config[mode].wxs.ext
|
|
44
|
+
const filename = toPosix(path.join(packageRoot, getOutputPath(resourcePath, ext.slice(1), { ext })))
|
|
45
|
+
this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'staticResource', filename, packageRoot))
|
|
46
|
+
|
|
47
|
+
const callback = (err) => {
|
|
48
|
+
if (err) return nativeCallback(err)
|
|
49
|
+
let relativePath = toPosix(path.relative(issuerDir, filename))
|
|
50
|
+
relativePath = fixRelative(relativePath, mode)
|
|
51
|
+
nativeCallback(null, `module.exports = ${JSON.stringify(relativePath)};`)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 清空issuerResource query避免文件内容输出报错并进行子编译缓存优化
|
|
55
|
+
const request = '!!' + addQuery(remainingRequest, {}, false, ['issuerResource'])
|
|
56
|
+
|
|
57
|
+
// request中已经包含全量构成filename的信息,故可以直接使用request作为key来进行缓存
|
|
58
|
+
if (!mpx.wxsAssetsCache.has(request)) {
|
|
59
|
+
mpx.wxsAssetsCache.set(request, new Promise((resolve, reject) => {
|
|
60
|
+
const outputOptions = {
|
|
61
|
+
filename,
|
|
62
|
+
// 避免输出的wxs中包含es语法
|
|
63
|
+
environment: {
|
|
64
|
+
// The environment supports arrow functions ('() => { ... }').
|
|
65
|
+
arrowFunction: false,
|
|
66
|
+
// The environment supports BigInt as literal (123n).
|
|
67
|
+
bigIntLiteral: false,
|
|
68
|
+
// The environment supports const and let for variable declarations.
|
|
69
|
+
const: false,
|
|
70
|
+
// The environment supports destructuring ('{ a, b } = obj').
|
|
71
|
+
destructuring: false,
|
|
72
|
+
// The environment supports an async import() function to import EcmaScript modules.
|
|
73
|
+
dynamicImport: false,
|
|
74
|
+
// The environment supports 'for of' iteration ('for (const x of array) { ... }').
|
|
75
|
+
forOf: false,
|
|
76
|
+
// The environment supports ECMAScript Module syntax to import ECMAScript modules (import ... from '...').
|
|
77
|
+
module: false
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const plugins = [
|
|
82
|
+
new WxsPlugin({ mode }),
|
|
83
|
+
new NodeTargetPlugin(),
|
|
84
|
+
new EntryPlugin(this.context, request, { name: getName(filename) }),
|
|
85
|
+
new LimitChunkCountPlugin({ maxChunks: 1 }),
|
|
86
|
+
new FlagEntryExportAsUsedPlugin(true, 'entry')
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
const childCompiler = this._compilation.createChildCompiler(resourcePath, outputOptions, plugins)
|
|
90
|
+
|
|
91
|
+
let assets = []
|
|
92
|
+
|
|
93
|
+
childCompiler.hooks.afterCompile.tap('MpxWebpackPlugin', (compilation) => {
|
|
94
|
+
// 持久化缓存,使用module.buildInfo.assets来输出文件
|
|
95
|
+
assets = compilation.getAssets()
|
|
96
|
+
compilation.clearAssets()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
childCompiler.runAsChild((err, entries, compilation) => {
|
|
100
|
+
if (err) return reject(err)
|
|
101
|
+
const fileDependencies = new LazySet()
|
|
102
|
+
const contextDependencies = new LazySet()
|
|
103
|
+
const missingDependencies = new LazySet()
|
|
104
|
+
const buildDependencies = new LazySet()
|
|
105
|
+
compilation.modules.forEach((module) => {
|
|
106
|
+
module.addCacheDependencies(
|
|
107
|
+
fileDependencies,
|
|
108
|
+
contextDependencies,
|
|
109
|
+
missingDependencies,
|
|
110
|
+
buildDependencies
|
|
111
|
+
)
|
|
112
|
+
})
|
|
113
|
+
resolve({
|
|
114
|
+
assets,
|
|
115
|
+
fileDependencies,
|
|
116
|
+
contextDependencies,
|
|
117
|
+
missingDependencies,
|
|
118
|
+
buildDependencies
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
}))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
mpx.wxsAssetsCache.get(request).then(({ assets, fileDependencies, contextDependencies, missingDependencies, buildDependencies }) => {
|
|
125
|
+
assets.forEach(({ name, source, info }) => {
|
|
126
|
+
this.emitFile(name, source.source(), undefined, info)
|
|
127
|
+
})
|
|
128
|
+
fileDependencies.forEach((dep) => {
|
|
129
|
+
this.addDependency(dep)
|
|
130
|
+
})
|
|
131
|
+
contextDependencies.forEach((dep) => {
|
|
132
|
+
this.addContextDependency(dep)
|
|
133
|
+
})
|
|
134
|
+
missingDependencies.forEach((dep) => {
|
|
135
|
+
this.addMissingDependency(dep)
|
|
136
|
+
})
|
|
137
|
+
buildDependencies.forEach((dep) => {
|
|
138
|
+
this.addBuildDependency(dep)
|
|
139
|
+
})
|
|
140
|
+
callback()
|
|
141
|
+
}).catch(callback)
|
|
142
|
+
}
|
|
@@ -2,21 +2,20 @@ const babylon = require('@babel/parser')
|
|
|
2
2
|
const traverse = require('@babel/traverse').default
|
|
3
3
|
const t = require('@babel/types')
|
|
4
4
|
const generate = require('@babel/generator').default
|
|
5
|
-
const getMainCompilation = require('../utils/get-main-compilation')
|
|
6
5
|
const parseRequest = require('../utils/parse-request')
|
|
7
6
|
const isEmptyObject = require('../utils/is-empty-object')
|
|
8
7
|
const parseQuery = require('loader-utils').parseQuery
|
|
9
8
|
|
|
10
9
|
module.exports = function (content) {
|
|
11
10
|
this.cacheable()
|
|
12
|
-
const
|
|
11
|
+
const mpx = this.getMpx()
|
|
13
12
|
const module = this._module
|
|
14
|
-
const mode =
|
|
13
|
+
const mode = mpx.mode
|
|
15
14
|
const wxsModule = parseQuery(this.resourceQuery || '?').wxsModule
|
|
16
15
|
|
|
17
16
|
// 处理内联wxs
|
|
18
17
|
if (wxsModule) {
|
|
19
|
-
const wxsContentMap =
|
|
18
|
+
const wxsContentMap = mpx.wxsContentMap
|
|
20
19
|
const resourcePath = parseRequest(this.resource).resourcePath
|
|
21
20
|
content = wxsContentMap[`${resourcePath}~${wxsModule}`] || content
|
|
22
21
|
}
|
|
@@ -45,6 +44,15 @@ module.exports = function (content) {
|
|
|
45
44
|
}
|
|
46
45
|
}
|
|
47
46
|
},
|
|
47
|
+
CallExpression (path) {
|
|
48
|
+
const callee = path.node.callee
|
|
49
|
+
if (t.isIdentifier(callee) && callee.name === 'getRegExp') {
|
|
50
|
+
const argPath = path.get('arguments')[0]
|
|
51
|
+
if (argPath.isStringLiteral()) {
|
|
52
|
+
argPath.replaceWith(t.stringLiteral(argPath.node.extra.raw.slice(1, -1)))
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
48
56
|
ForStatement (path) {
|
|
49
57
|
if (path.shouldStopTraverse) {
|
|
50
58
|
path.stop()
|
|
@@ -93,12 +101,19 @@ module.exports = function (content) {
|
|
|
93
101
|
},
|
|
94
102
|
CallExpression (path) {
|
|
95
103
|
const callee = path.node.callee
|
|
104
|
+
const args = path.node.arguments
|
|
96
105
|
const transMap = {
|
|
97
106
|
getDate: 'Date',
|
|
98
107
|
getRegExp: 'RegExp'
|
|
99
108
|
}
|
|
100
109
|
if (t.isIdentifier(callee) && transMap[callee.name]) {
|
|
101
|
-
|
|
110
|
+
if (callee.name === 'getRegExp') {
|
|
111
|
+
const arg = args[0]
|
|
112
|
+
if (t.isStringLiteral(arg)) {
|
|
113
|
+
args[0] = t.stringLiteral(arg.extra.raw.slice(1, -1))
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
path.replaceWith(t.newExpression(t.identifier(transMap[callee.name]), args))
|
|
102
117
|
}
|
|
103
118
|
}
|
|
104
119
|
})
|
package/lib/wxss/loader.js
CHANGED
|
@@ -5,25 +5,22 @@
|
|
|
5
5
|
*/
|
|
6
6
|
const loaderUtils = require('loader-utils')
|
|
7
7
|
const processCss = require('./processCss')
|
|
8
|
-
const getImportPrefix = require('./getImportPrefix')
|
|
9
8
|
const compileExports = require('./compile-exports')
|
|
10
9
|
const createResolver = require('./createResolver')
|
|
11
10
|
const isUrlRequest = require('../utils/is-url-request')
|
|
12
|
-
const
|
|
13
|
-
const addQuery = require('../utils/add-query')
|
|
11
|
+
const createHelpers = require('../helpers')
|
|
14
12
|
|
|
15
13
|
module.exports = function (content, map) {
|
|
16
14
|
if (this.cacheable) this.cacheable()
|
|
17
|
-
|
|
18
15
|
const callback = this.async()
|
|
19
16
|
const query = loaderUtils.getOptions(this) || {}
|
|
20
|
-
const root = query.root
|
|
21
17
|
const moduleMode = query.modules || query.module
|
|
22
18
|
const camelCaseKeys = query.camelCase || query.camelcase
|
|
23
|
-
const sourceMap = this.sourceMap || false
|
|
24
19
|
const resolve = createResolver(query.alias)
|
|
25
|
-
const mpx =
|
|
20
|
+
const mpx = this.getMpx()
|
|
26
21
|
const externals = mpx.externals
|
|
22
|
+
const root = mpx.projectRoot
|
|
23
|
+
const sourceMap = mpx.cssSourceMap || false
|
|
27
24
|
|
|
28
25
|
if (sourceMap) {
|
|
29
26
|
if (map) {
|
|
@@ -43,25 +40,24 @@ module.exports = function (content, map) {
|
|
|
43
40
|
map = null
|
|
44
41
|
}
|
|
45
42
|
|
|
43
|
+
const { getRequestString } = createHelpers(this)
|
|
44
|
+
|
|
46
45
|
processCss(content, map, {
|
|
47
46
|
mode: moduleMode ? 'local' : 'global',
|
|
48
47
|
from: loaderUtils.getRemainingRequest(this).split('!').pop(),
|
|
49
48
|
to: loaderUtils.getCurrentRequest(this).split('!').pop(),
|
|
50
|
-
query
|
|
51
|
-
resolve
|
|
49
|
+
query,
|
|
50
|
+
resolve,
|
|
52
51
|
minimize: this.minimize,
|
|
53
52
|
loaderContext: this,
|
|
54
|
-
sourceMap
|
|
55
|
-
},
|
|
53
|
+
sourceMap
|
|
54
|
+
}, (err, result) => {
|
|
56
55
|
if (err) return callback(err)
|
|
57
56
|
|
|
58
57
|
let cssAsString = JSON.stringify(result.source)
|
|
59
58
|
|
|
60
|
-
// for importing CSS
|
|
61
|
-
const importUrlPrefix = getImportPrefix(this)
|
|
62
|
-
|
|
63
59
|
const alreadyImported = {}
|
|
64
|
-
const importJs = result.importItems.filter(
|
|
60
|
+
const importJs = result.importItems.filter((imp) => {
|
|
65
61
|
if (!imp.mediaQuery) {
|
|
66
62
|
if (alreadyImported[imp.url]) {
|
|
67
63
|
return false
|
|
@@ -69,43 +65,35 @@ module.exports = function (content, map) {
|
|
|
69
65
|
alreadyImported[imp.url] = true
|
|
70
66
|
}
|
|
71
67
|
return true
|
|
72
|
-
}).map(
|
|
73
|
-
if (!isUrlRequest(imp.url, root
|
|
74
|
-
if (typeof external === 'string') {
|
|
75
|
-
return external === imp.url
|
|
76
|
-
} else if (external instanceof RegExp) {
|
|
77
|
-
return external.test(imp.url)
|
|
78
|
-
}
|
|
79
|
-
return false
|
|
80
|
-
})) {
|
|
68
|
+
}).map((imp, i) => {
|
|
69
|
+
if (!isUrlRequest(imp.url, root, externals)) {
|
|
81
70
|
return 'exports.push([module.id, ' +
|
|
82
71
|
JSON.stringify('@import url(' + imp.url + ');') + ', ' +
|
|
83
72
|
JSON.stringify(imp.mediaQuery) + ']);'
|
|
84
73
|
} else {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return 'exports.i(require(' + loaderUtils.stringifyRequest(this, importUrl) + '), ' + JSON.stringify(imp.mediaQuery) + ');'
|
|
74
|
+
const requestString = getRequestString('styles', { src: imp.url }, {
|
|
75
|
+
isStatic: true,
|
|
76
|
+
issuerResource: this.resource,
|
|
77
|
+
fromImport: true
|
|
78
|
+
}, i)
|
|
79
|
+
return 'exports.push([module.id, ' +
|
|
80
|
+
JSON.stringify('@import "') +
|
|
81
|
+
'+ require(' + requestString + ') +' +
|
|
82
|
+
JSON.stringify('";') + ', ' +
|
|
83
|
+
JSON.stringify(imp.mediaQuery) + ']);'
|
|
96
84
|
}
|
|
97
|
-
}
|
|
85
|
+
}).join('\n')
|
|
98
86
|
|
|
99
|
-
|
|
87
|
+
const importItemMatcher = (item) => {
|
|
100
88
|
const match = result.importItemRegExp.exec(item)
|
|
101
89
|
const idx = +match[1]
|
|
102
90
|
const importItem = result.importItems[idx]
|
|
103
|
-
const importUrl =
|
|
91
|
+
const importUrl = importItem.url
|
|
104
92
|
return '" + require(' + loaderUtils.stringifyRequest(this, importUrl) + ').locals' +
|
|
105
93
|
'[' + JSON.stringify(importItem.export) + '] + "'
|
|
106
94
|
}
|
|
107
95
|
|
|
108
|
-
cssAsString = cssAsString.replace(result.importItemRegExpG, importItemMatcher
|
|
96
|
+
cssAsString = cssAsString.replace(result.importItemRegExpG, importItemMatcher)
|
|
109
97
|
|
|
110
98
|
// helper for ensuring valid CSS strings from requires
|
|
111
99
|
let urlEscapeHelper = ''
|
|
@@ -113,7 +101,7 @@ module.exports = function (content, map) {
|
|
|
113
101
|
if (query.url !== false && result.urlItems.length > 0) {
|
|
114
102
|
urlEscapeHelper = 'var escape = require(' + loaderUtils.stringifyRequest(this, '!!' + require.resolve('./url/escape.js')) + ');\n'
|
|
115
103
|
|
|
116
|
-
cssAsString = cssAsString.replace(result.urlItemRegExpG,
|
|
104
|
+
cssAsString = cssAsString.replace(result.urlItemRegExpG, (item) => {
|
|
117
105
|
const match = result.urlItemRegExp.exec(item)
|
|
118
106
|
let idx = +match[1]
|
|
119
107
|
const urlItem = result.urlItems[idx]
|
|
@@ -129,10 +117,10 @@ module.exports = function (content, map) {
|
|
|
129
117
|
}
|
|
130
118
|
urlRequest = url
|
|
131
119
|
return '" + escape(require(' + loaderUtils.stringifyRequest(this, urlRequest) + ')) + "'
|
|
132
|
-
}
|
|
120
|
+
})
|
|
133
121
|
}
|
|
134
122
|
|
|
135
|
-
let exportJs = compileExports(result, importItemMatcher
|
|
123
|
+
let exportJs = compileExports(result, importItemMatcher, camelCaseKeys)
|
|
136
124
|
if (exportJs) {
|
|
137
125
|
exportJs = 'exports.locals = ' + exportJs + ';'
|
|
138
126
|
}
|
|
@@ -165,5 +153,5 @@ module.exports = function (content, map) {
|
|
|
165
153
|
moduleJs + '\n\n' +
|
|
166
154
|
'// exports\n' +
|
|
167
155
|
exportJs)
|
|
168
|
-
}
|
|
156
|
+
})
|
|
169
157
|
}
|
package/lib/wxss/localsLoader.js
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
*/
|
|
6
6
|
var loaderUtils = require('loader-utils')
|
|
7
7
|
var processCss = require('./processCss')
|
|
8
|
-
var getImportPrefix = require('./getImportPrefix')
|
|
9
8
|
var compileExports = require('./compile-exports')
|
|
10
9
|
var createResolver = require('./createResolver')
|
|
11
10
|
|
|
@@ -26,14 +25,11 @@ module.exports = function (content) {
|
|
|
26
25
|
}, function (err, result) {
|
|
27
26
|
if (err) return callback(err)
|
|
28
27
|
|
|
29
|
-
// for importing CSS
|
|
30
|
-
var importUrlPrefix = getImportPrefix(this, query)
|
|
31
|
-
|
|
32
28
|
function importItemMatcher (item) {
|
|
33
29
|
var match = result.importItemRegExp.exec(item)
|
|
34
30
|
var idx = +match[1]
|
|
35
31
|
var importItem = result.importItems[idx]
|
|
36
|
-
var importUrl =
|
|
32
|
+
var importUrl = importItem.url
|
|
37
33
|
return '" + require(' + loaderUtils.stringifyRequest(this, importUrl) + ')' +
|
|
38
34
|
'[' + JSON.stringify(importItem.export) + '] + "'
|
|
39
35
|
}
|