Skip to content

Commit 7ad6e51

Browse files
committed
feature: @putout/bundler: add
0 parents  commit 7ad6e51

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1024
-0
lines changed

.eslintrc.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": [
3+
"plugin:n/recommended",
4+
"plugin:putout/safe+align"
5+
],
6+
"plugins": [
7+
"putout",
8+
"n"
9+
],
10+
"ignorePatterns": ["**/fixture/*.*"]
11+
}

.github/FUNDING.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github: coderaiser
2+
patreon: coderaiser
3+
open_collective: cloudcmd
4+
ko_fi: coderaiser

.github/ISSUE_TEMPLATE.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--
2+
Thank you for reporting an issue. Please fill in the template below. If unsure
3+
about something, just do as best as you're able.
4+
-->
5+
6+
- **Version** (`npm -v`):
7+
- **Node Version** `node -v`:
8+
- **OS** (`uname -a` on Linux):
9+
- **Browser name/version**:
10+
- **Used Command Line Parameters**:

.github/PULL_REQUEST_TEMPLATE.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--
2+
Thank you for making pull request. Please fill in the template below. If unsure
3+
about something, just do as best as you're able.
4+
-->
5+
6+
- [ ] commit message named according to [Contributing Guide](https://github.com/coderaiser/cloudcmd/blob/master/CONTRIBUTING.md "Contributting Guide")
7+
- [ ] `npm run codestyle` is OK
8+
- [ ] `npm test` is OK

.github/workflows/nodejs.yml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Node CI
2+
on:
3+
- push
4+
- pull_request
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
env:
9+
NAME: '@putout/bundler'
10+
strategy:
11+
matrix:
12+
node-version:
13+
- 18.x
14+
- 20.x
15+
- 21.x
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: oven-sh/setup-bun@v1
19+
with:
20+
bun-version: latest
21+
- name: Use Node.js ${{ matrix.node-version }}
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: ${{ matrix.node-version }}
25+
- name: Install Redrun
26+
run: bun i redrun -g --no-save
27+
- name: Install
28+
run: bun i --no-save
29+
- name: Lint
30+
run: redrun fix:lint
31+
- name: Install Rust
32+
run: rustup update
33+
- uses: actions/cache@v3
34+
with:
35+
path: |
36+
~/.cargo/bin/
37+
~/.cargo/registry/index/
38+
~/.cargo/registry/cache/
39+
~/.cargo/git/db/
40+
target/
41+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
42+
- name: Typos Install
43+
run: cargo install typos-cli || echo 'already installed'
44+
- name: Typos
45+
run: typos --write-changes
46+
- name: Commit fixes
47+
uses: EndBug/add-and-commit@v9
48+
continue-on-error: true
49+
with:
50+
message: "chore: ${{ env.NAME }}: actions: lint ☘️"
51+
- name: Coverage
52+
run: redrun coverage report
53+
- name: Coveralls
54+
uses: coverallsapp/github-action@v2
55+
continue-on-error: true
56+
with:
57+
github-token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
*.swp
3+
yarn-error.log
4+
5+
coverage
6+
.idea
7+
.filesystem.json
8+
filesystem.red
9+
bundle.js

.madrun.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
run,
3+
cutEnv,
4+
} from 'madrun';
5+
6+
const env = {};
7+
8+
export default {
9+
'wisdom': () => run('lint'),
10+
'test': () => [env, `tape 'lib/**/*.spec.js' test/*.js 'rules/**/*.spec.js'`],
11+
'test:dts': () => 'check-dts test/*.ts',
12+
'watch:test': async () => [env, `nodemon -w lib -w test -x ${await cutEnv('test')}`],
13+
'lint': () => `putout .`,
14+
'fresh:lint': () => run('lint', '--fresh'),
15+
'lint:fresh': () => run('lint', '--fresh'),
16+
'fix:lint': () => run('lint', '--fix'),
17+
'coverage': async () => [env, `c8 ${await cutEnv('test')}`],
18+
'coverage:html': async () => [env, `c8 --reporter=lcov ${await cutEnv('test')}`],
19+
'report': () => 'c8 report --reporter=lcov',
20+
};

.npmignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.*
2+
*.spec.js
3+
test
4+
yarn-error.log
5+
6+
coverage
7+
fixture
8+
fixture.js
9+
rules
10+
filesystem.red

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.nycrc.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"check-coverage": true,
3+
"all": true,
4+
"exclude": [
5+
"**/*.spec.*",
6+
"**/fixture",
7+
"test",
8+
"**/debug.js",
9+
"**/dialog.js",
10+
"**/coverage",
11+
"**/.*.*",
12+
"**/*.d.ts",
13+
"**/fixture.js",
14+
"**/bin",
15+
"**/choose.js",
16+
"**/help.js",
17+
"**/master.js",
18+
"**/slave.js",
19+
"**/run.js",
20+
"**/spinner.js",
21+
"**/menu.js",
22+
"**/lint",
23+
"**/convert/index.js",
24+
"**/version.js",
25+
"**/example",
26+
"**/bundle/bundle.js"
27+
],
28+
"branches": 100,
29+
"lines": 100,
30+
"functions": 100,
31+
"statements": 100
32+
}

.putout.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"printer": "putout",
3+
"rules": {
4+
"filesystem/remove-files": ["on", {
5+
"names": ["coverage"]
6+
}],
7+
"remove-console": "off",
8+
"putout": "on"
9+
},
10+
"ignore": [
11+
"!**/coverage",
12+
"bundle.spec.js"
13+
]
14+
}

.typos.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[files]
2+
extend-exclude= ["ChangeLog"]

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) coderaiser
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Bundle [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]
2+
3+
[NPMURL]: https://npmjs.org/package/@putout/bundler "npm"
4+
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/bundler.svg?style=flat&longCache=true
5+
[BuildStatusURL]: https://github.com/putoutjs/printer/actions/workflows/nodejs.yml "Build Status"
6+
[BuildStatusIMGURL]: https://github.com/putoutjs/printer/actions/workflows/nodejs.yml/badge.svg
7+
[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License"
8+
[LicenseIMGURL]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat
9+
[CoverageURL]: https://coveralls.io/github/putoutjs/printer?branch=master
10+
[CoverageIMGURL]: https://coveralls.io/repos/putoutjs/printer/badge.svg?branch=master&service=github
11+
12+
## Install
13+
14+
```
15+
npm i @putout/bundle
16+
```
17+
18+
## API
19+
20+
```js
21+
import {bundle} from '@putout/bundler';
22+
23+
console.log(bundle(CWD, entry, filesystem));
24+
```
25+
26+
## Convert ESM to CommonJS
27+
28+
To Simplify things up all files converted to CommonJS first.
29+
Let's suppose none of them use top-level await to get things simpler.
30+
31+
## Parse filenames
32+
33+
Traverse all files starting from `entry` and get all filenames.
34+
35+
- [`parse-require`](https://putout.cloudcmd.io/#/gist/d973366be6b07ab705b5c9d793369904/ca8b6b15fa953d95f57b42e07136c65791f38ca1);
36+
- [`parse-filenames`](https://putout.cloudcmd.io/#/gist/d973366be6b07ab705b5c9d793369904/3067150ad161029e75b95e9bfff290e03953ef41);
37+
- [`resolve-filenames`](https://putout.cloudcmd.io/#/gist/8ca1ac9b5fb4d1a47d185836c3f0b393/edf99b8064fe0faf4545aa0cc66138a7fa34c557);
38+
- [`resolve-require`](https://putout.cloudcmd.io/#/gist/833539f66cb238fcc3b6ca6cee61ef9e/79a068c96b686bb0eacdf3f570d532981499b114);
39+
- [`bundle-files`](https://putout.cloudcmd.io/#/gist/7dd3bffa8e88f7542c84065f622b63d7/3b1e68e0babc3a72af947076ed9801c0034a096e);
40+
41+
## Bundle all files to object
42+
43+
Traverse filesystem and create object that contains filename and file content:
44+
45+
```js
46+
const __filesystem = {
47+
'/entry.js': () => {
48+
const client = require('/client.js');
49+
console.log(client);
50+
},
51+
'/client.js': (exports, require, module) => {
52+
module.exports = 'hello';
53+
},
54+
};
55+
```
56+
57+
## IIFE
58+
59+
Most likely we need IIFE so couple bundles can be loaded on page simultaneously.
60+
61+
## Result Example
62+
63+
```js
64+
const __modules = {};
65+
const __filesystem = {
66+
'/entry.js': () => {
67+
const client = require('/client.js');
68+
console.log(client);
69+
},
70+
'/client.js': (exports, require, module) => {
71+
module.exports = 'hello';
72+
},
73+
};
74+
75+
const require = (name) => {
76+
const exports = {};
77+
const module = {
78+
exports,
79+
};
80+
81+
if (__modules[name])
82+
return __modules[name];
83+
84+
__filesystem[name](exports, require, module);
85+
__modules[name] = module.exports;
86+
87+
return module.exports;
88+
};
89+
90+
require('/entry.js');
91+
```
92+
93+
## License
94+
95+
MIT

example/.putout.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"nodejs/convert-commonjs-to-esm": "off"
4+
}
5+
}

example/entry.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const one = require("./one.js");
2+
console.log(one);

example/one.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "hello";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(() => {
2+
const __filesystem = {
3+
'/index.js': (exports, require, module) => {
4+
require('./send.js');
5+
},
6+
'/send.js': (exports, require, module) => {
7+
module.exports = 'hello';
8+
},
9+
};
10+
const __modules = {};
11+
const require = (name) => {
12+
const exports = {};
13+
const module = {
14+
exports,
15+
};
16+
if (__modules[name])
17+
return __modules[name];
18+
19+
__filesystem[name](exports, require, module);
20+
__modules[name] = module.exports;
21+
return module.exports;
22+
};
23+
24+
require('/index.js');
25+
})();
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
__putout_processor_filesystem({
2+
"type": "directory",
3+
"filename": "/",
4+
"files": [{
5+
"filename": "/index.js",
6+
"type": "file",
7+
"content": "require('./send.js');",
8+
}, {
9+
"filename": "/send.js",
10+
"type": "file",
11+
"content": "module.exports = 'hello';",
12+
}],
13+
});
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
__putout_processor_filesystem({
2+
"type": "directory",
3+
"filename": "/",
4+
"files": [{
5+
"filename": "/index.js",
6+
"type": "file",
7+
"content": "require('./send.js');",
8+
}, {
9+
"filename": "/send.js",
10+
"type": "file",
11+
"content": "module.exports = 'hello';",
12+
}],
13+
});

0 commit comments

Comments
 (0)