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

issue/2824-react Added react template support #2963

Merged
merged 8 commits into from
Mar 11, 2021
Merged
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
6 changes: 4 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
"env": {
"browser": true,
"commonjs": false,
"es6": false,
"es2020": true,
"amd": true
},
"extends": [
"standard"
],
"globals": {
"ReactDOM": true,
"React": true,
"Promise": true,
"requirejs": true,
"Backbone": true,
Expand Down Expand Up @@ -39,4 +41,4 @@
"requirejs/no-commonjs-wrapper": 2,
"requirejs/no-object-define": 1
}
}
}
22 changes: 22 additions & 0 deletions grunt/config/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ module.exports = function(grunt, options) {
return grunt.config('helpers').includedFilter(filepath);
},
umdImports: [
'../node_modules/object.assign/dist/browser.js',
'../node_modules/react/umd/react.development.js',
'../node_modules/react-dom/umd/react-dom.development.js',
'../node_modules/html-react-parser/dist/html-react-parser.min.js'
],
reactTemplates: [
'<%= sourcedir %>core/templates/**/*.jsx',
'<%= sourcedir %>components/*/templates/**/*.jsx',
'<%= sourcedir %>extensions/*/templates/**/*.jsx',
'<%= sourcedir %>menu/*/templates/**/*.jsx',
'<%= sourcedir %>theme/*/templates/**/*.jsx'
],
external: {
jquery: 'empty:',
Expand Down Expand Up @@ -74,6 +85,17 @@ module.exports = function(grunt, options) {
return grunt.config('helpers').includedFilter(filepath);
},
umdImports: [
'../node_modules/object.assign/dist/browser.js',
'../node_modules/react/umd/react.production.min.js',
'../node_modules/react-dom/umd/react-dom.production.min.js',
'../node_modules/html-react-parser/dist/html-react-parser.min.js'
],
reactTemplates: [
'<%= sourcedir %>core/templates/**/*.jsx',
'<%= sourcedir %>components/*/templates/**/*.jsx',
'<%= sourcedir %>extensions/*/templates/**/*.jsx',
'<%= sourcedir %>menu/*/templates/**/*.jsx',
'<%= sourcedir %>theme/*/templates/**/*.jsx'
],
external: {
jquery: 'empty:',
Expand Down
2 changes: 1 addition & 1 deletion grunt/config/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
tasks: ['newer:copy:courseAssets']
},
js: {
files: ['<%= sourcedir %>**/*.js'],
files: ['<%= sourcedir %>**/*.js', '<%= sourcedir %>**/*.jsx'],
options: {
spawn: false
},
Expand Down
33 changes: 31 additions & 2 deletions grunt/tasks/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function(grunt) {
const isDisableCache = process.argv.includes('--disable-cache');
let cache;

const extensions = ['.js'];
const extensions = ['.js', '.jsx'];

const restoreCache = async (cachePath, basePath) => {
if (isDisableCache || cache || !fs.existsSync(cachePath)) return;
Expand Down Expand Up @@ -161,6 +161,14 @@ module.exports = function(grunt) {
});
}

// Collect react templates
const reactTemplatePaths = [];
options.reactTemplates.forEach(pattern => {
grunt.file.expand({
filter: options.pluginsFilter
}, pattern).forEach(templatePath => reactTemplatePaths.push(templatePath.replace(convertSlashes, '/')));
});

// Process remapping and external model configurations
const mapParts = Object.keys(options.map);
const externalParts = Object.keys(options.external);
Expand Down Expand Up @@ -257,6 +265,8 @@ module.exports = function(grunt) {
// Dynamically construct plugins.js with plugin dependencies
code = `define([${pluginPaths.map(filename => {
return `"${filename}"`;
}).join(',')}, ${reactTemplatePaths.map(filename => {
return `"${filename}"`;
}).join(',')}], function() {});`;
return code;
}
Expand All @@ -280,6 +290,12 @@ module.exports = function(grunt) {
'**/node_modules/**'
],
presets: [
[
'@babel/preset-react',
{
runtime: 'classic'
}
],
[
'@babel/preset-env',
{
Expand All @@ -302,7 +318,20 @@ module.exports = function(grunt) {
ignoreNestedRequires: true,
defineFunctionName: '__AMD',
defineModuleId: (moduleId) => moduleId.replace(convertSlashes, '/').replace(basePath, '').replace('.js', ''),
excludes: []
excludes: [
'**/templates/**/*.jsx'
]
}
],
[
'transform-react-templates',
{
includes: [
'**/templates/**/*.jsx'
],
importRegisterFunctionFromModule: path.resolve(basePath, 'core/js/reactHelpers.js').replace(convertSlashes, '/'),
registerFunctionName: 'register',
registerTemplateName: (moduleId) => path.parse(moduleId).name
}
]
]
Expand Down
Loading