mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(webpack): angular configuration support for environment handling (#8938)
This commit is contained in:
104
packages/webpack/helpers/angular-config-parser.js
vendored
Normal file
104
packages/webpack/helpers/angular-config-parser.js
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
const { resolve } = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const parseWorkspaceConfig = function(platform, envConfigs, projectName, debug) {
|
||||
if (debug) {
|
||||
console.log('-- config DEBUG ---');
|
||||
console.log('platform:', platform);
|
||||
console.log('configuration:', envConfigs);
|
||||
}
|
||||
// configuration file replacements
|
||||
const fileReplacements = {};
|
||||
// anything other than .ts files should be added as part of copy plugin
|
||||
const copyReplacements = [];
|
||||
if (hasConfigurations(envConfigs)) {
|
||||
envConfigs = envConfigs.split(',').map(e => e.trim());
|
||||
|
||||
const configData = findConfig(__dirname);
|
||||
const rootPath = configData.rootPath;
|
||||
const workspaceConfig = configData.workspaceConfig;
|
||||
|
||||
if (workspaceConfig && projectName) {
|
||||
const projectSettings = workspaceConfig.projects[projectName];
|
||||
if (projectSettings) {
|
||||
|
||||
// default project configurations
|
||||
for (const envConfig of envConfigs) {
|
||||
if (projectSettings.configurations && projectSettings.configurations[envConfig]) {
|
||||
if (projectSettings.configurations[envConfig].fileReplacements) {
|
||||
for (const fileReplace of projectSettings.configurations[envConfig].fileReplacements) {
|
||||
if (debug) {
|
||||
console.log('project fileReplacement:', fileReplace);
|
||||
}
|
||||
if (fileReplace.replace.indexOf('.ts') > -1) {
|
||||
fileReplacements[resolve(__dirname, `${rootPath}${fileReplace.replace}`)] = resolve(__dirname, `${rootPath}${fileReplace.with}`);
|
||||
} else {
|
||||
copyReplacements.push({ from: resolve(__dirname, `${rootPath}${fileReplace.with}`), to: resolve(__dirname, `${rootPath}${fileReplace.replace}`), force: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// platform specific configurations (always override top level project configurations)
|
||||
for (const envConfig of envConfigs) {
|
||||
if (projectSettings.architect && projectSettings.architect[platform]) {
|
||||
const platformConfig = projectSettings.architect[platform].configurations;
|
||||
if (platformConfig && platformConfig[envConfig] && platformConfig[envConfig].fileReplacements) {
|
||||
for (const fileReplace of platformConfig[envConfig].fileReplacements) {
|
||||
if (debug) {
|
||||
console.log(`"${platform}" specific fileReplacement:`, fileReplace);
|
||||
}
|
||||
if (fileReplace.replace.indexOf('.ts') > -1) {
|
||||
fileReplacements[resolve(__dirname, `${rootPath}${fileReplace.replace}`)] = resolve(__dirname, `${rootPath}${fileReplace.with}`);
|
||||
} else {
|
||||
copyReplacements.push({ from: resolve(__dirname, `${rootPath}${fileReplace.with}`), to: resolve(__dirname, `${rootPath}${fileReplace.replace}`), force: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (debug && copyReplacements.length) {
|
||||
console.log('Adding to CopyWebpackPlugin:', copyReplacements);
|
||||
}
|
||||
|
||||
return {
|
||||
fileReplacements,
|
||||
copyReplacements
|
||||
};
|
||||
}
|
||||
|
||||
const findConfig = function(projectDir, rootPath = '') {
|
||||
// support workspace.json and angular.json configurations
|
||||
const angularConfigName = 'angular.json';
|
||||
const angularConfig = resolve(projectDir, angularConfigName);
|
||||
const workspaceConfigName = 'workspace.json';
|
||||
const workspaceConfig = resolve(projectDir, workspaceConfigName);
|
||||
if (fs.existsSync(workspaceConfig)) {
|
||||
return {
|
||||
rootPath,
|
||||
workspaceConfig: require(workspaceConfig)
|
||||
};
|
||||
} else if (fs.existsSync(angularConfig)) {
|
||||
return {
|
||||
rootPath,
|
||||
workspaceConfig: require(angularConfig)
|
||||
};
|
||||
} else {
|
||||
rootPath += '../';
|
||||
return findConfig(resolve(projectDir, '..'), rootPath);
|
||||
}
|
||||
}
|
||||
|
||||
const hasConfigurations = function(envConfigs) {
|
||||
return envConfigs && envConfigs !== 'undefined';
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parseWorkspaceConfig,
|
||||
findConfig,
|
||||
hasConfigurations
|
||||
};
|
||||
Reference in New Issue
Block a user