UNPKG

2.44 kBJavaScriptView Raw
1const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
2const path = require('path');
3const fs = require('fs');
4const rootPath = process.cwd();
5
6const configFile = path.join(rootPath, '.demosrc.js');
7if (!fs.existsSync(configFile)) {
8 throw new Error('No .demosrc.js file found in project.');
9}
10
11let configCode = fs.readFileSync(configFile, 'utf-8');
12const babel = require('@babel/core');
13configCode = babel.transformSync(configCode, { presets: ['@babel/preset-env'] })
14 .code;
15
16const requireFromString = require('require-from-string');
17
18let config = requireFromString(configCode);
19config = config.default || config;
20
21if (typeof config === 'function') {
22 config = config(process.env.NODE_ENV);
23}
24
25let port = 3000;
26if (config.devServer && config.devServer.port) {
27 port = config.devServer.port;
28}
29let output = config.output || { dir: 'dist' };
30let demoList = config.demoList || '.demoList.json';
31let demosPath = config.demosPath || 'demos';
32
33if (typeof output === 'string') {
34 output = { dir: output };
35}
36
37let themeFilePath = path.resolve(__dirname, './src/css/default_theme.scss');
38
39if (config.themeFile) {
40 themeFilePath = path.resolve(rootPath, config.themeFile);
41}
42
43console.warn(`Output directory: ${output.dir}.`);
44console.warn(`PublicUrl: ${output.publicUrl || '/'}.`);
45
46module.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};