We’re excited to announce Bit’s new home at bit.dev!Learn more

webpack

v0.0.13arrow_drop_down
v0.0.13
v0.0.12
v0.0.11
v0.0.10
v0.0.9
v0.0.8
v0.0.7
v0.0.6
v0.0.5
v0.0.4
v0.0.3
v0.0.2
v0.0.1
STATUS
Passing
DOWNLOADS
949
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
A year ago
subject
Compiles and bundles components using webpack.
4 contributors
Like
Use webpack in your project ?
Copied
npm i @bit/bit.envs.bundlers.webpack
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
Files
index.js
93 Lines(75 sloc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import Vinyl from 'vinyl';
import path from 'path';
import webpack from 'webpack';
import MemoryFS from 'memory-fs';
import configure from './webpack.config';

const compiledFileTypes = ['js', 'jsx', 'ts', 'tsx'];
 
function compile(files, distRootPath, context) {
  let mainFile = files.find(file => file.relative === context.componentObject.mainFile);
  const testFiles = files.filter(x => x.test);
  const toCompile = _toCompile(mainFile);
  if (toCompile) {
    return runWebpack(mainFile, distRootPath, testFiles);
  } 
  const distsFiles = files.map(file => {
    return _getDistFile(file, distRootPath)
  })

  return distsFiles;
}

function _getDistFile(file, distPath, content){
  let distFile = file.clone();
  distFile.base = distPath;
  distFile.path = path.join(distPath, file.relative);

  if (content) {
    distFile.contents = content;
  }

  return distFile;
}

function _toCompile(file) {
  return compiledFileTypes.indexOf(file.extname.replace('.','')) > -1;
}

function runWebpack(mainFile, distRootPath, testFiles) {
  var conf = getConfig(mainFile, distRootPath, testFiles);
  var compiler = webpack(conf);
  
  var fs = new MemoryFS();
  compiler.outputFileSystem = fs;
  
  var compilationPromise = new Promise(function (resolve, reject) {
    return compiler.run(function (err, stats) {
      if (err || 0 < stats.compilation.errors.length) {
        console.log(err || stats.compilation.errors)
        reject(err || stats.compilation.errors);
        return;
      }
      
      resolve(stats.compilation.assets);
    });
  });
  
  return compilationPromise.then((assets) => extractFiles(fs, mainFile, distRootPath, testFiles, assets));
}

function extractFiles(fileSystem, mainFile, distRootPath, testFiles, assets) {
  var distPathFiles = fileSystem.readdirSync(distRootPath);
  
  return Object.keys(assets).map(assetName => {
    const asset = assets[assetName];
    const distFullPath = asset.existsAt;
    return new Vinyl({
      contents: fileSystem.readFileSync(asset.existsAt),
      base: distRootPath,
      path: distFullPath,
      basename: assetName,
      test:testFiles.find(x => x.basename === assetName ) ? true : false
    });
  })
}

function getConfig(mainFile, distRootPath, testFiles) {
  const conf = configure();
  conf.context = __dirname;
  conf.output.path = path.join(distRootPath, path.dirname(mainFile.relative));
  conf.entry = {
    [mainFile.stem] : mainFile.path
  }
  testFiles.forEach(x=>conf.entry[x.stem] = [x.path])
  conf.library;
  
  return conf;
};


module.exports = {
  compile
};