• 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          targets: 'node 8',
152          plugins: [loadBabelModule('@babel/plugin-transform-modules-commonjs')],
153          comments: 'false'
154        }
155      })
156    }
157  }
158  return stringifyLoaders(loaders)
159}
160
161function configLoaderString (loaders) {
162  loaders = [{
163    name: defaultLoaders.json
164  }]
165  return stringifyLoaders(loaders)
166}
167
168function dataLoaderString (loaders) {
169  loaders = [{
170    name: defaultLoaders.json
171  }]
172  return stringifyLoaders(loaders)
173}
174
175function codegenHmlAndCss() {
176  const options = {
177    lang: {
178      sass:['sass-loader'],
179      scss:['sass-loader'],
180      less:['less-loader']
181    }
182  }
183  const customLang = options.lang || {}
184  const loaderQuery = loaderUtils.getOptions(this) || {}
185  const isElement = loaderQuery.element
186  const resourceQuery = this.resourceQuery && loaderUtils.parseQuery(this.resourceQuery) || {}
187  const isEntry = resourceQuery.entry
188  let output = ''
189  let jsFileName = this.resourcePath.replace(process.env.aceSuperVisualPath, process.env.projectPath)
190  jsFileName = jsFileName.substr(0, jsFileName.length - 6) + 'js';
191
192  if (process.env.DEVICE_LEVEL === 'card') {
193    output = '//card_start\n'
194    output += `var card_template =` + getRequireString(this,
195      jsonLoaders('template', undefined, true, 'template'), this.resourcePath)
196    output += `var card_style =` + getRequireString(this,
197      jsonLoaders('style', undefined, true, 'style'), this.resourcePath)
198    output += `var card_json =` + getRequireString(this,
199      jsonLoaders('json', undefined, true, 'json'), this.resourcePath)
200    output += '\n//card_end'
201  } else {
202    const that = this
203    output = 'var $app_script$ = ' + generateOutput(that, 'script', jsFileName, isElement)
204    output += 'var $app_template$ = ' + generateOutput(that, 'template', jsFileName, isElement)
205    output += 'var $app_style$ = ' + generateOutput(that, 'style', jsFileName, isElement)
206    output += generateOutput(that, 'others', jsFileName, isElement)
207    if (isEntry) {
208      output += `$app_bootstrap$('@app-component/${getNameByPath(this.resourcePath)}'`
209        + ',undefined' + ',undefined' + `)`
210    }
211  }
212  return output
213}
214
215function generateOutput(that, type, jsFileName, isElement, customLang) {
216  let result
217  switch (type) {
218    case 'script':
219      result = getRequireString(that, getLoaderString('script', {
220        customLang,
221        lang: undefined,
222        element: undefined,
223        elementName: undefined,
224        source: jsFileName
225      }), jsFileName)
226      break
227    case 'template':
228      result = getRequireString(that, getLoaderString('template', {
229        customLang,
230        lang: undefined,
231        element: isElement,
232        elementName: undefined,
233        source: that.resourcePath
234      }), that.resourcePath)
235      break
236    case 'style':
237      result = getRequireString(that, getLoaderString('style', {
238        customLang,
239        lang: undefined,
240        element: isElement,
241        elementName: undefined,
242        source: that.resourcePath
243      }), that.resourcePath)
244      break
245    case 'others':
246      result = `
247      $app_define$('@app-component/${getNameByPath(that.resourcePath)}', [],
248        function($app_require$, $app_exports$, $app_module$) {
249      ` + `
250      $app_script$($app_module$, $app_exports$, $app_require$)
251      if ($app_exports$.__esModule && $app_exports$.default) {
252      $app_module$.exports = $app_exports$.default
253      }
254      ` + `
255      $app_module$.exports.template = $app_template$
256      ` + `
257      $app_module$.exports.style = $app_style$
258      ` + `
259      })
260      `
261      break
262  }
263  return result
264}
265
266module.exports = codegenHmlAndCss