mirror of
https://github.com/c8r/x0.git
synced 2024-09-11 21:57:26 +03:00
Add bundler for node
This commit is contained in:
parent
fb35ce31c9
commit
0fa3cc01de
10
docs/webpack.config.js
Normal file
10
docs/webpack.config.js
Normal file
@ -0,0 +1,10 @@
|
||||
module.exports = {
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.md$/,
|
||||
use: 'raw-loader'
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@ const config = {
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.js?$/,
|
||||
test: /\.js$/,
|
||||
exclude: /node_modules/,
|
||||
use: {
|
||||
loader: require.resolve('babel-loader'),
|
||||
|
@ -22,6 +22,7 @@ const { renderToString, renderToStaticMarkup } = require('react-dom/server')
|
||||
const Loadable = require('react-loadable')
|
||||
const { getBundles } = require('react-loadable/webpack')
|
||||
|
||||
const getWebpackApp = require('./webpackNodeApp')
|
||||
const client = require('./client')
|
||||
const getCSS = require('./getCSS')
|
||||
|
||||
@ -109,9 +110,17 @@ const writePage = async (Component, options) => {
|
||||
return html
|
||||
}
|
||||
|
||||
const getApp = async (filename, options) => {
|
||||
if (!options.config) {
|
||||
const req = require(filename)
|
||||
return req.default || req
|
||||
}
|
||||
const webpackApp = await getWebpackApp(filename, options)
|
||||
return webpackApp
|
||||
}
|
||||
|
||||
const createStatic = async (filename, baseOptions) => {
|
||||
const req = require(filename)
|
||||
const Component = req.default || req
|
||||
const Component = await getApp(filename, baseOptions)
|
||||
|
||||
const options = Object.assign({}, Component.defaultProps, baseOptions)
|
||||
|
||||
|
80
lib/static/webpackNodeApp.js
Normal file
80
lib/static/webpackNodeApp.js
Normal file
@ -0,0 +1,80 @@
|
||||
const path = require('path')
|
||||
const webpack = require('webpack')
|
||||
const merge = require('webpack-merge')
|
||||
|
||||
// bundle App for usage in node when a custom webpack config is provided
|
||||
const config = {
|
||||
// mode: 'production',
|
||||
output: {
|
||||
path: path.join(__dirname, './TMP'),
|
||||
filename: 'App.js',
|
||||
libraryExport: 'default',
|
||||
libraryTarget: 'umd',
|
||||
},
|
||||
target: 'node',
|
||||
resolve: {
|
||||
modules: []
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.md$/,
|
||||
use: 'raw-loader'
|
||||
},
|
||||
{
|
||||
test: /\.js$/,
|
||||
exclude: /node_modules/,
|
||||
use: {
|
||||
loader: require.resolve('babel-loader'),
|
||||
options: {
|
||||
presets: [
|
||||
'babel-preset-env',
|
||||
'babel-preset-stage-0',
|
||||
'babel-preset-react'
|
||||
].map(require.resolve),
|
||||
plugins: [
|
||||
require.resolve('babel-plugin-transform-runtime')
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
]
|
||||
},
|
||||
plugins: [],
|
||||
}
|
||||
|
||||
const bundleApp = (filename, options = {}) => {
|
||||
const dirname = path.dirname(filename)
|
||||
|
||||
config.entry = filename
|
||||
|
||||
config.resolve.modules.unshift(
|
||||
dirname,
|
||||
path.join(process.cwd(), 'node_modules'),
|
||||
path.join(dirname, 'node_modules'),
|
||||
)
|
||||
|
||||
const userConfig = require(
|
||||
path.join(process.cwd(), options.config)
|
||||
)
|
||||
const mergedConfig = merge(config, userConfig)
|
||||
const compiler = webpack(mergedConfig)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
compiler.run((err, stats) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const App = require('./TMP/App')
|
||||
resolve(App)
|
||||
} catch (e) {
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = bundleApp
|
@ -9,7 +9,7 @@
|
||||
"scripts": {
|
||||
"start": "./bin/cli.js dev docs/App.js -op 8888",
|
||||
"static": "./bin/cli.js build docs/App.js --static -d docs",
|
||||
"build": "./bin/cli.js build docs/App.js -d docs",
|
||||
"build": "./bin/cli.js build docs/App.js -d docs -c docs/webpack.config.js",
|
||||
"test": "nyc ava",
|
||||
"cover": "nyc report --reporter=html --reporter=lcov"
|
||||
},
|
||||
@ -51,6 +51,7 @@
|
||||
"isomorphic-fetch": "^2.2.1",
|
||||
"nano-style": "^1.0.0-20",
|
||||
"nyc": "^11.2.1",
|
||||
"raw-loader": "^0.5.1",
|
||||
"react-router-dom": "^4.2.2",
|
||||
"refunk": "^2.0.0-1",
|
||||
"styled-components": "^2.2.4",
|
||||
|
Loading…
Reference in New Issue
Block a user