@m00rl0ck/simple-builder 1.1.3 ā 1.6.9
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/bin/cli.js +0 -0
- package/bundler.js +9 -32
- package/dev-server.js +3 -6
- package/index.js +25 -41
- package/minify-css.js +1 -1
- package/minify-js.js +1 -1
- package/package.json +16 -18
package/bin/cli.js
CHANGED
|
File without changes
|
package/bundler.js
CHANGED
|
@@ -7,33 +7,17 @@ let ID = 0;
|
|
|
7
7
|
// š FIND IMPORTS
|
|
8
8
|
// ======================
|
|
9
9
|
function getImports(code) {
|
|
10
|
-
|
|
11
|
-
const namedRegex = /import\s+{([^}]+)}\s+from\s+['"](.+?)['"]/g;
|
|
12
|
-
// Default imports: import X from 'y'
|
|
13
|
-
const defaultRegex = /import\s+(\w+)\s+from\s+['"](.+?)['"]/g;
|
|
14
|
-
|
|
10
|
+
const regex = /import\s+{([^}]+)}\s+from\s+['"](.+?)['"]/g;
|
|
15
11
|
const imports = [];
|
|
16
12
|
let match;
|
|
17
13
|
|
|
18
|
-
while ((match =
|
|
14
|
+
while ((match = regex.exec(code))) {
|
|
19
15
|
imports.push({
|
|
20
|
-
type: 'named',
|
|
21
16
|
names: match[1].split(',').map(s => s.trim()),
|
|
22
17
|
path: match[2]
|
|
23
18
|
});
|
|
24
19
|
}
|
|
25
20
|
|
|
26
|
-
while ((match = defaultRegex.exec(code))) {
|
|
27
|
-
// Skip if already matched by namedRegex
|
|
28
|
-
const already = imports.some(i => i.type === 'named' && i.path === match[2]);
|
|
29
|
-
if (already) continue;
|
|
30
|
-
imports.push({
|
|
31
|
-
type: 'default',
|
|
32
|
-
name: match[1],
|
|
33
|
-
path: match[2]
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
21
|
return imports;
|
|
38
22
|
}
|
|
39
23
|
|
|
@@ -61,19 +45,12 @@ function transform(code) {
|
|
|
61
45
|
});
|
|
62
46
|
|
|
63
47
|
// 3. import ā require
|
|
64
|
-
code = code
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
)
|
|
71
|
-
.replace(
|
|
72
|
-
/import\s+(\w+)\s+from\s+['"](.+?)['"]/g,
|
|
73
|
-
(match, name, path) => {
|
|
74
|
-
return `const ${name} = require('${path}')`;
|
|
75
|
-
}
|
|
76
|
-
);
|
|
48
|
+
code = code.replace(
|
|
49
|
+
/import\s+{([^}]+)}\s+from\s+['"](.+?)['"]/g,
|
|
50
|
+
(match, names, path) => {
|
|
51
|
+
return `const { ${names} } = require('${path}')`;
|
|
52
|
+
}
|
|
53
|
+
);
|
|
77
54
|
|
|
78
55
|
// 4. Š“Š¾Š“Š°ŃŠ¼Š¾ exports
|
|
79
56
|
if (exports.length) {
|
|
@@ -171,4 +148,4 @@ ${mod.id}: [
|
|
|
171
148
|
require(${entryModule.id});
|
|
172
149
|
})({${modulesCode}});
|
|
173
150
|
`;
|
|
174
|
-
}
|
|
151
|
+
}
|
package/dev-server.js
CHANGED
|
@@ -71,7 +71,6 @@ function serveFile(req, res) {
|
|
|
71
71
|
res.writeHead(200, { 'Content-Type': contentType });
|
|
72
72
|
res.end(content);
|
|
73
73
|
}
|
|
74
|
-
|
|
75
74
|
// ======================
|
|
76
75
|
// š RELOAD CHANNEL (SSE)
|
|
77
76
|
// ======================
|
|
@@ -112,14 +111,12 @@ const server = http.createServer((req, res) => {
|
|
|
112
111
|
// ā¶ļø START
|
|
113
112
|
// ======================
|
|
114
113
|
|
|
115
|
-
function runServer({ isProd }
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
build({ watch, isProd });
|
|
114
|
+
function runServer({ isProd }) {
|
|
115
|
+
build({ watch: true, isProd })
|
|
119
116
|
|
|
120
117
|
server.listen(PORT, () => {
|
|
121
118
|
console.log(`\nš Dev server running: http://localhost:${PORT}\n`);
|
|
122
119
|
});
|
|
123
120
|
}
|
|
124
121
|
|
|
125
|
-
export { build, runServer };
|
|
122
|
+
export { build, runServer };
|
package/index.js
CHANGED
|
@@ -12,21 +12,10 @@ const ROOT = process.cwd();
|
|
|
12
12
|
const SRC_DIR = path.join(ROOT, 'src');
|
|
13
13
|
const DIST_DIR = path.join(ROOT, 'dist');
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
const envPath = path.join(ROOT, 'env.js');
|
|
17
|
-
if (fs.existsSync(envPath)) {
|
|
18
|
-
const envCode = fs.readFileSync(envPath, 'utf-8');
|
|
19
|
-
const match = envCode.match(/IN_POINT\s*=\s*["']([^"']+)["']/);
|
|
20
|
-
if (match) {
|
|
21
|
-
return path.join(SRC_DIR, `${match[1]}.js`);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return path.join(SRC_DIR, 'index.js');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const ENTRY = resolveEntry();
|
|
15
|
+
const ENTRY = path.join(SRC_DIR, `index.js`);
|
|
28
16
|
const HTML_INPUT = path.join(SRC_DIR, 'index.html');
|
|
29
17
|
const HTML_OUTPUT = path.join(DIST_DIR, 'index.html');
|
|
18
|
+
const isWatch = process.argv.includes('--watch');
|
|
30
19
|
|
|
31
20
|
// ======================
|
|
32
21
|
// š§¹ UTILS
|
|
@@ -48,10 +37,12 @@ function cleanDist() {
|
|
|
48
37
|
// š¦ BUILD JS
|
|
49
38
|
// ======================
|
|
50
39
|
function buildJS(isProd) {
|
|
51
|
-
const
|
|
52
|
-
const result = isProd ? minifyJS(bundle_result) : bundle_result;
|
|
40
|
+
const files = fs.readdirSync(SRC_DIR).filter(f => f.endsWith('.js'));
|
|
53
41
|
|
|
54
|
-
|
|
42
|
+
const bundle_result = bundle('./src/app.js');
|
|
43
|
+
const result = isProd ? minifyJS(bundle_result) : bundle_result;
|
|
44
|
+
|
|
45
|
+
fs.writeFileSync('./dist/bundle.min.js', result);
|
|
55
46
|
}
|
|
56
47
|
|
|
57
48
|
function buildCSS(isProd) {
|
|
@@ -83,10 +74,10 @@ function buildHTML(isProd) {
|
|
|
83
74
|
let html = fs.readFileSync(HTML_INPUT, 'utf-8');
|
|
84
75
|
|
|
85
76
|
if (isProd) {
|
|
86
|
-
// JS
|
|
87
|
-
html = html.replace(
|
|
77
|
+
// JS
|
|
78
|
+
html = html.replace(/src="(.+?)\.js"/g, (match, p1) => {
|
|
88
79
|
if (p1.endsWith('.min')) return match;
|
|
89
|
-
return
|
|
80
|
+
return `src="bundle.min.js"`;
|
|
90
81
|
});
|
|
91
82
|
|
|
92
83
|
// CSS
|
|
@@ -108,41 +99,34 @@ function buildHTML(isProd) {
|
|
|
108
99
|
function watchBuild() {
|
|
109
100
|
console.log('š Watch mode ON...\n');
|
|
110
101
|
|
|
111
|
-
// First build
|
|
112
|
-
buildTask(false);
|
|
113
|
-
|
|
114
|
-
// Then watch
|
|
115
102
|
fs.watch(SRC_DIR, { recursive: true }, (eventType, filename) => {
|
|
116
103
|
console.log(`š File changed: ${filename}`);
|
|
117
|
-
|
|
104
|
+
build();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
fs.watch(HTML_INPUT, () => {
|
|
108
|
+
console.log(`š HTML changed`);
|
|
109
|
+
build();
|
|
118
110
|
});
|
|
119
111
|
}
|
|
120
112
|
|
|
121
113
|
// ======================
|
|
122
|
-
// š BUILD
|
|
114
|
+
// š BUILD
|
|
123
115
|
// ======================
|
|
124
116
|
|
|
125
|
-
function
|
|
126
|
-
|
|
117
|
+
export function build({ watch, isProd }) {
|
|
118
|
+
if (!!watch) {
|
|
119
|
+
return watchBuild();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log(`\nš Build started (${isProd} ? 'production' : 'development')`);
|
|
127
123
|
|
|
128
124
|
cleanDist();
|
|
129
125
|
ensureDir(DIST_DIR);
|
|
130
126
|
|
|
131
|
-
|
|
132
|
-
buildCSS(isProd);
|
|
127
|
+
devbuildJS(isProd);
|
|
128
|
+
buildCSS(isProd);
|
|
133
129
|
buildHTML(isProd);
|
|
134
130
|
|
|
135
131
|
console.log(`š Build finished\n`);
|
|
136
132
|
}
|
|
137
|
-
|
|
138
|
-
// ======================
|
|
139
|
-
// š BUILD (public API)
|
|
140
|
-
// ======================
|
|
141
|
-
|
|
142
|
-
export function build({ watch, isProd } = {}) {
|
|
143
|
-
if (watch) {
|
|
144
|
-
return watchBuild();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
buildTask(isProd);
|
|
148
|
-
}
|
package/minify-css.js
CHANGED
package/minify-js.js
CHANGED
package/package.json
CHANGED
|
@@ -1,25 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@m00rl0ck/simple-builder",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.9",
|
|
4
4
|
"description": "Zero-dependency JS and CSS bundler with dev server and minifier",
|
|
5
|
-
"changelog": {
|
|
6
|
-
"1.1.1": "Fix: small bugs",
|
|
7
|
-
"1.1.2": "Fix: ternary console.log",
|
|
8
|
-
"1.1.3": "Fix: crash on build() without args, unused vars, hardcoded entry, watchBuild initial build, default imports support"
|
|
9
|
-
},
|
|
10
5
|
"type": "module",
|
|
11
6
|
"main": "index.js",
|
|
12
7
|
"bin": {
|
|
13
|
-
"simple-builder": "bin/cli.js"
|
|
8
|
+
"simple-builder": "./bin/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"dev": "node dev-server.js",
|
|
12
|
+
"build": "node bundler.js"
|
|
14
13
|
},
|
|
15
|
-
"files": [
|
|
16
|
-
"bundler.js",
|
|
17
|
-
"dev-server.js",
|
|
18
|
-
"index.js",
|
|
19
|
-
"minify-js.js",
|
|
20
|
-
"minify-css.js",
|
|
21
|
-
"bin/cli.js"
|
|
22
|
-
],
|
|
23
14
|
"keywords": [
|
|
24
15
|
"bundler",
|
|
25
16
|
"javascript",
|
|
@@ -27,6 +18,13 @@
|
|
|
27
18
|
"zero-deps",
|
|
28
19
|
"minifier"
|
|
29
20
|
],
|
|
30
|
-
"author": "
|
|
31
|
-
"license": "MIT"
|
|
32
|
-
|
|
21
|
+
"author": "m00rlIOck",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"files": [
|
|
24
|
+
"bundler.js",
|
|
25
|
+
"dev-server.js",
|
|
26
|
+
"index.js",
|
|
27
|
+
"minify-js.js",
|
|
28
|
+
"minify-css.js"
|
|
29
|
+
]
|
|
30
|
+
}
|