1 | const path = require('path');
|
2 | const fs = require('fs');
|
3 | const webpack = require('webpack');
|
4 | const common = require('./webpack-common.js');
|
5 |
|
6 | const LINC_DIR = common.LINC_DIR;
|
7 | const PROJECT_DIR = common.PROJECT_DIR;
|
8 | const DIST_DIR = path.resolve(PROJECT_DIR, 'dist');
|
9 | const LIB_DIR = path.resolve(DIST_DIR, 'lib');
|
10 |
|
11 | const srcDir = common.srcDir;
|
12 |
|
13 | const createConfig = (options) => {
|
14 | options = options || {}
|
15 |
|
16 | const url_loader_config = {
|
17 | exclude: [
|
18 | /\.html$/,
|
19 | /\.(js|jsx)$/,
|
20 | /\.css$/,
|
21 | /\.json$/,
|
22 | /\.svg$/,
|
23 | /\.woff$/,
|
24 | /\.woff2$/,
|
25 | /\.eot$/,
|
26 | /\.ttf$/,
|
27 | ],
|
28 | loader: 'url-loader',
|
29 | query: {
|
30 | limit: 10,
|
31 | name: '_assets/media/[name].[hash:8].[ext]'
|
32 | }
|
33 | }
|
34 |
|
35 | const babel_options = options.babel || { presets: [], plugins: [] }
|
36 | babel_options.presets.push(
|
37 | ['env', {
|
38 | targets: {
|
39 | node: 8
|
40 | },
|
41 | modules: false
|
42 | }]
|
43 | )
|
44 | babel_options.presets.push('stage-1')
|
45 |
|
46 | const linc_exenv_path = path.resolve(LINC_DIR, 'node_modules', 'fake-exenv');
|
47 | const prj_exenv_path = path.resolve(PROJECT_DIR, 'node_modules', 'fake-exenv');
|
48 | const exenvPath = fs.existsSync(linc_exenv_path) ? linc_exenv_path : prj_exenv_path;
|
49 |
|
50 | const alias = {
|
51 | 'linc-config-js': path.resolve(PROJECT_DIR, srcDir, 'linc.config.js'),
|
52 | 'linc-server-config-js': path.resolve(PROJECT_DIR, srcDir, 'linc.server.config.js'),
|
53 | 'asset-manifest': path.resolve(LIB_DIR,'asset-manifest.json'),
|
54 | 'server-strategy': path.resolve(DIST_DIR,'server-strategy.js'),
|
55 | 'includes': path.resolve(LIB_DIR, 'includes.js'),
|
56 | 'exenv': exenvPath
|
57 | }
|
58 | Object.assign(alias, options.alias)
|
59 |
|
60 | let plugins = [
|
61 | common.definePlugin,
|
62 | new webpack.optimize.OccurrenceOrderPlugin(),
|
63 | new webpack.ProvidePlugin({
|
64 | React: 'react',
|
65 | }),
|
66 | new webpack.optimize.LimitChunkCountPlugin({
|
67 | maxChunks: 1
|
68 | })
|
69 | ]
|
70 | plugins = plugins.concat(options.plugins.map((pluginOptions) => common.createPlugin(pluginOptions)))
|
71 |
|
72 | return {
|
73 | entry: {
|
74 | 'server-render': [path.resolve(LINC_DIR, 'dist', 'render.js')]
|
75 | },
|
76 | target: 'node',
|
77 | resolve: {
|
78 | alias,
|
79 | extensions: [".js", ".json", ".jsx"],
|
80 | modules: [srcDir, "node_modules", path.resolve(PROJECT_DIR, "node_modules")],
|
81 | },
|
82 | resolveLoader: {
|
83 | modules: ['node_modules', path.resolve(LINC_DIR, 'node_modules')]
|
84 | },
|
85 | output: {
|
86 | path: path.resolve(PROJECT_DIR, 'dist', 'static'),
|
87 | filename: '../lib/[name].js',
|
88 | library: 'server',
|
89 | libraryTarget: 'commonjs2',
|
90 | publicPath: common.publicPath
|
91 | },
|
92 |
|
93 | module: {
|
94 | rules: [
|
95 | url_loader_config,
|
96 | {
|
97 | test: /\.svg$/,
|
98 | loader: 'svg-url-loader',
|
99 | query: {
|
100 | limit: 10,
|
101 | name: '_assets/media/[name].[hash:8].[ext]'
|
102 | }
|
103 | },
|
104 | { enforce: "pre", test: /\.js$/, loader: "source-map-loader" },
|
105 | {
|
106 | test: /\.js$/,
|
107 | loader: 'babel-loader',
|
108 | exclude: /node_modules/,
|
109 | options: babel_options
|
110 | },
|
111 | {
|
112 | include: /\.(css)$/,
|
113 | loader: 'ignore-loader'
|
114 | },
|
115 | {
|
116 | test: /\.(woff|woff2|eot|ttf)$/,
|
117 | loader: 'file-loader',
|
118 | options: {
|
119 | name: '_assets/fonts/[name].[hash:8].[ext]'
|
120 | }
|
121 | }
|
122 | ]
|
123 | },
|
124 | externals: {
|
125 | 'follow-redirects': 'follow-redirects',
|
126 | 'faye-websocket': 'faye-websocket',
|
127 | 'xmlhttprequest': 'xmlhttprequest'
|
128 | },
|
129 |
|
130 | plugins,
|
131 |
|
132 | stats: {
|
133 | children: false
|
134 | },
|
135 |
|
136 |
|
137 | devtool: 'source-map'
|
138 | };
|
139 | }
|
140 |
|
141 | module.exports = createConfig |
\ | No newline at end of file |