-
-
Notifications
You must be signed in to change notification settings - Fork 486
/
bootstrap.loader.js
198 lines (162 loc) · 6.38 KB
/
bootstrap.loader.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* eslint-disable func-names, import/first */
import semver from 'semver';
// For Node <= v0.12.x Babel polyfill is required
if (semver.lt(process.version, '4.0.0') && !global._babelPolyfill) {
try {
// eslint-disable-next-line global-require
require('babel-polyfill');
} catch (e) {
try {
// eslint-disable-next-line global-require
require('babel-core/polyfill');
} catch (ee) {
try {
// eslint-disable-next-line global-require
require('babel/polyfill');
} catch (eee) {
throw new Error(`
For Node <= v0.12.x Babel polyfill is required.
Make sure it's installed in your 'node_modules/' directory.
${eee}
`);
}
}
}
}
// Read more about the next line
// at https://github.com/shakacode/bootstrap-loader/pull/139
/* eslint-disable import/imports-first */
import path from 'path';
import loaderUtils from 'loader-utils';
import resolveModule from './utils/resolveModule';
import checkBootstrapVersion from './utils/checkBootstrapVersion';
import processStyleLoaders from './utils/processStyleLoaders';
import joinLoaders from './utils/joinLoaders';
import buildExtractStylesLoader from './utils/buildExtractStylesLoader';
import createRequire from './utils/createRequire';
import logger from './utils/logger';
import fileExists from './utils/fileExists';
import getEnvProp from './utils/getEnvProp';
import createConfig from './bootstrap.config';
module.exports = function() {};
/**
* Bootstrap loader entry point
*
* @param {string} source - Path to dummy file with empty object.
* Needed b/c we have to apply loader to some file.
* @returns {string}
*/
module.exports.pitch = function(source) {
if (this.cacheable) this.cacheable();
const { extractStyles, configFilePath, bootstrapPath } = loaderUtils.getOptions(this) || {};
if (configFilePath) {
const fullPathToUserConfig = path.resolve(__dirname, configFilePath);
if (!fileExists(fullPathToUserConfig)) {
throw new Error(`
Cannot find config file ${fullPathToUserConfig}. You might want to pass the full path.
`);
}
}
const config = createConfig({ extractStyles, customConfigFilePath: configFilePath });
function isDebugEnabled() {
if (config.loglevel === 'debug') {
return true;
}
if (!process.env.DEBUG) {
return false;
}
switch (process.env.DEBUG.toString().toLowerCase()) {
case 'true':
case 'yes':
case '1':
return true;
default:
return false;
}
}
global.__DEBUG__ = isDebugEnabled();
const whichWayDebugEnabledMsg = process.env.DEBUG
? 'DEBUG defined in your ENV.'
: "your config log level set to 'debug'.";
logger.debug(`bootstrap-loader is in DEBUG mode because you have ${whichWayDebugEnabledMsg}`);
logger.debug(`Using config file ${config.configFilePath}`);
logger.debug('Query from webpack config:', this.query || '*none*');
const { bootstrapVersion } = config;
// Resolve `bootstrap` package
const bootstrapNPMModule = bootstrapVersion === 3 ? 'bootstrap-sass' : 'bootstrap';
logger.debug('Using Bootstrap module:', bootstrapNPMModule);
config.bootstrapPath = bootstrapPath || resolveModule(bootstrapNPMModule);
logger.debug(`Bootstrap module location (abs): ${config.bootstrapPath}`);
if (!config.bootstrapPath) {
const msg = `Could not resolve module '${bootstrapNPMModule}' which must be installed when bootstrap version is configured to v${bootstrapVersion}.
You must install 'bootstrap' for bootstrap v4 or 'bootstrap-sass' for bootstrap v3.
You can also specify the location manually by specifying 'bootstrapPath' in bootstrap-loader's query string.
See https://github.com/shakacode/bootstrap-loader/blob/master/README.md#usage.`;
throw new Error(msg);
}
config.bootstrapRelPath = path.relative(this.context, config.bootstrapPath);
logger.debug(`Bootstrap module location (rel): ${config.bootstrapRelPath}`);
logger.debug('Context:', this.context);
logger.debug('Using Bootstrap version:', bootstrapVersion);
if (!config.bootstrapPath) {
throw new Error(`
Could not find path to '${bootstrapNPMModule}' module.
Make sure it's installed in your 'node_modules/' directory.
`);
}
const bootstrapNPMVersion = checkBootstrapVersion(bootstrapVersion, config.bootstrapPath);
if (!bootstrapNPMVersion.allGood) {
throw new Error(`
Looks like you have wrong version of Bootstrap.
Loader wants: ${bootstrapVersion}.x.x
Installed version: ${bootstrapNPMVersion.version}
`);
}
logger.debug('Bootstrap NPM package version:', bootstrapNPMVersion.version);
logger.debug('Normalized params:', '\n', config);
const result = {};
const dummySourceRel = loaderUtils.urlToRequest(path.relative(this.context, source));
const bootstrapConfig = JSON.stringify(config);
// Handle styles
if (config.styles) {
if (!getEnvProp('styleLoaders', config)) {
throw new Error(`
Could not find 'styleLoaders' in your config.
You can use default ones:
styleLoaders: ['style', 'css', 'sass']
`);
}
const styleLoadersWithSourceMapsAndResolveUrlLoader = processStyleLoaders({
loaders: config.styleLoaders,
disableSassSourceMap: config.disableSassSourceMap,
disableResolveUrlLoader: config.disableResolveUrlLoader,
});
const styleLoaders = config.extractStyles
? buildExtractStylesLoader(styleLoadersWithSourceMapsAndResolveUrlLoader)
: joinLoaders(styleLoadersWithSourceMapsAndResolveUrlLoader);
const bootstrapStylesLoader = `${loaderUtils.urlToRequest(
path.relative(
this.context,
require.resolve(loaderUtils.urlToRequest('bootstrap.styles.loader.js')),
),
)}?${bootstrapConfig}!`;
const styles = styleLoaders + bootstrapStylesLoader + dummySourceRel;
result.css = createRequire(styles);
}
// Handle scripts
if (config.scripts) {
const bootstrapScriptsLoader = `${loaderUtils.urlToRequest(
path.relative(
this.context,
require.resolve(loaderUtils.urlToRequest('bootstrap.scripts.loader.js')),
),
)}?${bootstrapConfig}!`;
const scripts = bootstrapScriptsLoader + dummySourceRel;
result.js = createRequire(scripts);
}
const resultOutput = Object.keys(result)
.map(key => `module.exports.${key} = ${result[key]}\n`)
.join('');
logger.debug('Requiring:', '\n', resultOutput);
return resultOutput;
};