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