Skip to content
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

Compile .css files to JS modules so the load in browsers without bundling #914

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script type="module">
import * as monaco from './release/esm/vs/editor/editor.main.js';

monaco.editor.create(document.getElementById('container'), {
value: [
'function x() {',
'\tconsole.log("Hello world!");',
'}'
].join('\n'),
language: 'javascript'
});
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>
28 changes: 21 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ gulp.task('release', ['clean-release'], function() {
return es.merge(

// dev folder
releaseOne('dev'),
// releaseOne('dev'),

// min folder
releaseOne('min'),
// releaseOne('min'),

// esm folder
ESM_release(),
Expand All @@ -53,7 +53,7 @@ gulp.task('release', ['clean-release'], function() {
.pipe(gulp.dest('release')),

// min-maps folder
gulp.src('node_modules/monaco-editor-core/min-maps/**/*').pipe(gulp.dest('release/min-maps')),
// gulp.src('node_modules/monaco-editor-core/min-maps/**/*').pipe(gulp.dest('release/min-maps')),

// other files
gulp.src([
Expand Down Expand Up @@ -210,6 +210,7 @@ function ESM_release() {
'!node_modules/monaco-editor-core/esm/vs/editor/editor.api.d.ts'
])
.pipe(ESM_addImportSuffix())
.pipe(ESM_transformCSSModules())
.pipe(ESM_addPluginContribs('release/esm'))
.pipe(gulp.dest('release/esm')),
ESM_pluginStreams('release/esm/')
Expand Down Expand Up @@ -302,10 +303,27 @@ function ESM_pluginStream(plugin, destinationPath) {
this.emit('data', data);
}))
.pipe(ESM_addImportSuffix())
.pipe(ESM_transformCSSModules())
.pipe(gulp.dest(destinationPath + plugin.modulePrefix))
);
}

function ESM_transformCSSModules() {
return es.through(function(data) {
if (!/\.css$/.test(data.path)) {
this.emit('data', data);
return;
}
const contents = data.contents.toString();
data.contents = new Buffer(`
const styleElement = document.createElement('style');
styleElement.textContent = \`${contents}\`;
document.head.appendChild(styleElement);`);
data.path = data.path.replace(/\.css$/, '.css.js');
this.emit('data', data);
});
}

function ESM_addImportSuffix() {
return es.through(function(data) {
if (!/\.js$/.test(data.path)) {
Expand All @@ -321,10 +339,6 @@ function ESM_addImportSuffix() {
const pos = info.importedFiles[i].pos;
const end = info.importedFiles[i].end;

if (/\.css$/.test(importText)) {
continue;
}

contents = (
contents.substring(0, pos + 1)
+ importText + '.js'
Expand Down