|
| 1 | +const _ = require('lodash'); |
| 2 | +const yargs = require('yargs'); |
| 3 | +const webpack = require('webpack'); |
| 4 | +const nodeExternals = require('webpack-node-externals'); |
| 5 | +const MemoryFileSystem = require('memory-fs'); |
| 6 | +const { resolve } = require('path'); |
| 7 | + |
| 8 | +const componentExternals = _.curry((entry, context, request, done) => { |
| 9 | + if (request !== entry && !request.includes('!')) { |
| 10 | + done(null, `commonjs ${resolve(context, request)}`); |
| 11 | + } else { |
| 12 | + done(); |
| 13 | + } |
| 14 | +}); |
| 15 | + |
| 16 | +const run = (config, entry) => { |
| 17 | + const filename = 'output.js'; |
| 18 | + const conf = Object.assign({}, config, { |
| 19 | + entry, |
| 20 | + output: { |
| 21 | + libraryTarget: 'commonjs', |
| 22 | + filename, |
| 23 | + }, |
| 24 | + target: 'node', |
| 25 | + // Externalize everything except the entry. |
| 26 | + externals: [nodeExternals(), componentExternals(entry)], |
| 27 | + // Make sure Vue thinks its running in a "server" |
| 28 | + plugins: [ |
| 29 | + new webpack.DefinePlugin({ |
| 30 | + 'process.env.VUE_ENV': JSON.stringify('server'), |
| 31 | + }), |
| 32 | + ].concat(config.plugins || []), |
| 33 | + }); |
| 34 | + const compiler = webpack(conf); |
| 35 | + const fs = new MemoryFileSystem(); |
| 36 | + compiler.outputFileSystem = fs; |
| 37 | + compiler.run((err, stats) => { |
| 38 | + if (err) throw err; |
| 39 | + fs.createReadStream(stats.compilation.assets[filename].existsAt, 'utf8').pipe(process.stdout); |
| 40 | + }); |
| 41 | +}; |
| 42 | + |
| 43 | +const args = yargs.argv._; |
| 44 | + |
| 45 | +run(require(args[0]), args[1]); // eslint-disable-line import/no-dynamic-require |
0 commit comments