Skip to content

Instantly share code, notes, and snippets.

@b2whats
Last active June 16, 2020 19:22
Show Gist options
  • Save b2whats/142a765eb688bea2afa2e0f34b9a5d99 to your computer and use it in GitHub Desktop.
Save b2whats/142a765eb688bea2afa2e0f34b9a5d99 to your computer and use it in GitHub Desktop.
publish build componets
/* eslint-disable no-console */
const resolver = require('babel-plugin-module-resolver')
const resolve = require('enhanced-resolve')
const path = require('path')
const { DEBUG, BABEL_ENV, NODE_ENV, TARGET } = process.env
const isProduction = NODE_ENV === 'production'
const isServer = TARGET === 'server'
const resolverTs = resolve.create.sync({
extensions: ['.ts', '.tsx', '.js', '.json'],
})
const updateExtension = (sourcePath, currentFile, opts) => {
if (!sourcePath.startsWith('.')) return null
const resolvedPath = resolverTs(path.dirname(currentFile), sourcePath)
if (!resolvedPath.includes('index')) {
return sourcePath + '.esm'
}
return null
}
const config = {
presets: [
[
'@babel/preset-env',
{
targets: {
browsers: ['last 1 version'],
},
loose: true,
modules: BABEL_ENV === 'cjs' || isServer ? 'commonjs' : false,
debug: Boolean(DEBUG),
},
],
'@babel/preset-typescript',
'@babel/preset-react',
[
'@emotion/babel-preset-css-prop',
{
'sourceMap': isServer ? false : true,
},
],
],
plugins: [
['@babel/transform-runtime', {
useESModules: BABEL_ENV === 'esm',
}],
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-syntax-dynamic-import',
['module-resolver', {
root: ['./'], // Установить корень проекта
alias: isServer && {
'^@avito/([^/]+)$': './packages/\\1/src',
},
resolvePath: BABEL_ENV === 'esm' && updateExtension,
}],
],
}
if (DEBUG) {
console.log(
`\n\nLoaded ${__dirname}/babel.config.js\n`,
JSON.stringify(config, null, 2)
)
}
module.exports = config
const path = require('path')
const fs = require('fs').promises
const glob = require('glob')
const packagePath = process.cwd()
const relative = (sourcePath) => path.relative(path.resolve(process.cwd(), '../../'), sourcePath)
const buildPath = path.join(packagePath, './build')
const srcPath = path.join(packagePath, './src')
async function exists(sourcePath) {
return await fs.stat(sourcePath)
}
async function createModulePackages({ from, to }) {
const directoryPackages = glob.sync('*/**/index.ts', { cwd: from }).map(path.dirname)
await Promise.all(
directoryPackages.map(async (directoryPackage) => {
const packageJson = {
sideEffects: false,
main: './index.js',
module: './index.esm.js',
typings: './index.d.ts',
}
const packageJsonPath = path.join(to, directoryPackage, 'package.json')
await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2))
console.log(`Create ${relative(packageJsonPath)}`)
return packageJsonPath
})
)
}
async function copyOtherFile(files) {
await Promise.all(
files.map(async (file) => {
const sourcePath = path.resolve(packagePath, file)
const targetPath = path.resolve(buildPath, file)
if (!(await exists(sourcePath))) return
await fs.copyFile(sourcePath, targetPath)
console.log(`Copied ${relative(sourcePath)} to ${relative(targetPath)}`)
})
)
}
async function copyPackageFile() {
const referecePath = path.resolve(packagePath, 'package.json')
const targetPath = path.resolve(buildPath, 'package.json')
await fs.copyFile(referecePath, targetPath)
console.log(`Copied package.json in ${relative(targetPath)}`)
}
async function run() {
try {
if (!(await exists(buildPath))) {
console.warn(`Build dir ${buildPath} does not exists`)
}
await copyPackageFile()
await copyOtherFile(['README.md','CHANGELOG.md'])
await createModulePackages({ from: srcPath, to: buildPath })
} catch (err) {
console.error(err)
process.exit(1)
}
}
run()
"main": "./index.js",
"module": "./index.esm.js",
"types": "./index.d.ts",
"publishConfig": {
"directory": "build"
},
"scripts": {
"build": "yarn build:clean && yarn build:cjs & yarn build:esm & yarn build:types && yarn build:copy",
"build:clean": "rm -rf ./build",
"build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
"build:cjs": "cross-env NODE_ENV=production BABEL_ENV=cjs babel src --out-dir build --extensions \".ts,.tsx\" --source-maps --root-mode=upward",
"build:esm": "cross-env NODE_ENV=production BABEL_ENV=esm babel src --out-dir build --extensions \".ts,.tsx\" --source-maps --root-mode=upward --out-file-extension .esm.js",
"build:copy": "node ../../scripts/copy-files.js",
"test": "echo \"Error: no test specified\" && exit 1",
"prepublishOnly": "yarn build",
"typecheck": "tsc --noEmit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment