|
1 | | -const rollup = require('rollup') |
2 | | -const buble = require('rollup-plugin-buble') |
3 | | -const commonjs = require('rollup-plugin-commonjs') |
4 | | -const nodeResolve = require('rollup-plugin-node-resolve') |
5 | | -const terser = require('@rollup/plugin-terser') |
6 | | -const replace = require('rollup-plugin-replace') |
7 | | -const isProd = process.env.NODE_ENV === 'production' |
8 | | -const version = process.env.VERSION || require('../package.json').version |
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { minify } = require('terser'); |
| 4 | + |
| 5 | +const rootDir = path.resolve(__dirname, '..'); |
| 6 | +const inputFile = path.join(rootDir, 'src', 'index.js'); |
| 7 | +const distDir = path.join(rootDir, 'dist'); |
| 8 | +const outputFile = path.join(distDir, 'index.js'); |
| 9 | +const outputMinFile = path.join(distDir, 'index.min.js'); |
| 10 | +const outputMinMapFile = path.join(distDir, 'index.min.js.map'); |
| 11 | + |
| 12 | +function cleanDist() { |
| 13 | + if (fs.existsSync(distDir)) { |
| 14 | + fs.rmSync(distDir, { recursive: true, force: true }); |
| 15 | + console.log('🧹 Cleaned up dist directory'); |
| 16 | + } |
| 17 | + fs.mkdirSync(distDir, { recursive: true }); |
| 18 | +} |
9 | 19 |
|
10 | | -/** |
11 | | - * @param {{ |
12 | | - * input: string, |
13 | | - * output?: string, |
14 | | - * globalName?: string, |
15 | | - * plugins?: Array<import('rollup').Plugin> |
16 | | - * }} opts |
17 | | - */ |
18 | | -async function build(opts) { |
19 | | - await rollup |
20 | | - .rollup({ |
21 | | - input: opts.input, |
22 | | - plugins: (opts.plugins || []).concat([ |
23 | | - buble(), |
24 | | - commonjs(), |
25 | | - nodeResolve(), |
26 | | - replace({ |
27 | | - __VERSION__: version |
28 | | - }) |
29 | | - ]), |
30 | | - onwarn: function (message) { |
31 | | - if (message.code === 'UNRESOLVED_IMPORT') { |
32 | | - throw new Error( |
33 | | - `Could not resolve module ` + |
34 | | - message.source + |
35 | | - `. Try running 'npm install' or using rollup's 'external' option if this is an external dependency. ` + |
36 | | - `Module ${message.source} is imported in ${message.importer}` |
37 | | - ) |
38 | | - } |
39 | | - } |
40 | | - }) |
41 | | - .then(function (bundle) { |
42 | | - var dest = 'dist/' + (opts.output || opts.input) |
| 20 | +async function build() { |
| 21 | + cleanDist(); |
43 | 22 |
|
44 | | - console.log(dest) |
45 | | - return bundle.write({ |
46 | | - output: {file: dest, format: 'iife', strict: false}, |
47 | | - }) |
48 | | - }) |
49 | | -} |
| 23 | + if (!fs.existsSync(inputFile)) { |
| 24 | + console.error(`❌ Source file not found: ${inputFile}`); |
| 25 | + return; |
| 26 | + } |
50 | 27 |
|
51 | | -async function buildPlugin() { |
52 | | - var plugins = [ |
53 | | - {name: 'index', input: 'index.js'}, |
54 | | - ] |
| 28 | + let sourceCode = fs.readFileSync(inputFile, 'utf-8'); |
55 | 29 |
|
56 | | - const promises = plugins.map(item => { |
57 | | - return build({ |
58 | | - input: 'src/' + item.input, |
59 | | - output: item.name + '.js' |
60 | | - }) |
61 | | - }) |
| 30 | + const wrappedCode = `(function () {\n${sourceCode}\n})();`; |
| 31 | + fs.writeFileSync(outputFile, wrappedCode, 'utf-8'); |
| 32 | + console.log('✅ Generated non-minified version (IIFE): dist/index.js'); |
62 | 33 |
|
63 | | - if (isProd) { |
64 | | - plugins.forEach(item => { |
65 | | - promises.push(build({ |
66 | | - input: 'src/' + item.input, |
67 | | - output: item.name + '.min.js', |
68 | | - plugins: [terser()] |
69 | | - })) |
70 | | - }) |
71 | | - } |
| 34 | + try { |
| 35 | + const minified = await minify(sourceCode, { |
| 36 | + compress: true, |
| 37 | + mangle: true, |
| 38 | + sourceMap: { |
| 39 | + filename: 'index.min.js', |
| 40 | + url: 'index.min.js.map' |
| 41 | + } |
| 42 | + }); |
72 | 43 |
|
73 | | - await Promise.all(promises) |
74 | | -} |
| 44 | + fs.writeFileSync(outputMinFile, minified.code, 'utf-8'); |
| 45 | + console.log('✅ Generated minified version: dist/index.min.js'); |
75 | 46 |
|
76 | | -async function main() { |
77 | | - await Promise.all([ |
78 | | - buildPlugin() |
79 | | - ]) |
| 47 | + fs.writeFileSync(outputMinMapFile, minified.map, 'utf-8'); |
| 48 | + console.log('✅ Generated minified source map: dist/index.min.js.map'); |
| 49 | + } catch (err) { |
| 50 | + console.error('❌ Error during minification:', err); |
| 51 | + } |
80 | 52 | } |
81 | 53 |
|
82 | | -main().catch((e) => { |
83 | | - console.error(e) |
84 | | - process.exit(1) |
85 | | -}) |
| 54 | +build(); |
0 commit comments