Commit bc12e67b authored by catch's avatar catch

Issue #2880013 by alexpott, droplet: Add command to check if .es6.js has been transpiled

parent a83586c3
......@@ -14,6 +14,7 @@
"babel-core": "6.24.1",
"babel-plugin-add-header-comment": "1.0.3",
"babel-preset-env": "1.4.0",
"chalk": "^1.1.3",
"chokidar": "1.6.1",
"cross-env": "^4.0.0",
"eslint": "3.19.0",
......@@ -22,9 +23,10 @@
"eslint-plugin-jsx-a11y": "4.0.0",
"eslint-plugin-react": "6.10.3",
"glob": "7.1.1",
"minimist": "^1.2.0",
"stylelint": "^7.10.1",
"stylelint-config-standard": "^16.0.0",
"stylelint-checkstyle-formatter": "^0.1.0",
"stylelint-config-standard": "^16.0.0",
"stylelint-no-browser-hacks": "^1.0.2"
},
"babel": {
......
/**
* @file
*
* Compile *.es6.js files to ES5.
* Provides the build:js command to compile *.es6.js files to ES5.
*
* Run build:js with --file to only parse a specific file. Using the --check
* flag build:js can be run to check if files are compiled correctly.
* @example <caption>Only process misc/drupal.es6.js and misc/drupal.init.es6.js</caption
* yarn run build:js -- --file misc/drupal.es6.js --file misc/drupal.init.es6.js
* @example <caption>Check if all files have been compiled correctly</caption
* yarn run build:js -- --check
*
* @internal This file is part of the core javascript build process and is only
* meant to be used in that context.
......@@ -9,11 +16,10 @@
'use strict';
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const argv = require('minimist')(process.argv.slice(2));
const changeOrAdded = require('./changeOrAdded');
const check = require('./check');
const log = require('./log');
// Match only on .es6.js files.
......@@ -27,14 +33,15 @@ const processFiles = (error, filePaths) => {
process.exitCode = 1;
}
// Process all the found files.
filePaths.forEach(changeOrAdded);
let callback = changeOrAdded;
if (argv.check) {
callback = check;
}
filePaths.forEach(callback);
};
// Run build:js with some special arguments to only parse specific files.
// npm run build:js -- --files misc/drupal.es6.js misc/drupal.init.es6.js
// Only misc/drupal.es6.js misc/drupal.init.es6.js will be processed.
if (process.argv.length > 2 && process.argv[2] === '--files') {
processFiles(null, process.argv.splice(3, process.argv.length));
if (argv.file) {
processFiles(null, [].concat(argv.file));
}
else {
glob(fileMatch, globOptions, processFiles);
......
const fs = require('fs');
const babel = require('babel-core');
const log = require('./log');
const compile = require('./compile');
module.exports = (filePath) => {
const moduleName = filePath.slice(0, -7);
log(`'${filePath}' is being processed.`);
// Transform the file.
// Check process.env.NODE_ENV to see if we should create sourcemaps.
babel.transformFile(
filePath,
{
sourceMaps: process.env.NODE_ENV === 'development' ? 'inline' : false,
comments: false,
plugins: [
['add-header-comment', {
'header': [
`DO NOT EDIT THIS FILE.\nAll changes should be applied to ${filePath}\nSee the following change record for more information,\nhttps://www.drupal.org/node/2873849\n@preserve`
]
}]
]
},
(err, result) => {
if (err) {
throw new Error(err);
}
const fileName = filePath.slice(0, -7);
// Write the result to the filesystem.
fs.writeFile(`${fileName}.js`, result.code, () => {
log(`'${filePath}' is finished.`);
});
}
);
compile(filePath, function write(code) {
const fileName = filePath.slice(0, -7);
// Write the result to the filesystem.
fs.writeFile(`${fileName}.js`, code, () => {
log(`'${filePath}' is finished.`);
});
});
}
const chalk = require('chalk');
const fs = require('fs');
const log = require('./log');
const compile = require('./compile');
module.exports = (filePath) => {
log(`'${filePath}' is being checked.`);
// Transform the file.
compile(filePath, function check(code) {
const fileName = filePath.slice(0, -7);
fs.readFile(`${fileName}.js`, function read(err, data) {
if (err) {
log(chalk.red(err));
process.exitCode = 1;
return;
}
if (code !== data.toString()) {
log(chalk.red(`'${filePath}' is not updated.`));
process.exitCode = 1;
}
});
});
}
const babel = require('babel-core');
module.exports = (filePath, callback) => {
// Transform the file.
// Check process.env.NODE_ENV to see if we should create sourcemaps.
babel.transformFile(
filePath,
{
sourceMaps: process.env.NODE_ENV === 'development' ? 'inline' : false,
comments: false,
plugins: [
['add-header-comment', {
'header': [
`DO NOT EDIT THIS FILE.\nAll changes should be applied to ${filePath}\nSee the following change record for more information,\nhttps://www.drupal.org/node/2873849\n@preserve`
]
}]
]
},
(err, result) => {
if (err) {
throw new Error(err);
}
callback(result.code);
}
);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment