Skip to content

Commit

Permalink
refactor(package): bundle jquery and sui js
Browse files Browse the repository at this point in the history
  • Loading branch information
levithomason committed Jul 10, 2016
1 parent bfd5770 commit 0c0f018
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 28 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ export default class TryStardust extends Component {

<!-- SUI CSS -->
<link href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.1.8/semantic.css" rel="stylesheet">

<!-- Temporary script dependencies until we're done removing jQuery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.1.8/semantic.js"></script>
</head>
<body>
<script src="bundle.js"></script>
Expand Down
9 changes: 8 additions & 1 deletion build/karma.conf.babel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { argv } = require('yargs')
const config = require('../config')
const webpack = require('webpack')
const webpackConfig = require('./webpack.config')

const { paths } = config
Expand Down Expand Up @@ -50,7 +51,13 @@ module.exports = (karmaConfig) => {
...webpackConfig.module.loaders,
],
}),
plugins: webpackConfig.plugins,
plugins: [
...webpackConfig.plugins,
// utils/jquery loads real jQuery and semantic-ui-css
// we alias jquery and semantic-ui-css to mocks during tests
// ignore this module so we can use the mock versions in alias below
new webpack.NormalModuleReplacementPlugin(/utils\/jquery/, 'empty'),
],
resolve: Object.assign({}, webpackConfig.resolve, {
alias: Object.assign({}, webpackConfig.resolve.alias, {
jquery: `${paths.test('mocks')}/SemanticjQuery-mock.js`,
Expand Down
26 changes: 14 additions & 12 deletions build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ webpackConfig.output = {
// ------------------------------------
webpackConfig.plugins = [
new webpack.DefinePlugin(config.compiler_globals),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
}),
new webpack.DllReferencePlugin({
context: paths.base('node_modules'),
manifest: require(paths.base('dll/vendor-manifest.json')),
Expand Down Expand Up @@ -160,7 +155,8 @@ webpackConfig.module.loaders = [{
// ----------------------------------------
// For faster builds in dev, rely on prebuilt libraries
// Local modules can still be enabled (ie for offline development)
if (argv.localModules) {
// in TEST we need local modules because karma uses a different index.html (no CDNs)
if (__TEST__ || argv.localModules) {
webpackConfig.module.loaders = [
...webpackConfig.module.loaders,
{
Expand All @@ -178,21 +174,27 @@ if (argv.localModules) {
},
]
} else {
// we're not using local modules, we're loading deps via CDNs
webpackConfig.entry.vendor = _.without(webpackConfig.entry.vendor, [
'faker',
])

// these are browser ready modules or aliased to empty
// do not parse their source for faster builds
webpackConfig.module.noParse = [
...webpackConfig.module.noParse,
/jquery/,
/semantic-ui-css\/semantic\.js/,
/semantic-ui-css\/semantic\.css/,
/faker/,
]

// alias imports to empty
webpackConfig.resolve.alias = Object.assign({}, webpackConfig.resolve.alias, {
'semantic-ui-css/semantic.js': 'empty',
'semantic-ui-css/semantic.css': 'empty',
'highlight.js/styles/github.css': 'empty',
})

// find them on the window
webpackConfig.externals = {
jquery: 'jQuery',
faker: 'faker',
lodash: '_',
}
}

Expand Down
3 changes: 0 additions & 3 deletions config/_default.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ config = Object.assign({}, config, {
'bluebird',
'classnames',
'faker',
'jquery',
'lodash',
'react',
'react-dom',
'react-highlight',
'semantic-ui-css/semantic.js',
],
compiler_stats: {
hash: false, // the hash of the compilation
Expand Down
1 change: 0 additions & 1 deletion docs/app/Components/Root.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'semantic-ui-css/semantic.js'
import 'semantic-ui-css/semantic.css'
import 'highlight.js/styles/github.css'
import _ from 'lodash'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class FormValidationExamples extends Component {
title='Validating on Blur and other Events'
description={`
Validation messages can also appear inline.
UI Forms automatically format labels with the class name <code>prompt</code>.
UI Forms automatically format labels with the class name "prompt".
These validation prompts are also set to appear on input change instead of form submission.
`}
examplePath='collections/Form/Validation/FormValidatingOnBlurAndOtherEventsExample'
Expand Down
3 changes: 0 additions & 3 deletions docs/app/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/<%= htmlWebpackPlugin.options.versions.highlightjs %>/styles/github.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/<%= htmlWebpackPlugin.options.versions.sui %>/semantic.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/Faker/<%= htmlWebpackPlugin.options.versions.faker %>/faker.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/<%= htmlWebpackPlugin.options.versions.jquery %>/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/<%= htmlWebpackPlugin.options.versions.lodash %>/lodash.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/<%= htmlWebpackPlugin.options.versions.sui %>/semantic.min.js"></script>
<title>Stardust</title>
<script>
// Apply gh-pages SPA redirect that was applied in 404.html
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
"bluebird": "^3.3.4",
"classnames": "^2.1.5",
"debug": "^2.2.0",
"lodash": "^4.6.1"
"jquery": "^3.1.0",
"lodash": "^4.6.1",
"semantic-ui-css": "^2.2.1"
},
"devDependencies": {
"babel-cli": "^6.5.1",
Expand Down Expand Up @@ -76,6 +78,7 @@
"eslint-config-ta": "^5.1.0",
"eslint-plugin-mocha": "^2.2.0",
"eslint-plugin-react": "^5.1.1",
"expose-loader": "^0.7.1",
"express": "^4.13.4",
"faker": "^3.0.1",
"file-loader": "^0.8.5",
Expand Down Expand Up @@ -116,7 +119,6 @@
"require-dir": "^0.3.0",
"rimraf": "^2.5.2",
"sass-loader": "^3.2.0",
"semantic-ui-css": "^2.1.8",
"simulant": "^0.2.1",
"sinon": "^1.17.3",
"sinon-chai": "^2.8.0",
Expand All @@ -130,7 +132,6 @@
"yargs": "^4.3.2"
},
"peerDependencies": {
"jquery": "^2.1.4",
"react": ">=0.14.0 <= 15",
"react-dom": ">=0.14.0 <= 15"
}
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './utils/jquery'
import { deprecateComponent } from './utils/deprecate'

// ----------------------------------------
Expand Down
31 changes: 31 additions & 0 deletions src/utils/jquery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Ensure the user's app only uses our one copy of jQuery and SUI jQuery plugins.
*/
import _ from 'lodash'
import { makeDebugger } from './debug'

const debug = makeDebugger('jquery')

let jQuery
if (window.jQuery) jQuery = window.jQuery
else if (_.get(window, '$.prototype.jquery')) jQuery = window.$

if (jQuery) {
throw new Error([
'Do not load jQuery, it is bundled with Stardust.',
'Stardust will soon be jQuery free: https://github.com/TechnologyAdvice/stardust/issues/247.',
].join(' '))
}

debug('Loading jQuery')
// load jQuery, then load SUI jQuery
window.jQuery = window.$ = require('jquery')

debug('Loading SUI Checkbox plugin')
require('semantic-ui-css/components/checkbox')

debug('Loading SUI Form plugin')
require('semantic-ui-css/components/form')

debug('Loading SUI Progress plugin')
require('semantic-ui-css/components/progress')

0 comments on commit 0c0f018

Please sign in to comment.