@tmbr/bundler 1.7.0 → 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.
package/cli.js CHANGED
@@ -1,13 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
  const esbuild = require('esbuild');
3
+ const fs = require('fs');
3
4
  const path = require('path');
4
5
  const exec = require('child_process').execSync;
5
6
  const chalk = require('chalk');
6
- const styles = require('esbuild-sass-plugin').sassPlugin;
7
- const qrcode = require('qrcode-terminal');
8
7
  const server = require('browser-sync').create();
8
+ const styles = require('esbuild-sass-plugin').sassPlugin;
9
+ const silent = require('sass').Logger.silent;
9
10
 
10
11
  const dir = process.cwd();
12
+ const src = path.resolve(dir, 'src');
11
13
  const package = require(`${dir}/package.json`);
12
14
  const command = process.argv[2];
13
15
 
@@ -16,32 +18,36 @@ if (!['build', 'watch'].includes(command)) {
16
18
  process.exit();
17
19
  }
18
20
 
19
- function ok() {
20
-
21
- console.clear();
22
- const host = server.getOption('proxy').get('target');
23
- const port = server.getOption('port');
24
- const proxying = `${host}:${port}`;
25
- const external = server.getOption('urls').get('external');
26
-
27
- external && qrcode.generate(external, {small: true}, console.log);
28
- console.log(`Proxying: ${chalk.green(proxying)}`);
29
- console.log(`External: ${chalk.cyan(external || 'offline')}\n`);
30
- }
31
-
32
- const errors = (options = {}) => ({
33
- name: 'errors',
34
- setup(build) {
35
- // console.log(Object.keys(build));
36
- // console.log(build)
37
- // build.onEnd(({warnings, errors}) => (warnings.length || errors.length) ? console.log('\007') : ok());
38
- },
21
+ const logger = (options = {}) => ({
22
+ name: 'logger',
23
+ setup(context) {
24
+
25
+ // let reset = true;
26
+ // const path = `${context.initialOptions.outdir}/${Object.keys(context.initialOptions.entryPoints)[0]}`;
27
+
28
+ context.onStart(() => {
29
+ server.instance.active && server.info();
30
+ });
31
+
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
43
+ });
44
+ }
39
45
  });
40
46
 
41
- const buildConfig = {
42
- entryPoints: {'main.min': path.resolve(dir, './src')},
43
- alias: {'~': path.resolve(dir, 'src')},
44
- outdir: path.resolve(dir, 'build'),
47
+ const buildOptions = {
48
+ entryPoints: {'main.min': src},
49
+ alias: {'~': src},
50
+ outdir: 'build',
45
51
  bundle: true,
46
52
  minify: true,
47
53
  target: 'es2019',
@@ -50,49 +56,60 @@ const buildConfig = {
50
56
  sourcemap: false,
51
57
  treeShaking: true,
52
58
  legalComments: 'none',
53
- plugins: [styles({sourceMap: true})]
59
+ plugins: [
60
+ styles({sourceMap: false, logger: silent}),
61
+ logger()
62
+ ]
54
63
  };
55
64
 
56
- const watchConfig = Object.assign({}, buildConfig, {
57
- entryPoints: {'main.dev': path.resolve(dir, './src')},
65
+ const watchOptions = Object.assign({}, buildOptions, {
66
+ entryPoints: {'main.dev': src},
58
67
  minify: false,
59
68
  logLevel: 'silent',
60
69
  sourcemap: 'inline',
61
- plugins: [...buildConfig.plugins, errors()]
70
+ plugins: [
71
+ styles({sourceMap: true})
72
+ ]
62
73
  });
63
74
 
64
- async function main() {
65
-
66
- const builder = await esbuild.context(buildConfig);
67
- const watcher = await esbuild.context(watchConfig);
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
+ };
68
85
 
69
- try {
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');
70
91
 
71
- await builder.rebuild();
72
- await watcher.rebuild();
92
+ console.clear();
93
+ console.log();
94
+ console.log(`Proxying: ${chalk.green(proxying)}`);
95
+ console.log(`External: ${chalk.cyan(external || 'offline')}\n`);
96
+ };
73
97
 
74
- if (command === 'build') process.exit();
98
+ async function main() {
75
99
 
76
- await builder.watch();
77
- await watcher.watch();
100
+ const watcher = await esbuild.context(watchOptions);
101
+ const builder = await esbuild.context(buildOptions);
78
102
 
79
- } catch(e) {
80
- console.log(e);
103
+ if (command === 'build') {
104
+ await watcher.rebuild();
105
+ await builder.rebuild();
81
106
  process.exit();
82
107
  }
83
108
 
84
- const options = {
85
- proxy: `${package.name}.test`,
86
- files: ['assets/**', 'build/*', '**/*.php'],
87
- host: 'localhost',
88
- open: false,
89
- notify: false,
90
- logLevel: 'silent',
91
- injectChanges: false,
92
- ui: false,
93
- };
94
-
95
- server.init(options, ok);
109
+ server.init(serveOptions, () => {
110
+ builder.watch();
111
+ watcher.watch();
112
+ });
96
113
  }
97
114
 
98
115
  main();
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@tmbr/bundler",
3
- "version": "1.7.0",
3
+ "version": "1.8.1",
4
4
  "repository": "github:TMBR/tmbr-bundler",
5
5
  "bin": {
6
+ "bundler": "cli.js",
6
7
  "tmbr-bundler": "cli.js"
7
8
  },
8
9
  "dependencies": {
9
- "browser-sync": "^2.27.10",
10
+ "browser-sync": "^2.28.3",
10
11
  "chalk": "^4.1.2",
11
12
  "esbuild": "^0.17.11",
12
- "esbuild-sass-plugin": "^2.6.0",
13
- "qrcode-terminal": "^0.12.0"
13
+ "esbuild-sass-plugin": "^2.6.0"
14
14
  }
15
15
  }
package/cli-new-backup.js DELETED
@@ -1,127 +0,0 @@
1
- #!/usr/bin/env node
2
- const esbuild = require('esbuild');
3
- const fs = require('fs');
4
- const path = require('path');
5
- const exec = require('child_process').execSync;
6
- const chalk = require('chalk');
7
- const styles = require('esbuild-sass-plugin').sassPlugin;
8
- const qrcode = require('qrcode-terminal');
9
- const server = require('browser-sync').create();
10
-
11
- const dir = process.cwd();
12
- const package = require(`${dir}/package.json`);
13
- const command = process.argv[2];
14
-
15
- if (!['build', 'watch'].includes(command)) {
16
- console.log(`Invalid command: ${chalk.red(command)}\n`);
17
- process.exit();
18
- }
19
-
20
- exec(`rm -rf ${dir}/build/*`);
21
-
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');
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
- const assets = (options = {}) => ({
35
- name: 'assets',
36
- setup(build) {
37
- build.onResolve({filter: /..\/(assets|fonts|images)\//}, args => ({
38
- path: args.path,
39
- external: true
40
- }))
41
- },
42
- });
43
-
44
- const errors = (options = {}) => ({
45
- name: 'errors',
46
- setup(build) {
47
- build.onEnd(({warnings, errors}) => {
48
- (warnings.length || errors.length) ? console.log('\007') : ok();
49
- });
50
- },
51
- });
52
-
53
- const plugins = [
54
- assets(),
55
- styles({sourceMap: true}),
56
- ];
57
-
58
- const defaults = {
59
- outdir: path.resolve(dir, 'build'),
60
- bundle: true,
61
- legalComments: 'none',
62
- target: 'es2019',
63
- treeShaking: true,
64
- plugins,
65
- };
66
-
67
- const watchConfig = Object.assign({}, defaults, {
68
- entryPoints: {'main.dev': path.resolve(dir, './src')},
69
- minify: false,
70
- logLevel: 'silent',
71
- sourcemap: 'inline',
72
- plugins: [
73
- ...defaults.plugins,
74
- // errors()
75
- ]
76
- });
77
-
78
- const buildConfig = Object.assign({}, defaults, {
79
- entryPoints: {'main.min': path.resolve(dir, './src')},
80
- minify: true,
81
- logLevel: 'warning',
82
- sourcemap: false,
83
- });
84
-
85
- async function main() {
86
-
87
- const watcher = await esbuild.context(watchConfig);
88
- const builder = await esbuild.context(buildConfig);
89
-
90
- try {
91
-
92
- await watcher.rebuild();
93
- await builder.rebuild();
94
- if (command === 'build') process.exit();
95
-
96
- // await builder.watch();
97
- // await watcher.watch();
98
-
99
- } catch(e) {
100
- console.log(e)
101
- }
102
-
103
- // const watchedBuilds = Object.keys(watchConfig.entryPoints).map(name => (
104
- // `${watchConfig.outdir}/${name}.*`
105
- // ));
106
-
107
- // const files = [
108
- // ...watchedBuilds,
109
- // 'images/*',
110
- // '**/*.php'
111
- // ];
112
-
113
- /* const options = {
114
- // files,
115
- proxy: `${package.name}.test`,
116
- host: 'localhost',
117
- open: false,
118
- notify: false,
119
- logLevel: 'silent',
120
- ui: false,
121
- };
122
-
123
- server.init(options, ok); */
124
- }
125
-
126
- const noop = fn => fn;
127
- main();
package/cli-old-backup.js DELETED
@@ -1,105 +0,0 @@
1
- #!/usr/bin/env node
2
- const esbuild = require('esbuild');
3
- const fs = require('fs');
4
- const path = require('path');
5
- const exec = require('child_process').execSync;
6
- const chalk = require('chalk');
7
- const styles = require('esbuild-sass-plugin').sassPlugin;
8
- const qrcode = require('qrcode-terminal');
9
- const server = require('browser-sync').create();
10
-
11
- const dir = process.cwd();
12
- const package = require(`${dir}/package.json`);
13
- const command = process.argv[2];
14
-
15
- if (!['build', 'watch'].includes(command)) {
16
- console.log(`Invalid command: ${chalk.red(command)}\n`);
17
- process.exit();
18
- }
19
-
20
- exec(`rm -rf ${dir}/build/*`);
21
-
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');
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
- }
50
-
51
- const errors = (options = {}) => ({
52
- name: 'errors',
53
- setup(build) {
54
- build.onEnd(({warnings, errors}) => {
55
- (warnings.length || errors.length) ? console.log('\007') : ok();
56
- });
57
- },
58
- });
59
-
60
- const defaults = {
61
- watch: command === 'watch',
62
- alias: {'~': path.resolve(dir, 'src')},
63
- outdir: path.resolve(dir, 'build'),
64
- bundle: true,
65
- minify: true,
66
- target: 'es2019',
67
- external: 'jpg,jpeg,webp,png,svg,woff,woff2'.split(',').map(ext => `*.${ext}`),
68
- logLevel: 'warning',
69
- sourcemap: false,
70
- treeShaking: true,
71
- legalComments: 'none',
72
- plugins: [styles({sourceMap: true})],
73
- };
74
-
75
- const watchConfig = Object.assign({}, defaults, {
76
- entryPoints: entryPoints('dev'),
77
- minify: false,
78
- sourcemap: 'inline',
79
- logLevel: 'silent',
80
- plugins: [...defaults.plugins, errors()],
81
- });
82
-
83
- const buildConfig = Object.assign({}, defaults, {
84
- entryPoints: entryPoints('min'),
85
- });
86
-
87
- const noop = fn => fn;
88
- esbuild.build(watchConfig).catch(noop);
89
- esbuild.build(buildConfig).catch(noop);
90
-
91
- if (command === 'watch') {
92
-
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
- };
103
-
104
- server.init(options, ok);
105
- }