Skip to content

Commit

Permalink
[fix] dev mode babel run use includeRegExp and excludeRegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Apr 28, 2021
1 parent 9c99f5e commit f71e4f8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 42 deletions.
11 changes: 2 additions & 9 deletions packages/xarc-app-dev/src/lib/babel-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
import Path from "path";
import { loadXarcOptions } from "../lib/utils";
import { util } from "@xarc/webpack";

const serverDir = process.argv[2] || "src/server";

Expand All @@ -24,18 +25,10 @@ try {
// Try to load user's dev.js under src/server
start = require(Path.resolve(xarcCwd, serverDir, "dev.js"));
} catch (e) {
const cwdNM = Path.resolve(xarcCwd, "node_modules");
const cwd = xarcCwd;

// fallback to default action that loads babel-register and then requires
// src/server, under which there should be an index.js file.
require("@babel/register")({
only: [
x => {
const y = Path.normalize(x);
return y.startsWith(cwd) && !y.startsWith(cwdNM);
}
],
ignore: [util.getBabelExclude(xarcOptions)],
extensions: [".js", ".jsx"]
.concat(xarcOptions.babel.enableTypeScript && [".ts", ".tsx"])
.filter(x => x),
Expand Down
4 changes: 4 additions & 0 deletions packages/xarc-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,7 @@ export const defaultConfigs = {
return applyPartials(baseConfig, [...profiles.base, ...profiles.karma, moreParts]);
}
};

import * as util from "./util";

export { util };
30 changes: 2 additions & 28 deletions packages/xarc-webpack/src/partials/babel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import * as Path from "path";
import { getBabelExclude } from "../util";
const identity = require("lodash/identity");
const assign = require("lodash/assign");
const babelLoader = require.resolve("babel-loader");
Expand All @@ -10,41 +11,14 @@ import { loadXarcOptions } from "../util/load-xarc-options";

module.exports = function(options) {
const xarcOptions = loadXarcOptions();
const AppMode = xarcOptions.AppMode;

const clientVendor = Path.join(AppMode.src.client, "vendor/");
const { includeRegExp, excludeRegExp } = xarcOptions.babel;

const babelExclude = (x: string) => {
if (includeRegExp && includeRegExp.find((r: RegExp) => x.match(r))) {
return false;
}

if (excludeRegExp && excludeRegExp.find((r: RegExp) => x.match(r))) {
return true;
}

if (x.indexOf("node_modules") >= 0) {
if (x.indexOf("~es2x~") >= 0 || x.indexOf("~es6~") >= 0) {
return false;
}
return true;
}

if (x.indexOf(clientVendor) >= 0) {
return true;
}

return false;
};

const test = xarcOptions.babel.enableTypeScript ? /\.[tj]sx?$/ : /\.jsx?$/;

const cacheIdentifier = generateBabelLoaderCacheId(xarcOptions.cwd);
const babelLoaderConfig = {
_name: "babel",
test,
exclude: babelExclude,
exclude: getBabelExclude(xarcOptions),
use: [
{
loader: babelLoader,
Expand Down
40 changes: 40 additions & 0 deletions packages/xarc-webpack/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as Path from "path";

/**
* Get a babel exclude function to filter out files that should not
* transpile.
*
* @param xarcOptions - xarc dev options
* @returns babel exclude function
*/
export const getBabelExclude = (xarcOptions: any) => {
const AppMode = xarcOptions.AppMode;

const clientVendor = Path.join(AppMode.src.client, "vendor/");
const { includeRegExp, excludeRegExp } = xarcOptions.babel;

const babelExclude = (x: string) => {
if (includeRegExp && includeRegExp.find((r: RegExp) => x.match(r))) {
return false;
}

if (excludeRegExp && excludeRegExp.find((r: RegExp) => x.match(r))) {
return true;
}

if (x.indexOf("node_modules") >= 0) {
if (x.indexOf("~es2x~") >= 0 || x.indexOf("~es6~") >= 0) {
return false;
}
return true;
}

if (x.indexOf(clientVendor) >= 0) {
return true;
}

return false;
};

return babelExclude;
};
10 changes: 5 additions & 5 deletions samples/create-app-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
},
"dependencies": {
"@babel/runtime": "^7.12.5",
"@xarc/app": "^10.0.3",
"@xarc/app": "^10.0.7",
"@xarc/fastify-server": "^3.2.3",
"@xarc/react": "^0.2.1",
"@xarc/react-query": "^0.2.1",
"@xarc/react-redux": "^0.2.1"
"@xarc/react": "^0.3.0",
"@xarc/react-query": "^0.3.0",
"@xarc/react-redux": "^0.3.0"
},
"devDependencies": {
"@types/node": "^14.14.6",
"@xarc/app-dev": "^10.0.3",
"@xarc/app-dev": "^10.0.7",
"@xarc/opt-postcss": "^1.0.0",
"@xarc/opt-stylus": "^1.0.0",
"prettier": "^2.2.1",
Expand Down

0 comments on commit f71e4f8

Please sign in to comment.