Skip to content

Commit

Permalink
[v2] breaking: drop UMD/SystemJS builds and simplify babel config (#822)
Browse files Browse the repository at this point in the history
* [v2] breaking: drop UMD/SystemJS builds and simplify babel config

* format
  • Loading branch information
dai-shi authored Nov 14, 2023
1 parent 8902c4c commit 5e40034
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 107 deletions.
14 changes: 1 addition & 13 deletions .github/workflows/test-multiple-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
build: [cjs, esm, umd] # [cjs, esm, umd, system]
build: [cjs, esm]
env: [development] # [development, production]
steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -44,18 +44,6 @@ jobs:
sed -i~ "1s/^/import.meta.env=import.meta.env||{};import.meta.env.MODE='${NODE_ENV}';/" tests/*.ts tests/*.tsx
env:
NODE_ENV: ${{ matrix.env }}
- name: Patch for UMD
if: ${{ matrix.build == 'umd' }}
run: |
sed -i~ "s/<rootDir>\/src\(.*\)\.ts/<rootDir>\/dist\/umd\1.${NODE_ENV}.js/" package.json
env:
NODE_ENV: ${{ matrix.env }}
- name: Patch for SystemJS
if: ${{ matrix.build == 'system' }}
run: |
sed -i~ "s/<rootDir>\/src\(.*\)\.ts/<rootDir>\/dist\/system\1.${NODE_ENV}.js/" package.json
env:
NODE_ENV: ${{ matrix.env }}
- name: Test ${{ matrix.build }} ${{ matrix.env }}
run: |
yarn test:ci
Expand Down
28 changes: 0 additions & 28 deletions babel.config.js

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.5",
"@testing-library/react": "^14.0.0",
"@types/jsdom": "^21.1.4",
Expand Down
72 changes: 7 additions & 65 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ const alias = require('@rollup/plugin-alias')
const babelPlugin = require('@rollup/plugin-babel')
const resolve = require('@rollup/plugin-node-resolve')
const replace = require('@rollup/plugin-replace')
const terser = require('@rollup/plugin-terser')
const typescript = require('@rollup/plugin-typescript')
const { default: esbuild } = require('rollup-plugin-esbuild')
const createBabelConfig = require('./babel.config.js')

const extensions = ['.js', '.ts', '.tsx']
const { root } = path.parse(process.cwd())
Expand All @@ -23,7 +21,13 @@ function external(id) {

function getBabelOptions(targets) {
return {
...createBabelConfig({ env: (env) => env === 'build' }, targets),
babelrc: false,
ignore: ['./node_modules'],
presets: [['@babel/preset-env', { loose: true, modules: false, targets }]],
plugins: [
['@babel/plugin-transform-react-jsx', { runtime: 'automatic' }],
['@babel/plugin-transform-typescript', { isTSX: true }],
],
extensions,
comments: false,
babelHelpers: 'bundled',
Expand Down Expand Up @@ -98,64 +102,6 @@ function createCommonJSConfig(input, output) {
}
}

function createUMDConfig(input, output, env) {
let name = 'valtio'
const fileName = output.slice('dist/umd/'.length)
const capitalize = (s) => s.slice(0, 1).toUpperCase() + s.slice(1)
if (fileName !== 'index') {
name += fileName.replace(/(\w+)\W*/g, (_, p) => capitalize(p))
}
return {
input,
output: {
file: `${output}.${env}.js`,
format: 'umd',
name,
globals: {
react: 'React',
'valtio/vanilla': 'valtioVanilla',
'valtio/utils': 'valtioUtils',
'valtio/react': 'valtioReact',
'valtio/vanilla/utils': 'valtioVanillaUtils',
'valtio/react/utils': 'valtioReactUtils',
},
},
external,
plugins: [
alias({ entries: entries.filter((e) => !e.find.test(input)) }),
resolve({ extensions }),
replace({
'import.meta.env?.MODE': JSON.stringify(env),
delimiters: ['\\b', '\\b(?!(\\.|/))'],
preventAssignment: true,
}),
babelPlugin(getBabelOptions({ ie: 11 })),
...(env === 'production' ? [terser()] : []),
],
}
}

function createSystemConfig(input, output, env) {
return {
input,
output: {
file: `${output}.${env}.js`,
format: 'system',
},
external,
plugins: [
alias({ entries: entries.filter((e) => !e.find.test(input)) }),
resolve({ extensions }),
replace({
'import.meta.env?.MODE': JSON.stringify(env),
delimiters: ['\\b', '\\b(?!(\\.|/))'],
preventAssignment: true,
}),
getEsbuild('node12', env),
],
}
}

module.exports = function (args) {
let c = Object.keys(args).find((key) => key.startsWith('config-'))
if (c) {
Expand All @@ -167,10 +113,6 @@ module.exports = function (args) {
...(c === 'index' ? [createDeclarationConfig(`src/${c}.ts`, 'dist')] : []),
createCommonJSConfig(`src/${c}.ts`, `dist/${c}`),
createESMConfig(`src/${c}.ts`, `dist/esm/${c}.mjs`),
createUMDConfig(`src/${c}.ts`, `dist/umd/${c}`, 'development'),
createUMDConfig(`src/${c}.ts`, `dist/umd/${c}`, 'production'),
createSystemConfig(`src/${c}.ts`, `dist/system/${c}`, 'development'),
createSystemConfig(`src/${c}.ts`, `dist/system/${c}`, 'production'),
]
}

Expand Down

0 comments on commit 5e40034

Please sign in to comment.