Skip to content

Commit

Permalink
[dev] share sass build with script (#34323)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer authored Apr 2, 2019
1 parent 8290b56 commit 6703848
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 27 deletions.
21 changes: 21 additions & 0 deletions scripts/build_sass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

require('../src/setup_node_env');
require('../src/dev/sass/run_build_sass_cli');
30 changes: 5 additions & 25 deletions src/dev/build/tasks/transpile_scss_task.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,14 @@
* under the License.
*/

import { toArray } from 'rxjs/operators';
import { buildAll } from '../../../legacy/server/sass/build_all';
import { findPluginSpecs } from '../../../legacy/plugin_discovery/find_plugin_specs';
import { collectUiExports } from '../../../legacy/ui/ui_exports/collect_ui_exports';
import { buildSass } from '../../sass';

export const TranspileScssTask = {
description: 'Transpiling SCSS to CSS',

async run(config, log, build) {
const scanDirs = [ build.resolvePath('src/legacy/core_plugins') ];
const paths = [ build.resolvePath('node_modules/x-pack') ];

const { spec$ } = findPluginSpecs({ plugins: { scanDirs, paths } });
const enabledPlugins = await spec$.pipe(toArray()).toPromise();
const uiExports = collectUiExports(enabledPlugins);

try {
const bundles = await buildAll({
styleSheets: uiExports.styleSheetPaths,
log,
buildDir: build.resolvePath('built_assets/css'),
outputStyle: 'compressed',
sourceMap: false
});
bundles.forEach(bundle => log.info(`Compiled SCSS: ${bundle.sourcePath} (theme=${bundle.theme})`));
} catch (error) {
const { message, line, file } = error;
throw new Error(`${message} on line ${line} of ${file}`);
}
await buildSass({
log,
kibanaDir: build.resolvePath('.')
});
}
};
3 changes: 1 addition & 2 deletions src/dev/run/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,5 @@ export function getHelp(options: Options) {
.join('\n ')}
Options:
${optionHelp}
`;
${optionHelp + '\n\n'}`;
}
67 changes: 67 additions & 0 deletions src/dev/sass/build_sass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { resolve } from 'path';

import { toArray } from 'rxjs/operators';

import { createFailError } from '../run';
import { findPluginSpecs } from '../../legacy/plugin_discovery';
import { collectUiExports } from '../../legacy/ui';
import { buildAll } from '../../legacy/server/sass/build_all';

export async function buildSass({ log, kibanaDir }) {
log.info('running plugin discovery in', kibanaDir);

const scanDirs = [
resolve(kibanaDir, 'src/legacy/core_plugins')
];

const paths = [
resolve(kibanaDir, 'x-pack'),
resolve(kibanaDir, 'node_modules/x-pack')
];

const { spec$ } = findPluginSpecs({ plugins: { scanDirs, paths } });
const enabledPlugins = await spec$.pipe(toArray()).toPromise();
const uiExports = collectUiExports(enabledPlugins);
log.info('found %d styleSheetPaths', uiExports.styleSheetPaths.length);
log.verbose(uiExports.styleSheetPaths);

let bundleCount = 0;
try {
const bundles = await buildAll({
styleSheets: uiExports.styleSheetPaths,
log,
buildDir: resolve(kibanaDir, 'built_assets/css'),
sourceMap: true
});

bundles.forEach(bundle => {
log.debug(`Compiled SCSS: ${bundle.sourcePath} (theme=${bundle.theme})`);
});

bundleCount = bundles.length;
} catch (error) {
const { message, line, file } = error;
throw createFailError(`${message} on line ${line} of ${file}`);
}

log.success('%d scss bundles created', bundleCount);
}
20 changes: 20 additions & 0 deletions src/dev/sass/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { buildSass } from './build_sass';
40 changes: 40 additions & 0 deletions src/dev/sass/run_build_sass_cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { run } from '../run';
import { REPO_ROOT } from '../constants';
import { buildSass } from './build_sass';

run(async ({ log, flags: { kibanaDir } }) => {
await buildSass({
log,
kibanaDir
});
}, {
description: 'Simple CLI, useful for building scss files outside of the server',
flags: {
default: {
kibanaDir: REPO_ROOT
},
string: ['kibanaDir'],
help: `
--kibanaDir The root of the Kibana directory to build sass files in.
`
},
});

0 comments on commit 6703848

Please sign in to comment.