forked from vaadin/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.dspublisher.js
109 lines (89 loc) · 4.13 KB
/
webpack.dspublisher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const path = require('path');
const fs = require('fs');
const buildDirectory = path.resolve(__dirname, 'target');
const { ApplicationThemePlugin } = require(buildDirectory + '/plugins/application-theme-plugin');
// Folders in the project which can contain static assets.
const projectStaticAssetsFolders = [
path.resolve(__dirname, 'src', 'main', 'resources', 'META-INF', 'resources'),
path.resolve(__dirname, 'src', 'main', 'resources', 'static'),
path.resolve(__dirname, 'frontend')
];
const projectStaticAssetsOutputFolder = path.resolve(
__dirname,
'target/META-INF/VAADIN/webapp/VAADIN/static'
);
// Folders in the project which can contain application themes
const themeProjectFolders = projectStaticAssetsFolders.map(folder =>
path.resolve(folder, 'themes')
);
const frontendGeneratedFolder = path.resolve(__dirname, 'frontend/generated');
// Target flow-fronted auto generated to be the actual target folder
const flowFrontendFolder = path.resolve(__dirname, 'target/flow-frontend');
const flowFrontendThemesFolder = path.resolve(flowFrontendFolder, 'themes');
const themeOptions = {
devMode: false,
// The following matches folder 'target/flow-frontend/themes/'
// (not 'frontend/themes') for theme in JAR that is copied there
themeResourceFolder: flowFrontendThemesFolder,
themeProjectFolders,
projectStaticAssetsOutputFolder,
frontendGeneratedFolder
};
// this matches css files in the theme
const themeCssRegex = /(\\|\/).*frontend(\\|\/)themes\1[\s\S]*?\.css/;
const embeddedWcRegex = /(\\|\/).*target(\\|\/)frontend(\\|\/)[\s\S]*-wc.js/;
const projectThemePath = path.resolve(__dirname, 'frontend/themes');
const reusableThemesPath = path.resolve(__dirname, 'target/flow-frontend/themes');
// List of all the directories under projectThemePath (frontend/themes)
const projectThemeNames = fs.readdirSync(projectThemePath);
// Content of the DemoExporter.java file (has the @Theme annotation)
const demoExporterContent = fs.readFileSync(
path.resolve(__dirname, 'src/main/java/com/vaadin/demo/DemoExporter.java'),
'utf-8'
);
// Check if one of the project themes (default = "docs") is in use
const themeLine = demoExporterContent.split('\n').find((line) => line.includes('@Theme'));
const usesProjectTheme = projectThemeNames.some((themeName) =>
themeLine.includes(`"${themeName}"`)
);
const themesPath = usesProjectTheme ? projectThemePath : reusableThemesPath;
const applyThemePath = path.resolve(frontendGeneratedFolder, 'theme.js');
module.exports = function (config) {
const allFlowImportsPath = path.resolve(__dirname, 'target/frontend/generated-flow-imports.js');
config.resolve.alias['all-flow-imports-or-empty'] =
process.env.DOCS_IMPORT_EXAMPLE_RESOURCES === 'true'
? allFlowImportsPath
: // false not supported in Webpack 4, let's use a resource that would get included anyway
applyThemePath;
config.resolve.alias['Frontend/generated/theme'] = applyThemePath;
config.resolve.alias.themes = themesPath;
const frontendFolder = path.resolve(__dirname, 'frontend');
config.resolve.alias['Frontend'] = frontendFolder;
config.plugins.push(new ApplicationThemePlugin(themeOptions));
// If there are pre-existing rules that affect CSS files,
// make them exclude files that match the themeCssRegex pattern...
config.module.rules
.filter(rule => rule.oneOf && rule.oneOf.some(r => r.test.test('style.css')))
.forEach(rule => (rule.exclude = themeCssRegex));
// ...and add a custom rule to handle the CSS files matching the themeCssRegex pattern
config.module.rules.push({
test: themeCssRegex,
use: ['raw-loader', 'extract-loader', 'css-loader']
});
// The docs-app bundle should never contain the embedded Vaadin examples
config.module.rules.push({
test: embeddedWcRegex,
use: ['null-loader']
});
// Avoid having the docs-app dev server recompile whenever the Java-sources or generated files change
config.devServer = {
watchOptions: {
ignored: [
path.resolve(__dirname, 'target'),
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname, 'src', 'main', 'java'),
path.resolve(__dirname, 'frontend', 'generated')
]
}
};
};