1 | import rollupCommonJs from 'rollup-plugin-commonjs';
|
2 | import rollupJson from 'rollup-plugin-json';
|
3 | import rollupNodeResolve from 'rollup-plugin-node-resolve';
|
4 | import { terser as rollupTerser } from 'rollup-plugin-terser';
|
5 | import rollupBabel from 'rollup-plugin-babel';
|
6 | import babelPluginDynamicImportSyntax from '@babel/plugin-syntax-dynamic-import';
|
7 | import babelPluginImportMetaSyntax from '@babel/plugin-syntax-import-meta';
|
8 | import babelPresetEnv from '@babel/preset-env';
|
9 | import path from 'path';
|
10 | import fs from 'fs';
|
11 | import { MessageError } from '@pika/types';
|
12 | import { rollup } from 'rollup';
|
13 | export 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 | }
|
23 | export 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 | }
|
35 | export 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 | }
|