我有一个模式,在多个目录中有一组文件,例如:
sampleDir12
-> index.js
-> templateA.js
-> templateB.js
-> templateC.js
里面
index.js
我有:
import * as template from template.js
是否可以根据
.env
设置更改文件名?例如,在 .env
我会
TEMPLATE: templateC
在捆绑过程中我想做的是删除其他模板并更改其余模板的名称,以便正确导入。
我基本上想决定捆绑程序级别要在构建中包含哪些文件,并确保正确导入剩余的文件,因此如果有更好的方法来处理它,请告诉我。
dotenv-webpack
包从 .env
文件加载环境变量,并在编译阶段替换代码中的 process.env.TEMPLATE
。
例如
项目结构:
✗ tree -L 2 -I node_modules
.
├── dist
│ └── main.js
├── package-lock.json
├── package.json
├── src
│ ├── index.js
│ ├── templateA.js
│ └── templateB.js
└── webpack.config.js
webpack.config.js
:
const path = require('path');
const Dotenv = require('dotenv-webpack');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
clean: true,
},
plugins: [new Dotenv()],
};
src/index.js
:
const { name } = require(`./${process.env.TEMPLATE}`);
console.log(name);
src/templateA.js
:
const name = 'template a';
export { name };
src/templateB.js
:
const name = 'template b';
export { name };
构建日志:
> webpack
asset main.js 3.95 KiB [compared for emit] (name: main)
runtime modules 670 bytes 3 modules
cacheable modules 120 bytes
./src/index.js 75 bytes [built] [code generated]
./src/templateB.js 45 bytes [built] [code generated]
webpack 5.88.2 compiled successfully in 51 m
输出
dist/main.js
:
/*
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
* This devtool is neither made for production nor for readable output files.
* It uses "eval()" calls to create a separate source file in the browser devtools.
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
* or disable the default devtool with "devtool: false".
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
*/
/******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ({
/***/ "./src/index.js":
/*!**********************!*\
!*** ./src/index.js ***!
\**********************/
/***/ ((__unused_webpack_module, __unused_webpack_exports, __webpack_require__) => {
eval("const { name } = __webpack_require__(/*! ./templateB */ \"./src/templateB.js\");\n\nconsole.log(name);\n\n\n//# sourceURL=webpack:///./src/index.js?");
/***/ }),
/***/ "./src/templateB.js":
/*!**************************!*\
!*** ./src/templateB.js ***!
\**************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ name: () => (/* binding */ name)\n/* harmony export */ });\nconst name = 'template b';\n\n\n\n\n//# sourceURL=webpack:///./src/templateB.js?");
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
/******/
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module can't be inlined because the eval devtool is used.
/******/ var __webpack_exports__ = __webpack_require__("./src/index.js");
/******/
/******/ })()
;