Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] allow transpiling code under node_modules #1754

Merged
merged 1 commit into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 2 additions & 41 deletions packages/xarc-app-dev/src/config/archetype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,12 @@

import { XarcOptions } from "./opt2/xarc-options";
import { getDevArchetypeLegacy } from "./options";
import { createGitIgnoreDir } from "../lib/utils";
const Fs = require("fs");
const _ = require("lodash");
const getEnvProxy = require("./env-proxy");
const Path = require("path");
import { saveXarcOptions } from "../lib/utils";

let cachedArchetype = null;

function createElectrodeTmpDir(eTmpDir = ".etmp") {
createGitIgnoreDir(Path.resolve(eTmpDir), "Electrode tmp dir");
}

function jsonStringifyReplacer(key, value) {
if (value instanceof RegExp) {
return value.toString();
}
return value;
}

function saveArchetypeConfig(config) {
const filename = `${config.eTmpDir}/xarc-options.json`;
const copy = { ...config, pkg: undefined, devPkg: undefined };
let existStr;

try {
existStr = Fs.readFileSync(filename, "utf-8");
} catch (err) {
//
}

const str = JSON.stringify(copy, jsonStringifyReplacer, 2);
if (str !== existStr) {
try {
createElectrodeTmpDir(config.eTmpDir);

Fs.writeFileSync(filename, str);
} catch (err) {
console.error(
`Unable to save development options to ${filename} - this will cause other failures.\n`,
err
);
}
}
}

/**
* Get development options
*
Expand Down Expand Up @@ -82,7 +43,7 @@ module.exports = function getDevOptions(user: XarcOptions = {}) {
addOnFeatures: undefined
});

saveArchetypeConfig(legacy);
saveXarcOptions(legacy);

cachedArchetype = legacy;

Expand Down
16 changes: 16 additions & 0 deletions packages/xarc-app-dev/src/config/opt2/babel-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ export type BabelOptions = {
*/
transformClassProps?: boolean | { loose: boolean };

/**
* List of RegExp to match files that should be included for transpiling.
*
* By default, files under node_modules are not transpiled.
*
* You can set this to control that. ie: to compile everything under node_modules, use `[/node_modules/]`
*/
includeRegExp?: RegExp[];

/**
* List of RegExp to match files that should not be transpiled by babel.
*
* This is checked after includeRegExp
*/
excludeRegExp?: RegExp[];

// DEPRECATE: looseClassProps?: boolean;

// DEPRECATE:
Expand Down
50 changes: 49 additions & 1 deletion packages/xarc-app-dev/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ export function createGitIgnoreDir(dir, comment) {
}
}

const regExpSig = "@xarc/__RegExp__@";

export const jsonStringifier = (key, value) => {
if (value instanceof RegExp) {
return `${regExpSig}${value.toString()}`;
} else {
return value;
}
};

export const jsonParser = (key, value) => {
if (typeof value === "string" && value.startsWith(regExpSig)) {
return new RegExp(value.substr(regExpSig.length));
} else {
return value;
}
};

let cachedXarcOptions;

export function loadXarcOptions(dir: string = process.cwd()) {
Expand All @@ -78,7 +96,7 @@ export function loadXarcOptions(dir: string = process.cwd()) {
const filename = Path.join(dir, ".etmp/xarc-options.json");
try {
const data = Fs.readFileSync(filename, "utf-8");
return (cachedXarcOptions = JSON.parse(data));
return (cachedXarcOptions = JSON.parse(data, jsonParser));
} catch (err) {
// eslint-disable-next-line
console.error(ck`
Expand All @@ -100,6 +118,36 @@ xarc's development code.
}
}

function createElectrodeTmpDir(eTmpDir = ".etmp") {
createGitIgnoreDir(Path.resolve(eTmpDir), "Electrode tmp dir");
}

export function saveXarcOptions(config) {
const filename = `${config.eTmpDir}/xarc-options.json`;
const copy = { ...config, pkg: undefined, devPkg: undefined };
let existStr;

try {
existStr = Fs.readFileSync(filename, "utf-8");
} catch (err) {
//
}

const str = JSON.stringify(copy, jsonStringifier, 2);
if (str !== existStr) {
try {
createElectrodeTmpDir(config.eTmpDir);

Fs.writeFileSync(filename, str);
} catch (err) {
console.error(
`Unable to save development options to ${filename} - this will cause other failures.\n`,
err
);
}
}
}

export function detectCSSModule(xOptions) {
const cssModuleSupport = _.get(xOptions, "webpack.cssModuleSupport", undefined);
if (cssModuleSupport === undefined) {
Expand Down
27 changes: 23 additions & 4 deletions packages/xarc-webpack/src/partials/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,35 @@ const identity = require("lodash/identity");
const assign = require("lodash/assign");
const babelLoader = require.resolve("babel-loader");

import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

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

const clientVendor = Path.join(AppMode.src.client, "vendor/");
const babelExclude = x => {
if (x.indexOf("node_modules") >= 0) return true;
if (x.indexOf(clientVendor) >= 0) return true;
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;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as Url from "url";
// const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpackDevReporter = require("../util/webpack-dev-reporter");
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

const HTTP_PORT = 80;

Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/dll-entry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import * as Path from "path";
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

module.exports = () => {
const xarcOptions = loadXarcOptions();
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/dll-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as fs from "fs";
import * as Path from "path";
import * as webpack from "webpack";
const filterScanDir = require("filter-scan-dir");
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";
const logger = require("@xarc/app-dev/lib/logger");

module.exports = function(options) {
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const optionalRequire = require("optional-require")(require);
const chalk = require("chalk");
const logger = require("@xarc/app-dev/lib/logger");
const mkdirp = require("mkdirp");
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

const DEV_HMR_DIR = ".__dev_hmr";

Expand Down
20 changes: 7 additions & 13 deletions packages/xarc-webpack/src/partials/extract-style.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires, max-statements */
import * as Path from "path";

import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

const detectCssModule = require("../util/detect-css-module");

Expand Down Expand Up @@ -142,19 +142,13 @@ module.exports = function() {
{
_name: `extract-css`,
test: /\.css$/,
use: [
miniCssExtractLoader(false),
...getCssQueryUse(false)
],
...(enableCssModule && { exclude: cssModuleRegExp }),
use: [miniCssExtractLoader(false), ...getCssQueryUse(false)],
...(enableCssModule && { exclude: cssModuleRegExp })
},
enableCssModule && {
_name: `extract-css-modules`,
test: /\.css$/,
use: [
miniCssExtractLoader(true),
...getCssQueryUse(true)
],
use: [miniCssExtractLoader(true), ...getCssQueryUse(true)],
include: cssModuleRegExp
}
);
Expand All @@ -172,7 +166,7 @@ module.exports = function() {
miniCssExtractLoader(false),
...getCssQueryUse(false).concat({ loader: sassLoader } as any)
],
...(enableCssModule && { exclude: cssModuleRegExp }),
...(enableCssModule && { exclude: cssModuleRegExp })
},
enableCssModule && {
_name: `extract-css-modules-scss`,
Expand All @@ -198,7 +192,7 @@ module.exports = function() {
_name: `extract-css-stylus`,
test: /\.styl$/,
use: [miniCssExtractLoader(false), ...getCssQueryUse(false).concat(stylusQuery)],
...(enableCssModule && { exclude: cssModuleRegExp }),
...(enableCssModule && { exclude: cssModuleRegExp })
},
enableCssModule && {
_name: `extract-css-modules-stylus`,
Expand All @@ -221,7 +215,7 @@ module.exports = function() {
miniCssExtractLoader(false),
...getCssQueryUse(false).concat({ loader: lessLoader } as any)
],
...(enableCssModule && { exclude: cssModuleRegExp }),
...(enableCssModule && { exclude: cssModuleRegExp })
},
enableCssModule && {
_name: `extract-css-modules-less`,
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/fonts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";
import { xAppRequire } from "@xarc/app";

module.exports = function() {
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/isomorphic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import { xAppRequire } from "@xarc/app";
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

module.exports = function(opts) {
const xarcOptions = loadXarcOptions();
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/loadable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";
import * as LoadablePlugin from "@loadable/webpack-plugin";

module.exports = function() {
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";
const logger = require("@xarc/app-dev/lib/logger");

module.exports = function(options) {
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/output.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import * as Path from "path";
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

module.exports = () => {
const { babel } = loadXarcOptions();
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/prod-mode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

module.exports = function() {
const xarcOptions = loadXarcOptions();
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/pwa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as Path from "path";
const requireAt = require("require-at");
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

const assign = require("lodash/assign");
const fileLoader = require.resolve("file-loader");
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/resolve-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Path from "path";

const identity = require("lodash/identity");
const ModuleResolver = require("electrode-node-resolver/lib/webpack-plugin");
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

module.exports = () => {
const xarcOptions = loadXarcOptions();
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Path from "path";

const ModuleResolver = require("electrode-node-resolver/lib/webpack-plugin");
const identity = require("lodash/identity");
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

module.exports = () => {
const xarcOptions = loadXarcOptions();
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as _ from "lodash";

const mkdirp = require("mkdirp");
const INDENT = 2;
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

module.exports = function(opts) {
const xarcOptions = loadXarcOptions();
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/partials/subapp-chunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable global-require, no-magic-numbers */

import * as Crypto from "crypto";
import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

const splitMap = {};

Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-webpack/src/util/custom-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as Fs from "fs";
import * as Path from "path";

import { loadXarcOptions } from "../util/load-xarc-options";
import { loadXarcOptions } from "@xarc/app-dev/lib/utils";

function useAppWebpackConfig() {
return process.env.USE_APP_WEBPACK_CONFIG === "true";
Expand Down
Loading