1 | const { isAbsolute, join } = require('path');
|
2 | const { stringifyRequest } = require('loader-utils');
|
3 | const {
|
4 | stringify,
|
5 | stringifySymbol,
|
6 | generateImport,
|
7 | generateExport,
|
8 | generateSpritePlaceholder
|
9 | } = require('./utils');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function runtimeGenerator(params) {
|
20 | const { symbol, config, context, loaderContext } = params;
|
21 | const { extract, esModule, spriteModule, symbolModule, runtimeCompat, publicPath } = config;
|
22 | let runtime;
|
23 |
|
24 | if (extract) {
|
25 | const spritePlaceholder = generateSpritePlaceholder(symbol.request.file);
|
26 | const path = stringify(publicPath) || '__webpack_public_path__';
|
27 | const data = `{
|
28 | id: ${stringify(symbol.useId)},
|
29 | viewBox: ${stringify(symbol.viewBox)},
|
30 | url: ${path} + ${stringify(spritePlaceholder)},
|
31 | toString: function () {
|
32 | return this.url;
|
33 | }
|
34 | }`;
|
35 | runtime = generateExport(data, esModule);
|
36 | } else {
|
37 | const spriteModuleAbsPath = isAbsolute(spriteModule) ? spriteModule : join(context, spriteModule);
|
38 | const symbolModuleAbsPath = isAbsolute(symbolModule) ? symbolModule : join(context, symbolModule);
|
39 |
|
40 | const spriteModuleImport = stringifyRequest(loaderContext, spriteModuleAbsPath);
|
41 | const symbolModuleImport = stringifyRequest(loaderContext, symbolModuleAbsPath);
|
42 |
|
43 | runtime = [
|
44 | generateImport('SpriteSymbol', symbolModuleImport, esModule),
|
45 | generateImport('sprite', spriteModuleImport, esModule),
|
46 |
|
47 | `var symbol = new SpriteSymbol(${stringifySymbol(symbol)})`,
|
48 | 'var result = sprite.add(symbol)',
|
49 |
|
50 | generateExport(runtimeCompat ? '"#" + symbol.id' : 'symbol', esModule)
|
51 | ].join(';\n');
|
52 | }
|
53 |
|
54 | return runtime;
|
55 | }
|
56 |
|
57 | module.exports = runtimeGenerator;
|