-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed webpack.config with a new loader
Tried a different approach to tackle the enviroment configurations. Instead of a typescript file, which is replaced based on the environment, this strategy uses json file to hold the configuration. The loader takes the basic config file and applies that environmentspecific changes (change environment.json and environment.production.json).
- Loading branch information
Showing
4 changed files
with
64 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { | ||
getOptions | ||
} = require('loader-utils'); | ||
const validateOptions = require('schema-utils'); | ||
const log = require('webpack-log'); | ||
|
||
const loaderName = 'environment-loader'; | ||
const logger = log({ | ||
name: loaderName | ||
}); | ||
const schema = { | ||
env: 'string' | ||
}; | ||
const defaultOptions = { | ||
env: "development" | ||
}; | ||
|
||
const parseFileContent = (content, filePath) => { | ||
try { | ||
return JSON.parse(content); | ||
} catch (e) { | ||
logger.error(`Unable to parse the file ${filePath}; ${loaderName} can only be used to load and transform json files.`); | ||
} | ||
} | ||
|
||
module.exports = function (source) { | ||
const options = Object.assign(defaultOptions, getOptions(this)); | ||
validateOptions(schema, options, 'environment-loader'); | ||
|
||
const ext = path.extname(this.resourcePath); | ||
const envFile = path.join(path.dirname(this.resourcePath), `${path.basename(this.resourcePath, ext)}.${options.env}${ext}`); | ||
let envConfig = {}; | ||
|
||
if (fs.existsSync(envFile)) { | ||
envConfig = parseFileContent(fs.readFileSync(envFile), envFile); | ||
} | ||
const sourceConfig = parseFileContent(source, this.resourcePath); | ||
|
||
return JSON.stringify({ | ||
...sourceConfig, | ||
...envConfig | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"debug": true, | ||
"testing": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"debug": false, | ||
"testing": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters