UNPKG

3.2 kBJavaScriptView Raw
1import rollupCommonJs from 'rollup-plugin-commonjs';
2import rollupJson from 'rollup-plugin-json';
3import rollupNodeResolve from 'rollup-plugin-node-resolve';
4import { terser as rollupTerser } from 'rollup-plugin-terser';
5import rollupBabel from 'rollup-plugin-babel';
6import babelPluginDynamicImportSyntax from '@babel/plugin-syntax-dynamic-import';
7import babelPluginImportMetaSyntax from '@babel/plugin-syntax-import-meta';
8import babelPresetEnv from '@babel/preset-env';
9import path from 'path';
10import fs from 'fs';
11import { MessageError } from '@pika/types';
12import { rollup } from 'rollup';
13export async function beforeJob({ out }) {
14 const srcDirectory = path.join(out, 'dist-web/');
15 if (!fs.existsSync(srcDirectory)) {
16 throw new MessageError('"dist-web/" does not exist. "plugin-bundle-web" requires "plugin-build-dev" to precede in pipeline.');
17 }
18 const srcEntrypoint = path.join(out, 'dist-web/index.js');
19 if (!fs.existsSync(srcEntrypoint)) {
20 throw new MessageError('"dist-web/index.js" is the expected standard entrypoint, but it does not exist.');
21 }
22}
23export function manifest(manifest, { options }) {
24 if (options.entrypoint) {
25 if (options.entrypoint instanceof Array) {
26 options.entrypoint.forEach(entrypoint => {
27 manifest[entrypoint] = 'dist-web/index.bundled.js';
28 });
29 }
30 else {
31 manifest[options.entrypoint] = 'dist-web/index.bundled.js';
32 }
33 }
34}
35export async function build({ out, options, reporter }) {
36 const readFromWeb = path.join(out, 'dist-web', 'index.js');
37 const writeToWeb = path.join(out, 'dist-web');
38 const writeToWebBundled = path.join(writeToWeb, 'index.bundled.js');
39 const result = await rollup({
40 input: readFromWeb,
41 plugins: [
42 rollupNodeResolve({
43 preferBuiltins: true,
44 browser: !!options.browser,
45 }),
46 rollupCommonJs({
47 include: 'node_modules/**',
48 sourceMap: false,
49 namedExports: options.namedExports,
50 }),
51 rollupJson({
52 include: 'node_modules/**',
53 compact: true,
54 }),
55 rollupBabel({
56 babelrc: false,
57 compact: false,
58 presets: [
59 [
60 babelPresetEnv,
61 {
62 modules: false,
63 targets: options.targets || { esmodules: true },
64 },
65 ],
66 ],
67 plugins: [babelPluginDynamicImportSyntax, babelPluginImportMetaSyntax],
68 }),
69 options.minify !== false ? rollupTerser(typeof options.minify === 'object' ? options.minify : undefined) : {},
70 ],
71 });
72 await result.write({
73 dir: writeToWeb,
74 entryFileNames: '[name].bundled.js',
75 chunkFileNames: '[name]-[hash].bundled.js',
76 format: 'esm',
77 exports: 'named',
78 sourcemap: options.sourcemap === undefined ? true : options.sourcemap,
79 });
80 reporter.created(writeToWebBundled);
81}