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 | const virtual = require('rollup-plugin-virtual');
|
8 |
|
9 | const PLUGIN_ENTRY = require.resolve('./dist/__plugin__');
|
10 | const TCE_REGISTRY = '__TAILOR_CONTENT_ELEMENTS__';
|
11 |
|
12 | const moduleName = name => [TCE_REGISTRY, name].join('.');
|
13 | const normalize = modulePath => path.resolve(process.cwd(), modulePath);
|
14 |
|
15 | module.exports = () => ({
|
16 | |
17 |
|
18 |
|
19 | options(options) {
|
20 |
|
21 | const [entry] = Object.values(options.input);
|
22 | const name = path.basename(PLUGIN_ENTRY, path.extname(PLUGIN_ENTRY));
|
23 | const template = readFileSync(PLUGIN_ENTRY, 'utf-8');
|
24 | const code = render(template, {
|
25 | packagePath: normalize(packagePath),
|
26 | entryPath: normalize(entry)
|
27 | });
|
28 | options.plugins.push(virtual({ [name]: code }));
|
29 |
|
30 | const input = { [pkg.name]: name };
|
31 | return Object.assign(options, { input });
|
32 | },
|
33 | |
34 |
|
35 |
|
36 | outputOptions(options) {
|
37 | const name = moduleName(pkg.name);
|
38 | return Object.assign(options, { name });
|
39 | }
|
40 | });
|