1 | const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
|
2 | const path = require('path');
|
3 | const fs = require('fs');
|
4 | const rootPath = process.cwd();
|
5 |
|
6 | const configFile = path.join(rootPath, '.demosrc.js');
|
7 | if (!fs.existsSync(configFile)) {
|
8 | throw new Error('No .demosrc.js file found in project.');
|
9 | }
|
10 |
|
11 | let configCode = fs.readFileSync(configFile, 'utf-8');
|
12 | const babel = require('@babel/core');
|
13 | configCode = babel.transformSync(configCode, { presets: ['@babel/preset-env'] })
|
14 | .code;
|
15 |
|
16 | const requireFromString = require('require-from-string');
|
17 |
|
18 | let config = requireFromString(configCode);
|
19 | config = config.default || config;
|
20 |
|
21 | if (typeof config === 'function') {
|
22 | config = config(process.env.NODE_ENV);
|
23 | }
|
24 |
|
25 | let port = 3000;
|
26 | if (config.devServer && config.devServer.port) {
|
27 | port = config.devServer.port;
|
28 | }
|
29 | let output = config.output || { dir: 'dist' };
|
30 | let demoList = config.demoList || '.demoList.json';
|
31 | let demosPath = config.demosPath || 'demos';
|
32 |
|
33 | if (typeof output === 'string') {
|
34 | output = { dir: output };
|
35 | }
|
36 |
|
37 | let themeFilePath = path.resolve(__dirname, './src/css/default_theme.scss');
|
38 |
|
39 | if (config.themeFile) {
|
40 | themeFilePath = path.resolve(rootPath, config.themeFile);
|
41 | }
|
42 |
|
43 | console.warn(`Output directory: ${output.dir}.`);
|
44 | console.warn(`PublicUrl: ${output.publicUrl || '/'}.`);
|
45 |
|
46 | module.exports = {
|
47 | entry: path.join(__dirname, 'index.js'),
|
48 | output,
|
49 | publicFolder: path.join(
|
50 | rootPath,
|
51 | config.staticFolder || config.publicFolder || 'static'
|
52 | ),
|
53 | devServer: {
|
54 | port
|
55 | },
|
56 | cache: false,
|
57 | envs: {
|
58 | NODE_ENV: process.env.NODE_ENV
|
59 | },
|
60 | babel: {
|
61 | babelrc: !!config.babelrc,
|
62 | transpileModules: ['@demosify/core']
|
63 | },
|
64 | chainWebpack(config) {
|
65 | config.merge({
|
66 | plugin: {
|
67 | monaco: {
|
68 | plugin: MonacoWebpackPlugin
|
69 | }
|
70 | },
|
71 | node: {
|
72 | fs: 'empty'
|
73 | },
|
74 | resolve: {
|
75 | alias: {
|
76 | '@': path.join(__dirname, 'src'),
|
77 | '~': path.join(rootPath, demosPath),
|
78 | demos: path.join(rootPath, demosPath),
|
79 | '.demoList.json': path.join(rootPath, demosPath, demoList),
|
80 | manifest: path.join(rootPath, '.demosrc'),
|
81 | themeFile: themeFilePath
|
82 | }
|
83 | }
|
84 | });
|
85 | config.module
|
86 | .rule('json')
|
87 | .test(/\.json$/)
|
88 | .use('json')
|
89 | .loader('json-loader')
|
90 | .end();
|
91 | config.module.rule('json').store.set('type', 'javascript/auto');
|
92 | config.output.filename('[name].bundle.js');
|
93 | }
|
94 | };
|