-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[fix] resolve $lib alias when packaging #2453
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/kit': patch | ||
| --- | ||
|
|
||
| Resolve \$lib alias when packaging |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,11 +14,19 @@ const essential_files = ['README', 'LICENSE', 'CHANGELOG', '.gitignore', '.npmig | |
| * @param {string} cwd | ||
| */ | ||
| export async function make_package(config, cwd = process.cwd()) { | ||
| rimraf(path.join(cwd, config.kit.package.dir)); | ||
| const abs_package_dir = path.join(cwd, config.kit.package.dir); | ||
| rimraf(abs_package_dir); | ||
|
|
||
| if (config.kit.package.emitTypes) { | ||
| // Generate type definitions first so hand-written types can overwrite generated ones | ||
| await emit_dts(config); | ||
| // Resolve aliases, TS leaves them as-is | ||
| const files = walk(abs_package_dir); | ||
| for (const file of files) { | ||
| const filename = path.join(abs_package_dir, file); | ||
| const source = fs.readFileSync(filename, 'utf8'); | ||
| fs.writeFileSync(filename, resolve_$lib_alias(file, source, config)); | ||
| } | ||
| } | ||
|
|
||
| const files_filter = create_filter(config.kit.package.files); | ||
|
|
@@ -47,7 +55,7 @@ export async function make_package(config, cwd = process.cwd()) { | |
|
|
||
| if (!files_filter(file.replace(/\\/g, '/'))) { | ||
| const dts_file = (svelte_ext ? file : file.slice(0, -ext.length)) + '.d.ts'; | ||
| const dts_path = path.join(cwd, config.kit.package.dir, dts_file); | ||
| const dts_path = path.join(abs_package_dir, dts_file); | ||
| if (fs.existsSync(dts_path)) fs.unlinkSync(dts_path); | ||
| continue; | ||
| } | ||
|
|
@@ -72,7 +80,7 @@ export async function make_package(config, cwd = process.cwd()) { | |
| // TypeScript's declaration emit won't copy over the d.ts files, so we do it here | ||
| out_file = file; | ||
| out_contents = source; | ||
| if (fs.existsSync(path.join(cwd, config.kit.package.dir, out_file))) { | ||
| if (fs.existsSync(path.join(abs_package_dir, out_file))) { | ||
| console.warn( | ||
| 'Found already existing file from d.ts generation for ' + | ||
| out_file + | ||
|
|
@@ -86,8 +94,9 @@ export async function make_package(config, cwd = process.cwd()) { | |
| out_file = file; | ||
| out_contents = source; | ||
| } | ||
| out_contents = resolve_$lib_alias(out_file, out_contents, config); | ||
|
|
||
| write(path.join(cwd, config.kit.package.dir, out_file), out_contents); | ||
| write(path.join(abs_package_dir, out_file), out_contents); | ||
|
|
||
| if (exports_filter(file)) { | ||
| const original = `$lib/${file.replace(/\\/g, '/')}`; | ||
|
|
@@ -134,7 +143,7 @@ export async function make_package(config, cwd = process.cwd()) { | |
| } | ||
| } | ||
|
|
||
| write(path.join(cwd, config.kit.package.dir, 'package.json'), JSON.stringify(pkg, null, ' ')); | ||
| write(path.join(abs_package_dir, 'package.json'), JSON.stringify(pkg, null, ' ')); | ||
|
|
||
| const whitelist = fs.readdirSync(cwd).filter((file) => { | ||
| const lowercased = file.toLowerCase(); | ||
|
|
@@ -144,11 +153,46 @@ export async function make_package(config, cwd = process.cwd()) { | |
| const full_path = path.join(cwd, pathname); | ||
| if (fs.lstatSync(full_path).isDirectory()) continue; // just to be sure | ||
|
|
||
| const package_path = path.join(cwd, config.kit.package.dir, pathname); | ||
| const package_path = path.join(abs_package_dir, pathname); | ||
| if (!fs.existsSync(package_path)) fs.copyFileSync(full_path, package_path); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the `$lib` alias. | ||
| * | ||
| * TODO: make this more generic to also handle other aliases the user could have defined | ||
| * via `kit.vite.resolve.alias`. Also investage how to do this in a more robust way | ||
| * (right now regex string replacement is used). | ||
| * For more discussion see https://github.com/sveltejs/kit/pull/2453 | ||
| * | ||
| * @param {string} file Relative to the lib root | ||
| * @param {string} content | ||
| * @param {import('types/config').ValidatedConfig} config | ||
| * @returns {string} | ||
| */ | ||
| function resolve_$lib_alias(file, content, config) { | ||
| /** | ||
| * @param {string} match | ||
| * @param {string} _ | ||
| * @param {string} import_path | ||
| */ | ||
| const replace_import_path = (match, _, import_path) => { | ||
| if (!import_path.startsWith('$lib/')) { | ||
| return match; | ||
| } | ||
|
|
||
| const full_path = path.join(config.kit.files.lib, file); | ||
| const full_import_path = path.join(config.kit.files.lib, import_path.slice('$lib/'.length)); | ||
| let resolved = path.relative(path.dirname(full_path), full_import_path).replace(/\\/g, '/'); | ||
| resolved = resolved.startsWith('.') ? resolved : './' + resolved; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe inline this ternary below to keep |
||
| return match.replace(import_path, resolved); | ||
| }; | ||
| content = content.replace(/from\s+('|")([^"';,]+?)\1/g, replace_import_path); | ||
| content = content.replace(/import\s*\(\s*('|")([^"';,]+?)\1\s*\)/g, replace_import_path); | ||
| return content; | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} filename | ||
| * @param {string} source | ||
|
|
||
4 changes: 4 additions & 0 deletions
4
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/Test.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <script lang="ts"> | ||
| import { foo } from './sub/foo'; | ||
| export let bar = foo; | ||
| </script> |
15 changes: 15 additions & 0 deletions
15
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/Test.svelte.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { SvelteComponentTyped } from 'svelte'; | ||
| declare const __propDef: { | ||
| props: { | ||
| bar?: import('./sub/foo').Foo; | ||
| }; | ||
| events: { | ||
| [evt: string]: CustomEvent<any>; | ||
| }; | ||
| slots: {}; | ||
| }; | ||
| export declare type TestProps = typeof __propDef.props; | ||
| export declare type TestEvents = typeof __propDef.events; | ||
| export declare type TestSlots = typeof __propDef.slots; | ||
| export default class Test extends SvelteComponentTyped<TestProps, TestEvents, TestSlots> {} | ||
| export {}; |
4 changes: 4 additions & 0 deletions
4
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/baz.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export interface Baz { | ||
| baz: string; | ||
| } | ||
| export declare const baz: Baz; |
1 change: 1 addition & 0 deletions
1
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/baz.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const baz = { baz: 'baz' }; |
1 change: 1 addition & 0 deletions
1
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default as Test } from './Test.svelte'; |
1 change: 1 addition & 0 deletions
1
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default as Test } from './Test.svelte'; |
15 changes: 15 additions & 0 deletions
15
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "name": "resolve-alias", | ||
| "version": "1.0.0", | ||
| "description": "package using $lib alias", | ||
| "type": "module", | ||
| "exports": { | ||
| "./package.json": "./package.json", | ||
| "./Test.svelte": "./Test.svelte", | ||
| ".": "./index.js", | ||
| "./baz": "./baz.js", | ||
| "./sub/bar": "./sub/bar.js", | ||
| "./sub/foo": "./sub/foo.js" | ||
| }, | ||
| "svelte": "./index.js" | ||
| } |
2 changes: 2 additions & 0 deletions
2
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/sub/bar.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export declare const bar1: import('./foo').Foo; | ||
| export declare const bar2: import('../baz').Baz; |
4 changes: 4 additions & 0 deletions
4
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/sub/bar.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { baz } from '../baz'; | ||
| import { foo } from './foo'; | ||
| export const bar1 = foo; | ||
| export const bar2 = baz; |
4 changes: 4 additions & 0 deletions
4
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/sub/foo.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export interface Foo { | ||
| foo: string; | ||
| } | ||
| export declare const foo: Foo; |
1 change: 1 addition & 0 deletions
1
packages/kit/src/packaging/test/fixtures/resolve-alias/expected/sub/foo.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const foo = { foo: 'foo' }; |
5 changes: 5 additions & 0 deletions
5
packages/kit/src/packaging/test/fixtures/resolve-alias/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "name": "resolve-alias", | ||
| "version": "1.0.0", | ||
| "description": "package using $lib alias" | ||
| } |
12 changes: 12 additions & 0 deletions
12
packages/kit/src/packaging/test/fixtures/resolve-alias/src/app.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <link rel="icon" href="/favicon.ico" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| %svelte.head% | ||
| </head> | ||
| <body> | ||
| <div id="svelte">%svelte.body%</div> | ||
| </body> | ||
| </html> |
5 changes: 5 additions & 0 deletions
5
packages/kit/src/packaging/test/fixtures/resolve-alias/src/lib/Test.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <script lang="ts"> | ||
| import { foo } from '$lib/sub/foo'; | ||
|
|
||
| export let bar = foo; | ||
| </script> |
5 changes: 5 additions & 0 deletions
5
packages/kit/src/packaging/test/fixtures/resolve-alias/src/lib/baz.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface Baz { | ||
| baz: string; | ||
| } | ||
|
|
||
| export const baz: Baz = { baz: 'baz' }; |
1 change: 1 addition & 0 deletions
1
packages/kit/src/packaging/test/fixtures/resolve-alias/src/lib/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default as Test } from '$lib/Test.svelte'; |
5 changes: 5 additions & 0 deletions
5
packages/kit/src/packaging/test/fixtures/resolve-alias/src/lib/sub/bar.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { baz } from '$lib/baz'; | ||
| import { foo } from '$lib/sub/foo'; | ||
|
|
||
| export const bar1 = foo; | ||
| export const bar2 = baz; |
5 changes: 5 additions & 0 deletions
5
packages/kit/src/packaging/test/fixtures/resolve-alias/src/lib/sub/foo.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface Foo { | ||
| foo: string; | ||
| } | ||
|
|
||
| export const foo: Foo = { foo: 'foo' }; |
5 changes: 5 additions & 0 deletions
5
packages/kit/src/packaging/test/fixtures/resolve-alias/svelte.config.cjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const preprocess = require('svelte-preprocess'); | ||
|
|
||
| module.exports = { | ||
| preprocess: preprocess() | ||
| }; |
30 changes: 30 additions & 0 deletions
30
packages/kit/src/packaging/test/fixtures/resolve-alias/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "moduleResolution": "node", | ||
| "module": "es2020", | ||
| "lib": ["es2020", "DOM"], | ||
| "target": "es2019", | ||
| /** | ||
| svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript | ||
| to enforce using \`import type\` instead of \`import\` for Types. | ||
| */ | ||
| "importsNotUsedAsValues": "error", | ||
| "isolatedModules": true, | ||
| "resolveJsonModule": true, | ||
| /** | ||
| To have warnings/errors of the Svelte compiler at the correct position, | ||
| enable source maps by default. | ||
| */ | ||
| "sourceMap": true, | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "baseUrl": ".", | ||
| "allowJs": true, | ||
| "checkJs": true, | ||
| "paths": { | ||
| "$lib/*": ["src/lib/*"] | ||
| } | ||
| }, | ||
| "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when
full_import_pathdoesn't actually exists, for whatever reasons it may be (most likely a bad copy-paste or refactor)?path.relativewon't throw as long as it's a string, should we throw an error, leave it with the original alias in place, or use the non-existent resolved path (currently the case)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's on the user then, the also things like
svelte-checkshould be able find out. We don't check other file paths for correctness, so in my mind it would be inconsistent to check the aliases.