UNPKG

3.21 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';
13const DEFAULT_ENTRYPOINT = 'browser';
14export async function beforeJob({ out }) {
15 const srcDirectory = path.join(out, 'dist-web/');
16 if (!fs.existsSync(srcDirectory)) {
17 throw new MessageError('"dist-web/" does not exist. "plugin-bundle-web" requires "plugin-build-dev" to precede in pipeline.');
18 }
19 const srcEntrypoint = path.join(out, 'dist-web/index.js');
20 if (!fs.existsSync(srcEntrypoint)) {
21 throw new MessageError('"dist-web/index.js" is the expected standard entrypoint, but it does not exist.');
22 }
23}
24export function manifest(manifest, { options }) {
25 if (options.entrypoint !== null) {
26 let keys = options.entrypoint || [DEFAULT_ENTRYPOINT];
27 if (typeof keys === 'string') {
28 keys = [keys];
29 }
30 for (const key of keys) {
31 manifest[key] = manifest[key] || '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}