@tmbr/bundler 1.6.2 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/cli.js +74 -64
  2. package/package.json +6 -9
package/cli.js CHANGED
@@ -4,11 +4,12 @@ const fs = require('fs');
4
4
  const path = require('path');
5
5
  const exec = require('child_process').execSync;
6
6
  const chalk = require('chalk');
7
- const styles = require('esbuild-sass-plugin').sassPlugin;
8
- const qrcode = require('qrcode-terminal');
9
7
  const server = require('browser-sync').create();
8
+ const styles = require('esbuild-sass-plugin').sassPlugin;
9
+ const silent = require('sass').Logger.silent;
10
10
 
11
11
  const dir = process.cwd();
12
+ const src = path.resolve(dir, 'src');
12
13
  const package = require(`${dir}/package.json`);
13
14
  const command = process.argv[2];
14
15
 
@@ -17,89 +18,98 @@ if (!['build', 'watch'].includes(command)) {
17
18
  process.exit();
18
19
  }
19
20
 
20
- exec(`rm -rf ${dir}/build/*`);
21
+ const logger = (options = {}) => ({
22
+ name: 'logger',
23
+ setup(context) {
21
24
 
22
- function ok() {
23
- const host = server.getOption('proxy').get('target');
24
- const port = server.getOption('port');
25
- const proxying = `${host}:${port}`;
26
- const external = server.getOption('urls').get('external');
25
+ // let reset = true;
26
+ // const path = `${context.initialOptions.outdir}/${Object.keys(context.initialOptions.entryPoints)[0]}`;
27
27
 
28
- console.clear();
29
- external && qrcode.generate(external, {small: true}, console.log);
30
- console.log(`Proxying: ${chalk.green(proxying)}`);
31
- console.log(`External: ${chalk.cyan(external || 'offline')}\n`);
32
- }
33
-
34
- function entryPoints(suffix = '') {
35
-
36
- if (!suffix.startsWith('.')) {
37
- suffix = `.${suffix}`;
38
- }
39
-
40
- const entries = Object.entries({
41
- admin: path.resolve(dir, './src/admin'),
42
- main: path.resolve(dir, './src'),
43
- });
44
-
45
- return entries.reduce((result, [name, path]) => {
46
- if (fs.existsSync(path)) result[`${name}${suffix}`] = path;
47
- return result;
48
- }, {});
49
- }
28
+ context.onStart(() => {
29
+ server.instance.active && server.info();
30
+ });
50
31
 
51
- const errors = (options = {}) => ({
52
- name: 'errors',
53
- setup(build) {
54
- build.onEnd(({warnings, errors}) => {
55
- (warnings.length || errors.length) ? console.log('\007') : ok();
32
+ context.onEnd(result => {
33
+ if (result.warnings.length || result.errors.length) return console.log('\007');
34
+ // console.log(`${path}.css`);
35
+ // console.log(`${path}.js`);
36
+ // const stats = fs.statSync('main.min.js');
37
+ // const bytes = stats.size;
38
+ // console.log(`build/main.min.js ${bytes / 1000}`);
39
+ // console.log(`build/main.min.css ${Math.round(fs.statSync('build/main.min.css').size / 1000)} KB`);
40
+ // console.log(`build ended with ${result.errors.length} errors`);
41
+ // build/main.min.css 47KB
42
+ // build/main.min.js 170KB
56
43
  });
57
- },
44
+ }
58
45
  });
59
46
 
60
- const defaults = {
61
- watch: command === 'watch',
62
- alias: {'~': path.resolve(dir, 'src')},
63
- outdir: path.resolve(dir, 'build'),
47
+ const buildOptions = {
48
+ entryPoints: {'main.min': src},
49
+ alias: {'~': src},
50
+ outdir: 'build',
64
51
  bundle: true,
65
52
  minify: true,
66
53
  target: 'es2019',
67
- external: 'jpg,jpeg,webp,png,svg,woff,woff2'.split(',').map(ext => `*.${ext}`),
54
+ external: 'jpg,jpeg,webp,png,gif,svg,woff,woff2'.split(',').map(ext => `*.${ext}`),
68
55
  logLevel: 'warning',
69
56
  sourcemap: false,
70
57
  treeShaking: true,
71
58
  legalComments: 'none',
72
- plugins: [styles({sourceMap: true})],
59
+ plugins: [
60
+ styles({sourceMap: false, logger: silent}),
61
+ logger()
62
+ ]
73
63
  };
74
64
 
75
- const watchConfig = Object.assign({}, defaults, {
76
- entryPoints: entryPoints('dev'),
65
+ const watchOptions = Object.assign({}, buildOptions, {
66
+ entryPoints: {'main.dev': src},
77
67
  minify: false,
78
- sourcemap: 'inline',
79
68
  logLevel: 'silent',
80
- plugins: [...defaults.plugins, errors()],
69
+ sourcemap: 'inline',
70
+ plugins: [
71
+ styles({sourceMap: true})
72
+ ]
81
73
  });
82
74
 
83
- const buildConfig = Object.assign({}, defaults, {
84
- entryPoints: entryPoints('min'),
85
- });
75
+ const serveOptions = {
76
+ proxy: `${package.name}.test`,
77
+ files: ['assets/**', 'build/*', '**/*.php'],
78
+ host: 'localhost',
79
+ open: false,
80
+ notify: false,
81
+ logLevel: 'silent',
82
+ injectChanges: false,
83
+ ui: false,
84
+ };
85
+
86
+ server.info = function() {
87
+ const host = server.getOption('proxy').get('target');
88
+ const port = server.getOption('port');
89
+ const proxying = `${host}:${port}`;
90
+ const external = server.getOption('urls').get('external');
86
91
 
87
- const noop = fn => fn;
88
- esbuild.build(watchConfig).catch(noop);
89
- esbuild.build(buildConfig).catch(noop);
92
+ console.clear();
93
+ console.log();
94
+ console.log(`Proxying: ${chalk.green(proxying)}`);
95
+ console.log(`External: ${chalk.cyan(external || 'offline')}\n`);
96
+ };
90
97
 
91
- if (command === 'watch') {
98
+ async function main() {
92
99
 
93
- const options = {
94
- proxy: `${package.name}.test`,
95
- files: ['assets/**', 'build/*', '**/*.php'],
96
- injectChanges: false,
97
- host: 'localhost',
98
- open: false,
99
- notify: false,
100
- logLevel: 'silent',
101
- ui: false,
102
- };
100
+ const watcher = await esbuild.context(watchOptions);
101
+ const builder = await esbuild.context(buildOptions);
103
102
 
104
- server.init(options, ok);
103
+ if (command === 'build') {
104
+ await watcher.rebuild();
105
+ await builder.rebuild();
106
+ process.exit();
107
+ }
108
+
109
+ server.init(serveOptions, () => {
110
+ builder.watch();
111
+ watcher.watch();
112
+ });
105
113
  }
114
+
115
+ main();
package/package.json CHANGED
@@ -1,18 +1,15 @@
1
1
  {
2
2
  "name": "@tmbr/bundler",
3
- "author": "TMBR (https://wearetmbr.com/)",
4
- "version": "1.6.2",
5
- "license": "MIT",
6
- "repository": "https://github.com/TMBR/tmbr-bundler",
7
- "description": "WordPress development toolkit built on esbuild and browser-sync",
3
+ "version": "1.8.1",
4
+ "repository": "github:TMBR/tmbr-bundler",
8
5
  "bin": {
6
+ "bundler": "cli.js",
9
7
  "tmbr-bundler": "cli.js"
10
8
  },
11
9
  "dependencies": {
12
- "browser-sync": "^2.27.10",
10
+ "browser-sync": "^2.28.3",
13
11
  "chalk": "^4.1.2",
14
- "esbuild": "^0.16.10",
15
- "esbuild-sass-plugin": "^2.3.2",
16
- "qrcode-terminal": "^0.12.0"
12
+ "esbuild": "^0.17.11",
13
+ "esbuild-sass-plugin": "^2.6.0"
17
14
  }
18
15
  }