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

css module rework #1751

Merged
merged 2 commits into from
Nov 3, 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
9 changes: 8 additions & 1 deletion packages/xarc-app-dev/src/config/archetype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ 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 };
Expand All @@ -25,7 +32,7 @@ function saveArchetypeConfig(config) {
//
}

const str = JSON.stringify(copy, null, 2);
const str = JSON.stringify(copy, jsonStringifyReplacer, 2);
if (str !== existStr) {
try {
createElectrodeTmpDir(config.eTmpDir);
Expand Down
19 changes: 6 additions & 13 deletions packages/xarc-app-dev/src/config/babel/babelrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* When transpiling for node.js, env XARC_BABEL_TARGET should be set to "node"
* and this file will set preset-env targets accordingly.
*/
const requireAt = require("require-at");
const ck = require("chalker");
const optionalRequire = require("optional-require")(require);
const optFlow = optionalRequire("electrode-archetype-opt-flow");
import { getPluginFrom, loadXarcOptions } from "./common";
import { getPluginFrom, loadXarcOptions, detectCSSModule } from "./common";
const _ = require("lodash");

const xOptions = loadXarcOptions(process.env.XARC_APP_DIR);
Expand All @@ -34,7 +33,7 @@ const addFlowPlugin = Boolean(enableFlow && optFlow);

const { BABEL_ENV, NODE_ENV, XARC_BABEL_TARGET, ENABLE_KARMA_COV } = process.env;

const enableCssModule = Boolean(_.get(xOptions, "webpack.cssModuleSupport"));
const enableCssModule = detectCSSModule(xOptions);
const enableKarmaCov = ENABLE_KARMA_COV === "true";
const isProduction = (BABEL_ENV || NODE_ENV) === "production";
const isTest = (BABEL_ENV || NODE_ENV) === "test";
Expand All @@ -58,20 +57,14 @@ const getReactCssModulePlugin = () => {
return null;
}

const postCssPath = optionalRequire.resolve("@xarc/opt-postcss/package.json");

if (!postCssPath) {
return null;
}

const babelReactCssModulePlugin = requireAt(postCssPath)("babel-plugin-react-css-modules");

const enableShortenCSSNames = xOptions.webpack.enableShortenCSSNames;
const enableShortHash = isProduction && enableShortenCSSNames;
return [
[
babelReactCssModulePlugin,
"babel-plugin-react-css-modules",
{
context: "./src",
generateScopedName: `${isProduction ? "" : "[name]__[local]___"}[hash:base64:5]`,
generateScopedName: `${enableShortHash ? "" : "[name]__[local]___"}[hash:base64:5]`,
filetypes: {
".scss": {
syntax: "postcss-scss",
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/src/config/babel/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ export function getPluginFrom(host, pluginName) {
throw err;
}

export { loadXarcOptions } from "../../lib/utils";
export { loadXarcOptions, detectCSSModule } from "../../lib/utils";
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/src/config/opt2/webpack-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type WebpackOptions = {
* - **Default: `undefined` (auto detect)**
* - If not set, then check env `CSS_MODULE_SUPPORT`
*/
cssModuleSupport?: boolean;
cssModuleSupport?: boolean | RegExp;

/**
* Enable loading `@babel/polyfill` for application
Expand Down
11 changes: 1 addition & 10 deletions packages/xarc-app-dev/src/lib/dev-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ export function loadXarcDevTasks(xrun, xarcOptions: XarcOptions = {}) {
// eslint-disable-next-line complexity
function makeTasks(xclap2) {
assert(xclap2.concurrent, "xclap version must be 0.2.28+");
process.env.ENABLE_CSS_MODULE = "false";
process.env.ENABLE_KARMA_COV = "false";

const checkFrontendCov = (minimum = "5") => {
Expand Down Expand Up @@ -442,7 +441,7 @@ module.exports = {
".static-files-env": () => setStaticFilesEnv(),
".remove-log-files": () => removeLogFiles(),
build: {
dep: [".remove-log-files", ".production-env", ".set.css-module.env"],
dep: [".remove-log-files", ".production-env"],
desc: `Build your app's ${AppMode.src.dir} directory into ${AppMode.lib.dir} for production`,
task: [".build-lib", "build-dist", ".check.top.level.babelrc", "mv-to-dist"]
},
Expand All @@ -460,12 +459,6 @@ module.exports = {
`--colors`
);
},
".set.css-module.env": () => {
const cssModule = detectCssModule();
if (cssModule) {
process.env.ENABLE_CSS_MODULE = "true";
}
},
"build-browser-coverage": {
desc: "Build browser coverage",
task: [
Expand All @@ -483,7 +476,6 @@ module.exports = {

"build-dist": [
".production-env",
".set.css-module.env",
".clean.build",
".mk-dist-dir",
".copy-xarc-options-to-dist",
Expand Down Expand Up @@ -817,7 +809,6 @@ You only need to run this if you are doing something not through the xarc tasks.
dep: [".remove-log-files", ".development-env", ".build.babelrc"],
task() {
return [
".set.css-module.env",
".webpack-dev",
[`server-admin ${this.args.join(" ")}`, "generate-service-worker"]
];
Expand Down
9 changes: 9 additions & 0 deletions packages/xarc-app-dev/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const logger = require("./logger");
const ck = require("chalker");
const Path = require("path");
const Fs = require("fs");
const _ = require("lodash");

const Url = require("url");

Expand Down Expand Up @@ -98,3 +99,11 @@ xarc's development code.
});
}
}

export function detectCSSModule(xOptions) {
const cssModuleSupport = _.get(xOptions, "webpack.cssModuleSupport", undefined);
if (cssModuleSupport === undefined) {
return true;
}
return Boolean(cssModuleSupport);
}
1 change: 1 addition & 0 deletions packages/xarc-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
],
"dependencies": {
"@babel/runtime": "^7.8.3",
"babel-plugin-react-css-modules": "^5.2.6",
"css-modules-require-hook": "^4.0.2",
"ignore-styles": "^5.0.1",
"isomorphic-loader": "^4.0.5",
Expand Down
1 change: 0 additions & 1 deletion packages/xarc-opt-postcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"Joel Chen <[email protected]>"
],
"dependencies": {
"babel-plugin-react-css-modules": "^5.2.6",
"postcss-import": "^12.0.1",
"postcss-less": "^3.1.4",
"postcss-loader": "^3.0.0",
Expand Down
Loading