1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import loaderUtils from 'loader-utils' 17import path from 'path' 18 19import { 20 getNameByPath, 21 getRequireString, 22 stringifyLoaders, 23 jsonLoaders, 24 loadBabelModule 25} 26from './util' 27 28const loaderPath = __dirname 29const defaultLoaders = { 30 none: '', 31 main: path.resolve(loaderPath, 'loader.js'), 32 template: path.resolve(loaderPath, 'template.js'), 33 style: path.resolve(loaderPath, 'style.js'), 34 script: path.resolve(loaderPath, 'script.js'), 35 json: path.resolve(loaderPath, 'json.js'), 36 babel: loadBabelModule('babel-loader'), 37 manifest: path.resolve(loaderPath, 'manifest-loader.js'), 38 extgen: path.resolve(loaderPath, 'extgen.js') 39} 40 41function getLoaderString (type, config) { 42 config = config || {} 43 const customLoader = loadCustomLoader(config) 44 let loaders 45 switch (type) { 46 case 'main': 47 return mainLoaderString(loaders) 48 case 'element': 49 return elementLoaderString(loaders, config) 50 case 'template': 51 return templateLoaderString(loaders, config, customLoader) 52 case 'style': 53 return styleLoaderString(loaders, config, customLoader) 54 case 'script': 55 return scriptLoaderString(loaders, config, customLoader) 56 case 'config': 57 return configLoaderString(loaders, config) 58 case 'data': 59 return dataLoaderString(loaders, config) 60 } 61} 62 63function loadCustomLoader (config) { 64 if (config.lang && config.customLang[config.lang]) { 65 return loadBabelModule(config.customLang[config.lang][0]) 66 } 67} 68 69function mainLoaderString (loaders) { 70 loaders = [{ 71 name: defaultLoaders.main 72 }] 73 return stringifyLoaders(loaders) 74} 75 76function elementLoaderString (loaders, config) { 77 loaders = [{ 78 name: defaultLoaders.main, 79 query: { 80 element: config.source ? undefined : true 81 } 82 }] 83 return stringifyLoaders(loaders) 84} 85 86function templateLoaderString (loaders, config, customLoader) { 87 loaders = [{ 88 name: defaultLoaders.json 89 }, { 90 name: defaultLoaders.template 91 }] 92 if (customLoader) { 93 loaders = loaders.concat(customLoader) 94 } 95 loaders.push({ 96 name: defaultLoaders.extgen, 97 query: { 98 type: 'template' 99 } 100 }) 101 102 return stringifyLoaders(loaders) 103} 104 105function styleLoaderString (loaders, config, customLoader) { 106 loaders = [{ 107 name: defaultLoaders.json 108 }, { 109 name: defaultLoaders.style 110 }] 111 if (customLoader) { 112 loaders = loaders.concat(customLoader) 113 } 114 loaders.push({ 115 name: defaultLoaders.extgen, 116 query: { 117 type: 'style' 118 } 119 }) 120 return stringifyLoaders(loaders) 121} 122 123function scriptLoaderString (loaders, config, customLoader) { 124 if (process.env.DEVICE_LEVEL === 'card') { 125 loaders = [{ 126 name: defaultLoaders.json 127 }] 128 if (customLoader) { 129 loaders = loaders.concat(customLoader); 130 } 131 loaders.push({ 132 name: defaultLoaders.extgen, 133 query: { 134 type: 'json' 135 } 136 }) 137 } else { 138 loaders = [{ 139 name: defaultLoaders.script 140 }] 141 if (customLoader) { 142 loaders = loaders.concat(customLoader) 143 } else { 144 loaders.push({ 145 name: defaultLoaders.babel, 146 query: { 147 presets: [loadBabelModule('@babel/preset-env')], 148 targets: 'node 8', 149 plugins: [loadBabelModule('@babel/plugin-transform-modules-commonjs')], 150 comments: 'false' 151 } 152 }) 153 } 154 } 155 return stringifyLoaders(loaders) 156} 157 158function configLoaderString (loaders) { 159 loaders = [{ 160 name: defaultLoaders.json 161 }] 162 return stringifyLoaders(loaders) 163} 164 165function dataLoaderString (loaders) { 166 loaders = [{ 167 name: defaultLoaders.json 168 }] 169 return stringifyLoaders(loaders) 170} 171 172function codegenHmlAndCss() { 173 const options = { 174 lang: { 175 sass:['sass-loader'], 176 scss:['sass-loader'], 177 less:['less-loader'] 178 } 179 } 180 const customLang = options.lang || {} 181 const loaderQuery = loaderUtils.getOptions(this) || {} 182 const isElement = loaderQuery.element 183 const resourceQuery = this.resourceQuery && loaderUtils.parseQuery(this.resourceQuery) || {} 184 const isEntry = resourceQuery.entry 185 let output = '' 186 let jsFileName = this.resourcePath.replace(process.env.aceSuperVisualPath, process.env.projectPath) 187 jsFileName = jsFileName.substr(0, jsFileName.length - 6) + 'js'; 188 189 if (process.env.DEVICE_LEVEL === 'card') { 190 output = '//card_start\n' 191 output += `var card_template =` + getRequireString(this, 192 jsonLoaders('template', undefined, true, 'template'), this.resourcePath) 193 output += `var card_style =` + getRequireString(this, 194 jsonLoaders('style', undefined, true, 'style'), this.resourcePath) 195 output += `var card_json =` + getRequireString(this, 196 jsonLoaders('json', undefined, true, 'json'), this.resourcePath) 197 output += '\n//card_end' 198 } else { 199 const that = this 200 output = 'var $app_script$ = ' + generateOutput(that, 'script', jsFileName, isElement) 201 output += 'var $app_template$ = ' + generateOutput(that, 'template', jsFileName, isElement) 202 output += 'var $app_style$ = ' + generateOutput(that, 'style', jsFileName, isElement) 203 output += generateOutput(that, 'others', jsFileName, isElement) 204 if (isEntry) { 205 output += `$app_bootstrap$('@app-component/${getNameByPath(this.resourcePath)}'` 206 + ',undefined' + ',undefined' + `)` 207 } 208 } 209 return output 210} 211 212function generateOutput(that, type, jsFileName, isElement, customLang) { 213 let result 214 switch (type) { 215 case 'script': 216 result = getRequireString(that, getLoaderString('script', { 217 customLang, 218 lang: undefined, 219 element: undefined, 220 elementName: undefined, 221 source: jsFileName 222 }), jsFileName) 223 break 224 case 'template': 225 result = getRequireString(that, getLoaderString('template', { 226 customLang, 227 lang: undefined, 228 element: isElement, 229 elementName: undefined, 230 source: that.resourcePath 231 }), that.resourcePath) 232 break 233 case 'style': 234 result = getRequireString(that, getLoaderString('style', { 235 customLang, 236 lang: undefined, 237 element: isElement, 238 elementName: undefined, 239 source: that.resourcePath 240 }), that.resourcePath) 241 break 242 case 'others': 243 result = ` 244 $app_define$('@app-component/${getNameByPath(that.resourcePath)}', [], 245 function($app_require$, $app_exports$, $app_module$) { 246 ` + ` 247 $app_script$($app_module$, $app_exports$, $app_require$) 248 if ($app_exports$.__esModule && $app_exports$.default) { 249 $app_module$.exports = $app_exports$.default 250 } 251 ` + ` 252 $app_module$.exports.template = $app_template$ 253 ` + ` 254 $app_module$.exports.style = $app_style$ 255 ` + ` 256 }) 257 ` 258 break 259 } 260 return result 261} 262 263module.exports = codegenHmlAndCss