tw-migrate 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/native.js DELETED
@@ -1,41 +0,0 @@
1
- import { createRequire } from "node:module";
2
-
3
- const require = createRequire(import.meta.url);
4
- const targets = {
5
- "darwin-arm64": "darwin-arm64",
6
- "darwin-x64": "darwin-x64",
7
- "linux-arm64": "linux-arm64-gnu",
8
- "linux-x64": "linux-x64-gnu",
9
- "win32-x64": "win32-x64-msvc",
10
- };
11
- const target = targets[`${process.platform}-${process.arch}`];
12
-
13
- if (!target) throw new Error(`Unsupported platform: ${process.platform}-${process.arch}`);
14
-
15
- let binding;
16
- for (const load of [
17
- () => require(`./tw-migrate.${target}.node`),
18
- () => require(`tw-migrate-${target}`),
19
- ]) {
20
- try {
21
- binding = load();
22
- break;
23
- } catch (error) {
24
- if (error.code !== "MODULE_NOT_FOUND") throw error;
25
- }
26
- }
27
-
28
- if (!binding) {
29
- throw new Error(
30
- `No tw-migrate native addon was found for ${target}. Reinstall the package or build it locally.`,
31
- );
32
- }
33
-
34
- export const {
35
- decodeSourceMap,
36
- planBatchMigration,
37
- staticImportBindings,
38
- staticImports,
39
- staticStringExpression,
40
- validateCss,
41
- } = binding;
package/style-compiler.js DELETED
@@ -1,140 +0,0 @@
1
- import { realpath } from "node:fs/promises";
2
- import { createRequire } from "node:module";
3
- import { basename, dirname, extname, isAbsolute, join, resolve } from "node:path";
4
- import { fileURLToPath, pathToFileURL } from "node:url";
5
-
6
- import { decodeSourceMap } from "./native.js";
7
-
8
- const SASS_EXTENSIONS = new Set([".scss", ".sass"]);
9
-
10
- export function isSassPath(path) {
11
- return SASS_EXTENSIONS.has(extname(path));
12
- }
13
-
14
- export function isPreprocessorPath(path) {
15
- return isSassPath(path) || extname(path) === ".less";
16
- }
17
-
18
- async function loadProjectModule(packageRoot, name, errorMessage) {
19
- const projectRequire = createRequire(join(packageRoot, "package.json"));
20
- let modulePath;
21
- try {
22
- modulePath = projectRequire.resolve(name);
23
- } catch {
24
- throw new Error(errorMessage);
25
- }
26
- const imported = await import(pathToFileURL(modulePath));
27
- return imported.default ?? imported;
28
- }
29
-
30
- export function loadProjectSass(packageRoot) {
31
- return loadProjectModule(packageRoot, "sass", "Sass must be installed in the target project.");
32
- }
33
-
34
- export function loadProjectLess(packageRoot) {
35
- return loadProjectModule(packageRoot, "less", "Less must be installed in the target project.");
36
- }
37
-
38
- export async function compileSassEntry(sass, entryPath, source, { virtualEntry = false } = {}) {
39
- const options = {
40
- sourceMap: true,
41
- sourceMapIncludeSources: true,
42
- };
43
- const result =
44
- source === undefined
45
- ? await sass.compileAsync(entryPath, options)
46
- : await sass.compileStringAsync(source, {
47
- ...options,
48
- url: pathToFileURL(entryPath),
49
- syntax: extname(entryPath) === ".sass" ? "indented" : "scss",
50
- });
51
-
52
- if (!result.sourceMap) throw new Error(`Sass did not produce a source map for ${entryPath}`);
53
- const loadedPaths = result.loadedUrls
54
- .filter((url) => url.protocol === "file:")
55
- .map(fileURLToPath);
56
- const mappings = sourceMappings(result.sourceMap);
57
- const normalizedPaths = await normalizeEntryPaths(
58
- [...loadedPaths, ...mappings.map((mapping) => mapping.sourcePath)],
59
- entryPath,
60
- virtualEntry,
61
- );
62
- return {
63
- css: result.css,
64
- loadedPaths: loadedPaths.map((path) => normalizedPaths.get(path)),
65
- sourceMappings: mappings.map((mapping) => ({
66
- ...mapping,
67
- sourcePath: normalizedPaths.get(mapping.sourcePath),
68
- })),
69
- };
70
- }
71
-
72
- async function normalizeEntryPaths(paths, entryPath, virtualEntry) {
73
- // A virtual SFC block entry never exists on disk; a real entry that
74
- // vanished mid-run must keep failing as a source-integrity error.
75
- const canonicalEntryPath = virtualEntry ? entryPath : await realpath(entryPath);
76
- return new Map(
77
- await Promise.all(
78
- [...new Set(paths)].map(async (path) => {
79
- if (path === entryPath) return [path, path];
80
- try {
81
- return [path, (await realpath(path)) === canonicalEntryPath ? entryPath : path];
82
- } catch {
83
- return [path, path];
84
- }
85
- }),
86
- ),
87
- );
88
- }
89
-
90
- export async function compileLessEntry(less, entryPath, source) {
91
- const result = await less.render(source, {
92
- filename: entryPath,
93
- sourceMap: {
94
- outputSourceFiles: true,
95
- sourceMapBasepath: dirname(entryPath),
96
- sourceMapOutputFilename: `${basename(entryPath, ".less")}.css`,
97
- },
98
- });
99
- return {
100
- css: result.css,
101
- loadedPaths: result.imports.map((path) =>
102
- isAbsolute(path) ? path : resolve(dirname(entryPath), path),
103
- ),
104
- sourceMappings: result.map ? sourceMappings(JSON.parse(result.map), dirname(entryPath)) : [],
105
- };
106
- }
107
-
108
- export function sourceMappings(sourceMap, sourceBase) {
109
- return JSON.parse(decodeSourceMap(JSON.stringify(sourceMap))).flatMap(
110
- ({ source, ...mapping }) => {
111
- const sourcePath = sourcePathFromMap(
112
- sourceReferenceFromMap(source, sourceMap.sourceRoot),
113
- sourceBase,
114
- );
115
- return sourcePath ? [{ ...mapping, sourcePath }] : [];
116
- },
117
- );
118
- }
119
-
120
- function sourceReferenceFromMap(source, sourceRoot) {
121
- if (!sourceRoot) return source;
122
- if (isAbsolute(sourceRoot)) return resolve(sourceRoot, source);
123
- try {
124
- const root = new URL(sourceRoot);
125
- root.pathname = `${root.pathname.replace(/\/+$/, "")}/`;
126
- return new URL(source, root).href;
127
- } catch {
128
- return join(sourceRoot, source);
129
- }
130
- }
131
-
132
- function sourcePathFromMap(source, sourceBase) {
133
- if (isAbsolute(source)) return resolve(source);
134
- try {
135
- const url = new URL(source);
136
- return url.protocol === "file:" ? resolve(fileURLToPath(url)) : undefined;
137
- } catch {
138
- return sourceBase ? resolve(sourceBase, source) : undefined;
139
- }
140
- }