Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.

Commit 512f3aa

Browse files
author
Matt Goo
committed
fix(infrastructure): condense webpack configuration build step into one json file (#571)
BREAKING CHANGE: Remove <package-name>.es.js files. The alternate is now to use the index.tsx files.
1 parent 8cb4b1b commit 512f3aa

File tree

4 files changed

+65
-28
lines changed

4 files changed

+65
-28
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ If you're using an older version (v1) of `create-react-app`, please refer to our
6060
6161
#### Step 1: Install create-react-app
6262

63+
> _NOTE:_ all npm commands can be replaced with yarn
64+
6365
Install `create-react-app`:
6466

6567
```
@@ -68,7 +70,14 @@ cd my-app
6870
npm start
6971
```
7072

71-
> _NOTE:_ all npm commands can be replaced with yarn
73+
##### Use with Typescript
74+
75+
It is recommended to use Typescript with our components. You will need to add a few more modules for this to work"
76+
77+
```
78+
npm i @types/react @types/react-dom @types/classnames @types/node typescript
79+
npm start
80+
```
7281

7382
#### Step 2: Install Components
7483

packages/webpack.config.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,34 @@ const getAbsolutePath = (url) => path.resolve(__dirname, url);
4444
const filename = '[name]';
4545

4646
function getWebpackConfigs() {
47-
const webpackConfigs = [];
47+
const webpackEntries = {};
48+
const cssWebpackEntries = {};
49+
const cssWebpackEntriesMin = {};
50+
const webpackConfig = getJavaScriptWebpackConfig();
51+
const webpackConfigCss = getCssWebpackConfig();
52+
const webpackConfigCssMin = getCssWebpackConfig(true);
4853

4954
chunks.forEach((chunk) => {
5055
const tsxPath = getAbsolutePath(`${chunk}/index.tsx`);
5156
const cssPath = getAbsolutePath(`${chunk}/index.scss`);
52-
53-
webpackConfigs.push(getJavaScriptWebpackConfig(tsxPath, chunk, 'commonjs'));
54-
webpackConfigs.push(getJavaScriptWebpackConfig(tsxPath, chunk, false));
55-
webpackConfigs.push(getJavaScriptWebpackConfig(tsxPath, `${chunk}.min`, 'commonjs'));
56-
57-
webpackConfigs.push(getCssWebpackConfig(cssPath, chunk));
58-
webpackConfigs.push(getCssWebpackConfig(cssPath, `${chunk}.min`));
57+
webpackEntries[chunk] = tsxPath;
58+
webpackEntries[`${chunk}.min`] = tsxPath;
59+
cssWebpackEntries[chunk] = cssPath;
60+
cssWebpackEntriesMin[`${chunk}.min`] = cssPath;
5961
});
6062

61-
return webpackConfigs;
63+
webpackConfig.entry = webpackEntries;
64+
webpackConfigCss.entry = cssWebpackEntries;
65+
webpackConfigCssMin.entry = cssWebpackEntriesMin;
66+
67+
return [webpackConfig, webpackConfigCss, webpackConfigCssMin];
6268
}
6369

64-
function getCommonWebpackParams(entryPath, chunk, {isCss, modules}) {
65-
const entry = {[chunk]: entryPath};
70+
function getCommonWebpackParams({isCss} = {}) {
6671
return {
67-
entry,
6872
output: {
6973
path: getAbsolutePath('../build'),
70-
filename: `${filename}${isCss ? '.css' : ''}${modules === false ? '.es' : ''}.js`,
74+
filename: `${filename}${isCss ? '.css' : ''}.js`,
7175
libraryTarget: 'umd',
7276
},
7377
resolve: {
@@ -97,21 +101,36 @@ function getMaterialExternals() {
97101
return externals;
98102
}
99103

100-
function getJavaScriptWebpackConfig(entryPath, chunk, modules) {
104+
const materialExternals = getMaterialExternals();
105+
106+
function getJavaScriptWebpackConfig() {
101107
return Object.assign(
102-
getCommonWebpackParams(entryPath, chunk, {modules}), {
108+
getCommonWebpackParams(), {
103109
externals: Object.assign(
104110
{
105111
'react': 'react',
106112
'classnames': 'classnames',
107113
'prop-types': 'prop-types',
114+
'@material/textfield/constants': `@material/textfield/constants.js`,
108115
},
109-
getMaterialExternals(),
116+
materialExternals,
110117
),
111118
module: {
112119
rules: [{
113120
test: /\.ts(x)?$/,
114121
loader: 'ts-loader',
122+
}, {
123+
test: /\.js$/,
124+
loader: 'babel-loader',
125+
options: {
126+
babelrc: false,
127+
compact: true,
128+
presets: [['es2015', {modules: false}], 'react'],
129+
plugins: [
130+
'transform-class-properties',
131+
'transform-object-rest-spread',
132+
],
133+
},
115134
}],
116135
},
117136
plugins: [
@@ -122,10 +141,9 @@ function getJavaScriptWebpackConfig(entryPath, chunk, modules) {
122141
});
123142
}
124143

125-
function getCssWebpackConfig(entryPath, chunk) {
126-
const shouldMinify = chunk.includes('.min');
144+
function getCssWebpackConfig(shouldMinify) {
127145
return Object.assign(
128-
getCommonWebpackParams(entryPath, chunk, {isCss: true}), {
146+
getCommonWebpackParams({isCss: true}), {
129147
module: {
130148
rules: [{
131149
test: /\.scss$/,
@@ -155,5 +173,4 @@ function getCssWebpackConfig(entryPath, chunk) {
155173
});
156174
}
157175

158-
159176
module.exports = getWebpackConfigs();

scripts/release/cp-pkgs.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ function cpAsset(asset) {
8080
.then(() => console.log(`cp ${asset} -> ${destDir}`));
8181
}
8282

83+
// this takes a file path to an index.d.ts file and adds an //@ts-ignore comment
84+
// above the MDC Web imports (any line that includes `/dist/`). We need to ignore
85+
// these lines since MDC Web does not have typing files
86+
// TODO: https://github.com/material-components/material-components-web-react/issues/574
87+
function addTsIgnore(filePath) {
88+
const data = fs.readFileSync(filePath).toString().split('\n');
89+
const lineNumber = data.findIndex((lineText) => lineText.includes('/dist/'));
90+
if (lineNumber <= -1) return;
91+
92+
data.splice(lineNumber, 0, '// @ts-ignore');
93+
const text = data.join('\n');
94+
fs.writeFile(filePath, text, function(err) {
95+
if (err) return console.log(err);
96+
});
97+
}
98+
8399
// takes assetPath, computes the destination file directory path
84100
// and copies file into destination directory
85101
function cpTypes(typeAsset) {
@@ -88,6 +104,7 @@ function cpTypes(typeAsset) {
88104
destDir = destDir.split('/');
89105
destDir.splice(2, 0, 'dist');
90106
destDir = `${destDir.join('/')}/${base}`;
107+
addTsIgnore(typeAsset);
91108
return cpFile(typeAsset, destDir)
92109
.then(() => console.log(`cp ${typeAsset} -> ${destDir}`));
93110
}

tsconfig.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"extends": "gts/tsconfig-google.json",
33
"compilerOptions": {
44
"outDir": "./types/",
5-
"declaration": true,
65
"sourceMap": true,
76
"strictNullChecks": true,
87
"module": "commonjs",
@@ -17,10 +16,5 @@
1716
"exclude": [
1817
"node_modules",
1918
"build"
20-
],
21-
"baseUrl": ".",
22-
"paths": {
23-
"@material/react-ripple": ["packages/ripple/"],
24-
"@material/react-material-icon": ["packages/material-icon/"],
25-
}
19+
]
2620
}

0 commit comments

Comments
 (0)