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

fix(windows): normalize path in watcher #466

Merged
merged 5 commits into from Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .github/workflows/nodejs.yml
Expand Up @@ -32,6 +32,7 @@ jobs:
- run: npm ci
- run: npm run test --if-present
- run: npx semantic-release
if: "!contains(github.event.head_commit.message, 'skip-release')"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
34 changes: 15 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -25,6 +25,7 @@
"build:simple": "tsup src/cli-*.ts src/index.ts src/rollup.ts --clean --splitting"
},
"dependencies": {
"bundle-require": "^2.1.2",
"cac": "^6.7.2",
"chalk": "^4.1.0",
"chokidar": "^3.5.1",
Expand Down Expand Up @@ -61,7 +62,6 @@
"resolve": "^1.20.0",
"rollup-plugin-dts": "^3.0.2",
"rollup-plugin-hashbang": "^2.2.2",
"slash": "^4.0.0",
"string-argv": "^0.3.1",
"strip-json-comments": "^3.1.1",
"svelte": "3.37.0",
Expand Down
2 changes: 1 addition & 1 deletion src/cli-main.ts
Expand Up @@ -2,8 +2,8 @@ import { readFileSync } from 'fs'
import { join } from 'path'
import { cac } from 'cac'
import flat from 'flat'
import slash from 'slash'
import { Format, Options } from '.'
import { slash } from './utils'

function ensureArray(input: string): string[] {
return Array.isArray(input) ? input : input.split(',')
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import fs from 'fs'
import { Worker } from 'worker_threads'
import type { MarkRequired } from 'ts-essentials'
import { removeFiles, debouncePromise } from './utils'
import { removeFiles, debouncePromise, slash } from './utils'
import { loadTsupConfig, resolveTsConfig } from './load'
import glob from 'globby'
import { handleError, PrettyError } from './errors'
Expand Down Expand Up @@ -242,6 +242,7 @@ export async function build(_options: Options) {
ignored,
})
watcher.on('all', async (type, file) => {
file = slash(file)
if (!buildDependencies.has(file)) return

logger.info('CLI', `Change detected: ${type} ${file}`)
Expand Down
77 changes: 8 additions & 69 deletions src/load.ts
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import { parse as parseJson } from 'jju/lib/parse'
import JoyCon from 'joycon'
import path from 'path'
import { Loader } from 'esbuild'
import { bundleRequire } from 'bundle-require'
import stripJsonComments from 'strip-json-comments'
import { defineConfig } from './'

Expand Down Expand Up @@ -65,79 +65,18 @@ export async function loadTsupConfig(
return {}
}

const config = await bundleConfig(configPath)
return { path: configPath, data: config }
const config = await bundleRequire({
filepath: configPath,
})
return {
path: configPath,
data: config.mod.tsup || config.mod.default || config.mod,
}
}

return {}
}

function removeFile(filepath: string) {
if (fs.existsSync(filepath)) {
fs.unlinkSync(filepath)
}
}

async function bundleConfig(configFile: string) {
const { build } = await import('esbuild')
const outFile = configFile.replace(/\.[a-z]+$/, '.bundled.cjs')
const readConfig = () => {
delete require.cache[outFile]
const result = require(outFile)
removeFile(outFile)
return result.tsup || result.default || result
}
try {
await build({
entryPoints: [configFile],
format: 'cjs',
outfile: outFile,
platform: 'node',
bundle: true,
plugins: [
{
name: 'ignore',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (!path.isAbsolute(args.path) && !/^[\.\/]/.test(args.path)) {
return { external: true }
}
})
build.onLoad(
{ filter: /\.(js|ts|mjs|cjs|jsx|tsx)$/ },
async (args) => {
const contents = await fs.promises.readFile(args.path, 'utf8')
const ext = path.extname(args.path)
return {
contents: contents
.replace(
/\b__dirname\b/g,
JSON.stringify(path.dirname(args.path))
)
.replace(/\b__filename\b/g, JSON.stringify(args.path))
.replace(
/\bimport\.meta\.url\b/g,
JSON.stringify(`file://${args.path}`)
),
loader:
ext === '.mjs' || ext === '.cjs'
? 'js'
: (ext.slice(1) as Loader),
}
}
)
},
},
],
})
const config = readConfig()
return config
} catch (error) {
removeFile(outFile)
throw error
}
}

export async function loadPkg(cwd: string) {
const { data } = await joycon.load(['package.json'], cwd, path.dirname(cwd))
return data || {}
Expand Down
17 changes: 12 additions & 5 deletions src/utils.ts
Expand Up @@ -2,11 +2,6 @@ import fs from 'fs'
import glob from 'globby'
import resolveFrom from 'resolve-from'

// No backslash in path
function slash(input: string) {
return input.replace(/\\/g, '/')
}

export type External =
| string
| RegExp
Expand Down Expand Up @@ -108,3 +103,15 @@ export function debouncePromise<T extends unknown[]>(
}
}
}

// Taken from https://github.com/sindresorhus/slash/blob/main/index.js (MIT)
export function slash(path: string) {
const isExtendedLengthPath = /^\\\\\?\\/.test(path)
const hasNonAscii = /[^\u0000-\u0080]+/.test(path)

if (isExtendedLengthPath || hasNonAscii) {
return path
}

return path.replace(/\\/g, '/')
}