diff --git a/.eslintignore b/.eslintignore index 73f63fc0a..9dba3ee90 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,4 @@ dist/ -node_modules \ No newline at end of file +node_modules +scripts +examples \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4084b2cea..ec7f18575 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ dist examples/**/yarn.lock package-lock.json tsconfig.tsbuildinfo +**/*.yalc +yalc.lock \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 38d319ebb..7eb5b3114 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -27,14 +27,13 @@ To run SWR locally, you can start it with any example in `examples` folder. You First of all, build SWR assets ```sh -# or `yarn watch` -yarn build +yarn watch ``` Install dependency of the target example, for instance `examples/basic`: ```sh -cd examples/basic && yarn +cd examples/basic && yarn && npx yalc link swr ``` After setup, back to the root directory and run: diff --git a/package.json b/package.json index 3c1746f3e..5eeb5a70a 100644 --- a/package.json +++ b/package.json @@ -37,10 +37,10 @@ "scripts": { "clean": "rimraf dist infinite/dist immutable/dist", "build": "yarn build:core && yarn build:infinite && yarn build:immutable", - "watch": "yarn run-p watch:core watch:infinite watch:immutable", - "watch:core": "bunchee src/index.ts --watch", - "watch:infinite": "bunchee index.ts --cwd infinite --watch", - "watch:immutable": "bunchee index.ts --cwd immutable --watch", + "watch": "yalc publish && node scripts/watch.js", + "watch:core": "node scripts/watch.js core", + "watch:infinite": "node scripts/watch.js infinite", + "watch:immutable": "node scripts/watch.js immutable", "build:core": "bunchee src/index.ts -m --no-sourcemap", "build:infinite": "bunchee index.ts --cwd infinite -m --no-sourcemap", "build:immutable": "bunchee index.ts --cwd immutable -m --no-sourcemap", @@ -51,7 +51,6 @@ "lint": "eslint . --ext .ts,.tsx --cache", "lint:fix": "yarn lint --fix", "test": "jest", - "register": "yarn link && cd node_modules/react && yarn link", "dev-next": "node scripts/dev-next.js" }, "husky": { @@ -94,7 +93,8 @@ "react-experimental": "npm:react@alpha", "rimraf": "3.0.2", "ts-jest": "27.0.3", - "typescript": "4.4.3" + "typescript": "4.4.3", + "yalc": "1.0.0-pre.53" }, "peerDependencies": { "react": "^16.11.0 || ^17.0.0 || ^18.0.0" diff --git a/scripts/dev-next.js b/scripts/dev-next.js index 57d95f0af..be8c65ea8 100755 --- a/scripts/dev-next.js +++ b/scripts/dev-next.js @@ -22,16 +22,10 @@ const exampleDir = resolve(examplesDir, target) const nextBin = resolve(exampleDir, 'node_modules/.bin/next') if (!fs.existsSync(nextBin)) { - error(`Please run "yarn install" inside ${target} example directory\n`) + error(`Please run "yarn install && npx yalc link swr" inside ${target} example directory\n`) } -const requireHookPath = resolve(__dirname, 'require-hook.js') -const nextConfigPath = resolve(exampleDir, 'next.config.js') -const devCmd = `node -r ${requireHookPath} ${nextBin} ${nextCmd}`.split(' ') - -if (!fs.existsSync(nextConfigPath)) { - fs.symlinkSync(resolve(__dirname, 'next-config.js'), nextConfigPath, 'file') -} +const devCmd = `node ${nextBin} ${nextCmd}`.split(' ') const sub = childProcess.spawn(devCmd.shift(), devCmd, { cwd: exampleDir }) @@ -47,7 +41,6 @@ sub.stderr.on('close', () => { const teardown = () => { if (sub.killed) sub.kill('SIGINT') - if (fs.existsSync(nextConfigPath)) fs.unlinkSync(nextConfigPath) } process.on('exit', teardown) diff --git a/scripts/next-config.js b/scripts/next-config.js deleted file mode 100644 index d67aae30b..000000000 --- a/scripts/next-config.js +++ /dev/null @@ -1,21 +0,0 @@ -const { resolve } = require('path') - -module.exports = { - webpack(config) { - const { alias } = config.resolve - - // FIXME: resolving react/jsx-runtime https://github.com/facebook/react/issues/20235 - alias['react/jsx-dev-runtime'] = require.resolve('react/jsx-dev-runtime.js') - alias['react/jsx-runtime'] = require.resolve('react/jsx-runtime.js') - - alias['swr'] = resolve(__dirname, '../dist/index.js') - alias['swr/infinite'] = resolve(__dirname, '../infinite/dist/index.js') - alias['swr/immutable'] = resolve(__dirname, '../immutable/dist/index.js') - - alias['react'] = require.resolve('react') - alias['react-dom'] = require.resolve('react-dom') - alias['react-dom/server'] = require.resolve('react-dom/server') - - return config - }, -} \ No newline at end of file diff --git a/scripts/require-hook.js b/scripts/require-hook.js deleted file mode 100644 index a0b7b1876..000000000 --- a/scripts/require-hook.js +++ /dev/null @@ -1,20 +0,0 @@ -const { resolve } = require('path') -const mod = require('module') - -const rootDir = resolve(__dirname, '..') -const nodeModulesDir = resolve(rootDir, 'node_modules') - -const hookPropertyMap = new Map([ - ['swr', resolve(rootDir, 'dist/index.js')], - ['swr/infinite', resolve(rootDir, 'infinite/dist/index.js')], - ['swr/immutable', resolve(rootDir, 'immutable/dist/index.js')], - ['react', resolve(nodeModulesDir, 'react')], - ['react-dom', resolve(nodeModulesDir, 'react-dom')], -]) - -const resolveFilename = mod._resolveFilename -mod._resolveFilename = function (request, ...args) { - const hookResolved = hookPropertyMap.get(request) - if (hookResolved) request = hookResolved - return resolveFilename.call(mod, request, ...args) -} diff --git a/scripts/watch.js b/scripts/watch.js new file mode 100644 index 000000000..3f7b5164c --- /dev/null +++ b/scripts/watch.js @@ -0,0 +1,81 @@ +/** + * @type {import('bunchee/dist/src/bundle').default} + */ +const bundle = require('bunchee').bundle +const childProcess = require('child_process') + +const args = process.argv +const target = args[2] + +const entryMap = { + core: { + entry: 'src/index.ts', + cwd: '' + }, + infinite: { + entry: 'index.ts', + cwd: 'infinite' + }, + immutable: { + entry: 'index.ts', + cwd: 'immutable' + } +} + +function error(message) { + console.log(message) + process.exit(1) +} + +function start() { + const option = entryMap[target] + if (option) { + bundle(option.entry, { + watch: true, + cwd: option.cwd, + sourcemap: true + }) + .then(watch) + .catch(e => { + console.log(e) + }) + } else { + error(`package ${target} not found`) + } +} +/** + * + * @param {import('rollup').RollupWatcher} rollupWatcher + */ +function watch(rollupWatcher) { + rollupWatcher.on('event', event => { + if (event.code === 'BUNDLE_END') { + event.result.close() + push() + } + if (event.code === 'ERROR') { + error(event.error) + event.result.close() + } + }) + teardown(rollupWatcher.close) +} + +function push() { + const sub = childProcess.spawn('yalc', ['push']) + sub.stdout.on('data', logStdout) + sub.stderr.on('data', logStderr) + sub.stderr.on('close', () => { + sub.stdout.off('data', logStdout) + sub.stderr.off('data', logStderr) + }) +} + +function teardown(cb) { + process.on('exit', cb) + process.on('SIGINT', cb) +} + +const logStdout = data => process.stdout.write(data.toString()) +const logStderr = data => process.stderr.write(data.toString()) +start() diff --git a/yarn.lock b/yarn.lock index a9da28aeb..a1546cbee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2476,6 +2476,11 @@ delayed-stream@~1.0.0: resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk= +detect-indent@^6.0.0: + version "6.1.0" + resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-6.1.0.tgz#592485ebbbf6b3b1ab2be175c8393d04ca0d57e6" + integrity sha512-reYkTUJAZb9gUuZ2RvVCNhVHdg62RHnJ7WJl8ftMi4diZ6NWlciOzQN88pUhSELEwflJht4oQDv0F0BMlwaYtA== + detect-newline@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651" @@ -3021,6 +3026,15 @@ fragment-cache@^0.2.1: dependencies: map-cache "^0.2.2" +fs-extra@^8.0.1: + version "8.1.0" + resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0" + integrity sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g== + dependencies: + graceful-fs "^4.2.0" + jsonfile "^4.0.0" + universalify "^0.1.0" + fs.realpath@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" @@ -3168,7 +3182,7 @@ globby@^6.1.0: pify "^2.0.0" pinkie-promise "^2.0.0" -graceful-fs@^4.1.2, graceful-fs@^4.2.4: +graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.4: version "4.2.8" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.8.tgz#e412b8d33f5e006593cbd3cee6df9f2cebbe802a" integrity sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg== @@ -3307,12 +3321,19 @@ iconv-lite@0.4.24: dependencies: safer-buffer ">= 2.1.2 < 3" +ignore-walk@^3.0.3: + version "3.0.4" + resolved "https://registry.yarnpkg.com/ignore-walk/-/ignore-walk-3.0.4.tgz#c9a09f69b7c7b479a5d74ac1a3c0d4236d2a6335" + integrity sha512-PY6Ii8o1jMRA1z4F2hRkH/xN59ox43DavKvD3oDpfurRlOJyAHpifIwpbdv1n4jt4ov0jSpw3kQ4GhJnpBL6WQ== + dependencies: + minimatch "^3.0.4" + ignore@^4.0.6: version "4.0.6" resolved "https://registry.yarnpkg.com/ignore/-/ignore-4.0.6.tgz#750e3db5862087b4737ebac8207ffd1ef27b25fc" integrity sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg== -ignore@^5.1.4: +ignore@^5.0.4, ignore@^5.1.4: version "5.1.8" resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57" integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw== @@ -3369,6 +3390,11 @@ inherits@2, inherits@^2.0.4: resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== +ini@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ini/-/ini-2.0.0.tgz#e5fd556ecdd5726be978fa1001862eacb0a94bc5" + integrity sha512-7PnF4oN3CvZF23ADhA5wRaYEQpJ8qygSkbtTXWBeXWXmEVRXK+1ITciHWwHhsjv1TmW0MgacIv6hEi5pX5NQdA== + internal-slot@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/internal-slot/-/internal-slot-1.0.3.tgz#7347e307deeea2faac2ac6205d4bc7d34967f59c" @@ -4235,6 +4261,13 @@ json5@2.x, json5@^2.1.2: dependencies: minimist "^1.2.5" +jsonfile@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb" + integrity sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss= + optionalDependencies: + graceful-fs "^4.1.6" + jsx-ast-utils@^2.4.1: version "2.4.1" resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-2.4.1.tgz#1114a4c1209481db06c690c2b4f488cc665f657e" @@ -4675,6 +4708,28 @@ normalize-path@^3.0.0: resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65" integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA== +npm-bundled@^1.1.1: + version "1.1.2" + resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.1.2.tgz#944c78789bd739035b70baa2ca5cc32b8d860bc1" + integrity sha512-x5DHup0SuyQcmL3s7Rx/YQ8sbw/Hzg0rj48eN0dV7hf5cmQq5PXIeioroH3raV1QC1yh3uTYuMThvEQF3iKgGQ== + dependencies: + npm-normalize-package-bin "^1.0.1" + +npm-normalize-package-bin@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/npm-normalize-package-bin/-/npm-normalize-package-bin-1.0.1.tgz#6e79a41f23fd235c0623218228da7d9c23b8f6e2" + integrity sha512-EPfafl6JL5/rU+ot6P3gRSCpPDW5VmIzX959Ob1+ySFUuuYHWHekXpwdUZcKP5C+DS4GEtdJluwBjnsNDl+fSA== + +npm-packlist@^2.1.5: + version "2.2.2" + resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-2.2.2.tgz#076b97293fa620f632833186a7a8f65aaa6148c8" + integrity sha512-Jt01acDvJRhJGthnUJVF/w6gumWOZxO7IkpY/lsX9//zqQgnF7OJaxgQXcerd4uQOLu7W5bkb4mChL9mdfm+Zg== + dependencies: + glob "^7.1.6" + ignore-walk "^3.0.3" + npm-bundled "^1.1.1" + npm-normalize-package-bin "^1.0.1" + npm-path@^2.0.2: version "2.0.4" resolved "https://registry.yarnpkg.com/npm-path/-/npm-path-2.0.4.tgz#c641347a5ff9d6a09e4d9bce5580c4f505278e64" @@ -6124,7 +6179,7 @@ union-value@^1.0.0: is-extendable "^0.1.1" set-value "^2.0.1" -universalify@^0.1.2: +universalify@^0.1.0, universalify@^0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66" integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg== @@ -6310,6 +6365,20 @@ y18n@^5.0.5: resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55" integrity sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA== +yalc@1.0.0-pre.53: + version "1.0.0-pre.53" + resolved "https://registry.yarnpkg.com/yalc/-/yalc-1.0.0-pre.53.tgz#c51db2bb924a6908f4cb7e82af78f7e5606810bc" + integrity sha512-tpNqBCpTXplnduzw5XC+FF8zNJ9L/UXmvQyyQj7NKrDNavbJtHvzmZplL5ES/RCnjX7JR7W9wz5GVDXVP3dHUQ== + dependencies: + chalk "^4.1.0" + detect-indent "^6.0.0" + fs-extra "^8.0.1" + glob "^7.1.4" + ignore "^5.0.4" + ini "^2.0.0" + npm-packlist "^2.1.5" + yargs "^16.1.1" + yallist@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" @@ -6320,7 +6389,7 @@ yargs-parser@20.x, yargs-parser@^20.2.2: resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee" integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w== -yargs@^16.0.3: +yargs@^16.0.3, yargs@^16.1.1: version "16.2.0" resolved "https://registry.yarnpkg.com/yargs/-/yargs-16.2.0.tgz#1c82bf0f6b6a66eafce7ef30e376f49a12477f66" integrity sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==