1 | 'use strict';
|
2 |
|
3 | const { packageJson: pkg, path: packagePath } = require('read-pkg-up').sync();
|
4 | const path = require('path');
|
5 | const { readFileSync } = require('fs');
|
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 |
|
21 | return {
|
22 | name: NAME,
|
23 |
|
24 | options(options) {
|
25 |
|
26 | const [entry] = getInput(options.input);
|
27 | const entryPath = normalize(entry);
|
28 | entryId = [PREFIX, entryPath].join('');
|
29 | entryCode = render(TEMPLATE, {
|
30 | packagePath: normalize(packagePath),
|
31 | entryPath
|
32 | });
|
33 |
|
34 | const entryName = pkg.name.replace(SCOPE, '');
|
35 | const input = { [entryName]: entryId };
|
36 | return Object.assign(options, { input, shimMissingExports: true });
|
37 | },
|
38 |
|
39 | outputOptions(options) {
|
40 | const name = [REGISTRY, pkg.name].join('.');
|
41 | return Object.assign(options, { name });
|
42 | },
|
43 |
|
44 | resolveId(id) {
|
45 | return id === entryId ? id : null;
|
46 | },
|
47 |
|
48 | load(id) {
|
49 | return id === entryId ? entryCode : null;
|
50 | }
|
51 | };
|
52 | };
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function getInput(input) {
|
59 | if (isObject(input)) return Object.values(input);
|
60 | if (Array.isArray(input)) return input;
|
61 | return [input];
|
62 | }
|