From cce90f18f930c9d7fc0509bb8c97656cfa895fa1 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Tue, 5 Feb 2019 09:24:37 +0100 Subject: [PATCH] [core] Use common copy-files script (#14406) * [core] Add full esmodules build * [core] Add support for yarn workspaces run * Revert "[core] Add support for yarn workspaces run" This reverts commit e5653d7099bcd628b9b0d42f785d9fbd69b9fba7. * [core] Update size-limit * [core] esnext -> es * [core] Fix incorrect bundle sizes from merge * [core] Normalize build process * [core] sort scripts alphanumberic * [core] Add size test for esm build * [core] Fix unresolved module entry * [core] revert size limit changes for cjs build * [core] Fix increased size limit * [core] update size limits * [core] Normalize build scripts in icons and docs * fix size limit * fix utils build * [core] Use single workspace copy-files * [scripts] extract paths * [scripts] Use same copy-files in workspace * [scripts] warn for missing typings instead of throwing Box is internal and not documented yet * [scripts] improve method naming * [scripts] Fix module path * [scripts] Use typings instead of types they are synomous but typings was used in core so we stick with it * [scripts] Use workspace root readme for core only * [scripts] warn -> error if no typings exists * [scripts] cleanup diff * [scripts] improve code comments * alternative implementation proposal --- packages/material-ui-docs/package.json | 2 +- .../material-ui-docs/scripts/copy-files.js | 53 ------- .../src/MarkdownElement/index.d.ts | 0 .../src/NProgressBar/index.d.ts | 0 packages/material-ui-docs/src/index.d.ts | 0 packages/material-ui-icons/package.json | 2 +- .../material-ui-icons/scripts/copy-files.js | 55 ------- packages/material-ui-lab/package.json | 2 +- .../material-ui-lab/scripts/copy-files.js | 107 ------------- packages/material-ui-styles/package.json | 2 +- .../material-ui-styles/scripts/copy-files.js | 69 --------- packages/material-ui-system/package.json | 2 +- .../material-ui-system/scripts/copy-files.js | 68 --------- packages/material-ui-utils/package.json | 2 +- .../material-ui-utils/scripts/copy-files.js | 68 --------- packages/material-ui/.size-snapshot.json | 6 +- packages/material-ui/package.json | 2 +- packages/material-ui/scripts/copy-files.js | 114 -------------- scripts/copy-files.js | 143 ++++++++++++++++++ 19 files changed, 153 insertions(+), 544 deletions(-) delete mode 100644 packages/material-ui-docs/scripts/copy-files.js create mode 100644 packages/material-ui-docs/src/MarkdownElement/index.d.ts create mode 100644 packages/material-ui-docs/src/NProgressBar/index.d.ts create mode 100644 packages/material-ui-docs/src/index.d.ts delete mode 100644 packages/material-ui-icons/scripts/copy-files.js delete mode 100644 packages/material-ui-lab/scripts/copy-files.js delete mode 100644 packages/material-ui-styles/scripts/copy-files.js delete mode 100644 packages/material-ui-system/scripts/copy-files.js delete mode 100644 packages/material-ui-utils/scripts/copy-files.js delete mode 100644 packages/material-ui/scripts/copy-files.js create mode 100644 scripts/copy-files.js diff --git a/packages/material-ui-docs/package.json b/packages/material-ui-docs/package.json index 7ba7f99dd1b298..86709f0958a7f8 100644 --- a/packages/material-ui-docs/package.json +++ b/packages/material-ui-docs/package.json @@ -25,7 +25,7 @@ "build": "yarn build:cjs && yarn build:esm && yarn build:copy-files", "build:cjs": "cross-env NODE_ENV=production BABEL_ENV=cjs babel --config-file ../../babel.config.js ./src --out-dir ./build --ignore \"**/*.test.js\"", "build:esm": "cross-env NODE_ENV=production BABEL_ENV=esm babel --config-file ../../babel.config.js ./src --out-dir ./build/esm --ignore \"**/*.test.js\"", - "build:copy-files": "babel-node --config-file ../../babel.config.js ./scripts/copy-files.js", + "build:copy-files": "node ../../scripts/copy-files.js", "prebuild": "rimraf build", "release": "yarn build && npm publish build", "test": "exit 0" diff --git a/packages/material-ui-docs/scripts/copy-files.js b/packages/material-ui-docs/scripts/copy-files.js deleted file mode 100644 index 51841eac171da6..00000000000000 --- a/packages/material-ui-docs/scripts/copy-files.js +++ /dev/null @@ -1,53 +0,0 @@ -/* eslint-disable no-console */ - -import path from 'path'; -import fse from 'fs-extra'; - -async function copyFile(file) { - const buildPath = path.resolve(__dirname, '../build/', path.basename(file)); - await fse.copy(file, buildPath); - console.log(`Copied ${file} to ${buildPath}`); -} - -async function createPackageFile() { - const packageData = await fse.readFile(path.resolve(__dirname, '../package.json'), 'utf8'); - const { scripts, devDependencies, ...packageDataOther } = JSON.parse(packageData); - const newPackageData = { - ...packageDataOther, - main: './index.js', - module: './index.es.js', - }; - const buildPath = path.resolve(__dirname, '../build/package.json'); - - await fse.writeFile(buildPath, JSON.stringify(newPackageData, null, 2), 'utf8'); - console.log(`Created package.json in ${buildPath}`); - - return newPackageData; -} - -async function prepend(file, string) { - const data = await fse.readFile(file, 'utf8'); - await fse.writeFile(file, string + data, 'utf8'); -} - -async function addLicense(packageData) { - const license = `/** @license Material-UI v${packageData.version} - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -`; - await Promise.all( - ['../build/index.js', '../build/esm/index.js'].map(file => - prepend(path.resolve(__dirname, file), license), - ), - ); -} - -async function run() { - await ['README.md', '../../LICENSE'].map(file => copyFile(file)); - const packageData = await createPackageFile(); - await addLicense(packageData); -} - -run(); diff --git a/packages/material-ui-docs/src/MarkdownElement/index.d.ts b/packages/material-ui-docs/src/MarkdownElement/index.d.ts new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/material-ui-docs/src/NProgressBar/index.d.ts b/packages/material-ui-docs/src/NProgressBar/index.d.ts new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/material-ui-docs/src/index.d.ts b/packages/material-ui-docs/src/index.d.ts new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/material-ui-icons/package.json b/packages/material-ui-icons/package.json index 8b5c4aab8db27d..008c65b692b7d0 100644 --- a/packages/material-ui-icons/package.json +++ b/packages/material-ui-icons/package.json @@ -26,7 +26,7 @@ "build:cjs": "cross-env NODE_ENV=production BABEL_ENV=cjs babel --config-file ../../babel.config.js ./src --out-dir ./build --ignore \"**/*.test.js\"", "build:esm": "cross-env NODE_ENV=production BABEL_ENV=esm babel --config-file ../../babel.config.js ./src --out-dir ./build/esm --ignore \"**/*.test.js\"", "build:es": "cross-env NODE_ENV=production BABEL_ENV=es babel --config-file ../../babel.config.js ./src --out-dir ./build/es --ignore \"**/*.test.js\"", - "build:copy-files": "babel-node --config-file ../../babel.config.js ./scripts/copy-files.js", + "build:copy-files": "node ../../scripts/copy-files.js", "build:typings": "babel-node --config-file ../../babel.config.js ./scripts/create-typings.js", "prebuild": "rimraf material-design-icons && rimraf build", "release": "yarn build && npm publish build", diff --git a/packages/material-ui-icons/scripts/copy-files.js b/packages/material-ui-icons/scripts/copy-files.js deleted file mode 100644 index 38cc6d631c2056..00000000000000 --- a/packages/material-ui-icons/scripts/copy-files.js +++ /dev/null @@ -1,55 +0,0 @@ -/* eslint-disable no-console */ - -import path from 'path'; -import fse from 'fs-extra'; - -async function copyFile(file) { - const buildPath = path.resolve(__dirname, '../build/', path.basename(file)); - await fse.copy(file, buildPath); - console.log(`Copied ${file} to ${buildPath}`); -} - -async function createPackageFile() { - const packageData = await fse.readFile(path.resolve(__dirname, '../package.json'), 'utf8'); - const { scripts, devDependencies, ...packageDataOther } = JSON.parse(packageData); - const newPackageData = { - ...packageDataOther, - main: './index.js', - module: './index.es.js', - private: false, - typings: './index.d.ts', - }; - const buildPath = path.resolve(__dirname, '../build/package.json'); - - await fse.writeFile(buildPath, JSON.stringify(newPackageData, null, 2), 'utf8'); - console.log(`Created package.json in ${buildPath}`); - - return newPackageData; -} - -async function prepend(file, string) { - const data = await fse.readFile(file, 'utf8'); - await fse.writeFile(file, string + data, 'utf8'); -} - -async function addLicense(packageData) { - const license = `/** @license Material-UI v${packageData.version} - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -`; - await Promise.all( - ['../build/index.js', '../build/index.es.js'].map(file => - prepend(path.resolve(__dirname, file), license), - ), - ); -} - -async function run() { - await ['README.md', '../../LICENSE'].map(file => copyFile(file)); - const packageData = await createPackageFile(); - await addLicense(packageData); -} - -run(); diff --git a/packages/material-ui-lab/package.json b/packages/material-ui-lab/package.json index 6154caed242eb0..3989c14492f6e7 100644 --- a/packages/material-ui-lab/package.json +++ b/packages/material-ui-lab/package.json @@ -26,7 +26,7 @@ "build:cjs": "cross-env NODE_ENV=production BABEL_ENV=cjs babel --config-file ../../babel.config.js ./src --out-dir ./build --ignore \"**/*.test.js\"", "build:esm": "cross-env NODE_ENV=production BABEL_ENV=esm babel --config-file ../../babel.config.js ./src --out-dir ./build/esm --ignore \"**/*.test.js\"", "build:es": "cross-env NODE_ENV=production BABEL_ENV=es babel --config-file ../../babel.config.js ./src --out-dir ./build/es --ignore \"**/*.test.js\"", - "build:copy-files": "babel-node --config-file ../../babel.config.js ./scripts/copy-files.js", + "build:copy-files": "node ../../scripts/copy-files.js", "prebuild": "rimraf build", "release": "yarn build && npm publish build", "test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/material-ui-lab/**/*.test.js' --exclude '**/node_modules/**'", diff --git a/packages/material-ui-lab/scripts/copy-files.js b/packages/material-ui-lab/scripts/copy-files.js deleted file mode 100644 index e5746ab1787867..00000000000000 --- a/packages/material-ui-lab/scripts/copy-files.js +++ /dev/null @@ -1,107 +0,0 @@ -/* eslint-disable no-console */ - -import path from 'path'; -import fse from 'fs-extra'; -import glob from 'glob'; - -async function copyFile(file) { - const buildPath = path.resolve(__dirname, '../build/', path.basename(file)); - await fse.copy(file, buildPath); - console.log(`Copied ${file} to ${buildPath}`); -} - -/** - * Puts a package.json into every immediate child directory of rootDir. - * That package.json contains information about esm for bundlers so that imports - * like import Typography from '@material-ui/core/Typography' are tree-shakeable. - * - * It also tests that an this import can be used in typescript by checking - * if an index.d.ts is present at that path. - * - * @param {string} rootDir - */ -function createModulePackages(srcDir, outDir) { - const directoryPackages = glob.sync('*/index.js', { cwd: srcDir }).map(path.dirname); - return Promise.all( - directoryPackages.map(directoryPackage => { - const packageJson = { - sideEffects: false, - module: path.join('..', 'esm', directoryPackage, 'index.js'), - }; - const packageJsonPath = path.join(outDir, directoryPackage, 'package.json'); - - return Promise.all([ - fse.exists(path.join(outDir, directoryPackage, 'index.d.ts')), - fse.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)), - ]).then(([typingsExist]) => { - if (!typingsExist) { - return Promise.reject(new Error(`index.d.ts for ${directoryPackage} is missing`)); - } - return Promise.resolve(packageJsonPath); - }); - }), - ); -} - -function typescriptCopy(from, to) { - const files = glob.sync('**/*.d.ts', { cwd: from }); - const cmds = files.map(file => fse.copy(path.resolve(from, file), path.resolve(to, file))); - return Promise.all(cmds); -} - -async function createPackageFile() { - const packageData = await fse.readFile(path.resolve(__dirname, '../package.json'), 'utf8'); - const { scripts, devDependencies, ...packageDataOther } = JSON.parse(packageData); - const newPackageData = { - ...packageDataOther, - main: './index.js', - module: './esm/index.js', - private: false, - typings: './index.d.ts', - }; - const buildPath = path.resolve(__dirname, '../build/package.json'); - - await fse.writeFile(buildPath, JSON.stringify(newPackageData, null, 2), 'utf8'); - console.log(`Created package.json in ${buildPath}`); - - return newPackageData; -} - -async function prepend(file, string) { - const data = await fse.readFile(file, 'utf8'); - await fse.writeFile(file, string + data, 'utf8'); -} - -async function addLicense(packageData) { - const license = `/** @license Material-UI v${packageData.version} - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -`; - await Promise.all( - ['../build/index.js', '../build/esm/index.js'].map(file => - prepend(path.resolve(__dirname, file), license), - ), - ); -} - -async function run() { - await ['README.md', '../../LICENSE'].map(file => copyFile(file)); - const packageData = await createPackageFile(); - await addLicense(packageData); - - // TypeScript - const from = path.resolve(__dirname, '../src'); - await Promise.all([ - typescriptCopy(from, path.resolve(__dirname, '../build')), - typescriptCopy(from, path.resolve(__dirname, '../build/es')), - ]); - - await createModulePackages( - path.resolve(__dirname, '../src'), - path.resolve(__dirname, '../build'), - ); -} - -run(); diff --git a/packages/material-ui-styles/package.json b/packages/material-ui-styles/package.json index 74fbbf6db30fa0..885563279daaa9 100644 --- a/packages/material-ui-styles/package.json +++ b/packages/material-ui-styles/package.json @@ -26,7 +26,7 @@ "build:cjs": "cross-env NODE_ENV=production BABEL_ENV=cjs babel --config-file ../../babel.config.js ./src --out-dir ./build --ignore \"**/*.test.js\"", "build:esm": "cross-env NODE_ENV=production BABEL_ENV=esm babel --config-file ../../babel.config.js ./src --out-dir ./build/esm --ignore \"**/*.test.js\"", "build:es": "cross-env NODE_ENV=production BABEL_ENV=es babel --config-file ../../babel.config.js ./src --out-dir ./build/es --ignore \"**/*.test.js\"", - "build:copy-files": "babel-node --config-file ../../babel.config.js ./scripts/copy-files.js", + "build:copy-files": "node ../../scripts/copy-files.js", "prebuild": "rimraf build", "release": "yarn build && npm publish build", "test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/material-ui-styles/**/*.test.js' --exclude '**/node_modules/**'", diff --git a/packages/material-ui-styles/scripts/copy-files.js b/packages/material-ui-styles/scripts/copy-files.js deleted file mode 100644 index 85fdb65904d8de..00000000000000 --- a/packages/material-ui-styles/scripts/copy-files.js +++ /dev/null @@ -1,69 +0,0 @@ -/* eslint-disable no-console */ - -import path from 'path'; -import fse from 'fs-extra'; -import glob from 'glob'; - -async function copyFile(file) { - const buildPath = path.resolve(__dirname, '../build/', path.basename(file)); - await fse.copy(file, buildPath); - console.log(`Copied ${file} to ${buildPath}`); -} - -function typescriptCopy(from, to) { - const files = glob.sync('**/*.d.ts', { cwd: from }); - const cmds = files.map(file => fse.copy(path.resolve(from, file), path.resolve(to, file))); - return Promise.all(cmds); -} - -async function createPackageFile() { - const packageData = await fse.readFile(path.resolve(__dirname, '../package.json'), 'utf8'); - const { scripts, devDependencies, ...packageDataOther } = JSON.parse(packageData); - const newPackageData = { - ...packageDataOther, - main: './index.js', - module: './esm/index.js', - private: false, - typings: './index.d.ts', - }; - const buildPath = path.resolve(__dirname, '../build/package.json'); - - await fse.writeFile(buildPath, JSON.stringify(newPackageData, null, 2), 'utf8'); - console.log(`Created package.json in ${buildPath}`); - - return newPackageData; -} - -async function prepend(file, string) { - const data = await fse.readFile(file, 'utf8'); - await fse.writeFile(file, string + data, 'utf8'); -} - -async function addLicense(packageData) { - const license = `/** @license Material-UI v${packageData.version} - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -`; - await Promise.all( - ['../build/index.js', '../build/esm/index.js'].map(file => - prepend(path.resolve(__dirname, file), license), - ), - ); -} - -async function run() { - await ['README.md', '../../LICENSE'].map(file => copyFile(file)); - const packageData = await createPackageFile(); - await addLicense(packageData); - - // TypeScript - const from = path.resolve(__dirname, '../src'); - await Promise.all([ - typescriptCopy(from, path.resolve(__dirname, '../build')), - typescriptCopy(from, path.resolve(__dirname, '../build/es')), - ]); -} - -run(); diff --git a/packages/material-ui-system/package.json b/packages/material-ui-system/package.json index 4074095bd600fe..a445e7875c4fcd 100644 --- a/packages/material-ui-system/package.json +++ b/packages/material-ui-system/package.json @@ -26,7 +26,7 @@ "build:cjs": "cross-env NODE_ENV=production BABEL_ENV=cjs babel --config-file ../../babel.config.js ./src --out-dir ./build --ignore \"**/*.test.js\"", "build:esm": "cross-env NODE_ENV=production BABEL_ENV=esm babel --config-file ../../babel.config.js ./src --out-dir ./build/esm --ignore \"**/*.test.js\"", "build:es": "cross-env NODE_ENV=production BABEL_ENV=es babel --config-file ../../babel.config.js ./src --out-dir ./build/es --ignore \"**/*.test.js\"", - "build:copy-files": "babel-node --config-file ../../babel.config.js ./scripts/copy-files.js", + "build:copy-files": "node ../../scripts/copy-files.js", "prebuild": "rimraf build", "release": "yarn build && npm publish build", "test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/material-ui-system/**/*.test.js' --exclude '**/node_modules/**'" diff --git a/packages/material-ui-system/scripts/copy-files.js b/packages/material-ui-system/scripts/copy-files.js deleted file mode 100644 index 36759654258700..00000000000000 --- a/packages/material-ui-system/scripts/copy-files.js +++ /dev/null @@ -1,68 +0,0 @@ -/* eslint-disable no-console */ - -import path from 'path'; -import fse from 'fs-extra'; -import glob from 'glob'; - -async function copyFile(file) { - const buildPath = path.resolve(__dirname, '../build/', path.basename(file)); - await fse.copy(file, buildPath); - console.log(`Copied ${file} to ${buildPath}`); -} - -function typescriptCopy(from, to) { - const files = glob.sync('**/*.d.ts', { cwd: from }); - const cmds = files.map(file => fse.copy(path.resolve(from, file), path.resolve(to, file))); - return Promise.all(cmds); -} - -async function createPackageFile() { - const packageData = await fse.readFile(path.resolve(__dirname, '../package.json'), 'utf8'); - const { scripts, devDependencies, ...packageDataOther } = JSON.parse(packageData); - const newPackageData = { - ...packageDataOther, - main: './index.js', - module: './esm/index.js', - private: false, - }; - const buildPath = path.resolve(__dirname, '../build/package.json'); - - await fse.writeFile(buildPath, JSON.stringify(newPackageData, null, 2), 'utf8'); - console.log(`Created package.json in ${buildPath}`); - - return newPackageData; -} - -async function prepend(file, string) { - const data = await fse.readFile(file, 'utf8'); - await fse.writeFile(file, string + data, 'utf8'); -} - -async function addLicense(packageData) { - const license = `/** @license Material-UI v${packageData.version} - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -`; - await Promise.all( - ['../build/index.js', '../build/esm/index.js'].map(file => - prepend(path.resolve(__dirname, file), license), - ), - ); -} - -async function run() { - await ['README.md', '../../LICENSE'].map(file => copyFile(file)); - const packageData = await createPackageFile(); - await addLicense(packageData); - - // TypeScript - const from = path.resolve(__dirname, '../src'); - await Promise.all([ - typescriptCopy(from, path.resolve(__dirname, '../build')), - typescriptCopy(from, path.resolve(__dirname, '../build/es')), - ]); -} - -run(); diff --git a/packages/material-ui-utils/package.json b/packages/material-ui-utils/package.json index 8b8107deaa2e70..8a72df578f1919 100644 --- a/packages/material-ui-utils/package.json +++ b/packages/material-ui-utils/package.json @@ -26,7 +26,7 @@ "build:cjs": "cross-env NODE_ENV=production BABEL_ENV=cjs babel --config-file ../../babel.config.js ./src --out-dir ./build --ignore \"**/*.test.js\"", "build:esm": "cross-env NODE_ENV=production BABEL_ENV=esm babel --config-file ../../babel.config.js ./src --out-dir ./build/esm --ignore \"**/*.test.js\"", "build:es": "cross-env NODE_ENV=production BABEL_ENV=es babel --config-file ../../babel.config.js ./src --out-dir ./build/es --ignore \"**/*.test.js\"", - "build:copy-files": "babel-node --config-file ../../babel.config.js ./scripts/copy-files.js", + "build:copy-files": "node ../../scripts/copy-files.js", "prebuild": "rimraf build", "release": "yarn build && npm publish build", "test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/material-ui-utils/**/*.test.js' --exclude '**/node_modules/**'" diff --git a/packages/material-ui-utils/scripts/copy-files.js b/packages/material-ui-utils/scripts/copy-files.js deleted file mode 100644 index f3c96018a2f9a9..00000000000000 --- a/packages/material-ui-utils/scripts/copy-files.js +++ /dev/null @@ -1,68 +0,0 @@ -/* eslint-disable no-console */ - -import path from 'path'; -import fse from 'fs-extra'; -import glob from 'glob'; - -async function copyFile(file) { - const buildPath = path.resolve(__dirname, '../build/', path.basename(file)); - await fse.copy(file, buildPath); - console.log(`Copied ${file} to ${buildPath}`); -} - -function typescriptCopy(from, to) { - const files = glob.sync('**/*.d.ts', { cwd: from }); - const cmds = files.map(file => fse.copy(path.resolve(from, file), path.resolve(to, file))); - return Promise.all(cmds); -} - -async function createPackageFile() { - const packageData = await fse.readFile(path.resolve(__dirname, '../package.json'), 'utf8'); - const { scripts, devDependencies, ...packageDataOther } = JSON.parse(packageData); - const newPackageData = { - ...packageDataOther, - main: './index.js', - module: './esm/index.js', - private: false, - }; - const buildPath = path.resolve(__dirname, '../build/package.json'); - - await fse.writeFile(buildPath, JSON.stringify(newPackageData, null, 2), 'utf8'); - console.log(`Created package.json in ${buildPath}`); - - return newPackageData; -} - -async function prepend(file, string) { - const data = await fse.readFile(file, 'utf8'); - await fse.writeFile(file, string + data, 'utf8'); -} - -async function addLicense(packageData) { - const license = `/** @license Material-UI v${packageData.version} - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -`; - await Promise.all( - ['../build/index.js', '../esm/index.js'].map(file => - prepend(path.resolve(__dirname, file), license), - ), - ); -} - -async function run() { - await ['README.md', '../../LICENSE'].map(file => copyFile(file)); - const packageData = await createPackageFile(); - await addLicense(packageData); - - // TypeScript - const from = path.resolve(__dirname, '../src'); - await Promise.all([ - typescriptCopy(from, path.resolve(__dirname, '../build')), - typescriptCopy(from, path.resolve(__dirname, '../build/es')), - ]); -} - -run(); diff --git a/packages/material-ui/.size-snapshot.json b/packages/material-ui/.size-snapshot.json index 992bd67b5ce3f9..88410e4c757953 100644 --- a/packages/material-ui/.size-snapshot.json +++ b/packages/material-ui/.size-snapshot.json @@ -1,7 +1,7 @@ { "build/umd/material-ui.production.min.js": { - "bundled": 868516, - "minified": 320858, - "gzipped": 84838 + "bundled": 867738, + "minified": 320738, + "gzipped": 84774 } } diff --git a/packages/material-ui/package.json b/packages/material-ui/package.json index 5ef5db66c758b1..205303c822b14d 100644 --- a/packages/material-ui/package.json +++ b/packages/material-ui/package.json @@ -25,7 +25,7 @@ "build:esm": "cross-env NODE_ENV=production BABEL_ENV=esm babel --config-file ../../babel.config.js ./src --out-dir ./build/esm --ignore \"**/*.test.js\"", "build:es": "cross-env NODE_ENV=production BABEL_ENV=es babel --config-file ../../babel.config.js ./src --out-dir ./build/es --ignore \"**/*.test.js\"", "build:umd": "cross-env BABEL_ENV=production-umd rollup -c scripts/rollup.config.js", - "build:copy-files": "babel-node --config-file ../../babel.config.js ./scripts/copy-files.js", + "build:copy-files": "node ../../scripts/copy-files.js", "prebuild": "rimraf build", "release": "yarn build && npm publish build", "test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/material-ui/**/*.test.js' --exclude '**/node_modules/**'", diff --git a/packages/material-ui/scripts/copy-files.js b/packages/material-ui/scripts/copy-files.js deleted file mode 100644 index e206cb092a7cda..00000000000000 --- a/packages/material-ui/scripts/copy-files.js +++ /dev/null @@ -1,114 +0,0 @@ -/* eslint-disable no-console */ - -import path from 'path'; -import fse from 'fs-extra'; -import glob from 'glob'; - -async function copyFile(file) { - const buildPath = path.resolve(__dirname, '../build/', path.basename(file)); - await fse.copy(file, buildPath); - console.log(`Copied ${file} to ${buildPath}`); -} - -/** - * Puts a package.json into every immediate child directory of rootDir. - * That package.json contains information about esm for bundlers so that imports - * like import Typography from '@material-ui/core/Typography' are tree-shakeable. - * - * It also tests that an this import can be used in typescript by checking - * if an index.d.ts is present at that path. - * - * @param {string} rootDir - */ -function createModulePackages(srcDir, outDir) { - const directoryPackages = glob.sync('*/index.js', { cwd: srcDir }).map(path.dirname); - return Promise.all( - directoryPackages.map(directoryPackage => { - const packageJson = { - sideEffects: false, - module: path.join('..', 'esm', directoryPackage, 'index.js'), - }; - const packageJsonPath = path.join(outDir, directoryPackage, 'package.json'); - - return Promise.all([ - fse.exists(path.join(outDir, directoryPackage, 'index.d.ts')), - fse.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)), - ]).then(([typingsExist]) => { - if (!typingsExist) { - return Promise.reject(new Error(`index.d.ts for ${directoryPackage} is missing`)); - } - return Promise.resolve(packageJsonPath); - }); - }), - ); -} - -function typescriptCopy(from, to) { - const files = glob.sync('**/*.d.ts', { cwd: from }); - const cmds = files.map(file => fse.copy(path.resolve(from, file), path.resolve(to, file))); - return Promise.all(cmds); -} - -async function createPackageFile() { - const packageData = await fse.readFile(path.resolve(__dirname, '../package.json'), 'utf8'); - const { nyc, scripts, devDependencies, workspaces, ...packageDataOther } = JSON.parse( - packageData, - ); - const newPackageData = { - ...packageDataOther, - main: './index.js', - module: './esm/index.js', - private: false, - typings: './index.d.ts', - }; - const buildPath = path.resolve(__dirname, '../build/package.json'); - - await fse.writeFile(buildPath, JSON.stringify(newPackageData, null, 2), 'utf8'); - console.log(`Created package.json in ${buildPath}`); - - return newPackageData; -} - -async function prepend(file, string) { - const data = await fse.readFile(file, 'utf8'); - await fse.writeFile(file, string + data, 'utf8'); -} - -async function addLicense(packageData) { - const license = `/** @license Material-UI v${packageData.version} - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -`; - await Promise.all( - [ - '../build/index.js', - '../build/esm/index.js', - '../build/umd/material-ui.development.js', - '../build/umd/material-ui.production.min.js', - ].map(file => prepend(path.resolve(__dirname, file), license)), - ); -} - -async function run() { - await Promise.all( - ['../../README.md', '../../CHANGELOG.md', '../../LICENSE'].map(file => copyFile(file)), - ); - const packageData = await createPackageFile(); - await addLicense(packageData); - - // TypeScript - const from = path.resolve(__dirname, '../src'); - await Promise.all([ - typescriptCopy(from, path.resolve(__dirname, '../build')), - typescriptCopy(from, path.resolve(__dirname, '../build/es')), - ]); - - await createModulePackages( - path.resolve(__dirname, '../src'), - path.resolve(__dirname, '../build'), - ); -} - -run(); diff --git a/scripts/copy-files.js b/scripts/copy-files.js new file mode 100644 index 00000000000000..9a100ef8748446 --- /dev/null +++ b/scripts/copy-files.js @@ -0,0 +1,143 @@ +/* eslint-disable no-console */ +const path = require('path'); +const fse = require('fs-extra'); +const glob = require('glob'); + +const packagePath = process.cwd(); +const buildPath = path.join(packagePath, './build'); +const srcPath = path.join(packagePath, './src'); + +async function includeFileInBuild(file) { + const sourcePath = path.resolve(packagePath, file); + const targetPath = path.resolve(buildPath, path.basename(file)); + await fse.copy(sourcePath, targetPath); + console.log(`Copied ${sourcePath} to ${targetPath}`); +} + +/** + * Puts a package.json into every immediate child directory of rootDir. + * That package.json contains information about esm for bundlers so that imports + * like import Typography from '@material-ui/core/Typography' are tree-shakeable. + * + * It also tests that an this import can be used in typescript by checking + * if an index.d.ts is present at that path. + * + * @param {string} rootDir + */ +async function createModulePackages({ from, to }) { + const directoryPackages = glob.sync('*/index.js', { cwd: from }).map(path.dirname); + + await Promise.all( + directoryPackages.map(async directoryPackage => { + const packageJson = { + sideEffects: false, + module: path.join('../esm', directoryPackage, 'index.js'), + }; + const packageJsonPath = path.join(to, directoryPackage, 'package.json'); + + const [typingsExist] = await Promise.all([ + fse.exists(path.join(to, directoryPackage, 'index.d.ts')), + fse.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)), + ]); + + if (!typingsExist) { + throw new Error(`index.d.ts for ${directoryPackage} is missing`); + } + + return packageJsonPath; + }), + ); +} + +async function typescriptCopy({ from, to }) { + if (!(await fse.exists(to))) { + console.warn(`path ${to} does not exists`); + return []; + } + + const files = glob.sync('**/*.d.ts', { cwd: from }); + const cmds = files.map(file => fse.copy(path.resolve(from, file), path.resolve(to, file))); + return Promise.all(cmds); +} + +async function createPackageFile() { + const packageData = await fse.readFile(path.resolve(packagePath, './package.json'), 'utf8'); + const { nyc, scripts, devDependencies, workspaces, ...packageDataOther } = JSON.parse( + packageData, + ); + const newPackageData = { + ...packageDataOther, + private: false, + main: './index.js', + module: './esm/index.js', + typings: './index.d.ts', + }; + const targetPath = path.resolve(buildPath, './package.json'); + + await fse.writeFile(targetPath, JSON.stringify(newPackageData, null, 2), 'utf8'); + console.log(`Created package.json in ${targetPath}`); + + return newPackageData; +} + +async function prepend(file, string) { + const data = await fse.readFile(file, 'utf8'); + await fse.writeFile(file, string + data, 'utf8'); +} + +async function addLicense(packageData) { + const license = `/** @license Material-UI v${packageData.version} + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +`; + await Promise.all( + [ + './index.js', + './esm/index.js', + './umd/material-ui.development.js', + './umd/material-ui.production.min.js', + ].map(async file => { + try { + await prepend(path.resolve(buildPath, file), license); + } catch (err) { + if (err.code === 'ENOENT') { + console.log(`Skipped license for ${file}`); + } else { + throw err; + } + } + }), + ); +} + +async function run() { + try { + const packageData = await createPackageFile(); + + await Promise.all( + [ + // use enhanced readme from workspace root for `@material-ui/core` + packageData.name === '@material-ui/core' ? '../../README.md' : './README.md', + '../../CHANGELOG.md', + '../../LICENSE', + ].map(file => includeFileInBuild(file)), + ); + + await addLicense(packageData); + + // TypeScript + await Promise.all([ + typescriptCopy({ from: srcPath, to: buildPath }), + typescriptCopy({ from: srcPath, to: path.resolve(buildPath, './es') }), + ]); + + await createModulePackages({ from: srcPath, to: buildPath }); + } catch (err) { + console.error(err); + process.exit(1); + } +} + +run();