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

t/5: Introducing the CKEDITOR namespace, the AMD API and the Builder. #6

Merged
merged 34 commits into from Jan 19, 2015
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1c16b97
Added the ckeditor5-core dependency.
fredck Jan 9, 2015
b44682c
Added the original RequireJS script.
fredck Jan 9, 2015
03f78c4
Added the entry point script: ckeditor.js.
fredck Jan 9, 2015
2c6e785
Added minimum configuration for RequireJS.
fredck Jan 9, 2015
a23bda8
Exposed the AMD API through the CKEDITOR namespace.
fredck Jan 9, 2015
1eddc34
Added support for "plugin!" and properly initialized the CKEDITOR mod…
fredck Jan 12, 2015
9368232
Introduced the basePath calculation, making RequireJS paths relative …
fredck Jan 12, 2015
e0ebc2c
Made the dev version of CKEDITOR override the core one through module…
fredck Jan 13, 2015
ba2f8c6
Workaround for the name conflict between the core "plugin" module and…
fredck Jan 13, 2015
130b2c7
Linting fixes.
fredck Jan 13, 2015
a3a4956
Introduced "grunt build".
fredck Jan 14, 2015
1dce371
Added the Almond source to the build.
fredck Jan 14, 2015
767f6ac
Do not initialize the dependency tree as this is out of the scope of …
fredck Jan 14, 2015
ef522dd
Shared the ckeditor5/ckeditor.js code between dev and build versions …
fredck Jan 14, 2015
255d2b5
Enabled uglify by default.
fredck Jan 14, 2015
2f578cf
Removed unnecessary comments.
fredck Jan 15, 2015
051574a
Got rid of a weird way to read a json file.
fredck Jan 15, 2015
6113982
Removed an unnecessary callback wrapping.
fredck Jan 15, 2015
c2413f0
Removed yet another unnecessary callback wrapping.
fredck Jan 15, 2015
c3fc22c
Extracted the builder logic into a class. Added minimum documentation…
fredck Jan 16, 2015
667acb1
Moved the start and end code fragments to be injected in the build to…
fredck Jan 16, 2015
76d64c6
Added documentation for the start and end build fragments.
fredck Jan 16, 2015
f4074f0
Stop the building process if the tmp folder already exists.
fredck Jan 19, 2015
9a86ebb
Exposed the `getBasePath` method to enable testing it more easily.
fredck Jan 19, 2015
10246ac
Minor fixes on style, typos, etc.
fredck Jan 19, 2015
3d4fbbe
Minor simplification.
fredck Jan 19, 2015
950871b
Fixed `@type` to `@property`.
fredck Jan 19, 2015
66297bb
Changed the way the Builder class is exported.
fredck Jan 19, 2015
49b17a2
Removed the RequireJS distro from the repo making it a npm dependency.
fredck Jan 19, 2015
8880b20
Added proper information about the build directory.
fredck Jan 19, 2015
990f135
Using console.error where appropriate.
fredck Jan 19, 2015
2c89d5d
Added reference to avoid newbies confusion.
fredck Jan 19, 2015
ef856f4
Removed an unnecessary argument passed to define().
fredck Jan 19, 2015
1a37506
Follow the naming standard.
Reinmar Jan 19, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions LICENSE.md
Expand Up @@ -24,6 +24,15 @@ Where not otherwise indicated, all CKEditor content is authored by CKSource engi
intellectual property. In some specific instances, CKEditor will incorporate work done by developers outside of CKSource
with their express permission.

(Ignore this line: %REMOVE_START%)

Software available at our [repository](https://github.com/ckeditor/ckeditor5) and developer version only:

- RequireJS (lib/requirejs) <br>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use * insteaf of -.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 10246ac.

Licensed under the terms of the MIT or new BSD license. <br>
Copyright (c) 2010-2014, The Dojo Foundation All Rights Reserved.

(Ignore this line: %REMOVE_END%)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove the first or last blank line inside the above block we will be able to add a blank line after it, so the next header will be visually separated from the contents of this section :P.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't understand this one. Feel free to change it yourself.

Trademarks
----------

Expand Down
105 changes: 105 additions & 0 deletions ckeditor.js
@@ -0,0 +1,105 @@
/**
* @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/* global requirejs, define, require, window, document, location */

'use strict';

// This file is shared by the dev and release versions of CKEditor. It bootstraps the API.

( function( root ) {
var CKEDITOR = root.CKEDITOR = {
/**
* The full URL for the CKEditor installation directory.
*
* It is possible to manually provide the base path by setting a global variable named `CKEDITOR_BASEPATH`. This
* global variable must be set **before** the editor script loading.
*
* alert( CKEDITOR.basePath ); // e.g. 'http://www.example.com/ckeditor/'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console.log will be more appropriate these days :D.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 10246ac.

*
* @property {String}
*/
basePath: getBasePath(),

/**
* Defines an AMD module.
*
* See https://github.com/ckeditor/ckeditor5-design/wiki/AMD for more details about our AMD API.
*
* @method
* @member CKEDITOR
*/
define: define,

/**
* Retrieves one or more AMD modules.
*
* Note that the CKEditor AMD API does not download modules on demand so be sure to have their relative scripts
* available in the page.
*
* See https://github.com/ckeditor/ckeditor5-design/wiki/AMD for more details about our AMD API.
*
* @method
* @member CKEDITOR
*/
require: require
};

requirejs.config( {
// Modules are generally relative to the core project.
baseUrl: CKEDITOR.basePath + 'node_modules/ckeditor5-core/src/',

// These configurations will make no difference in the build version because the following paths will be
// already defined there.
paths: {
// Hide the core "ckeditor" under a different name.
'ckeditor-core': CKEDITOR.basePath + 'node_modules/ckeditor5-core/src/ckeditor',

// The dev version overrides for the "ckeditor" module. This is empty on release.
'ckeditor-dev': CKEDITOR.basePath + 'src/ckeditor-dev'
}
} );

// Define a new "ckeditor" module, which overrides the core one with the above and the dev stuff.
define( 'ckeditor', [ 'ckeditor-core', 'ckeditor-dev', 'utils' ], function( core, dev, utils ) {
utils.extend( core, root.CKEDITOR, ( dev || {} ) );
root.CKEDITOR = core;

return core;
} );

function getBasePath() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be testable. We already had problem in CKEditor 4 that in order to test the base path we had to load multiple iframes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 9a86ebb.

if ( window.CKEDITOR_BASEPATH ) {
return window.CKEDITOR_BASEPATH;
}

var scripts = document.getElementsByTagName( 'script' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why so much spacing between variable declarations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 10246ac.


var basePathSrcPattern = /(^|.*[\\\/])ckeditor\.js(?:\?.*|;.*)?$/i;

var path;

// Find the first script that src matches ckeditor.js.
[].some.call( scripts, function( script ) {
var match = script.src.match( basePathSrcPattern );

if ( match ) {
path = match[ 1 ];

return true;
}
} );

if ( path.indexOf( ':/' ) == -1 && path.slice( 0, 2 ) != '//' ) {
if ( path.indexOf( '/' ) === 0 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path[ 0 ] == '/' would be shorter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 3d4fbbe.

path = location.href.match( /^.*?:\/\/[^\/]*/ )[ 0 ] + path;
} else {
path = location.href.match( /^[^\?]*\/(?:)/ )[ 0 ] + path;
}
}

return path;
}
} )( window );
158 changes: 158 additions & 0 deletions dev/tasks/build.js
@@ -0,0 +1,158 @@
/**
* @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/* jshint node: true */

'use strict';

module.exports = function( grunt ) {
grunt.registerTask( 'build', 'Build a release out of the current development code.', function() {
var done = this.async();
module.exports.build( done );
} );
};

// Exports the build method so it can be used from plain node code as well.
module.exports.build = function( done ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we create a class for the builder? And then wouldn't it be good to have unit tests for builder too?

It's not super complicated right now, but there's no doubt that it will grow, and tdd would be very handy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mlewand

Can't we create a class for the builder?

Good suggestion. I've just done that with c3fc22c.

And then wouldn't it be good to have unit tests for builder too?

I thought about that but I didn't want to spend time on this as there is no infrastructure for such tests in place. There are too many things to be bootstrapped right now, so I would propose to move ahead without tests here and make this a future enhancement.

var target = 'build';
var tmp = 'tmp';
var stepCounter = 0;

var tasks = [
[ cleanup, 'Cleaning the "' + target + '" directory...' ],
[ copyToTmp, 'Copying source files for manipulation...' ],
[ removeAmdNamespace, 'AMD cleanup...' ],
[ optimize, 'Creating the optimized code...' ],
[ cleanupTmp, 'Removing the "' + tmp + '" directory...' ]
];

runNext();

function runNext() {
var next = tasks.shift();

if ( next ) {
stepCounter++;
console.log( stepCounter + '. ' + next[ 1 ] );
next[ 0 ]( runNext );
} else {
if ( done ) {
done();
}
}
}

function cleanup( callback ) {
var del = require( 'del' );
del.sync( target );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why synchronously when you have a callback to call when ready?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make the code simpler, as we have another call right after that. In any case we need to wait for you anyway, so it should not be a big del, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be a big del

Right ^_^

del.sync( tmp );

return callback();
}

function copyToTmp( callback ) {
var ncp = require( 'ncp' ).ncp;
var path = require( 'path' );
var fs = require( 'fs' );

var deps = JSON.parse( fs.readFileSync( 'package.json', 'utf8' ) ).dependencies;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a plain require here:

var deps = require('../../package.json').dependencies;


var toCopy = Object.keys( deps ).filter( function( name ) {
return name.indexOf( 'ckeditor5-' ) === 0;
} );

if ( !fs.existsSync( tmp ) ) {
fs.mkdirSync( tmp );
}

function copy() {
var module = toCopy.shift();

if ( !module ) {
return callback();
}

var dest = path.join( tmp + '/', module );

if ( !fs.existsSync( dest ) ) {
fs.mkdirSync( dest );
}

// Copy the "src" directory only.
ncp( path.join( 'node_modules', module, 'src' ), path.join( dest, 'src' ), {
dereference: true
}, function( err ) {
if ( err ) {
throw( err );
}

copy();
} );
}

copy();
}

function removeAmdNamespace( callback ) {
var replace = require( 'replace' );

replace( {
regex: /^\s*CKEDITOR\.(define|require)/mg,
replacement: '$1',
paths: [ 'tmp' ],
recursive: true,
silent: true
} );

callback();
}

function optimize( callback ) {
var requirejs = require( 'requirejs' );

var config = {
out: target + '/ckeditor.js',

baseUrl: tmp + '/ckeditor5-core/src/',
paths: {
'ckeditor': '../../../ckeditor',
'ckeditor-dev': '../../../src/ckeditor-dev',
'ckeditor-core': 'ckeditor'
// depTree: '../../../lib/depTree',
// plugins: '../../../lib/plugins'
},

// include: ['depTree', 'ckeditor' ].concat( getPlugins() ),
include: [ 'ckeditor' ],
stubModules: [ 'ckeditor-dev' ],

// onBuildWrite: replacePaths,
// optimize: 'none',
optimize: 'uglify2',
//uglify2: {
// output: {
// beautify: true
// },
// mangle: false
//},
preserveLicenseComments: false,
wrap: {
startFile: [ 'dev/tasks/build/start.frag', require.resolve( 'almond' ) ],
endFile: 'dev/tasks/build/end.frag'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should put start and end fragments somewhere near the sources, not in dev/tasks/build. Eg. jQuery uses src/intro.js, src/outro.js, angular uses eg. src/module.prefix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 from me because those are closer to cource code then to tasks source code/configuration.

Be careful with extension. The .frag is good because it can be white listed on jshint/jscss and in IDE/text editor. Those files can be "invalid" like a function that is cut in half and put in two separate files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense. I've moved them with 76d64c6.

}
};

requirejs.optimize( config, function() {
callback();
} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to wrap it in a function.

}

function cleanupTmp( callback ) {
var del = require( 'del' );
del.sync( tmp );

return callback();
}
};
4 changes: 4 additions & 0 deletions dev/tasks/build/end.frag
@@ -0,0 +1,4 @@
/************************ end.frag START */

return require( 'ckeditor' );
} );
13 changes: 13 additions & 0 deletions dev/tasks/build/start.frag
@@ -0,0 +1,13 @@
( function ( root, factory ) {
// Register the CKEDITOR global.
root.CKEDITOR = factory();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we pollute the global namespace even though someone wants to use CKEditor5 as an AMD module? That's completely against AMD principles...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that... but just the fact that require is available in the page doesn't guarantee that developers will be using CKEditor in the AMD way. They could still go with the namespace way. For example when copying pasting code from documentation or other sources.

For instance, developers may not even be developing with AMD at all, but maybe their CMSs are making require available and they don't know it. They'll try to use the CKEDITOR global then and have troubles.

Therefore, it sounded like the safest approach to enable both ways.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safe doesn't always mean right. They can still expose the global namespace themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, but you must understand that this level of flexibility is required because we deal with all kinds of developers out there. Some would be prepared for AMD but many will just have troubles.

Note that our goal is not making CKEditor an AMD module, but instead enable it as a AMD module to satisfy AMD devs.

For the sake of being "right" we may even consider a configuration option for the builder where one could force "pure AMD", so no global is created. But by default we should go the "satisfy all" way for easy adoption.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, having this configurable might do the trick.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll open an issue for this once this PR goes accepted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found an interesting thing - https://github.com/jrburke/requirejs/wiki/Updating-existing-libraries#anon

If I understand correctly, if ckeditor.js was concatenated with other scripts (e.g. by an assets pipeline like in RoR) it may lead to an error if we use an anonymous module definition (which we do now).

This means that there must be (somehow available) a totally non-AMD version of ckeditor.js available for situations like this - concatenation by some simple tool. Therefore at least the builder must have an option for that.

The remaining questions are:

  1. What options should the builder have?
  2. What do we want to ship in a release?

Ad 1. I think that 3 - AMD with polluting (could be the default), AMD without polluting (for pedants like many of us), no AMD at all (for concatenation).

Ad 2. jQuery is doing the "AMD with polluting", so I think we can do it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Reinmar, it makes sense.

I think we can proceed with the existing code and the do what I already said, to avoid such important thoughts to be left inside commits comments.:

Ok, I'll open an issue for this once this PR goes accepted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up: #8.


// Make the build an AMD module.
if ( typeof define == 'function' && define.amd ) {
define( [], function() {
return root.CKEDITOR;
} );
}
} )( this, function () {

/************************ start.frag END */
1 change: 1 addition & 0 deletions gruntfile.js
Expand Up @@ -9,6 +9,7 @@ module.exports = function( grunt ) {
// Files that will be ignored by the "jscs" and "jshint" tasks.
var ignoreFiles = [
// Automatically loaded from .gitignore. Add more if necessary.
'lib/**'
];

// Basic configuration which will be overloaded by the tasks.
Expand Down