-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
feat(remix-dev/compiler): add CSS plugin to esbuild
#4130
Merged
Merged
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
6361c2f
create and add css file plugin
KingSora 6f3332e
Update contributors.yml
KingSora 4bc5657
absolute url paths are considered external
KingSora 3b94b24
Merge branch 'dev' of https://github.com/KingSora/remix into dev
KingSora facd266
add correct sourcemap and minify behavior
KingSora 797022a
improve code
KingSora 54c8b54
add comment why sourcemap false
KingSora f18bd54
improve code and output
KingSora 000a13a
improve entrypoint search
KingSora 56f8688
Merge branch 'dev' into dev
KingSora db271ed
better strategy for deletion of duplicates
KingSora 3650a95
Merge branch 'dev' of https://github.com/KingSora/remix into dev
KingSora 1f9ee91
refine process and add watchFiles
KingSora 362250b
pr review
KingSora a0cb142
Merge branch 'dev' into dev
KingSora 92473d0
add error and warnings
KingSora 47ebf83
Merge branch 'dev' of https://github.com/KingSora/remix into dev
KingSora a4106a8
Merge branch 'dev' into dev
KingSora a804d4e
windows slashes fix
KingSora fc5c271
Merge branch 'dev' of https://github.com/KingSora/remix into dev
KingSora 85d5a9d
fix errors
KingSora e65fb5e
Merge branch 'dev' into dev
KingSora 0d1344a
chore: couple nit picks
mcansh c1f5c2b
test: add basic test confirming a font was copied
mcansh 8d95db4
Merge branch 'dev' into dev
mcansh f77eeca
Create large-colts-drop.md
mcansh 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 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 |
---|---|---|
|
@@ -227,6 +227,7 @@ | |
- kilian | ||
- kiliman | ||
- kimdontdoit | ||
- KingSora | ||
- klauspaiva | ||
- knowler | ||
- konradkalemba | ||
|
This file contains 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 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,103 @@ | ||
import * as path from "path"; | ||
import * as fse from "fs-extra"; | ||
import esbuild from "esbuild"; | ||
|
||
import { BuildMode } from "../../build"; | ||
import type { BuildConfig } from "../../compiler"; | ||
|
||
const isExtendedLengthPath = /^\\\\\?\\/; | ||
|
||
const normalizePathSlashes = (path: string) => | ||
isExtendedLengthPath.test(path) ? path : path.replace(/\\/g, "/"); | ||
|
||
/** | ||
* This plugin loads css files with the "css" loader (bundles and moves assets to assets directory) | ||
* and exports the url of the css file as its default export. | ||
*/ | ||
export function cssFilePlugin( | ||
buildConfig: Pick<Partial<BuildConfig>, "mode"> | ||
): esbuild.Plugin { | ||
return { | ||
name: "css-file", | ||
|
||
async setup(build) { | ||
let buildOps = build.initialOptions; | ||
|
||
build.onLoad({ filter: /\.css$/ }, async (args) => { | ||
let { outfile, outdir, assetNames } = buildOps; | ||
let { metafile, outputFiles, warnings, errors } = await esbuild.build({ | ||
...buildOps, | ||
minify: buildConfig.mode === BuildMode.Production, | ||
minifySyntax: true, | ||
metafile: true, | ||
write: false, | ||
sourcemap: false, | ||
incremental: false, | ||
splitting: false, | ||
stdin: undefined, | ||
outfile: undefined, | ||
outdir: outfile ? path.dirname(outfile) : outdir, | ||
entryNames: assetNames, | ||
entryPoints: [args.path], | ||
loader: { | ||
...buildOps.loader, | ||
".css": "css", | ||
}, | ||
// this plugin treats absolute paths in 'url()' css rules as external to prevent breaking changes | ||
plugins: [ | ||
{ | ||
name: "resolve-absolute", | ||
async setup(build) { | ||
build.onResolve({ filter: /.*/ }, async (args) => { | ||
let { kind, path: resolvePath } = args; | ||
if (kind === "url-token" && path.isAbsolute(resolvePath)) { | ||
return { | ||
path: resolvePath, | ||
external: true, | ||
}; | ||
} | ||
}); | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
if (errors) { | ||
return { errors }; | ||
} | ||
|
||
let { outputs } = metafile!; | ||
let entry = Object.keys(outputs).find( | ||
(out) => outputs[out].entryPoint | ||
)!; | ||
let entryFile = outputFiles!.find((file) => | ||
normalizePathSlashes(file.path).endsWith(normalizePathSlashes(entry)) | ||
)!; | ||
let outputFilesWithoutEntry = outputFiles!.filter( | ||
(file) => file !== entryFile | ||
); | ||
|
||
// write all assets | ||
await Promise.all( | ||
outputFilesWithoutEntry.map(({ path: filepath, contents }) => | ||
fse.outputFile(filepath, contents) | ||
) | ||
); | ||
|
||
return { | ||
contents: entryFile.contents, | ||
loader: "file", | ||
// add all css assets to watchFiles | ||
watchFiles: Object.values(outputs).reduce((arr, { inputs }) => { | ||
let resolvedInputs = Object.keys(inputs).map((input) => | ||
path.resolve(input) | ||
); | ||
arr.push(...resolvedInputs); | ||
return arr; | ||
}, [] as string[]), | ||
warnings, | ||
}; | ||
}); | ||
}, | ||
}; | ||
} |
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.
I think we need to change to
errors && errors.length !== 0
here. I've patched remix with this patch, and give it try. But failed withimport xx from "katex/dist/katex.min.css"
, finding thaterrors = []
. @KingSoraThere 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.
@KingSora @xhebox - would this CSS plugin allow us to import css files like this at the top of JS files, similar to CSS modules?
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.
@xhebox thanks! Fixed that with: 85d5a9d :)
@cliffordfajardo Yes, but the
styles
variable is the url of the stylesheet