UNPKG

1.33 kBJavaScriptView Raw
1'use strict';
2
3const { packageJson: pkg, path: packagePath } = require('read-pkg-up').sync();
4const path = require('path');
5const { readFileSync } = require('fs');
6const { render } = require('mustache');
7const virtual = require('rollup-plugin-virtual');
8
9const PLUGIN_ENTRY = require.resolve('./dist/__plugin__');
10const TCE_REGISTRY = '__TAILOR_CONTENT_ELEMENTS__';
11
12const moduleName = name => [TCE_REGISTRY, name].join('.');
13const normalize = modulePath => path.resolve(process.cwd(), modulePath);
14
15module.exports = () => ({
16 /**
17 * @param {import('rollup').InputOptions} options
18 */
19 options(options) {
20 // Create virtual entry module.
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 // Set `options.input` to newly created entry.
30 const input = { [pkg.name]: name };
31 return Object.assign(options, { input });
32 },
33 /**
34 * @param {import('rollup').OutputOptions} options
35 */
36 outputOptions(options) {
37 const name = moduleName(pkg.name);
38 return Object.assign(options, { name });
39 }
40});