1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const { readFileSync } = require('fs');
|
5 | const readPkgUp = require('read-pkg-up');
|
6 | const { render } = require('mustache');
|
7 |
|
8 | const NAME = '@extensionengine/tailor-ce';
|
9 | const PREFIX = '\0virtual:';
|
10 | const REGISTRY = '__TAILOR_CONTENT_ELEMENTS__';
|
11 | const SCOPE = /^@[^/]+\//;
|
12 | const TEMPLATE = readFileSync(require.resolve('./dist/plugin'), 'utf-8');
|
13 |
|
14 | const isObject = arg => arg !== null && typeof arg === 'object';
|
15 | const normalize = modulePath => path.resolve(process.cwd(), modulePath);
|
16 |
|
17 | module.exports = function () {
|
18 | let entryId;
|
19 | let entryCode;
|
20 | let pkg;
|
21 |
|
22 | return {
|
23 | name: NAME,
|
24 |
|
25 | async options(options) {
|
26 |
|
27 | const [entry] = getInput(options.input);
|
28 | const entryPath = normalize(entry);
|
29 | const cwd = path.dirname(entryPath);
|
30 | const { packageJson, path: packagePath } = await readPkgUp({ cwd });
|
31 | pkg = packageJson;
|
32 | entryId = [PREFIX, entryPath].join('');
|
33 | entryCode = render(TEMPLATE, {
|
34 | packagePath: normalize(packagePath),
|
35 | entryPath
|
36 | });
|
37 |
|
38 | const entryName = pkg.name.replace(SCOPE, '');
|
39 | const input = { [entryName]: entryId };
|
40 | Object.assign(options, { input });
|
41 | return options;
|
42 | },
|
43 |
|
44 | outputOptions(options) {
|
45 | const name = [REGISTRY, pkg.name].join('.');
|
46 | return Object.assign(options, { name });
|
47 | },
|
48 |
|
49 | resolveId(id) {
|
50 | return id === entryId ? id : null;
|
51 | },
|
52 |
|
53 | load(id) {
|
54 | return id === entryId ? entryCode : null;
|
55 | }
|
56 | };
|
57 | };
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | function getInput(input) {
|
64 | if (isObject(input)) return Object.values(input);
|
65 | if (Array.isArray(input)) return input;
|
66 | return [input];
|
67 | }
|