Skip to content

Commit a0b4e75

Browse files
committed
start implementing the root aliasing feature
this is not the final design, currently `/` aliases to the target folder which is not preferable
1 parent 34a1e78 commit a0b4e75

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

src/processScript/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import babelPluginTransformUnicodeSetsRegex from "@babel/plugin-transform-unicod
1919
import babelTraverse from "@babel/traverse"
2020
import type { LVal, Program } from "@babel/types"
2121
import t from "@babel/types"
22+
import rollupPluginAlias from "@rollup/plugin-alias"
2223
import { babel as rollupPluginBabel } from "@rollup/plugin-babel"
2324
import rollupPluginCommonJS from "@rollup/plugin-commonjs"
2425
import rollupPluginJSON from "@rollup/plugin-json"
2526
import rollupPluginNodeResolve from "@rollup/plugin-node-resolve"
2627
import type { LaxPartial } from "@samual/lib"
2728
import { assert } from "@samual/lib/assert"
28-
import { resolve as resolvePath } from "path"
29+
import { relative as getRelativePath } from "path"
2930
import prettier from "prettier"
3031
import { rollup } from "rollup"
3132
import { supportedExtensions as extensions } from "../constants"
@@ -60,6 +61,8 @@ export type ProcessOptions = LaxPartial<{
6061
* when left unset or set to `undefined`, automatically uses or doesn't use quine cheats based on character count
6162
*/
6263
forceQuineCheats: boolean
64+
65+
rootFolderPath: string
6366
}> & { /** the name of this script (or set to `true` if not yet known) */ scriptName: string | true }
6467

6568
/** Minifies a given script
@@ -72,7 +75,8 @@ export async function processScript(code: string, {
7275
scriptName,
7376
filePath,
7477
mangleNames = false,
75-
forceQuineCheats
78+
forceQuineCheats,
79+
rootFolderPath
7680
}: ProcessOptions): Promise<{ script: string, warnings: { message: string }[] }> {
7781
assert(/^\w{11}$/.exec(uniqueId), HERE)
7882

@@ -178,7 +182,7 @@ export async function processScript(code: string, {
178182
let filePathResolved
179183

180184
if (filePath) {
181-
filePathResolved = resolvePath(filePath)
185+
filePathResolved = getRelativePath(`.`, filePath)
182186

183187
if (filePath.endsWith(`.ts`)) {
184188
plugins.push([
@@ -249,6 +253,8 @@ export async function processScript(code: string, {
249253
)
250254
}
251255

256+
console.debug(HERE, extensions)
257+
252258
const bundle = await rollup({
253259
input: filePathResolved,
254260
plugins: [
@@ -289,7 +295,8 @@ export async function processScript(code: string, {
289295
extensions
290296
}),
291297
rollupPluginCommonJS(),
292-
rollupPluginNodeResolve({ extensions })
298+
rollupPluginNodeResolve({ extensions }),
299+
!!rootFolderPath && rollupPluginAlias({ entries: [ { find: /^\//, replacement: `${rootFolderPath}/` } ] })
293300
],
294301
treeshake: { moduleSideEffects: false }
295302
})

src/push.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,23 @@ export async function push(
156156
}
157157

158158
const allInfo: Info[] = []
159+
const sourcePathResolved = resolvePath(sourcePath)
159160

160161
await Promise.all([ ...pathsToUsers ].map(async ([ path, [ ...users ] ]) => {
161162
const scriptName = getBaseName(path.slice(0, -3))
162163

163164
const uniqueId = Math.floor(Math.random() * (2 ** 52)).toString(36).padStart(11, `0`)
164165

165-
const { script: minifiedCode, warnings } = await processScript(
166-
await readFile(path, { encoding: `utf8` }),
167-
{ minify, scriptUser: true, scriptName, uniqueId, filePath: path, mangleNames, forceQuineCheats }
168-
)
166+
const { script: minifiedCode, warnings } = await processScript(await readFile(path, { encoding: `utf8` }), {
167+
minify,
168+
scriptUser: true,
169+
scriptName,
170+
uniqueId,
171+
filePath: path,
172+
mangleNames,
173+
forceQuineCheats,
174+
rootFolderPath: sourcePathResolved
175+
})
169176

170177
const info: Info = { path, users, characterCount: countHackmudCharacters(minifiedCode), error: undefined, warnings }
171178

src/watch.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,22 @@ export async function watch(sourceDirectory: string, hackmudDirectory: string, {
176176
)
177177
return
178178

179-
const filePath = resolvePath(sourceDirectory, path)
179+
const sourceDirectoryResolved = resolvePath(sourceDirectory)
180+
const filePath = resolvePath(sourceDirectoryResolved, path)
180181
const sourceCode = await readFile(filePath, { encoding: `utf8` })
181182
let script
182183
let warnings
183184

184185
try {
185-
({ script, warnings } = await processScript(
186-
sourceCode,
187-
{ minify, scriptUser: user, scriptName, filePath, mangleNames, forceQuineCheats }
188-
))
186+
({ script, warnings } = await processScript(sourceCode, {
187+
minify,
188+
scriptUser: user,
189+
scriptName,
190+
filePath,
191+
mangleNames,
192+
forceQuineCheats,
193+
rootFolderPath: sourceDirectoryResolved
194+
}))
189195
} catch (error) {
190196
assert(error instanceof Error, HERE)
191197
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })

0 commit comments

Comments
 (0)