@remix-run/assets 0.1.0 → 0.2.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.
Files changed (57) hide show
  1. package/README.md +53 -47
  2. package/dist/lib/asset-server.d.ts +45 -43
  3. package/dist/lib/asset-server.d.ts.map +1 -1
  4. package/dist/lib/asset-server.js +142 -46
  5. package/dist/lib/compilation-error.d.ts +2 -2
  6. package/dist/lib/compilation-error.d.ts.map +1 -1
  7. package/dist/lib/compilation-error.js +1 -1
  8. package/dist/lib/file-matcher.d.ts.map +1 -1
  9. package/dist/lib/file-matcher.js +3 -2
  10. package/dist/lib/module-store.d.ts +41 -0
  11. package/dist/lib/module-store.d.ts.map +1 -0
  12. package/dist/lib/{scripts/store.js → module-store.js} +43 -41
  13. package/dist/lib/scripts/compiler.d.ts +15 -15
  14. package/dist/lib/scripts/compiler.d.ts.map +1 -1
  15. package/dist/lib/scripts/compiler.js +50 -46
  16. package/dist/lib/scripts/emit.js +2 -2
  17. package/dist/lib/scripts/resolve.d.ts +7 -17
  18. package/dist/lib/scripts/resolve.d.ts.map +1 -1
  19. package/dist/lib/scripts/resolve.js +15 -9
  20. package/dist/lib/scripts/transform.d.ts +11 -9
  21. package/dist/lib/scripts/transform.d.ts.map +1 -1
  22. package/dist/lib/scripts/transform.js +32 -21
  23. package/dist/lib/source-maps.d.ts +1 -1
  24. package/dist/lib/source-maps.d.ts.map +1 -1
  25. package/dist/lib/source-maps.js +7 -1
  26. package/dist/lib/styles/compiler.d.ts +52 -0
  27. package/dist/lib/styles/compiler.d.ts.map +1 -0
  28. package/dist/lib/styles/compiler.js +272 -0
  29. package/dist/lib/styles/emit.d.ts +25 -0
  30. package/dist/lib/styles/emit.d.ts.map +1 -0
  31. package/dist/lib/styles/emit.js +78 -0
  32. package/dist/lib/styles/resolve.d.ts +48 -0
  33. package/dist/lib/styles/resolve.d.ts.map +1 -0
  34. package/dist/lib/styles/resolve.js +188 -0
  35. package/dist/lib/styles/transform.d.ts +47 -0
  36. package/dist/lib/styles/transform.d.ts.map +1 -0
  37. package/dist/lib/styles/transform.js +131 -0
  38. package/dist/lib/target.d.ts +21 -0
  39. package/dist/lib/target.d.ts.map +1 -0
  40. package/dist/lib/target.js +127 -0
  41. package/package.json +7 -2
  42. package/src/lib/asset-server.ts +218 -96
  43. package/src/lib/compilation-error.ts +7 -7
  44. package/src/lib/file-matcher.ts +3 -2
  45. package/src/lib/{scripts/store.ts → module-store.ts} +100 -87
  46. package/src/lib/scripts/compiler.ts +88 -70
  47. package/src/lib/scripts/emit.ts +2 -2
  48. package/src/lib/scripts/resolve.ts +34 -23
  49. package/src/lib/scripts/transform.ts +49 -34
  50. package/src/lib/source-maps.ts +8 -1
  51. package/src/lib/styles/compiler.ts +400 -0
  52. package/src/lib/styles/emit.ts +137 -0
  53. package/src/lib/styles/resolve.ts +316 -0
  54. package/src/lib/styles/transform.ts +226 -0
  55. package/src/lib/target.ts +196 -0
  56. package/dist/lib/scripts/store.d.ts +0 -40
  57. package/dist/lib/scripts/store.d.ts.map +0 -1
@@ -0,0 +1,131 @@
1
+ import * as fs from 'node:fs/promises';
2
+ import { transform } from 'lightningcss';
3
+ import { createAssetServerCompilationError, isAssetServerCompilationError, } from "../compilation-error.js";
4
+ import { generateFingerprint } from "../fingerprint.js";
5
+ import { rewriteSourceMapSources, stringifySourceMap } from "../source-maps.js";
6
+ export async function transformStyle(record, args) {
7
+ let resolvedPath = record.identityPath;
8
+ let trackedFiles = args.isWatchIgnored(resolvedPath) ? [] : [resolvedPath];
9
+ let rawBytes;
10
+ try {
11
+ rawBytes = new Uint8Array(await fs.readFile(resolvedPath));
12
+ }
13
+ catch (error) {
14
+ if (isNoEntityError(error)) {
15
+ return {
16
+ ok: false,
17
+ error: createAssetServerCompilationError(`File not found: ${resolvedPath}`, {
18
+ cause: error,
19
+ code: 'FILE_NOT_FOUND',
20
+ }),
21
+ tracking: {
22
+ trackedFiles,
23
+ },
24
+ };
25
+ }
26
+ return {
27
+ ok: false,
28
+ error: toTransformFailedError(error, resolvedPath),
29
+ tracking: {
30
+ trackedFiles,
31
+ },
32
+ };
33
+ }
34
+ try {
35
+ let stableUrlPathname = args.routes.toUrlPathname(record.identityPath);
36
+ if (!stableUrlPathname) {
37
+ throw createAssetServerCompilationError(`File ${record.identityPath} is outside all configured fileMap entries.`, {
38
+ code: 'FILE_OUTSIDE_FILE_MAP',
39
+ });
40
+ }
41
+ let transformResult = runLightningTransform(resolvedPath, rawBytes, {
42
+ minify: args.minify,
43
+ sourceMap: args.sourceMaps != null,
44
+ targets: args.targets,
45
+ });
46
+ let sourceText = Buffer.from(rawBytes).toString('utf8');
47
+ let sourceMap = stringifySourceMap(transformResult.map);
48
+ sourceMap = sourceMap
49
+ ? rewriteSourceMapSources(sourceMap, resolvedPath, stableUrlPathname, args.sourceMapSourcePaths, sourceText)
50
+ : null;
51
+ let unresolvedDependencies = [];
52
+ for (let dependency of transformResult.dependencies ?? []) {
53
+ if (dependency.type === 'import') {
54
+ unresolvedDependencies.push({
55
+ placeholder: dependency.placeholder,
56
+ type: 'import',
57
+ url: dependency.url,
58
+ });
59
+ continue;
60
+ }
61
+ if (dependency.type === 'url') {
62
+ unresolvedDependencies.push({
63
+ placeholder: dependency.placeholder,
64
+ type: 'url',
65
+ url: dependency.url,
66
+ });
67
+ }
68
+ }
69
+ return {
70
+ ok: true,
71
+ tracking: {
72
+ trackedFiles,
73
+ },
74
+ value: {
75
+ fingerprint: args.buildId === null
76
+ ? null
77
+ : await generateFingerprint({
78
+ buildId: args.buildId,
79
+ content: sourceText,
80
+ }),
81
+ identityPath: record.identityPath,
82
+ rawCode: Buffer.from(transformResult.code).toString('utf8'),
83
+ resolvedPath,
84
+ sourceMap,
85
+ stableUrlPathname,
86
+ trackedFiles,
87
+ unresolvedDependencies,
88
+ },
89
+ };
90
+ }
91
+ catch (error) {
92
+ return {
93
+ ok: false,
94
+ error: toTransformFailedError(error, resolvedPath),
95
+ tracking: {
96
+ trackedFiles,
97
+ },
98
+ };
99
+ }
100
+ }
101
+ function runLightningTransform(identityPath, code, options) {
102
+ try {
103
+ return transform({
104
+ analyzeDependencies: {
105
+ preserveImports: true,
106
+ },
107
+ code,
108
+ filename: identityPath,
109
+ minify: options.minify,
110
+ sourceMap: options.sourceMap,
111
+ targets: options.targets ?? undefined,
112
+ });
113
+ }
114
+ catch (error) {
115
+ throw createAssetServerCompilationError(`Failed to transform style ${identityPath}. ${error instanceof Error ? error.message : String(error)}`, {
116
+ cause: error,
117
+ code: 'TRANSFORM_FAILED',
118
+ });
119
+ }
120
+ }
121
+ function toTransformFailedError(error, resolvedPath) {
122
+ if (isAssetServerCompilationError(error))
123
+ return error;
124
+ return createAssetServerCompilationError(`Failed to transform style ${resolvedPath}. ${error instanceof Error ? error.message : String(error)}`, {
125
+ cause: error,
126
+ code: 'TRANSFORM_FAILED',
127
+ });
128
+ }
129
+ function isNoEntityError(error) {
130
+ return (error instanceof Error && 'code' in error && error.code === 'ENOENT');
131
+ }
@@ -0,0 +1,21 @@
1
+ import type { Targets as LightningCssTargets } from 'lightningcss';
2
+ declare const browserTargetNames: readonly ["chrome", "edge", "firefox", "ie", "ios", "opera", "safari", "samsung"];
3
+ export type AssetTargetVersion = `${number}` | `${number}.${number}` | `${number}.${number}.${number}`;
4
+ export type BrowserTargetName = (typeof browserTargetNames)[number];
5
+ export type ResolvedScriptTarget = string[];
6
+ export type ResolvedStyleTarget = LightningCssTargets;
7
+ export interface AssetTarget {
8
+ chrome?: AssetTargetVersion;
9
+ edge?: AssetTargetVersion;
10
+ firefox?: AssetTargetVersion;
11
+ ie?: AssetTargetVersion;
12
+ ios?: AssetTargetVersion;
13
+ opera?: AssetTargetVersion;
14
+ safari?: AssetTargetVersion;
15
+ samsung?: AssetTargetVersion;
16
+ es?: string;
17
+ }
18
+ export declare function resolveScriptTarget(target: AssetTarget | undefined): ResolvedScriptTarget | undefined;
19
+ export declare function resolveStyleTarget(target: AssetTarget | undefined): ResolvedStyleTarget | undefined;
20
+ export {};
21
+ //# sourceMappingURL=target.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"target.d.ts","sourceRoot":"","sources":["../../src/lib/target.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAElE,QAAA,MAAM,kBAAkB,mFASd,CAAA;AAeV,MAAM,MAAM,kBAAkB,GAC1B,GAAG,MAAM,EAAE,GACX,GAAG,MAAM,IAAI,MAAM,EAAE,GACrB,GAAG,MAAM,IAAI,MAAM,IAAI,MAAM,EAAE,CAAA;AACnC,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAA;AACnE,MAAM,MAAM,oBAAoB,GAAG,MAAM,EAAE,CAAA;AAC3C,MAAM,MAAM,mBAAmB,GAAG,mBAAmB,CAAA;AAErD,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,kBAAkB,CAAA;IAC3B,IAAI,CAAC,EAAE,kBAAkB,CAAA;IACzB,OAAO,CAAC,EAAE,kBAAkB,CAAA;IAC5B,EAAE,CAAC,EAAE,kBAAkB,CAAA;IACvB,GAAG,CAAC,EAAE,kBAAkB,CAAA;IACxB,KAAK,CAAC,EAAE,kBAAkB,CAAA;IAC1B,MAAM,CAAC,EAAE,kBAAkB,CAAA;IAC3B,OAAO,CAAC,EAAE,kBAAkB,CAAA;IAC5B,EAAE,CAAC,EAAE,MAAM,CAAA;CACZ;AAMD,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,WAAW,GAAG,SAAS,GAC9B,oBAAoB,GAAG,SAAS,CAgBlC;AAED,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,WAAW,GAAG,SAAS,GAC9B,mBAAmB,GAAG,SAAS,CAajC"}
@@ -0,0 +1,127 @@
1
+ const browserTargetNames = [
2
+ 'chrome',
3
+ 'edge',
4
+ 'firefox',
5
+ 'ie',
6
+ 'ios',
7
+ 'opera',
8
+ 'safari',
9
+ 'samsung',
10
+ ];
11
+ const browserTargetNameSet = new Set(browserTargetNames);
12
+ const lightningCssTargetNameByBrowserTargetName = {
13
+ chrome: 'chrome',
14
+ edge: 'edge',
15
+ firefox: 'firefox',
16
+ ie: 'ie',
17
+ ios: 'ios_saf',
18
+ opera: 'opera',
19
+ safari: 'safari',
20
+ samsung: 'samsung',
21
+ };
22
+ export function resolveScriptTarget(target) {
23
+ let resolvedTarget = normalizeScriptTargetObject(target, 'target');
24
+ if (!resolvedTarget)
25
+ return undefined;
26
+ let oxcTarget = [];
27
+ if (resolvedTarget.es) {
28
+ oxcTarget.push(resolvedTarget.es);
29
+ }
30
+ for (let browserTargetName of browserTargetNames) {
31
+ let version = resolvedTarget[browserTargetName];
32
+ if (version == null)
33
+ continue;
34
+ oxcTarget.push(`${browserTargetName}${version}`);
35
+ }
36
+ return oxcTarget;
37
+ }
38
+ export function resolveStyleTarget(target) {
39
+ let resolvedTarget = normalizeStyleTargetObject(target, 'target');
40
+ if (!resolvedTarget)
41
+ return undefined;
42
+ let lightningCssTargets = {};
43
+ for (let browserTargetName of browserTargetNames) {
44
+ let version = resolvedTarget[browserTargetName];
45
+ if (version == null)
46
+ continue;
47
+ lightningCssTargets[lightningCssTargetNameByBrowserTargetName[browserTargetName]] =
48
+ toLightningCssTargetVersion(version);
49
+ }
50
+ return lightningCssTargets;
51
+ }
52
+ function normalizeScriptTargetObject(target, optionPath) {
53
+ if (target == null)
54
+ return undefined;
55
+ if (!isPlainObject(target)) {
56
+ throw new TypeError(`${optionPath} must be an object`);
57
+ }
58
+ let normalizedTarget = {};
59
+ for (let [key, value] of Object.entries(target)) {
60
+ if (key === 'es') {
61
+ normalizedTarget.es = normalizeScriptTargetVersion(value, `${optionPath}.es`);
62
+ continue;
63
+ }
64
+ if (!browserTargetNameSet.has(key)) {
65
+ throw new TypeError(`${optionPath}.${key} is not a supported target`);
66
+ }
67
+ normalizedTarget[key] = normalizeBrowserTargetVersion(value, `${optionPath}.${key}`);
68
+ }
69
+ return Object.keys(normalizedTarget).length === 0 ? undefined : normalizedTarget;
70
+ }
71
+ function normalizeStyleTargetObject(target, optionPath) {
72
+ if (target == null)
73
+ return undefined;
74
+ if (!isPlainObject(target)) {
75
+ throw new TypeError(`${optionPath} must be an object`);
76
+ }
77
+ let normalizedTarget = {};
78
+ for (let [key, value] of Object.entries(target)) {
79
+ if (key === 'es') {
80
+ continue;
81
+ }
82
+ if (!browserTargetNameSet.has(key)) {
83
+ throw new TypeError(`${optionPath}.${key} is not a supported target`);
84
+ }
85
+ normalizedTarget[key] = normalizeBrowserTargetVersion(value, `${optionPath}.${key}`);
86
+ }
87
+ return Object.keys(normalizedTarget).length === 0 ? undefined : normalizedTarget;
88
+ }
89
+ function normalizeScriptTargetVersion(value, optionPath) {
90
+ if (typeof value !== 'string') {
91
+ throw new TypeError(`${optionPath} must be a string`);
92
+ }
93
+ let normalizedValue = value.trim();
94
+ if (normalizedValue.length === 0) {
95
+ throw new TypeError(`${optionPath} must be a non-empty string`);
96
+ }
97
+ if (!/^\d+$/.test(normalizedValue)) {
98
+ throw new TypeError(`${optionPath} must use a single numeric year like "2020"`);
99
+ }
100
+ if (!/^\d{4}$/.test(normalizedValue) || Number(normalizedValue) < 2015) {
101
+ throw new TypeError(`${optionPath} must use a four-digit year of 2015 or higher`);
102
+ }
103
+ return `es${normalizedValue}`;
104
+ }
105
+ function normalizeBrowserTargetVersion(value, optionPath) {
106
+ if (typeof value !== 'string') {
107
+ throw new TypeError(`${optionPath} must be a string`);
108
+ }
109
+ if (value.trim().length === 0) {
110
+ throw new TypeError(`${optionPath} must be a non-empty string`);
111
+ }
112
+ if (!/^\d+(\.\d+){0,2}$/.test(value)) {
113
+ throw new TypeError(`${optionPath} must use "X", "X.Y", or "X.Y.Z" version format`);
114
+ }
115
+ let segments = value.split('.').map(Number);
116
+ if (segments.some((segment) => segment > 255)) {
117
+ throw new TypeError(`${optionPath} must use version components between 0 and 255`);
118
+ }
119
+ return value;
120
+ }
121
+ function toLightningCssTargetVersion(version) {
122
+ let [major, minor = 0, patch = 0] = version.split('.').map(Number);
123
+ return major * 65536 + minor * 256 + patch;
124
+ }
125
+ function isPlainObject(value) {
126
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
127
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@remix-run/assets",
3
- "version": "0.1.0",
4
- "description": "Fetch-based server for compiling browser JS/TS assets on demand",
3
+ "version": "0.2.0",
4
+ "description": "Fetch-based server for compiling browser JS/TS and CSS assets on demand",
5
5
  "author": "Michael Jackson <mjijackson@gmail.com>",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -29,22 +29,27 @@
29
29
  "chokidar": "^5.0.0",
30
30
  "es-module-lexer": "^2.0.0",
31
31
  "get-tsconfig": "^4.13.6",
32
+ "lightningcss": "^1.32.0",
32
33
  "magic-string": "^0.30.21",
33
34
  "oxc-minify": "^0.121.0",
34
35
  "oxc-parser": "^0.121.0",
35
36
  "oxc-resolver": "^11.19.1",
36
37
  "oxc-transform": "^0.121.0",
38
+ "picomatch": "^4.0.4",
37
39
  "source-map-js": "^1.2.1",
38
40
  "@remix-run/headers": "0.19.0",
39
41
  "@remix-run/route-pattern": "0.20.1"
40
42
  },
41
43
  "devDependencies": {
42
44
  "@types/node": "^24.6.0",
45
+ "@types/picomatch": "^4.0.3",
43
46
  "@typescript/native-preview": "7.0.0-dev.20251125.1"
44
47
  },
45
48
  "keywords": [
46
49
  "remix",
47
50
  "scripts",
51
+ "styles",
52
+ "css",
48
53
  "assets",
49
54
  "transform",
50
55
  "server"