Skip to content

Commit a5aca5a

Browse files
committed
bundlers/webpack
1 parent 8b42493 commit a5aca5a

File tree

8 files changed

+357
-3
lines changed

8 files changed

+357
-3
lines changed

.bit.map.json

+43-1
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,47 @@
247247
"mainFile": "src/bundlers/vue/index.js",
248248
"origin": "AUTHORED"
249249
},
250-
"version": "0.12.6"
250+
"bit.envs/bundlers/[email protected]": {
251+
"files": [
252+
{
253+
"relativePath": "src/bundlers/webpack/index.js",
254+
"test": false,
255+
"name": "index.js"
256+
},
257+
{
258+
"relativePath": "src/bundlers/webpack/readme.md",
259+
"test": false,
260+
"name": "readme.md"
261+
},
262+
{
263+
"relativePath": "src/bundlers/webpack/webpack.config.js",
264+
"test": false,
265+
"name": "webpack.config.js"
266+
}
267+
],
268+
"mainFile": "src/bundlers/webpack/index.js",
269+
"origin": "AUTHORED"
270+
},
271+
"bit.envs/bundlers/[email protected]": {
272+
"files": [
273+
{
274+
"relativePath": "src/bundlers/webpack-css-modules/index.js",
275+
"test": false,
276+
"name": "index.js"
277+
},
278+
{
279+
"relativePath": "src/bundlers/webpack-css-modules/readme.md",
280+
"test": false,
281+
"name": "readme.md"
282+
},
283+
{
284+
"relativePath": "src/bundlers/webpack-css-modules/webpack.config.js",
285+
"test": false,
286+
"name": "webpack.config.js"
287+
}
288+
],
289+
"mainFile": "src/bundlers/webpack-css-modules/index.js",
290+
"origin": "AUTHORED"
291+
},
292+
"version": "0.12.8-dev.1"
251293
}

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@
5757
"sass-loader": "^6.0.6",
5858
"sinon": "^3.2.1",
5959
"sinon-chai": "^2.13.0",
60+
"style-loader": "^0.20.2",
6061
"typescript": "^2.4.2",
6162
"uglifyjs-webpack-plugin": "^1.2.0",
6263
"vinyl": "^2.1.0",
6364
"vinyl-file": "^3.0.0",
6465
"vue-loader": "^14.1.1",
6566
"vue-style-loader": "^4.0.2",
6667
"vue-template-compiler": "^2.5.13",
67-
"webpack": "^3.11.0",
68+
"webpack": "^4.0.1",
6869
"webpack-node-externals": "^1.6.0"
69-
}
70+
},
71+
"workspaces": []
7072
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Vinyl from 'vinyl';
2+
import path from 'path';
3+
import webpack from 'webpack';
4+
import MemoryFS from 'memory-fs';
5+
import configure from './webpack.config';
6+
7+
export const compile = (files, distPath, componentObject) => {
8+
const mainFile = files.find(file => file.relative === componentObject.mainFile);
9+
const testFiles = files.filter(file => file.test);
10+
11+
return runWebpack(mainFile, testFiles, distPath);
12+
}
13+
14+
const runWebpack = (mainFile, testFiles, distPath) => {
15+
const conf = getConfig(mainFile, testFiles, distPath);
16+
const compiler = webpack(conf);
17+
18+
const fs = new MemoryFS();
19+
compiler.outputFileSystem = fs;
20+
21+
const compilationPromise = new Promise(function (resolve, reject) {
22+
return compiler.run(function (err, stats) {
23+
if (err || 0 < stats.compilation.errors.length) {
24+
reject(err || stats.compilation.errors);
25+
return;
26+
}
27+
28+
resolve(stats.compilation.assets);
29+
});
30+
});
31+
32+
return compilationPromise.then((assets) => extractFiles(fs, mainFile, testFiles, distPath, assets));
33+
}
34+
35+
const extractFiles = (fileSystem, mainFile, testFiles, distPath, assets) => {
36+
const distPathFiles = fileSystem.readdirSync(distPath);
37+
38+
return Object.keys(assets).map(assetName => {
39+
const asset = assets[assetName];
40+
const distFullPath = asset.existsAt;
41+
return new Vinyl({
42+
contents: fileSystem.readFileSync(asset.existsAt),
43+
base: distPath,
44+
path: distFullPath,
45+
basename: assetName,
46+
test:testFiles.find(t => t.basename === assetName) ? true : false
47+
});
48+
});
49+
}
50+
51+
const getConfig = (mainFile, testFiles, distPath) => {
52+
const conf = configure();
53+
54+
const ext = {
55+
context: __dirname,
56+
output: {
57+
path: path.join(distPath, path.dirname(mainFile.relative)),
58+
},
59+
entry: {
60+
[mainFile.stem] : mainFile.path
61+
}
62+
};
63+
64+
testFiles.forEach(t => ext.entry[t.stem] = [t.path]);
65+
66+
return Object.assign(conf, ext);
67+
};
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Webpack bundler
2+
3+
A component [compiler](https://docs.bitsrc.io/docs/ext-compiling.html) that bundles the component using [webpack](https://webpack.js.org/) v4.0.1.
4+
5+
## What is bundled?
6+
The bundler bundles the component's main file and test files.
7+
In case there are component files that are not in those files' dependency trees, they won't be bundled. Note that this is in accordance with webpack's usual behavior, but not in accordance with the usual behavior of Bit compilers, which usually compile all the files.
8+
9+
## How to use?
10+
11+
Import the environment.
12+
```bash
13+
bit import bit.envs/bundlers/webpack -c
14+
```
15+
16+
Then build using [bit build](https://docs.bitsrc.io/docs/cli-build.html).
17+
```bash
18+
bit build
19+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const babelPresetLatest = require('babel-preset-latest');
2+
const babelPresetReact = require('babel-preset-react');
3+
4+
//indirect
5+
require('babel-loader');
6+
require('babel-core');
7+
require('style-loader');
8+
require('css-loader');
9+
require('sass-loader');
10+
require('node-sass');
11+
12+
const nodeExternals = require('webpack-node-externals');
13+
const PACKAGE_TYPE = 'umd';
14+
15+
const configure = () => {
16+
return {
17+
output: {
18+
filename: '[name].js',
19+
libraryTarget: PACKAGE_TYPE,
20+
},
21+
module: {
22+
rules: [{
23+
test: /.(js|jsx)$/,
24+
loader: 'babel-loader',
25+
options: {
26+
babelrc: false,
27+
presets: [babelPresetLatest, babelPresetReact]
28+
}
29+
}, {
30+
test: /\.(scss|css)$/,
31+
use: [{
32+
loader: "style-loader" // creates style nodes from JS strings
33+
}, {
34+
loader: "css-loader", // translates CSS into CommonJS
35+
options: {
36+
import: true,
37+
modules: true,
38+
}
39+
}, {
40+
loader: "sass-loader" // compiles Sass to CSS
41+
}]
42+
}, {
43+
test: /\.less$/,
44+
use: [{
45+
loader: "style-loader" // creates style nodes from JS strings
46+
}, {
47+
loader: "css-loader", // translates CSS into CommonJS
48+
options: {
49+
import: true,
50+
modules: true,
51+
}
52+
}, {
53+
loader: "less-loader" // compiles Less to CSS
54+
}]
55+
},
56+
// JSON is not enabled by default in Webpack but both Node and Browserify
57+
// allow it implicitly so we also enable it.
58+
{
59+
test: /\.json$/,
60+
loader: 'json-loader'
61+
}]
62+
},
63+
externals: [ nodeExternals({
64+
importType: PACKAGE_TYPE
65+
}) ]
66+
};
67+
};
68+
69+
export default configure;

src/bundlers/webpack/index.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Vinyl from 'vinyl';
2+
import path from 'path';
3+
import webpack from 'webpack';
4+
import MemoryFS from 'memory-fs';
5+
import configure from './webpack.config';
6+
7+
export const compile = (files, distPath, componentObject) => {
8+
const mainFile = files.find(file => file.relative === componentObject.mainFile);
9+
const testFiles = files.filter(file => file.test);
10+
11+
return runWebpack(mainFile, testFiles, distPath);
12+
}
13+
14+
const runWebpack = (mainFile, testFiles, distPath) => {
15+
const conf = getConfig(mainFile, testFiles, distPath);
16+
const compiler = webpack(conf);
17+
18+
const fs = new MemoryFS();
19+
compiler.outputFileSystem = fs;
20+
21+
const compilationPromise = new Promise(function (resolve, reject) {
22+
return compiler.run(function (err, stats) {
23+
if (err || 0 < stats.compilation.errors.length) {
24+
reject(err || stats.compilation.errors);
25+
return;
26+
}
27+
28+
resolve(stats.compilation.assets);
29+
});
30+
});
31+
32+
return compilationPromise.then((assets) => extractFiles(fs, mainFile, testFiles, distPath, assets));
33+
}
34+
35+
const extractFiles = (fileSystem, mainFile, testFiles, distPath, assets) => {
36+
const distPathFiles = fileSystem.readdirSync(distPath);
37+
38+
return Object.keys(assets).map(assetName => {
39+
const asset = assets[assetName];
40+
const distFullPath = asset.existsAt;
41+
return new Vinyl({
42+
contents: fileSystem.readFileSync(asset.existsAt),
43+
base: distPath,
44+
path: distFullPath,
45+
basename: assetName,
46+
test:testFiles.find(t => t.basename === assetName) ? true : false
47+
});
48+
});
49+
}
50+
51+
const getConfig = (mainFile, testFiles, distPath) => {
52+
const conf = configure();
53+
54+
const ext = {
55+
context: __dirname,
56+
output: {
57+
path: path.join(distPath, path.dirname(mainFile.relative)),
58+
},
59+
entry: {
60+
[mainFile.stem] : mainFile.path
61+
}
62+
};
63+
64+
testFiles.forEach(t => ext.entry[t.stem] = [t.path]);
65+
66+
return Object.assign(conf, ext);
67+
};

src/bundlers/webpack/readme.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Webpack bundler
2+
3+
A component [compiler](https://docs.bitsrc.io/docs/ext-compiling.html) that bundles the component using [webpack](https://webpack.js.org/) v4.0.1.
4+
5+
## What is bundled?
6+
The bundler bundles the component's main file and test files.
7+
In case there are component files that are not in those files' dependency trees, they won't be bundled. Note that this is in accordance with webpack's usual behavior, but not in accordance with the usual behavior of Bit compilers, which usually compile all the files.
8+
9+
## How to use?
10+
11+
Import the environment.
12+
```bash
13+
bit import bit.envs/bundlers/webpack -c
14+
```
15+
16+
Then build using [bit build](https://docs.bitsrc.io/docs/cli-build.html).
17+
```bash
18+
bit build
19+
```
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const babelPresetLatest = require('babel-preset-latest');
2+
const babelPresetReact = require('babel-preset-react');
3+
4+
//indirect
5+
require('babel-loader');
6+
require('babel-core');
7+
require('style-loader');
8+
require('css-loader');
9+
require('sass-loader');
10+
require('node-sass');
11+
12+
const nodeExternals = require('webpack-node-externals');
13+
const PACKAGE_TYPE = 'umd';
14+
15+
const configure = () => {
16+
return {
17+
output: {
18+
filename: '[name].js',
19+
libraryTarget: PACKAGE_TYPE,
20+
},
21+
module: {
22+
rules: [{
23+
test: /.(js|jsx)$/,
24+
loader: 'babel-loader',
25+
options: {
26+
babelrc: false,
27+
presets: [babelPresetLatest, babelPresetReact]
28+
}
29+
}, {
30+
test: /\.(scss|css)$/,
31+
use: [{
32+
loader: "style-loader" // creates style nodes from JS strings
33+
}, {
34+
loader: "css-loader", // translates CSS into CommonJS
35+
options: {
36+
import: true,
37+
modules: false,
38+
}
39+
}, {
40+
loader: "sass-loader" // compiles Sass to CSS
41+
}]
42+
}, {
43+
test: /\.less$/,
44+
use: [{
45+
loader: "style-loader" // creates style nodes from JS strings
46+
}, {
47+
loader: "css-loader", // translates CSS into CommonJS
48+
options: {
49+
import: true,
50+
modules: false,
51+
}
52+
}, {
53+
loader: "less-loader" // compiles Less to CSS
54+
}]
55+
},
56+
// JSON is not enabled by default in Webpack but both Node and Browserify
57+
// allow it implicitly so we also enable it.
58+
{
59+
test: /\.json$/,
60+
loader: 'json-loader'
61+
}]
62+
},
63+
externals: [ nodeExternals({
64+
importType: PACKAGE_TYPE
65+
}) ]
66+
};
67+
};
68+
69+
export default configure;

0 commit comments

Comments
 (0)