Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rebuild when package.json dependencies changed #759

Merged
merged 3 commits into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/index.ts
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import fs from 'fs'
import { Worker } from 'worker_threads'
import { removeFiles, debouncePromise, slash, MaybePromise } from './utils'
import { loadTsupConfig } from './load'
import { getDepsHash, loadTsupConfig } from './load'
import glob from 'globby'
import { loadTsConfig } from 'bundle-require'
import { handleError, PrettyError } from './errors'
Expand Down Expand Up @@ -185,6 +185,8 @@ export async function build(_options: Options) {
let onSuccessCleanup: (() => any) | undefined | void
/** Files imported by the entry */
const buildDependencies: Set<string> = new Set()

let depsHash = await getDepsHash(process.cwd())

const doOnSuccessCleanup = async () => {
if (onSuccessProcess) {
Expand Down Expand Up @@ -319,14 +321,27 @@ export async function build(_options: Options) {
ignorePermissionErrors: true,
ignored,
})
watcher.on('all', (type, file) => {
watcher.on('all', async (type, file) => {
file = slash(file)
// By default we only rebuild when imported files change
// If you specify custom `watch`, a string or multiple strings
// We rebuild when those files change
if (options.watch === true && !buildDependencies.has(file)) {
let shouldSkipChange = false

if (options.watch === true ) {
if (file === 'package.json') {
const currentHash = await getDepsHash(process.cwd())
shouldSkipChange = currentHash === depsHash
depsHash = currentHash
} else if (!buildDependencies.has(file)) {
shouldSkipChange = true
}
}

if (shouldSkipChange) {
return
}

logger.info('CLI', `Change detected: ${type} ${file}`)
debouncedBuildAll()
})
Expand Down
16 changes: 12 additions & 4 deletions src/load.ts
Expand Up @@ -3,7 +3,7 @@ import JoyCon from 'joycon'
import path from 'path'
import { bundleRequire } from 'bundle-require'
import { defineConfig } from './'
import { jsoncParse } from './utils'
import { hash, jsoncParse } from './utils'

const joycon = new JoyCon()

Expand Down Expand Up @@ -77,13 +77,16 @@ export async function loadTsupConfig(
return {}
}

export async function loadPkg(cwd: string) {
export async function loadPkg(cwd: string, clearCache: boolean = false) {
if (clearCache) {
joycon.clearCache();
}
const { data } = await joycon.load(['package.json'], cwd, path.dirname(cwd))
return data || {}
}

export async function getDeps(cwd: string) {
const data = await loadPkg(cwd)
export async function getDeps(cwd: string, clearCache: boolean = false) {
const data = await loadPkg(cwd, clearCache)

const deps = Array.from(
new Set([
Expand All @@ -94,3 +97,8 @@ export async function getDeps(cwd: string) {

return deps
}

export async function getDepsHash(cwd: string) {
const deps = await getDeps(cwd, true)
return hash(deps.join(','))
egoist marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 5 additions & 0 deletions src/utils.ts
@@ -1,3 +1,4 @@
import { createHash } from 'crypto'
import fs from 'fs'
import glob from 'globby'
import resolveFrom from 'resolve-from'
Expand Down Expand Up @@ -129,3 +130,7 @@ export function jsoncParse(data: string) {
return {}
}
}

export function hash(content: string) {
return createHash('sha256').update(content).digest('hex');
}
18 changes: 9 additions & 9 deletions test/__snapshots__/index.test.ts.snap
Expand Up @@ -167,6 +167,15 @@ console.log(import_node_fs.default);
"
`;

exports[`not bundle \`package/subpath\` in dts (resolve) 1`] = `
"import * as foo_bar from 'foo/bar';

declare const stuff: foo_bar.Foobar;

export { stuff };
"
`;

exports[`simple 1`] = `
"\\"use strict\\";
var __defProp = Object.defineProperty;
Expand Down Expand Up @@ -204,15 +213,6 @@ var input_default = foo_default;
"
`;

exports[`not bundle \`package/subpath\` in dts (resolve) 1`] = `
"import * as foo_bar from 'foo/bar';

declare const stuff: foo_bar.Foobar;

export { stuff };
"
`;

exports[`support baseUrl and paths in tsconfig.json 1`] = `
"var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
Expand Down