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 OPTIONAL_EXPORTS = ['install'];
|
12 | const SCOPE = /^@[^/]+\//;
|
13 | const TEMPLATE = readFileSync(require.resolve('./dist/plugin'), 'utf-8');
|
14 |
|
15 | const isObject = arg => arg !== null && typeof arg === 'object';
|
16 | const isString = arg => typeof arg === 'string';
|
17 | const noop = () => {};
|
18 | const normalize = modulePath => path.resolve(process.cwd(), modulePath);
|
19 |
|
20 | module.exports = function () {
|
21 | let entryId;
|
22 | let entryCode;
|
23 |
|
24 | return {
|
25 | name: NAME,
|
26 |
|
27 | options(options) {
|
28 |
|
29 | const [entry] = getInput(options.input);
|
30 | const entryPath = normalize(entry);
|
31 | entryId = [PREFIX, entryPath].join('');
|
32 | entryCode = render(TEMPLATE, {
|
33 | packagePath: normalize(packagePath),
|
34 | entryPath
|
35 | });
|
36 |
|
37 | const entryName = pkg.name.replace(SCOPE, '');
|
38 | const input = { [entryName]: entryId };
|
39 | Object.assign(options, { input, shimMissingExports: true });
|
40 |
|
41 | const { onwarn: warn = noop } = options;
|
42 | options.onwarn = function (warning) {
|
43 | if (onwarn.call(this, entry, warning)) return;
|
44 | warn.apply(this, arguments);
|
45 | };
|
46 | return options;
|
47 | },
|
48 |
|
49 | outputOptions(options) {
|
50 | const name = [REGISTRY, pkg.name].join('.');
|
51 | return Object.assign(options, { name });
|
52 | },
|
53 |
|
54 | resolveId(id) {
|
55 | return id === entryId ? id : null;
|
56 | },
|
57 |
|
58 | load(id) {
|
59 | return id === entryId ? entryCode : null;
|
60 | }
|
61 | };
|
62 | };
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function onwarn(entryPath, warning) {
|
70 | if (isString(warning)) return false;
|
71 | const code = (warning.code || '').toLowerCase();
|
72 | return code === 'shimmed_export' &&
|
73 | OPTIONAL_EXPORTS.includes(warning.exportName) &&
|
74 | normalize(warning.exporter) === normalize(entryPath);
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | function getInput(input) {
|
82 | if (isObject(input)) return Object.values(input);
|
83 | if (Array.isArray(input)) return input;
|
84 | return [input];
|
85 | }
|