-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrequire-style.js
149 lines (132 loc) · 5.33 KB
/
require-style.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License.txt in the project root for license information.
// Custom RequireJS plugin to handle style dependencies from Javascript modules
// Has special WinJS-specific behavior that wasn't achievable with libraries like require-less
define(['require'], function (req) {
"use strict";
var api = {};
var lessImports = [];
var moduleNames = {};
var configData;
// Called to load a resource. This is the only mandatory API method that
// needs to be implemented for the plugin to be useful.
api.load = function (name, parentRequire, onLoad, config) {
// Do nothing outside of optimized build
if (!config.isBuild) {
onLoad();
return;
}
// Get absolute path to file and skip it if
// 'empty:'
var filePath = parentRequire.toUrl(name);
if (filePath.indexOf('empty:') === 0) {
onLoad();
return;
}
// Store the file as an import
configData = config;
lessImports.push('@import "' + filePath + '";');
onLoad();
};
// Used to get the full path to a resource by name
api.normalize = function (name) {
var relativePath = name;
if (!relativePath.match(/\.less$/)) {
relativePath += ".less";
}
var filePath = req.toUrl(relativePath);
if (!moduleNames[filePath]) {
moduleNames[filePath] = name;
}
return filePath;
};
// Used by the optimizer to indicate when the plugin should write out
// a representation of the the resource in the optimized file.
api.write = function (pluginName, filePath, write) {
// Output a stub module definition
var name = moduleNames[filePath];
write.asModule(pluginName + '!' + name, 'define(function(){})');
};
function writeCssFile(theme, inverseTheme, outputFilePath) {
var fs = require.nodeRequire('fs-extra');
var less = require.nodeRequire('less');
var path = require.nodeRequire('path');
// Build a custom LESS file that imports every resource
// with theme defines at the top
var defines = [
'@theme: ' + theme + ';',
'@inverseTheme: ' + inverseTheme + ';'
].join('\n');
var colors = [
'.Colors(@theme) {}',
'.ColorsHover(@theme) {}',
'.Colors(@theme);',
'html.win-hoverable {',
' .ColorsHover(@theme);',
'}',
'.win-ui-@{inverseTheme} {',
' .Colors(@inverseTheme);',
'}',
'html.win-hoverable {',
' .win-ui-@{inverseTheme} {',
' .ColorsHover(@inverseTheme);',
' }',
'}',
// Stub define the HC mixins in case they don't exist
'.HighContrast() {}',
'.HighContrastHover() {}',
'.HighContrastThemed(@theme) {}',
'.HighContrastThemedHover(@theme) {}',
'@media (-ms-high-contrast) {',
// Call the HC mixins similar to the Color mixins
' .HighContrast();',
' html.win-hoverable {',
' .HighContrastHover();',
' }',
' .HighContrastThemed(@theme);',
' html.win-hoverable {',
' .HighContrastThemedHover(@theme);',
' }',
' .win-ui-@{inverseTheme} {',
' .HighContrastThemed(@inverseTheme);',
' }',
' html.win-hoverable {',
' .win-ui-@{inverseTheme} {',
' .HighContrastThemedHover(@inverseTheme);',
' }',
' }',
'}'
].join('\n');
var copyright = '/* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License.txt in the project root for license information. */';
var lessFile = copyright + '\n' + defines + '\n' + lessImports.join('\n') + '\n' + colors;
// Build the less file
var lessConfig = configData.less || {};
lessConfig.async = false;
lessConfig.syncImport = true;
var parser = new less.Parser(lessConfig);
parser.parse(lessFile, function (error, tree) {
if (error) {
console.error(error + ' in ' + error.filename + '\n' + 'line number: ' + error.line);
return;
}
// Write css to file
var cssText = tree.toCSS(lessConfig);
fs.writeFileSync(path.join(outputFilePath, 'ui-' + theme + '.css'), cssText, 'utf8');
});
}
// Called after the modules for the layer have been written to the layer.
api.onLayerEnd = function (write, data) {
if (!configData) {
return;
}
var fs = require.nodeRequire('fs-extra');
var path = require.nodeRequire('path');
// Go up one directory, out of the js module output path
// and then add the css/ path
var outputFilePath = path.resolve(data.path, '../../css');
fs.ensureDirSync(outputFilePath);
// Build a less file for each theme
writeCssFile('dark', 'light', outputFilePath);
writeCssFile('light', 'dark', outputFilePath);
};
return api;
});