|
@@ -1,5 +1,9 @@
|
|
import type { UserConfigExport } from '@tarojs/cli';
|
|
import type { UserConfigExport } from '@tarojs/cli';
|
|
import path from 'path';
|
|
import path from 'path';
|
|
|
|
+import dotenv from 'dotenv';
|
|
|
|
+
|
|
|
|
+// 加载 .env.development 文件中的环境变量
|
|
|
|
+dotenv.config({ path: path.resolve(__dirname, '../.env.development') });
|
|
|
|
|
|
export default {
|
|
export default {
|
|
logger: {
|
|
logger: {
|
|
@@ -26,6 +30,33 @@ export default {
|
|
// Use webpackChain to customize Webpack
|
|
// Use webpackChain to customize Webpack
|
|
// eslint-disable-next-line
|
|
// eslint-disable-next-line
|
|
webpackChain(chain, webpack) {
|
|
webpackChain(chain, webpack) {
|
|
|
|
+ // 读取环境变量,告诉webpack在打包时,使用环境变量中定义的值 替换掉代码中的process.env.USE_MSW
|
|
|
|
+ // 打印环境变量,检查是否正确读取
|
|
|
|
+ console.log('环境变量 process.env.USE_MSW:', process.env.USE_MSW);
|
|
|
|
+ console.log('环境变量 process.env.NODE_ENV:', process.env.NODE_ENV);
|
|
|
|
+
|
|
|
|
+ // 确保使用正确的值,直接从.env文件读取
|
|
|
|
+ const useMSW = JSON.stringify(
|
|
|
|
+ process.env.USE_MSW === 'true' ? 'true' : 'false'
|
|
|
|
+ );
|
|
|
|
+ console.log('使用的 useMSW 值:', useMSW);
|
|
|
|
+
|
|
|
|
+ // 检查 define 插件是否已存在,如果不存在则先创建它
|
|
|
|
+ const hasDefinePlugin = chain.plugins.has('define');
|
|
|
|
+ if (!hasDefinePlugin) {
|
|
|
|
+ chain.plugin('define').use(webpack.DefinePlugin, [{}]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 然后再修改插件配置
|
|
|
|
+ chain.plugin('define').tap((args) => {
|
|
|
|
+ // 确保args[0]存在
|
|
|
|
+ if (!args[0]) {
|
|
|
|
+ args[0] = {};
|
|
|
|
+ }
|
|
|
|
+ args[0]['process.env.USE_MSW'] = useMSW;
|
|
|
|
+ console.log('DefinePlugin配置:', args[0]);
|
|
|
|
+ return args;
|
|
|
|
+ });
|
|
chain.devServer.merge({
|
|
chain.devServer.merge({
|
|
setupMiddlewares: (middlewares, devServer) => {
|
|
setupMiddlewares: (middlewares, devServer) => {
|
|
devServer.app.get('/mockServiceWorker.js', (req, res) => {
|
|
devServer.app.get('/mockServiceWorker.js', (req, res) => {
|